1 /*
2 * Copyright (c) 2014-2018, 2020, 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 "hwc_layers.h"
21 #include <utils/debug.h>
22 #include <stdint.h>
23 #include <utility>
24 #include <cmath>
25 #include <qdMetaData.h>
26
27 #define __CLASS__ "HWCLayer"
28
29 namespace sdm {
30
31 std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
32
SetCSC(const private_handle_t * pvt_handle,ColorMetaData * color_metadata)33 DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata) {
34 if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_METADATA,
35 color_metadata) != 0) {
36 ColorSpace_t csc = ITU_R_601;
37 if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_SPACE,
38 &csc) == 0) {
39 if (csc == ITU_R_601_FR || csc == ITU_R_2020_FR) {
40 color_metadata->range = Range_Full;
41 }
42
43 switch (csc) {
44 case ITU_R_601:
45 case ITU_R_601_FR:
46 // video and display driver uses 601_525
47 color_metadata->colorPrimaries = ColorPrimaries_BT601_6_525;
48 break;
49 case ITU_R_709:
50 color_metadata->colorPrimaries = ColorPrimaries_BT709_5;
51 break;
52 case ITU_R_2020:
53 case ITU_R_2020_FR:
54 color_metadata->colorPrimaries = ColorPrimaries_BT2020;
55 break;
56 default:
57 DLOGE("Unsupported CSC: %d", csc);
58 return kErrorNotSupported;
59 }
60 } else {
61 return kErrorNotSupported;
62 }
63 }
64
65 return kErrorNone;
66 }
67
68 // Returns true when color primary is supported
GetColorPrimary(const int32_t & dataspace,ColorPrimaries * color_primary)69 bool GetColorPrimary(const int32_t &dataspace, ColorPrimaries *color_primary) {
70 auto standard = dataspace & HAL_DATASPACE_STANDARD_MASK;
71 bool supported_csc = true;
72 switch (standard) {
73 case HAL_DATASPACE_STANDARD_BT709:
74 *color_primary = ColorPrimaries_BT709_5;
75 break;
76 case HAL_DATASPACE_STANDARD_BT601_525:
77 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
78 *color_primary = ColorPrimaries_BT601_6_525;
79 break;
80 case HAL_DATASPACE_STANDARD_BT601_625:
81 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
82 *color_primary = ColorPrimaries_BT601_6_625;
83 break;
84 case HAL_DATASPACE_STANDARD_DCI_P3:
85 *color_primary = ColorPrimaries_DCIP3;
86 break;
87 case HAL_DATASPACE_STANDARD_BT2020:
88 *color_primary = ColorPrimaries_BT2020;
89 break;
90 default:
91 DLOGV_IF(kTagClient, "Unsupported Standard Request = %d", standard);
92 supported_csc = false;
93 }
94 return supported_csc;
95 }
96
GetTransfer(const int32_t & dataspace,GammaTransfer * gamma_transfer)97 bool GetTransfer(const int32_t &dataspace, GammaTransfer *gamma_transfer) {
98 auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK;
99 bool supported_transfer = true;
100 switch (transfer) {
101 case HAL_DATASPACE_TRANSFER_SRGB:
102 *gamma_transfer = Transfer_sRGB;
103 break;
104 case HAL_DATASPACE_TRANSFER_SMPTE_170M:
105 *gamma_transfer = Transfer_SMPTE_170M;
106 break;
107 case HAL_DATASPACE_TRANSFER_ST2084:
108 *gamma_transfer = Transfer_SMPTE_ST2084;
109 break;
110 case HAL_DATASPACE_TRANSFER_HLG:
111 *gamma_transfer = Transfer_HLG;
112 break;
113 case HAL_DATASPACE_TRANSFER_GAMMA2_2:
114 *gamma_transfer = Transfer_Gamma2_2;
115 break;
116 default:
117 DLOGV_IF(kTagClient, "Unsupported Transfer Request = %d", transfer);
118 supported_transfer = false;
119 }
120 return supported_transfer;
121 }
122
GetRange(const int32_t & dataspace,ColorRange * color_range)123 void GetRange(const int32_t &dataspace, ColorRange *color_range) {
124 auto range = dataspace & HAL_DATASPACE_RANGE_MASK;
125 switch (range) {
126 case HAL_DATASPACE_RANGE_FULL:
127 *color_range = Range_Full;
128 break;
129 case HAL_DATASPACE_RANGE_LIMITED:
130 *color_range = Range_Limited;
131 break;
132 default:
133 DLOGV_IF(kTagClient, "Unsupported Range Request = %d", range);
134 break;
135 }
136 }
137
IsBT2020(const ColorPrimaries & color_primary)138 bool IsBT2020(const ColorPrimaries &color_primary) {
139 switch (color_primary) {
140 case ColorPrimaries_BT2020:
141 return true;
142 break;
143 default:
144 return false;
145 }
146 }
147
148 // Map the known color modes to dataspace.
GetDataspace(ColorMode mode)149 int32_t GetDataspace(ColorMode mode) {
150 switch (mode) {
151 case ColorMode::SRGB:
152 case ColorMode::NATIVE:
153 return HAL_DATASPACE_V0_SRGB;
154 case ColorMode::DCI_P3:
155 return HAL_DATASPACE_DCI_P3;
156 case ColorMode::DISPLAY_P3:
157 return HAL_DATASPACE_DISPLAY_P3;
158 case ColorMode::BT2100_PQ:
159 return HAL_DATASPACE_BT2020_PQ;
160 case ColorMode::BT2100_HLG:
161 return HAL_DATASPACE_BT2020_HLG;
162 default:
163 return HAL_DATASPACE_UNKNOWN;
164 }
165 }
166
167 // Retrieve ColorMetaData from android_data_space_t (STANDARD|TRANSFER|RANGE)
GetSDMColorSpace(const int32_t & dataspace,ColorMetaData * color_metadata)168 bool GetSDMColorSpace(const int32_t &dataspace, ColorMetaData *color_metadata) {
169 bool valid = false;
170 valid = GetColorPrimary(dataspace, &(color_metadata->colorPrimaries));
171 if (!valid) {
172 return valid;
173 }
174 valid = GetTransfer(dataspace, &(color_metadata->transfer));
175 if (!valid) {
176 return valid;
177 }
178 GetRange(dataspace, &(color_metadata->range));
179
180 return true;
181 }
182
183 // Layer operations
HWCLayer(hwc2_display_t display_id,HWCBufferAllocator * buf_allocator)184 HWCLayer::HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator)
185 : id_(next_id_++), display_id_(display_id), buffer_allocator_(buf_allocator) {
186 layer_ = new Layer();
187 // Fences are deferred, so the first time this layer is presented, return -1
188 // TODO(user): Verify that fences are properly obtained on suspend/resume
189 release_fences_.push_back(-1);
190 }
191
~HWCLayer()192 HWCLayer::~HWCLayer() {
193 // Close any fences left for this layer
194 while (!release_fences_.empty()) {
195 ::close(release_fences_.front());
196 release_fences_.pop_front();
197 }
198 if (layer_) {
199 if (layer_->input_buffer.acquire_fence_fd >= 0) {
200 ::close(layer_->input_buffer.acquire_fence_fd);
201 }
202 if (buffer_fd_ >= 0) {
203 ::close(buffer_fd_);
204 }
205 delete layer_;
206 }
207 }
208
SetLayerBuffer(buffer_handle_t buffer,int32_t acquire_fence)209 HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence) {
210 if (!buffer) {
211 if (client_requested_ == HWC2::Composition::Device ||
212 client_requested_ == HWC2::Composition::Cursor) {
213 DLOGE("Invalid buffer handle: %p on layer: %d client requested comp type %d", buffer, id_,
214 client_requested_);
215 ::close(acquire_fence);
216 return HWC2::Error::BadParameter;
217 } else {
218 return HWC2::Error::None;
219 }
220 }
221
222 if (acquire_fence == 0) {
223 DLOGE("acquire_fence is zero");
224 return HWC2::Error::BadParameter;
225 }
226
227 const private_handle_t *handle = static_cast<const private_handle_t *>(buffer);
228
229 if (handle->fd < 0) {
230 return HWC2::Error::BadParameter;
231 }
232
233 LayerBuffer *layer_buffer = &layer_->input_buffer;
234 int aligned_width, aligned_height;
235 buffer_allocator_->GetCustomWidthAndHeight(handle, &aligned_width, &aligned_height);
236
237 LayerBufferFormat format = GetSDMFormat(handle->format, handle->flags);
238 if ((format != layer_buffer->format) || (UINT32(aligned_width) != layer_buffer->width) ||
239 (UINT32(aligned_height) != layer_buffer->height)) {
240 // Layer buffer geometry has changed.
241 geometry_changes_ |= kBufferGeometry;
242 }
243
244 layer_buffer->format = format;
245 layer_buffer->width = UINT32(aligned_width);
246 layer_buffer->height = UINT32(aligned_height);
247 layer_buffer->unaligned_width = UINT32(handle->unaligned_width);
248 layer_buffer->unaligned_height = UINT32(handle->unaligned_height);
249
250 if (SetMetaData(const_cast<private_handle_t *>(handle), layer_) != kErrorNone) {
251 return HWC2::Error::BadLayer;
252 }
253
254 layer_buffer->flags.video = (handle->buffer_type == BUFFER_TYPE_VIDEO) ? true : false;
255
256 // TZ Protected Buffer - L1
257 bool secure = (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER);
258 bool secure_camera = secure && (handle->flags & private_handle_t::PRIV_FLAGS_CAMERA_WRITE);
259 bool secure_display = (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY);
260 if (secure != layer_buffer->flags.secure || secure_camera != layer_buffer->flags.secure_camera ||
261 secure_display != layer_buffer->flags.secure_display) {
262 // Secure attribute of layer buffer has changed.
263 needs_validate_ = true;
264 }
265
266 layer_buffer->flags.secure = secure;
267 layer_buffer->flags.secure_camera = secure_camera;
268 layer_buffer->flags.secure_display = secure_display;
269
270 if (layer_buffer->acquire_fence_fd >= 0) {
271 ::close(layer_buffer->acquire_fence_fd);
272 }
273 layer_buffer->acquire_fence_fd = acquire_fence;
274 if (buffer_fd_ >= 0) {
275 ::close(buffer_fd_);
276 }
277 buffer_fd_ = ::dup(handle->fd);
278 layer_buffer->planes[0].fd = buffer_fd_;
279 layer_buffer->planes[0].offset = handle->offset;
280 layer_buffer->planes[0].stride = UINT32(handle->width);
281 layer_buffer->size = handle->size;
282 layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
283 layer_buffer->handle_id = handle->id;
284
285 return HWC2::Error::None;
286 }
287
SetLayerSurfaceDamage(hwc_region_t damage)288 HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
289 surface_updated_ = true;
290 if ((damage.numRects == 1) && (damage.rects[0].bottom == 0) && (damage.rects[0].right == 0)) {
291 surface_updated_ = false;
292 }
293
294 if (!layer_->flags.updating && surface_updated_) {
295 needs_validate_ = true;
296 }
297
298 if (!partial_update_enabled_) {
299 SetDirtyRegions(damage);
300 return HWC2::Error::None;
301 }
302
303 // Check if there is an update in SurfaceDamage rects.
304 if (layer_->dirty_regions.size() != damage.numRects) {
305 needs_validate_ = true;
306 } else {
307 for (uint32_t j = 0; j < damage.numRects; j++) {
308 LayerRect damage_rect;
309 SetRect(damage.rects[j], &damage_rect);
310 if (damage_rect != layer_->dirty_regions.at(j)) {
311 needs_validate_ = true;
312 break;
313 }
314 }
315 }
316
317 SetDirtyRegions(damage);
318 return HWC2::Error::None;
319 }
320
SetLayerBlendMode(HWC2::BlendMode mode)321 HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) {
322 LayerBlending blending = kBlendingPremultiplied;
323 switch (mode) {
324 case HWC2::BlendMode::Coverage:
325 blending = kBlendingCoverage;
326 break;
327 case HWC2::BlendMode::Premultiplied:
328 blending = kBlendingPremultiplied;
329 break;
330 case HWC2::BlendMode::None:
331 blending = kBlendingOpaque;
332 break;
333 default:
334 return HWC2::Error::BadParameter;
335 }
336
337 if (layer_->blending != blending) {
338 geometry_changes_ |= kBlendMode;
339 layer_->blending = blending;
340 }
341 return HWC2::Error::None;
342 }
343
SetLayerColor(hwc_color_t color)344 HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) {
345 if (client_requested_ != HWC2::Composition::SolidColor) {
346 return HWC2::Error::None;
347 }
348 layer_->solid_fill_color = GetUint32Color(color);
349 layer_->input_buffer.format = kFormatARGB8888;
350 DLOGV_IF(kTagClient, "[%" PRIu64 "][%" PRIu64 "] Layer color set to %x", display_id_, id_,
351 layer_->solid_fill_color);
352 return HWC2::Error::None;
353 }
354
SetLayerCompositionType(HWC2::Composition type)355 HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) {
356 // Validation is required when the client changes the composition type
357 if (client_requested_ != type) {
358 needs_validate_ = true;
359 }
360 client_requested_ = type;
361 switch (type) {
362 case HWC2::Composition::Client:
363 break;
364 case HWC2::Composition::Device:
365 // We try and default to this in SDM
366 break;
367 case HWC2::Composition::SolidColor:
368 break;
369 case HWC2::Composition::Cursor:
370 break;
371 case HWC2::Composition::Invalid:
372 return HWC2::Error::BadParameter;
373 default:
374 return HWC2::Error::Unsupported;
375 }
376
377 return HWC2::Error::None;
378 }
379
SetLayerDataspace(int32_t dataspace)380 HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) {
381 // Map deprecated dataspace values to appropriate
382 // new enums
383 if (dataspace & 0xffff) {
384 switch (dataspace & 0xffff) {
385 case HAL_DATASPACE_SRGB:
386 dataspace = HAL_DATASPACE_V0_SRGB;
387 break;
388 case HAL_DATASPACE_JFIF:
389 dataspace = HAL_DATASPACE_V0_JFIF;
390 break;
391 case HAL_DATASPACE_SRGB_LINEAR:
392 dataspace = HAL_DATASPACE_V0_SRGB_LINEAR;
393 break;
394 case HAL_DATASPACE_BT601_625:
395 dataspace = HAL_DATASPACE_V0_BT601_625;
396 break;
397 case HAL_DATASPACE_BT601_525:
398 dataspace = HAL_DATASPACE_V0_BT601_525;
399 break;
400 case HAL_DATASPACE_BT709:
401 dataspace = HAL_DATASPACE_V0_BT709;
402 break;
403 default:
404 // unknown legacy dataspace
405 DLOGW_IF(kTagClient, "Unsupported dataspace type %d", dataspace);
406 }
407 }
408
409 // cache the dataspace, to be used later to update SDM ColorMetaData
410 if (dataspace_ != dataspace) {
411 geometry_changes_ |= kDataspace;
412 dataspace_ = dataspace;
413 }
414 return HWC2::Error::None;
415 }
416
SetLayerDisplayFrame(hwc_rect_t frame)417 HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
418 LayerRect dst_rect = {};
419
420 SetRect(frame, &dst_rect);
421 if (dst_rect_ != dst_rect) {
422 geometry_changes_ |= kDisplayFrame;
423 dst_rect_ = dst_rect;
424 }
425
426 return HWC2::Error::None;
427 }
428
ResetPerFrameData()429 void HWCLayer::ResetPerFrameData() {
430 layer_->dst_rect = dst_rect_;
431 layer_->transform = layer_transform_;
432 }
433
SetCursorPosition(int32_t x,int32_t y)434 HWC2::Error HWCLayer::SetCursorPosition(int32_t x, int32_t y) {
435 hwc_rect_t frame = {};
436 frame.left = x;
437 frame.top = y;
438 frame.right = x + INT(layer_->dst_rect.right - layer_->dst_rect.left);
439 frame.bottom = y + INT(layer_->dst_rect.bottom - layer_->dst_rect.top);
440 SetLayerDisplayFrame(frame);
441
442 return HWC2::Error::None;
443 }
444
SetLayerPlaneAlpha(float alpha)445 HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
446 if (alpha < 0.0f || alpha > 1.0f) {
447 return HWC2::Error::BadParameter;
448 }
449
450 // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
451 uint8_t plane_alpha = static_cast<uint8_t>(std::round(255.0f * alpha));
452
453 if (layer_->plane_alpha != plane_alpha) {
454 geometry_changes_ |= kPlaneAlpha;
455 layer_->plane_alpha = plane_alpha;
456 }
457
458 return HWC2::Error::None;
459 }
460
SetLayerSourceCrop(hwc_frect_t crop)461 HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) {
462 LayerRect src_rect = {};
463 SetRect(crop, &src_rect);
464 non_integral_source_crop_ = ((crop.left != roundf(crop.left)) ||
465 (crop.top != roundf(crop.top)) ||
466 (crop.right != roundf(crop.right)) ||
467 (crop.bottom != roundf(crop.bottom)));
468 if (layer_->src_rect != src_rect) {
469 geometry_changes_ |= kSourceCrop;
470 layer_->src_rect = src_rect;
471 }
472
473 return HWC2::Error::None;
474 }
475
SetLayerTransform(HWC2::Transform transform)476 HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
477 LayerTransform layer_transform = {};
478 switch (transform) {
479 case HWC2::Transform::FlipH:
480 layer_transform.flip_horizontal = true;
481 break;
482 case HWC2::Transform::FlipV:
483 layer_transform.flip_vertical = true;
484 break;
485 case HWC2::Transform::Rotate90:
486 layer_transform.rotation = 90.0f;
487 break;
488 case HWC2::Transform::Rotate180:
489 layer_transform.flip_horizontal = true;
490 layer_transform.flip_vertical = true;
491 break;
492 case HWC2::Transform::Rotate270:
493 layer_transform.rotation = 90.0f;
494 layer_transform.flip_horizontal = true;
495 layer_transform.flip_vertical = true;
496 break;
497 case HWC2::Transform::FlipHRotate90:
498 layer_transform.rotation = 90.0f;
499 layer_transform.flip_horizontal = true;
500 break;
501 case HWC2::Transform::FlipVRotate90:
502 layer_transform.rotation = 90.0f;
503 layer_transform.flip_vertical = true;
504 break;
505 case HWC2::Transform::None:
506 break;
507 default:
508 // bad transform
509 return HWC2::Error::BadParameter;
510 }
511
512 if (layer_transform_ != layer_transform) {
513 geometry_changes_ |= kTransform;
514 layer_transform_ = layer_transform;
515 }
516
517 return HWC2::Error::None;
518 }
519
SetLayerVisibleRegion(hwc_region_t visible)520 HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
521 layer_->visible_regions.clear();
522 for (uint32_t i = 0; i < visible.numRects; i++) {
523 LayerRect rect;
524 SetRect(visible.rects[i], &rect);
525 layer_->visible_regions.push_back(rect);
526 }
527
528 return HWC2::Error::None;
529 }
530
SetLayerZOrder(uint32_t z)531 HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
532 if (z_ != z) {
533 geometry_changes_ |= kZOrder;
534 z_ = z;
535 }
536 return HWC2::Error::None;
537 }
538
SetLayerColorTransform(const float * matrix)539 HWC2::Error HWCLayer::SetLayerColorTransform(const float *matrix) {
540 color_transform_matrix_set_ =
541 (std::memcmp(matrix, kIdentityMatrix, sizeof(kIdentityMatrix)) != 0);
542 return HWC2::Error::None;
543 }
544
SetLayerPerFrameMetadata(uint32_t num_elements,const PerFrameMetadataKey * keys,const float * metadata)545 HWC2::Error HWCLayer::SetLayerPerFrameMetadata(uint32_t num_elements,
546 const PerFrameMetadataKey *keys,
547 const float *metadata) {
548 auto &mastering_display = layer_->input_buffer.color_metadata.masteringDisplayInfo;
549 auto &content_light = layer_->input_buffer.color_metadata.contentLightLevel;
550 for (uint32_t i = 0; i < num_elements; i++) {
551 switch (keys[i]) {
552 case PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X:
553 mastering_display.colorVolumeSEIEnabled = true;
554 mastering_display.primaries.rgbPrimaries[0][0] = UINT32(metadata[i] * 50000);
555 break;
556 case PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y:
557 mastering_display.primaries.rgbPrimaries[0][1] = UINT32(metadata[i] * 50000);
558 break;
559 case PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X:
560 mastering_display.primaries.rgbPrimaries[1][0] = UINT32(metadata[i] * 50000);
561 break;
562 case PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y:
563 mastering_display.primaries.rgbPrimaries[1][1] = UINT32(metadata[i] * 50000);
564 break;
565 case PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X:
566 mastering_display.primaries.rgbPrimaries[2][0] = UINT32(metadata[i] * 50000);
567 break;
568 case PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y:
569 mastering_display.primaries.rgbPrimaries[2][1] = UINT32(metadata[i] * 50000);
570 break;
571 case PerFrameMetadataKey::WHITE_POINT_X:
572 mastering_display.primaries.whitePoint[0] = UINT32(metadata[i] * 50000);
573 break;
574 case PerFrameMetadataKey::WHITE_POINT_Y:
575 mastering_display.primaries.whitePoint[1] = UINT32(metadata[i] * 50000);
576 break;
577 case PerFrameMetadataKey::MAX_LUMINANCE:
578 mastering_display.maxDisplayLuminance = UINT32(metadata[i]);
579 break;
580 case PerFrameMetadataKey::MIN_LUMINANCE:
581 mastering_display.minDisplayLuminance = UINT32(metadata[i] * 10000);
582 break;
583 case PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL:
584 content_light.lightLevelSEIEnabled = true;
585 content_light.maxContentLightLevel = UINT32(metadata[i]);
586 break;
587 case PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL:
588 content_light.minPicAverageLightLevel = UINT32(metadata[i] * 10000);
589 break;
590 }
591 }
592 return HWC2::Error::None;
593 }
594
SetRect(const hwc_rect_t & source,LayerRect * target)595 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
596 target->left = FLOAT(source.left);
597 target->top = FLOAT(source.top);
598 target->right = FLOAT(source.right);
599 target->bottom = FLOAT(source.bottom);
600 }
601
SetRect(const hwc_frect_t & source,LayerRect * target)602 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
603 // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
604 target->left = std::ceil(source.left);
605 target->top = std::ceil(source.top);
606 target->right = std::floor(source.right);
607 target->bottom = std::floor(source.bottom);
608 }
609
GetUint32Color(const hwc_color_t & source)610 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
611 // Returns 32 bit ARGB
612 uint32_t a = UINT32(source.a) << 24;
613 uint32_t r = UINT32(source.r) << 16;
614 uint32_t g = UINT32(source.g) << 8;
615 uint32_t b = UINT32(source.b);
616 uint32_t color = a | r | g | b;
617 return color;
618 }
619
GetSDMFormat(const int32_t & source,const int flags)620 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
621 LayerBufferFormat format = kFormatInvalid;
622 if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
623 switch (source) {
624 case HAL_PIXEL_FORMAT_RGBA_8888:
625 format = kFormatRGBA8888Ubwc;
626 break;
627 case HAL_PIXEL_FORMAT_RGBX_8888:
628 format = kFormatRGBX8888Ubwc;
629 break;
630 case HAL_PIXEL_FORMAT_BGR_565:
631 format = kFormatBGR565Ubwc;
632 break;
633 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
634 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
635 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
636 format = kFormatYCbCr420SPVenusUbwc;
637 break;
638 case HAL_PIXEL_FORMAT_RGBA_1010102:
639 format = kFormatRGBA1010102Ubwc;
640 break;
641 case HAL_PIXEL_FORMAT_RGBX_1010102:
642 format = kFormatRGBX1010102Ubwc;
643 break;
644 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
645 format = kFormatYCbCr420TP10Ubwc;
646 break;
647 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
648 format = kFormatYCbCr420P010Ubwc;
649 break;
650 default:
651 DLOGE("Unsupported format type for UBWC %d", source);
652 return kFormatInvalid;
653 }
654 return format;
655 }
656
657 switch (source) {
658 case HAL_PIXEL_FORMAT_RGBA_8888:
659 format = kFormatRGBA8888;
660 break;
661 case HAL_PIXEL_FORMAT_RGBA_5551:
662 format = kFormatRGBA5551;
663 break;
664 case HAL_PIXEL_FORMAT_RGBA_4444:
665 format = kFormatRGBA4444;
666 break;
667 case HAL_PIXEL_FORMAT_BGRA_8888:
668 format = kFormatBGRA8888;
669 break;
670 case HAL_PIXEL_FORMAT_RGBX_8888:
671 format = kFormatRGBX8888;
672 break;
673 case HAL_PIXEL_FORMAT_BGRX_8888:
674 format = kFormatBGRX8888;
675 break;
676 case HAL_PIXEL_FORMAT_RGB_888:
677 format = kFormatRGB888;
678 break;
679 case HAL_PIXEL_FORMAT_BGR_888:
680 format = kFormatBGR888;
681 break;
682 case HAL_PIXEL_FORMAT_RGB_565:
683 format = kFormatRGB565;
684 break;
685 case HAL_PIXEL_FORMAT_BGR_565:
686 format = kFormatBGR565;
687 break;
688 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
689 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
690 format = kFormatYCbCr420SemiPlanarVenus;
691 break;
692 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
693 format = kFormatYCrCb420SemiPlanarVenus;
694 break;
695 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
696 format = kFormatYCbCr420SPVenusUbwc;
697 break;
698 case HAL_PIXEL_FORMAT_YV12:
699 format = kFormatYCrCb420PlanarStride16;
700 break;
701 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
702 case HAL_PIXEL_FORMAT_NV21_ZSL:
703 format = kFormatYCrCb420SemiPlanar;
704 break;
705 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
706 format = kFormatYCbCr420SemiPlanar;
707 break;
708 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
709 format = kFormatYCbCr422H2V1SemiPlanar;
710 break;
711 case HAL_PIXEL_FORMAT_YCbCr_422_I:
712 format = kFormatYCbCr422H2V1Packed;
713 break;
714 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
715 format = kFormatCbYCrY422H2V1Packed;
716 break;
717 case HAL_PIXEL_FORMAT_RGBA_1010102:
718 format = kFormatRGBA1010102;
719 break;
720 case HAL_PIXEL_FORMAT_ARGB_2101010:
721 format = kFormatARGB2101010;
722 break;
723 case HAL_PIXEL_FORMAT_RGBX_1010102:
724 format = kFormatRGBX1010102;
725 break;
726 case HAL_PIXEL_FORMAT_XRGB_2101010:
727 format = kFormatXRGB2101010;
728 break;
729 case HAL_PIXEL_FORMAT_BGRA_1010102:
730 format = kFormatBGRA1010102;
731 break;
732 case HAL_PIXEL_FORMAT_ABGR_2101010:
733 format = kFormatABGR2101010;
734 break;
735 case HAL_PIXEL_FORMAT_BGRX_1010102:
736 format = kFormatBGRX1010102;
737 break;
738 case HAL_PIXEL_FORMAT_XBGR_2101010:
739 format = kFormatXBGR2101010;
740 break;
741 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
742 format = kFormatYCbCr420P010;
743 break;
744 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
745 format = kFormatYCbCr420TP10Ubwc;
746 break;
747 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
748 format = kFormatYCbCr420P010Ubwc;
749 break;
750 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
751 format = kFormatYCbCr420P010Venus;
752 break;
753 case HAL_PIXEL_FORMAT_RGBA_FP16:
754 format = kFormatInvalid;
755 break;
756 default:
757 DLOGW("Unsupported format type = %d", source);
758 return kFormatInvalid;
759 }
760
761 return format;
762 }
763
GetS3DFormat(uint32_t s3d_format)764 LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
765 LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
766 switch (s3d_format) {
767 case HAL_NO_3D:
768 sdm_s3d_format = kS3dFormatNone;
769 break;
770 case HAL_3D_SIDE_BY_SIDE_L_R:
771 sdm_s3d_format = kS3dFormatLeftRight;
772 break;
773 case HAL_3D_SIDE_BY_SIDE_R_L:
774 sdm_s3d_format = kS3dFormatRightLeft;
775 break;
776 case HAL_3D_TOP_BOTTOM:
777 sdm_s3d_format = kS3dFormatTopBottom;
778 break;
779 default:
780 DLOGW("Invalid S3D format %d", s3d_format);
781 }
782 return sdm_s3d_format;
783 }
784
GetUBWCStatsFromMetaData(UBWCStats * cr_stats,UbwcCrStatsVector * cr_vec)785 void HWCLayer::GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec) {
786 // TODO(user): Check if we can use UBWCStats directly
787 // in layer_buffer or copy directly to Vector
788 if (cr_stats->bDataValid) {
789 switch (cr_stats->version) {
790 case UBWC_2_0:
791 cr_vec->push_back(std::make_pair(32, cr_stats->ubwc_stats.nCRStatsTile32));
792 cr_vec->push_back(std::make_pair(64, cr_stats->ubwc_stats.nCRStatsTile64));
793 cr_vec->push_back(std::make_pair(96, cr_stats->ubwc_stats.nCRStatsTile96));
794 cr_vec->push_back(std::make_pair(128, cr_stats->ubwc_stats.nCRStatsTile128));
795 cr_vec->push_back(std::make_pair(160, cr_stats->ubwc_stats.nCRStatsTile160));
796 cr_vec->push_back(std::make_pair(192, cr_stats->ubwc_stats.nCRStatsTile192));
797 cr_vec->push_back(std::make_pair(256, cr_stats->ubwc_stats.nCRStatsTile256));
798 break;
799 default:
800 DLOGW("Invalid UBWC Version %d", cr_stats->version);
801 break;
802 } // switch(cr_stats->version)
803 } // if (cr_stats->bDatvalid)
804 }
805
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)806 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
807 LayerBuffer *layer_buffer = &layer->input_buffer;
808 private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
809 IGC_t igc = {};
810 LayerIGC layer_igc = layer_buffer->igc;
811 if (getMetaData(handle, GET_IGC, &igc) == 0) {
812 if (SetIGC(igc, &layer_igc) != kErrorNone) {
813 return kErrorNotSupported;
814 }
815 }
816
817 float fps = 0;
818 uint32_t frame_rate = layer->frame_rate;
819 if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
820 frame_rate = (fps != 0) ? RoundToStandardFPS(fps) : layer->frame_rate;
821 has_metadata_refresh_rate_ = true;
822 }
823
824 int32_t interlaced = 0;
825 getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced);
826 bool interlace = interlaced ? true : false;
827
828 if (interlace != layer_buffer->flags.interlace) {
829 DLOGI("Layer buffer interlaced metadata has changed. old=%d, new=%d",
830 layer_buffer->flags.interlace, interlace);
831 }
832
833 uint32_t linear_format = 0;
834 if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
835 layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
836 }
837
838 uint32_t s3d = 0;
839 LayerBufferS3DFormat s3d_format = layer_buffer->s3d_format;
840 if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
841 s3d_format = GetS3DFormat(s3d);
842 }
843
844 if ((layer_igc != layer_buffer->igc) || (interlace != layer_buffer->flags.interlace) ||
845 (frame_rate != layer->frame_rate) || (s3d_format != layer_buffer->s3d_format)) {
846 // Layer buffer metadata has changed.
847 needs_validate_ = true;
848 layer_buffer->igc = layer_igc;
849 layer->frame_rate = frame_rate;
850 layer_buffer->s3d_format = s3d_format;
851 layer_buffer->flags.interlace = interlace;
852 }
853
854 // Check if metadata is set
855 struct UBWCStats cr_stats[NUM_UBWC_CR_STATS_LAYERS] = {};
856
857 for (int i = 0; i < NUM_UBWC_CR_STATS_LAYERS; i++) {
858 layer_buffer->ubwc_crstats[i].clear();
859 }
860
861 if (getMetaData(handle, GET_UBWC_CR_STATS_INFO, cr_stats) == 0) {
862 // Only copy top layer for now as only top field for interlaced is used
863 GetUBWCStatsFromMetaData(&cr_stats[0], &(layer_buffer->ubwc_crstats[0]));
864 } // if (getMetaData)
865
866 single_buffer_ = false;
867 getMetaData(const_cast<private_handle_t *>(handle), GET_SINGLE_BUFFER_MODE, &single_buffer_);
868
869 return kErrorNone;
870 }
871
SetIGC(IGC_t source,LayerIGC * target)872 DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
873 switch (source) {
874 case IGC_NotSpecified:
875 *target = kIGCNotSpecified;
876 break;
877 case IGC_sRGB:
878 *target = kIGCsRGB;
879 break;
880 default:
881 DLOGE("Unsupported IGC: %d", source);
882 return kErrorNotSupported;
883 }
884
885 return kErrorNone;
886 }
887
888
889
SupportLocalConversion(ColorPrimaries working_primaries)890 bool HWCLayer::SupportLocalConversion(ColorPrimaries working_primaries) {
891 if (layer_->input_buffer.color_metadata.colorPrimaries <= ColorPrimaries_BT601_6_525 &&
892 working_primaries <= ColorPrimaries_BT601_6_525) {
893 return true;
894 }
895
896 if ((working_primaries == ColorPrimaries_BT709_5 || working_primaries == ColorPrimaries_DCIP3) &&
897 layer_->input_buffer.flags.video &&
898 IsBT2020(layer_->input_buffer.color_metadata.colorPrimaries) &&
899 layer_->input_buffer.color_metadata.transfer == Transfer_SMPTE_170M) {
900 return true;
901 }
902
903 if (layer_->input_buffer.flags.secure)
904 return true;
905
906 return false;
907 }
908
ValidateAndSetCSC()909 bool HWCLayer::ValidateAndSetCSC() {
910 if (client_requested_ != HWC2::Composition::Device &&
911 client_requested_ != HWC2::Composition::Cursor &&
912 client_requested_ != HWC2::Composition::SolidColor) {
913 // Check the layers which are configured to Device
914 return true;
915 }
916
917 LayerBuffer *layer_buffer = &layer_->input_buffer;
918 bool use_color_metadata = true;
919 ColorMetaData csc = {};
920 if (dataspace_ != HAL_DATASPACE_UNKNOWN) {
921 use_color_metadata = false;
922 bool valid_csc = GetSDMColorSpace(dataspace_, &csc);
923 if (!valid_csc) {
924 return false;
925 }
926 // if we are here here, update the sdm layer csc.
927 layer_buffer->color_metadata.transfer = csc.transfer;
928 layer_buffer->color_metadata.colorPrimaries = csc.colorPrimaries;
929 layer_buffer->color_metadata.range = csc.range;
930 }
931
932 if (IsBT2020(layer_buffer->color_metadata.colorPrimaries)) {
933 // android_dataspace_t doesnt support mastering display and light levels
934 // so retrieve it from metadata for BT2020(HDR)
935 use_color_metadata = true;
936 }
937
938 if (use_color_metadata && client_requested_ != HWC2::Composition::SolidColor) {
939 const private_handle_t *handle =
940 reinterpret_cast<const private_handle_t *>(layer_buffer->buffer_id);
941 if (sdm::SetCSC(handle, &layer_buffer->color_metadata) != kErrorNone) {
942 return false;
943 }
944 }
945
946 return true;
947 }
948
949
RoundToStandardFPS(float fps)950 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
951 static const uint32_t standard_fps[4] = {24, 30, 48, 60};
952 uint32_t frame_rate = (uint32_t)(fps);
953
954 int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
955 for (int i = 0; i < count; i++) {
956 if ((standard_fps[i] - frame_rate) < 2) {
957 // Most likely used for video, the fps can fluctuate
958 // Ex: b/w 29 and 30 for 30 fps clip
959 return standard_fps[i];
960 }
961 }
962
963 return frame_rate;
964 }
965
SetComposition(const LayerComposition & sdm_composition)966 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
967 auto hwc_composition = HWC2::Composition::Invalid;
968 switch (sdm_composition) {
969 case kCompositionGPU:
970 hwc_composition = HWC2::Composition::Client;
971 break;
972 case kCompositionCursor:
973 hwc_composition = HWC2::Composition::Cursor;
974 break;
975 default:
976 hwc_composition = HWC2::Composition::Device;
977 break;
978 }
979 // Update solid fill composition
980 if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
981 hwc_composition = HWC2::Composition::SolidColor;
982 }
983 device_selected_ = hwc_composition;
984
985 return;
986 }
987
PushBackReleaseFence(int32_t fence)988 void HWCLayer::PushBackReleaseFence(int32_t fence) {
989 release_fences_.push_back(fence);
990 }
991
PopBackReleaseFence()992 int32_t HWCLayer::PopBackReleaseFence() {
993 if (release_fences_.empty())
994 return -1;
995
996 auto fence = release_fences_.back();
997 release_fences_.pop_back();
998
999 return fence;
1000 }
1001
PopFrontReleaseFence()1002 int32_t HWCLayer::PopFrontReleaseFence() {
1003 if (release_fences_.empty())
1004 return -1;
1005
1006 auto fence = release_fences_.front();
1007 release_fences_.pop_front();
1008
1009 return fence;
1010 }
1011
IsRotationPresent()1012 bool HWCLayer::IsRotationPresent() {
1013 return ((layer_->transform.rotation != 0.0f) ||
1014 layer_->transform.flip_horizontal ||
1015 layer_->transform.flip_vertical);
1016 }
1017
IsScalingPresent()1018 bool HWCLayer::IsScalingPresent() {
1019 uint32_t src_width = static_cast<uint32_t>(layer_->src_rect.right - layer_->src_rect.left);
1020 uint32_t src_height = static_cast<uint32_t>(layer_->src_rect.bottom - layer_->src_rect.top);
1021 uint32_t dst_width = static_cast<uint32_t>(layer_->dst_rect.right - layer_->dst_rect.left);
1022 uint32_t dst_height = static_cast<uint32_t>(layer_->dst_rect.bottom - layer_->dst_rect.top);
1023
1024 return ((src_width != dst_width) || (dst_height != src_height));
1025 }
1026
SetDirtyRegions(hwc_region_t surface_damage)1027 void HWCLayer::SetDirtyRegions(hwc_region_t surface_damage) {
1028 layer_->dirty_regions.clear();
1029 for (uint32_t i = 0; i < surface_damage.numRects; i++) {
1030 LayerRect rect;
1031 SetRect(surface_damage.rects[i], &rect);
1032 layer_->dirty_regions.push_back(rect);
1033 }
1034 }
1035
1036 } // namespace sdm
1037