1 /*
2 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
5 * Copyright 2015 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include <stdint.h>
21 #include <qdMetaData.h>
22
23 #include "hwc_layers.h"
24 #ifndef USE_GRALLOC1
25 #include <gr.h>
26 #endif
27 #include <utils/debug.h>
28 #include <cmath>
29
30 #define __CLASS__ "HWCLayer"
31
32 namespace sdm {
33
34 std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
35
36 // Layer operations
HWCLayer(hwc2_display_t display_id,HWCBufferAllocator * buf_allocator)37 HWCLayer::HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator)
38 : id_(next_id_++), display_id_(display_id), buffer_allocator_(buf_allocator) {
39 layer_ = new Layer();
40 layer_->input_buffer = new LayerBuffer();
41 // Fences are deferred, so the first time this layer is presented, return -1
42 // TODO(user): Verify that fences are properly obtained on suspend/resume
43 release_fences_.push(-1);
44 }
45
~HWCLayer()46 HWCLayer::~HWCLayer() {
47 // Close any fences left for this layer
48 while (!release_fences_.empty()) {
49 close(release_fences_.front());
50 release_fences_.pop();
51 }
52 close(ion_fd_);
53 if (layer_) {
54 if (layer_->input_buffer) {
55 delete (layer_->input_buffer);
56 }
57 delete layer_;
58 }
59 }
60
SetLayerBuffer(buffer_handle_t buffer,int32_t acquire_fence)61 HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence) {
62 if (!buffer) {
63 DLOGE("Invalid buffer handle: %p on layer: %d", buffer, id_);
64 return HWC2::Error::BadParameter;
65 }
66
67 if (acquire_fence == 0) {
68 DLOGE("acquire_fence is zero");
69 return HWC2::Error::BadParameter;
70 }
71
72 const private_handle_t *handle = static_cast<const private_handle_t *>(buffer);
73
74 // Validate and dup ion fd from surfaceflinger
75 // This works around bug 30281222
76 if (handle->fd < 0) {
77 return HWC2::Error::BadParameter;
78 } else {
79 close(ion_fd_);
80 ion_fd_ = dup(handle->fd);
81 }
82
83 LayerBuffer *layer_buffer = layer_->input_buffer;
84
85 layer_buffer->width = UINT32(handle->width);
86 layer_buffer->height = UINT32(handle->height);
87 auto format = layer_buffer->format;
88 layer_buffer->format = GetSDMFormat(handle->format, handle->flags);
89 if (format != layer_buffer->format) {
90 needs_validate_ = true;
91 }
92 if (SetMetaData(handle, layer_) != kErrorNone) {
93 return HWC2::Error::BadLayer;
94 }
95
96 layer_buffer->flags.video = (handle->buffer_type == BUFFER_TYPE_VIDEO) ? true : false;
97 // TZ Protected Buffer - L1
98 layer_buffer->flags.secure =
99 (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) ? true: false;
100 layer_buffer->flags.secure_display =
101 (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) ? true : false;
102
103 layer_buffer->planes[0].fd = ion_fd_;
104 layer_buffer->planes[0].offset = handle->offset;
105 layer_buffer->planes[0].stride = UINT32(handle->width);
106 layer_buffer->acquire_fence_fd = acquire_fence;
107 layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
108
109 return HWC2::Error::None;
110 }
111
SetLayerSurfaceDamage(hwc_region_t damage)112 HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
113 // Check if there is an update in SurfaceDamage rects
114 if (layer_->dirty_regions.size() != damage.numRects) {
115 needs_validate_ = true;
116 } else {
117 for (uint32_t j = 0; j < damage.numRects; j++) {
118 LayerRect damage_rect;
119 SetRect(damage.rects[j], &damage_rect);
120 if (damage_rect != layer_->dirty_regions.at(j)) {
121 needs_validate_ = true;
122 break;
123 }
124 }
125 }
126
127 layer_->dirty_regions.clear();
128 for (uint32_t i = 0; i < damage.numRects; i++) {
129 LayerRect rect;
130 SetRect(damage.rects[i], &rect);
131 layer_->dirty_regions.push_back(rect);
132 }
133 return HWC2::Error::None;
134 }
135
SetLayerBlendMode(HWC2::BlendMode mode)136 HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) {
137 LayerBlending blending = kBlendingPremultiplied;
138 switch (mode) {
139 case HWC2::BlendMode::Coverage:
140 blending = kBlendingCoverage;
141 break;
142 case HWC2::BlendMode::Premultiplied:
143 blending = kBlendingPremultiplied;
144 break;
145 case HWC2::BlendMode::None:
146 blending = kBlendingOpaque;
147 break;
148 default:
149 return HWC2::Error::BadParameter;
150 }
151
152 if (layer_->blending != blending) {
153 geometry_changes_ |= kBlendMode;
154 layer_->blending = blending;
155 }
156 return HWC2::Error::None;
157 }
158
SetLayerColor(hwc_color_t color)159 HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) {
160 layer_->solid_fill_color = GetUint32Color(color);
161 layer_->input_buffer->format = kFormatARGB8888;
162 DLOGV_IF(kTagCompManager, "[%" PRIu64 "][%" PRIu64 "] Layer color set to %x", display_id_, id_,
163 layer_->solid_fill_color);
164 return HWC2::Error::None;
165 }
166
SetLayerCompositionType(HWC2::Composition type)167 HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) {
168 client_requested_ = type;
169 switch (type) {
170 case HWC2::Composition::Client:
171 break;
172 case HWC2::Composition::Device:
173 // We try and default to this in SDM
174 break;
175 case HWC2::Composition::SolidColor:
176 break;
177 case HWC2::Composition::Cursor:
178 break;
179 case HWC2::Composition::Invalid:
180 return HWC2::Error::BadParameter;
181 default:
182 return HWC2::Error::Unsupported;
183 }
184
185 return HWC2::Error::None;
186 }
187
SetLayerDataspace(int32_t dataspace)188 HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) {
189 if (dataspace != dataspace_) {
190 dataspace_ = dataspace;
191 geometry_changes_ |= kDataspace;
192 }
193 return HWC2::Error::None;
194 }
195
SetLayerDisplayFrame(hwc_rect_t frame)196 HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
197 LayerRect dst_rect = {};
198 SetRect(frame, &dst_rect);
199 if (layer_->dst_rect != dst_rect) {
200 geometry_changes_ |= kDisplayFrame;
201 layer_->dst_rect = dst_rect;
202 }
203 return HWC2::Error::None;
204 }
205
SetLayerPlaneAlpha(float alpha)206 HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
207 // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
208 uint8_t plane_alpha = static_cast<uint8_t>(std::round(255.0f * alpha));
209 if (layer_->plane_alpha != plane_alpha) {
210 geometry_changes_ |= kPlaneAlpha;
211 layer_->plane_alpha = plane_alpha;
212 }
213
214 return HWC2::Error::None;
215 }
216
SetLayerSourceCrop(hwc_frect_t crop)217 HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) {
218 LayerRect src_rect = {};
219 SetRect(crop, &src_rect);
220 if (layer_->src_rect != src_rect) {
221 geometry_changes_ |= kSourceCrop;
222 layer_->src_rect = src_rect;
223 }
224
225 return HWC2::Error::None;
226 }
227
SetLayerTransform(HWC2::Transform transform)228 HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
229 LayerTransform layer_transform = {};
230 switch (transform) {
231 case HWC2::Transform::FlipH:
232 layer_transform.flip_horizontal = true;
233 break;
234 case HWC2::Transform::FlipV:
235 layer_transform.flip_vertical = true;
236 break;
237 case HWC2::Transform::Rotate90:
238 layer_transform.rotation = 90.0f;
239 break;
240 case HWC2::Transform::Rotate180:
241 layer_transform.flip_horizontal = true;
242 layer_transform.flip_vertical = true;
243 break;
244 case HWC2::Transform::Rotate270:
245 layer_transform.rotation = 90.0f;
246 layer_transform.flip_horizontal = true;
247 layer_transform.flip_vertical = true;
248 break;
249 case HWC2::Transform::FlipHRotate90:
250 layer_transform.rotation = 90.0f;
251 layer_transform.flip_horizontal = true;
252 break;
253 case HWC2::Transform::FlipVRotate90:
254 layer_transform.rotation = 90.0f;
255 layer_transform.flip_vertical = true;
256 break;
257 case HWC2::Transform::None:
258 // do nothing
259 break;
260 }
261
262 if (layer_->transform != layer_transform) {
263 geometry_changes_ |= kTransform;
264 layer_->transform = layer_transform;
265 }
266 return HWC2::Error::None;
267 }
268
SetLayerVisibleRegion(hwc_region_t visible)269 HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
270 layer_->visible_regions.clear();
271 for (uint32_t i = 0; i < visible.numRects; i++) {
272 LayerRect rect;
273 SetRect(visible.rects[i], &rect);
274 layer_->visible_regions.push_back(rect);
275 }
276
277 return HWC2::Error::None;
278 }
279
SetLayerZOrder(uint32_t z)280 HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
281 if (z_ != z) {
282 geometry_changes_ |= kZOrder;
283 z_ = z;
284 }
285 return HWC2::Error::None;
286 }
287
SetRect(const hwc_rect_t & source,LayerRect * target)288 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
289 target->left = FLOAT(source.left);
290 target->top = FLOAT(source.top);
291 target->right = FLOAT(source.right);
292 target->bottom = FLOAT(source.bottom);
293 }
294
SetRect(const hwc_frect_t & source,LayerRect * target)295 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
296 // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
297 target->left = std::ceil(source.left);
298 target->top = std::ceil(source.top);
299 target->right = std::floor(source.right);
300 target->bottom = std::floor(source.bottom);
301 }
302
GetUint32Color(const hwc_color_t & source)303 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
304 // Returns 32 bit ARGB
305 uint32_t a = UINT32(source.a) << 24;
306 uint32_t r = UINT32(source.r) << 16;
307 uint32_t g = UINT32(source.g) << 8;
308 uint32_t b = UINT32(source.b);
309 uint32_t color = a | r | g | b;
310 return color;
311 }
312
GetSDMFormat(const int32_t & source,const int flags)313 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
314 LayerBufferFormat format = kFormatInvalid;
315 if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
316 switch (source) {
317 case HAL_PIXEL_FORMAT_RGBA_8888:
318 format = kFormatRGBA8888Ubwc;
319 break;
320 case HAL_PIXEL_FORMAT_RGBX_8888:
321 format = kFormatRGBX8888Ubwc;
322 break;
323 case HAL_PIXEL_FORMAT_BGR_565:
324 format = kFormatBGR565Ubwc;
325 break;
326 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
327 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
328 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
329 format = kFormatYCbCr420SPVenusUbwc;
330 break;
331 case HAL_PIXEL_FORMAT_RGBA_1010102:
332 format = kFormatRGBA1010102Ubwc;
333 break;
334 case HAL_PIXEL_FORMAT_RGBX_1010102:
335 format = kFormatRGBX1010102Ubwc;
336 break;
337 default:
338 DLOGE("Unsupported format type for UBWC %d", source);
339 return kFormatInvalid;
340 }
341 return format;
342 }
343
344 switch (source) {
345 case HAL_PIXEL_FORMAT_RGBA_8888:
346 format = kFormatRGBA8888;
347 break;
348 case HAL_PIXEL_FORMAT_RGBA_5551:
349 format = kFormatRGBA5551;
350 break;
351 case HAL_PIXEL_FORMAT_RGBA_4444:
352 format = kFormatRGBA4444;
353 break;
354 case HAL_PIXEL_FORMAT_BGRA_8888:
355 format = kFormatBGRA8888;
356 break;
357 case HAL_PIXEL_FORMAT_RGBX_8888:
358 format = kFormatRGBX8888;
359 break;
360 case HAL_PIXEL_FORMAT_BGRX_8888:
361 format = kFormatBGRX8888;
362 break;
363 case HAL_PIXEL_FORMAT_RGB_888:
364 format = kFormatRGB888;
365 break;
366 case HAL_PIXEL_FORMAT_RGB_565:
367 format = kFormatRGB565;
368 break;
369 case HAL_PIXEL_FORMAT_BGR_565:
370 format = kFormatBGR565;
371 break;
372 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
373 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
374 format = kFormatYCbCr420SemiPlanarVenus;
375 break;
376 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
377 format = kFormatYCrCb420SemiPlanarVenus;
378 break;
379 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
380 format = kFormatYCbCr420SPVenusUbwc;
381 break;
382 case HAL_PIXEL_FORMAT_YV12:
383 format = kFormatYCrCb420PlanarStride16;
384 break;
385 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
386 format = kFormatYCrCb420SemiPlanar;
387 break;
388 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
389 format = kFormatYCbCr420SemiPlanar;
390 break;
391 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
392 format = kFormatYCbCr422H2V1SemiPlanar;
393 break;
394 case HAL_PIXEL_FORMAT_YCbCr_422_I:
395 format = kFormatYCbCr422H2V1Packed;
396 break;
397 case HAL_PIXEL_FORMAT_RGBA_1010102:
398 format = kFormatRGBA1010102;
399 break;
400 case HAL_PIXEL_FORMAT_ARGB_2101010:
401 format = kFormatARGB2101010;
402 break;
403 case HAL_PIXEL_FORMAT_RGBX_1010102:
404 format = kFormatRGBX1010102;
405 break;
406 case HAL_PIXEL_FORMAT_XRGB_2101010:
407 format = kFormatXRGB2101010;
408 break;
409 case HAL_PIXEL_FORMAT_BGRA_1010102:
410 format = kFormatBGRA1010102;
411 break;
412 case HAL_PIXEL_FORMAT_ABGR_2101010:
413 format = kFormatABGR2101010;
414 break;
415 case HAL_PIXEL_FORMAT_BGRX_1010102:
416 format = kFormatBGRX1010102;
417 break;
418 case HAL_PIXEL_FORMAT_XBGR_2101010:
419 format = kFormatXBGR2101010;
420 break;
421 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
422 format = kFormatYCbCr420P010;
423 break;
424 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
425 format = kFormatYCbCr420TP10Ubwc;
426 break;
427 default:
428 DLOGW("Unsupported format type = %d", source);
429 return kFormatInvalid;
430 }
431
432 return format;
433 }
434
GetS3DFormat(uint32_t s3d_format)435 LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
436 LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
437 switch (s3d_format) {
438 case HAL_NO_3D:
439 sdm_s3d_format = kS3dFormatNone;
440 break;
441 case HAL_3D_SIDE_BY_SIDE_L_R:
442 sdm_s3d_format = kS3dFormatLeftRight;
443 break;
444 case HAL_3D_SIDE_BY_SIDE_R_L:
445 sdm_s3d_format = kS3dFormatRightLeft;
446 break;
447 case HAL_3D_TOP_BOTTOM:
448 sdm_s3d_format = kS3dFormatTopBottom;
449 break;
450 default:
451 DLOGW("Invalid S3D format %d", s3d_format);
452 }
453 return sdm_s3d_format;
454 }
455
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)456 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
457 LayerBuffer *layer_buffer = layer->input_buffer;
458 private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
459
460 BufferDim_t buffer_dim = {};
461 buffer_dim.sliceWidth = pvt_handle->width;
462 buffer_dim.sliceHeight = pvt_handle->height;
463 if (getMetaData(handle, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
464 #ifdef USE_GRALLOC1
465 buffer_allocator_->GetCustomWidthAndHeight(pvt_handle, &buffer_dim.sliceWidth,
466 &buffer_dim.sliceHeight);
467 #else
468 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(handle, &buffer_dim.sliceWidth,
469 &buffer_dim.sliceHeight);
470 #endif
471 layer_buffer->width = UINT32(buffer_dim.sliceWidth);
472 layer_buffer->height = UINT32(buffer_dim.sliceHeight);
473 }
474
475 ColorSpace_t csc = ITU_R_601;
476 if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_SPACE, &csc) == 0) {
477 if (SetCSC(csc, &layer_buffer->csc) != kErrorNone) {
478 return kErrorNotSupported;
479 }
480 }
481
482 IGC_t igc = {};
483 if (getMetaData(handle, GET_IGC, &igc) == 0) {
484 if (SetIGC(igc, &layer_buffer->igc) != kErrorNone) {
485 return kErrorNotSupported;
486 }
487 }
488
489 uint32_t fps = 0;
490 if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
491 layer->frame_rate = RoundToStandardFPS(fps);
492 }
493
494 int32_t interlaced = 0;
495 if (getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
496 layer_buffer->flags.interlace = interlaced ? true : false;
497 }
498
499 uint32_t linear_format = 0;
500 if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
501 layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
502 }
503
504 uint32_t s3d = 0;
505 if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
506 layer_buffer->s3d_format = GetS3DFormat(s3d);
507 }
508
509 return kErrorNone;
510 }
511
SetCSC(ColorSpace_t source,LayerCSC * target)512 DisplayError HWCLayer::SetCSC(ColorSpace_t source, LayerCSC *target) {
513 switch (source) {
514 case ITU_R_601:
515 *target = kCSCLimitedRange601;
516 break;
517 case ITU_R_601_FR:
518 *target = kCSCFullRange601;
519 break;
520 case ITU_R_709:
521 *target = kCSCLimitedRange709;
522 break;
523 default:
524 DLOGE("Unsupported CSC: %d", source);
525 return kErrorNotSupported;
526 }
527
528 return kErrorNone;
529 }
530
SetIGC(IGC_t source,LayerIGC * target)531 DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
532 switch (source) {
533 case IGC_NotSpecified:
534 *target = kIGCNotSpecified;
535 break;
536 case IGC_sRGB:
537 *target = kIGCsRGB;
538 break;
539 default:
540 DLOGE("Unsupported IGC: %d", source);
541 return kErrorNotSupported;
542 }
543
544 return kErrorNone;
545 }
546
RoundToStandardFPS(float fps)547 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
548 static const uint32_t standard_fps[4] = {24, 30, 48, 60};
549 uint32_t frame_rate = (uint32_t)(fps);
550
551 int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
552 for (int i = 0; i < count; i++) {
553 if ((standard_fps[i] - frame_rate) < 2) {
554 // Most likely used for video, the fps can fluctuate
555 // Ex: b/w 29 and 30 for 30 fps clip
556 return standard_fps[i];
557 }
558 }
559
560 return frame_rate;
561 }
562
SetComposition(const LayerComposition & sdm_composition)563 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
564 auto hwc_composition = HWC2::Composition::Invalid;
565 switch (sdm_composition) {
566 case kCompositionGPU:
567 hwc_composition = HWC2::Composition::Client;
568 break;
569 case kCompositionHWCursor:
570 hwc_composition = HWC2::Composition::Cursor;
571 break;
572 default:
573 hwc_composition = HWC2::Composition::Device;
574 break;
575 }
576 // Update solid fill composition
577 if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
578 hwc_composition = HWC2::Composition::SolidColor;
579 }
580 device_selected_ = hwc_composition;
581
582 return;
583 }
PushReleaseFence(int32_t fence)584 void HWCLayer::PushReleaseFence(int32_t fence) {
585 release_fences_.push(fence);
586 }
PopReleaseFence(void)587 int32_t HWCLayer::PopReleaseFence(void) {
588 if (release_fences_.empty())
589 return -1;
590 auto fence = release_fences_.front();
591 release_fences_.pop();
592 return fence;
593 }
594
595 } // namespace sdm
596