• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014-2018, 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 
SetLayerPerFrameMetadata(uint32_t num_elements,const PerFrameMetadataKey * keys,const float * metadata)539 HWC2::Error HWCLayer::SetLayerPerFrameMetadata(uint32_t num_elements,
540                                                const PerFrameMetadataKey *keys,
541                                                const float *metadata) {
542   auto &mastering_display = layer_->input_buffer.color_metadata.masteringDisplayInfo;
543   auto &content_light = layer_->input_buffer.color_metadata.contentLightLevel;
544   for (uint32_t i = 0; i < num_elements; i++) {
545     switch (keys[i]) {
546       case PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X:
547         mastering_display.colorVolumeSEIEnabled = true;
548         mastering_display.primaries.rgbPrimaries[0][0] = UINT32(metadata[i] * 50000);
549         break;
550       case PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y:
551         mastering_display.primaries.rgbPrimaries[0][1] = UINT32(metadata[i] * 50000);
552         break;
553       case PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X:
554         mastering_display.primaries.rgbPrimaries[1][0] = UINT32(metadata[i] * 50000);
555         break;
556       case PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y:
557         mastering_display.primaries.rgbPrimaries[1][1] = UINT32(metadata[i] * 50000);
558         break;
559       case PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X:
560         mastering_display.primaries.rgbPrimaries[2][0] = UINT32(metadata[i] * 50000);
561         break;
562       case PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y:
563         mastering_display.primaries.rgbPrimaries[2][1] = UINT32(metadata[i] * 50000);
564         break;
565       case PerFrameMetadataKey::WHITE_POINT_X:
566         mastering_display.primaries.whitePoint[0] = UINT32(metadata[i] * 50000);
567         break;
568       case PerFrameMetadataKey::WHITE_POINT_Y:
569         mastering_display.primaries.whitePoint[1] = UINT32(metadata[i] * 50000);
570         break;
571       case PerFrameMetadataKey::MAX_LUMINANCE:
572         mastering_display.maxDisplayLuminance = UINT32(metadata[i]);
573         break;
574       case PerFrameMetadataKey::MIN_LUMINANCE:
575         mastering_display.minDisplayLuminance = UINT32(metadata[i] * 10000);
576         break;
577       case PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL:
578         content_light.lightLevelSEIEnabled = true;
579         content_light.maxContentLightLevel = UINT32(metadata[i]);
580         break;
581       case PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL:
582         content_light.minPicAverageLightLevel = UINT32(metadata[i] * 10000);
583         break;
584     }
585   }
586   return HWC2::Error::None;
587 }
588 
SetRect(const hwc_rect_t & source,LayerRect * target)589 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
590   target->left = FLOAT(source.left);
591   target->top = FLOAT(source.top);
592   target->right = FLOAT(source.right);
593   target->bottom = FLOAT(source.bottom);
594 }
595 
SetRect(const hwc_frect_t & source,LayerRect * target)596 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
597   // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
598   target->left = std::ceil(source.left);
599   target->top = std::ceil(source.top);
600   target->right = std::floor(source.right);
601   target->bottom = std::floor(source.bottom);
602 }
603 
GetUint32Color(const hwc_color_t & source)604 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
605   // Returns 32 bit ARGB
606   uint32_t a = UINT32(source.a) << 24;
607   uint32_t r = UINT32(source.r) << 16;
608   uint32_t g = UINT32(source.g) << 8;
609   uint32_t b = UINT32(source.b);
610   uint32_t color = a | r | g | b;
611   return color;
612 }
613 
GetSDMFormat(const int32_t & source,const int flags)614 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
615   LayerBufferFormat format = kFormatInvalid;
616   if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
617     switch (source) {
618       case HAL_PIXEL_FORMAT_RGBA_8888:
619         format = kFormatRGBA8888Ubwc;
620         break;
621       case HAL_PIXEL_FORMAT_RGBX_8888:
622         format = kFormatRGBX8888Ubwc;
623         break;
624       case HAL_PIXEL_FORMAT_BGR_565:
625         format = kFormatBGR565Ubwc;
626         break;
627       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
628       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
629       case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
630         format = kFormatYCbCr420SPVenusUbwc;
631         break;
632       case HAL_PIXEL_FORMAT_RGBA_1010102:
633         format = kFormatRGBA1010102Ubwc;
634         break;
635       case HAL_PIXEL_FORMAT_RGBX_1010102:
636         format = kFormatRGBX1010102Ubwc;
637         break;
638       case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
639         format = kFormatYCbCr420TP10Ubwc;
640         break;
641       case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
642         format = kFormatYCbCr420P010Ubwc;
643         break;
644       default:
645         DLOGE("Unsupported format type for UBWC %d", source);
646         return kFormatInvalid;
647     }
648     return format;
649   }
650 
651   switch (source) {
652     case HAL_PIXEL_FORMAT_RGBA_8888:
653       format = kFormatRGBA8888;
654       break;
655     case HAL_PIXEL_FORMAT_RGBA_5551:
656       format = kFormatRGBA5551;
657       break;
658     case HAL_PIXEL_FORMAT_RGBA_4444:
659       format = kFormatRGBA4444;
660       break;
661     case HAL_PIXEL_FORMAT_BGRA_8888:
662       format = kFormatBGRA8888;
663       break;
664     case HAL_PIXEL_FORMAT_RGBX_8888:
665       format = kFormatRGBX8888;
666       break;
667     case HAL_PIXEL_FORMAT_BGRX_8888:
668       format = kFormatBGRX8888;
669       break;
670     case HAL_PIXEL_FORMAT_RGB_888:
671       format = kFormatRGB888;
672       break;
673     case HAL_PIXEL_FORMAT_BGR_888:
674       format = kFormatBGR888;
675       break;
676     case HAL_PIXEL_FORMAT_RGB_565:
677       format = kFormatRGB565;
678       break;
679     case HAL_PIXEL_FORMAT_BGR_565:
680       format = kFormatBGR565;
681       break;
682     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
683     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
684       format = kFormatYCbCr420SemiPlanarVenus;
685       break;
686     case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
687       format = kFormatYCrCb420SemiPlanarVenus;
688       break;
689     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
690       format = kFormatYCbCr420SPVenusUbwc;
691       break;
692     case HAL_PIXEL_FORMAT_YV12:
693       format = kFormatYCrCb420PlanarStride16;
694       break;
695     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
696       format = kFormatYCrCb420SemiPlanar;
697       break;
698     case HAL_PIXEL_FORMAT_YCbCr_420_SP:
699       format = kFormatYCbCr420SemiPlanar;
700       break;
701     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
702       format = kFormatYCbCr422H2V1SemiPlanar;
703       break;
704     case HAL_PIXEL_FORMAT_YCbCr_422_I:
705       format = kFormatYCbCr422H2V1Packed;
706       break;
707     case HAL_PIXEL_FORMAT_CbYCrY_422_I:
708       format = kFormatCbYCrY422H2V1Packed;
709       break;
710     case HAL_PIXEL_FORMAT_RGBA_1010102:
711       format = kFormatRGBA1010102;
712       break;
713     case HAL_PIXEL_FORMAT_ARGB_2101010:
714       format = kFormatARGB2101010;
715       break;
716     case HAL_PIXEL_FORMAT_RGBX_1010102:
717       format = kFormatRGBX1010102;
718       break;
719     case HAL_PIXEL_FORMAT_XRGB_2101010:
720       format = kFormatXRGB2101010;
721       break;
722     case HAL_PIXEL_FORMAT_BGRA_1010102:
723       format = kFormatBGRA1010102;
724       break;
725     case HAL_PIXEL_FORMAT_ABGR_2101010:
726       format = kFormatABGR2101010;
727       break;
728     case HAL_PIXEL_FORMAT_BGRX_1010102:
729       format = kFormatBGRX1010102;
730       break;
731     case HAL_PIXEL_FORMAT_XBGR_2101010:
732       format = kFormatXBGR2101010;
733       break;
734     case HAL_PIXEL_FORMAT_YCbCr_420_P010:
735       format = kFormatYCbCr420P010;
736       break;
737     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
738       format = kFormatYCbCr420TP10Ubwc;
739       break;
740     case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
741       format = kFormatYCbCr420P010Ubwc;
742       break;
743     case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
744       format = kFormatYCbCr420P010Venus;
745       break;
746     case HAL_PIXEL_FORMAT_RGBA_FP16:
747       format = kFormatInvalid;
748       break;
749     default:
750       DLOGW("Unsupported format type = %d", source);
751       return kFormatInvalid;
752   }
753 
754   return format;
755 }
756 
GetS3DFormat(uint32_t s3d_format)757 LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
758   LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
759   switch (s3d_format) {
760     case HAL_NO_3D:
761       sdm_s3d_format = kS3dFormatNone;
762       break;
763     case HAL_3D_SIDE_BY_SIDE_L_R:
764       sdm_s3d_format = kS3dFormatLeftRight;
765       break;
766     case HAL_3D_SIDE_BY_SIDE_R_L:
767       sdm_s3d_format = kS3dFormatRightLeft;
768       break;
769     case HAL_3D_TOP_BOTTOM:
770       sdm_s3d_format = kS3dFormatTopBottom;
771       break;
772     default:
773       DLOGW("Invalid S3D format %d", s3d_format);
774   }
775   return sdm_s3d_format;
776 }
777 
GetUBWCStatsFromMetaData(UBWCStats * cr_stats,UbwcCrStatsVector * cr_vec)778 void HWCLayer::GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec) {
779   // TODO(user): Check if we can use UBWCStats directly
780   // in layer_buffer or copy directly to Vector
781   if (cr_stats->bDataValid) {
782     switch (cr_stats->version) {
783       case UBWC_2_0:
784         cr_vec->push_back(std::make_pair(32, cr_stats->ubwc_stats.nCRStatsTile32));
785         cr_vec->push_back(std::make_pair(64, cr_stats->ubwc_stats.nCRStatsTile64));
786         cr_vec->push_back(std::make_pair(96, cr_stats->ubwc_stats.nCRStatsTile96));
787         cr_vec->push_back(std::make_pair(128, cr_stats->ubwc_stats.nCRStatsTile128));
788         cr_vec->push_back(std::make_pair(160, cr_stats->ubwc_stats.nCRStatsTile160));
789         cr_vec->push_back(std::make_pair(192, cr_stats->ubwc_stats.nCRStatsTile192));
790         cr_vec->push_back(std::make_pair(256, cr_stats->ubwc_stats.nCRStatsTile256));
791         break;
792       default:
793         DLOGW("Invalid UBWC Version %d", cr_stats->version);
794         break;
795     }  // switch(cr_stats->version)
796   }  // if (cr_stats->bDatvalid)
797 }
798 
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)799 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
800   LayerBuffer *layer_buffer = &layer->input_buffer;
801   private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
802   IGC_t igc = {};
803   LayerIGC layer_igc = layer_buffer->igc;
804   if (getMetaData(handle, GET_IGC, &igc) == 0) {
805     if (SetIGC(igc, &layer_igc) != kErrorNone) {
806       return kErrorNotSupported;
807     }
808   }
809 
810   float fps = 0;
811   uint32_t frame_rate = layer->frame_rate;
812   if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
813     frame_rate = (fps != 0) ? RoundToStandardFPS(fps) : layer->frame_rate;
814     has_metadata_refresh_rate_ = true;
815   }
816 
817   int32_t interlaced = 0;
818   getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced);
819   bool interlace = interlaced ? true : false;
820 
821   if (interlace != layer_buffer->flags.interlace) {
822     DLOGI("Layer buffer interlaced metadata has changed. old=%d, new=%d",
823           layer_buffer->flags.interlace, interlace);
824   }
825 
826   uint32_t linear_format = 0;
827   if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
828     layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
829   }
830 
831   uint32_t s3d = 0;
832   LayerBufferS3DFormat s3d_format = layer_buffer->s3d_format;
833   if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
834     s3d_format = GetS3DFormat(s3d);
835   }
836 
837   if ((layer_igc != layer_buffer->igc) || (interlace != layer_buffer->flags.interlace) ||
838       (frame_rate != layer->frame_rate) || (s3d_format != layer_buffer->s3d_format)) {
839     // Layer buffer metadata has changed.
840     needs_validate_ = true;
841     layer_buffer->igc = layer_igc;
842     layer->frame_rate = frame_rate;
843     layer_buffer->s3d_format = s3d_format;
844     layer_buffer->flags.interlace = interlace;
845   }
846 
847   // Check if metadata is set
848   struct UBWCStats cr_stats[NUM_UBWC_CR_STATS_LAYERS] = {};
849 
850   for (int i = 0; i < NUM_UBWC_CR_STATS_LAYERS; i++) {
851     layer_buffer->ubwc_crstats[i].clear();
852   }
853 
854   if (getMetaData(handle, GET_UBWC_CR_STATS_INFO, cr_stats) == 0) {
855   // Only copy top layer for now as only top field for interlaced is used
856     GetUBWCStatsFromMetaData(&cr_stats[0], &(layer_buffer->ubwc_crstats[0]));
857   }  // if (getMetaData)
858 
859   single_buffer_ = false;
860   getMetaData(const_cast<private_handle_t *>(handle), GET_SINGLE_BUFFER_MODE, &single_buffer_);
861 
862   return kErrorNone;
863 }
864 
SetIGC(IGC_t source,LayerIGC * target)865 DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
866   switch (source) {
867     case IGC_NotSpecified:
868       *target = kIGCNotSpecified;
869       break;
870     case IGC_sRGB:
871       *target = kIGCsRGB;
872       break;
873     default:
874       DLOGE("Unsupported IGC: %d", source);
875       return kErrorNotSupported;
876   }
877 
878   return kErrorNone;
879 }
880 
881 
882 
SupportLocalConversion(ColorPrimaries working_primaries)883 bool HWCLayer::SupportLocalConversion(ColorPrimaries working_primaries) {
884   if (layer_->input_buffer.color_metadata.colorPrimaries <= ColorPrimaries_BT601_6_525 &&
885       working_primaries <= ColorPrimaries_BT601_6_525) {
886     return true;
887   }
888 
889   if ((working_primaries == ColorPrimaries_BT709_5 || working_primaries == ColorPrimaries_DCIP3) &&
890       layer_->input_buffer.flags.video &&
891       IsBT2020(layer_->input_buffer.color_metadata.colorPrimaries) &&
892       layer_->input_buffer.color_metadata.transfer == Transfer_SMPTE_170M) {
893     return true;
894   }
895 
896   if (layer_->input_buffer.flags.secure)
897     return true;
898 
899   return false;
900 }
901 
ValidateAndSetCSC()902 bool HWCLayer::ValidateAndSetCSC() {
903   if (client_requested_ != HWC2::Composition::Device &&
904       client_requested_ != HWC2::Composition::Cursor &&
905       client_requested_ != HWC2::Composition::SolidColor) {
906     // Check the layers which are configured to Device
907     return true;
908   }
909 
910   LayerBuffer *layer_buffer = &layer_->input_buffer;
911   bool use_color_metadata = true;
912   ColorMetaData csc = {};
913   if (dataspace_ != HAL_DATASPACE_UNKNOWN) {
914     use_color_metadata = false;
915     bool valid_csc = GetSDMColorSpace(dataspace_, &csc);
916     if (!valid_csc) {
917       return false;
918     }
919     // if we are here here, update the sdm layer csc.
920     layer_buffer->color_metadata.transfer = csc.transfer;
921     layer_buffer->color_metadata.colorPrimaries = csc.colorPrimaries;
922     layer_buffer->color_metadata.range = csc.range;
923   }
924 
925   if (IsBT2020(layer_buffer->color_metadata.colorPrimaries)) {
926      // android_dataspace_t doesnt support mastering display and light levels
927      // so retrieve it from metadata for BT2020(HDR)
928      use_color_metadata = true;
929   }
930 
931   if (use_color_metadata && client_requested_ != HWC2::Composition::SolidColor) {
932     const private_handle_t *handle =
933       reinterpret_cast<const private_handle_t *>(layer_buffer->buffer_id);
934     if (sdm::SetCSC(handle, &layer_buffer->color_metadata) != kErrorNone) {
935       return false;
936     }
937   }
938 
939   return true;
940 }
941 
942 
RoundToStandardFPS(float fps)943 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
944   static const uint32_t standard_fps[4] = {24, 30, 48, 60};
945   uint32_t frame_rate = (uint32_t)(fps);
946 
947   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
948   for (int i = 0; i < count; i++) {
949     if ((standard_fps[i] - frame_rate) < 2) {
950       // Most likely used for video, the fps can fluctuate
951       // Ex: b/w 29 and 30 for 30 fps clip
952       return standard_fps[i];
953     }
954   }
955 
956   return frame_rate;
957 }
958 
SetComposition(const LayerComposition & sdm_composition)959 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
960   auto hwc_composition = HWC2::Composition::Invalid;
961   switch (sdm_composition) {
962     case kCompositionGPU:
963       hwc_composition = HWC2::Composition::Client;
964       break;
965     case kCompositionCursor:
966       hwc_composition = HWC2::Composition::Cursor;
967       break;
968     default:
969       hwc_composition = HWC2::Composition::Device;
970       break;
971   }
972   // Update solid fill composition
973   if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
974     hwc_composition = HWC2::Composition::SolidColor;
975   }
976   device_selected_ = hwc_composition;
977 
978   return;
979 }
980 
PushBackReleaseFence(int32_t fence)981 void HWCLayer::PushBackReleaseFence(int32_t fence) {
982   release_fences_.push_back(fence);
983 }
984 
PopBackReleaseFence()985 int32_t HWCLayer::PopBackReleaseFence() {
986   if (release_fences_.empty())
987     return -1;
988 
989   auto fence = release_fences_.back();
990   release_fences_.pop_back();
991 
992   return fence;
993 }
994 
PopFrontReleaseFence()995 int32_t HWCLayer::PopFrontReleaseFence() {
996   if (release_fences_.empty())
997     return -1;
998 
999   auto fence = release_fences_.front();
1000   release_fences_.pop_front();
1001 
1002   return fence;
1003 }
1004 
IsRotationPresent()1005 bool HWCLayer::IsRotationPresent() {
1006   return ((layer_->transform.rotation != 0.0f) ||
1007          layer_->transform.flip_horizontal ||
1008          layer_->transform.flip_vertical);
1009 }
1010 
IsScalingPresent()1011 bool HWCLayer::IsScalingPresent() {
1012   uint32_t src_width  = static_cast<uint32_t>(layer_->src_rect.right - layer_->src_rect.left);
1013   uint32_t src_height = static_cast<uint32_t>(layer_->src_rect.bottom - layer_->src_rect.top);
1014   uint32_t dst_width  = static_cast<uint32_t>(layer_->dst_rect.right - layer_->dst_rect.left);
1015   uint32_t dst_height = static_cast<uint32_t>(layer_->dst_rect.bottom - layer_->dst_rect.top);
1016 
1017   return ((src_width != dst_width) || (dst_height != src_height));
1018 }
1019 
SetDirtyRegions(hwc_region_t surface_damage)1020 void HWCLayer::SetDirtyRegions(hwc_region_t surface_damage) {
1021   layer_->dirty_regions.clear();
1022   for (uint32_t i = 0; i < surface_damage.numRects; i++) {
1023     LayerRect rect;
1024     SetRect(surface_damage.rects[i], &rect);
1025     layer_->dirty_regions.push_back(rect);
1026   }
1027 }
1028 
1029 }  // namespace sdm
1030