1 /*
2  * Copyright (c) 2014-2021, 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 <qdMetaData.h>
22 #include <qd_utils.h>
23 #include <utils/debug.h>
24 #include <stdint.h>
25 #include <utility>
26 #include <cmath>
27 
28 #define __CLASS__ "HWCLayer"
29 
30 namespace sdm {
31 
32 std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
33 
SetCSC(const private_handle_t * pvt_handle,ColorMetaData * color_metadata)34 DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata) {
35   if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_METADATA,
36                   color_metadata) != 0) {
37     ColorSpace_t csc = ITU_R_601;
38     if (getMetaData(const_cast<private_handle_t *>(pvt_handle),  GET_COLOR_SPACE,
39                     &csc) == 0) {
40       if (csc == ITU_R_601_FR || csc == ITU_R_2020_FR) {
41         color_metadata->range = Range_Full;
42       }
43       color_metadata->transfer = Transfer_sRGB;
44 
45       switch (csc) {
46         case ITU_R_601:
47         case ITU_R_601_FR:
48           // video and display driver uses 601_525
49           color_metadata->colorPrimaries = ColorPrimaries_BT601_6_525;
50           break;
51         case ITU_R_709:
52           color_metadata->colorPrimaries = ColorPrimaries_BT709_5;
53           break;
54         case ITU_R_2020:
55         case ITU_R_2020_FR:
56           color_metadata->colorPrimaries = ColorPrimaries_BT2020;
57           break;
58         default:
59           DLOGE("Unsupported CSC: %d", csc);
60           return kErrorNotSupported;
61       }
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       DLOGW_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_LINEAR:
114       *gamma_transfer = Transfer_Linear;
115       break;
116     case HAL_DATASPACE_TRANSFER_GAMMA2_2:
117       *gamma_transfer = Transfer_Gamma2_2;
118       break;
119     case HAL_DATASPACE_TRANSFER_GAMMA2_8:
120       *gamma_transfer = Transfer_Gamma2_8;
121       break;
122     default:
123       DLOGW_IF(kTagClient, "Unsupported Transfer Request = %d", transfer);
124       supported_transfer = false;
125   }
126   return supported_transfer;
127 }
128 
GetRange(const int32_t & dataspace,ColorRange * color_range)129 bool GetRange(const int32_t &dataspace, ColorRange *color_range) {
130   auto range = dataspace & HAL_DATASPACE_RANGE_MASK;
131   switch (range) {
132     case HAL_DATASPACE_RANGE_FULL:
133       *color_range = Range_Full;
134       break;
135     case HAL_DATASPACE_RANGE_LIMITED:
136       *color_range = Range_Limited;
137       break;
138     case HAL_DATASPACE_RANGE_EXTENDED:
139       *color_range = Range_Extended;
140       return false;
141     default:
142       DLOGW_IF(kTagClient, "Unsupported Range Request = %d", range);
143       return false;
144   }
145   return true;
146 }
147 
IsBT2020(const ColorPrimaries & color_primary)148 bool IsBT2020(const ColorPrimaries &color_primary) {
149   switch (color_primary) {
150   case ColorPrimaries_BT2020:
151     return true;
152     break;
153   default:
154     return false;
155   }
156 }
157 
TranslateFromLegacyDataspace(const int32_t & legacy_ds)158 int32_t TranslateFromLegacyDataspace(const int32_t &legacy_ds) {
159   int32_t dataspace = legacy_ds;
160 
161   if (dataspace & 0xffff) {
162     switch (dataspace & 0xffff) {
163       case HAL_DATASPACE_SRGB:
164         dataspace = HAL_DATASPACE_V0_SRGB;
165         break;
166       case HAL_DATASPACE_JFIF:
167         dataspace = HAL_DATASPACE_V0_JFIF;
168         break;
169       case HAL_DATASPACE_SRGB_LINEAR:
170         dataspace = HAL_DATASPACE_V0_SRGB_LINEAR;
171         break;
172       case HAL_DATASPACE_BT601_625:
173         dataspace = HAL_DATASPACE_V0_BT601_625;
174         break;
175       case HAL_DATASPACE_BT601_525:
176         dataspace = HAL_DATASPACE_V0_BT601_525;
177         break;
178       case HAL_DATASPACE_BT709:
179         dataspace = HAL_DATASPACE_V0_BT709;
180         break;
181       default:
182         // unknown legacy dataspace
183         DLOGW_IF(kTagClient, "Unsupported dataspace type %d", dataspace);
184     }
185   }
186 
187   if (dataspace == HAL_DATASPACE_UNKNOWN) {
188     dataspace = HAL_DATASPACE_V0_SRGB;
189   }
190 
191   return dataspace;
192 }
193 
194 // Retrieve ColorMetaData from android_data_space_t (STANDARD|TRANSFER|RANGE)
GetSDMColorSpace(const int32_t & dataspace,ColorMetaData * color_metadata)195 bool GetSDMColorSpace(const int32_t &dataspace, ColorMetaData *color_metadata) {
196   bool valid = false;
197   valid = GetColorPrimary(dataspace, &(color_metadata->colorPrimaries));
198   if (valid) {
199     valid = GetTransfer(dataspace, &(color_metadata->transfer));
200   }
201   if (valid) {
202     valid = GetRange(dataspace, &(color_metadata->range));
203   }
204 
205   return valid;
206 }
207 
ColorMetadataToDataspace(ColorMetaData color_metadata,Dataspace * dataspace)208 DisplayError ColorMetadataToDataspace(ColorMetaData color_metadata, Dataspace *dataspace) {
209   Dataspace primaries, transfer, range = Dataspace::UNKNOWN;
210 
211   switch (color_metadata.colorPrimaries) {
212     case ColorPrimaries_BT709_5:
213       primaries = Dataspace::STANDARD_BT709;
214       break;
215     case ColorPrimaries_BT470_6M:
216       primaries = Dataspace::STANDARD_BT470M;
217       break;
218     case ColorPrimaries_BT601_6_625:
219       primaries = Dataspace::STANDARD_BT601_625;
220       break;
221     case ColorPrimaries_BT601_6_525:
222       primaries = Dataspace::STANDARD_BT601_525;
223       break;
224     case ColorPrimaries_GenericFilm:
225       primaries = Dataspace::STANDARD_FILM;
226       break;
227     case ColorPrimaries_BT2020:
228       primaries = Dataspace::STANDARD_BT2020;
229       break;
230     case ColorPrimaries_AdobeRGB:
231       primaries = Dataspace::STANDARD_ADOBE_RGB;
232       break;
233     case ColorPrimaries_DCIP3:
234       primaries = Dataspace::STANDARD_DCI_P3;
235       break;
236     default:
237       return kErrorNotSupported;
238       /*
239        ColorPrimaries_SMPTE_240M;
240        ColorPrimaries_SMPTE_ST428;
241        ColorPrimaries_EBU3213;
242       */
243   }
244 
245   switch (color_metadata.transfer) {
246     case Transfer_sRGB:
247       transfer = Dataspace::TRANSFER_SRGB;
248       break;
249     case Transfer_Gamma2_2:
250       transfer = Dataspace::TRANSFER_GAMMA2_2;
251       break;
252     case Transfer_Gamma2_8:
253       transfer = Dataspace::TRANSFER_GAMMA2_8;
254       break;
255     case Transfer_SMPTE_170M:
256       transfer = Dataspace::TRANSFER_SMPTE_170M;
257       break;
258     case Transfer_Linear:
259       transfer = Dataspace::TRANSFER_LINEAR;
260       break;
261     case Transfer_SMPTE_ST2084:
262       transfer = Dataspace::TRANSFER_ST2084;
263       break;
264     case Transfer_HLG:
265       transfer = Dataspace::TRANSFER_HLG;
266       break;
267     default:
268       return kErrorNotSupported;
269       /*
270       Transfer_SMPTE_240M
271       Transfer_Log
272       Transfer_Log_Sqrt
273       Transfer_XvYCC
274       Transfer_BT1361
275       Transfer_sYCC
276       Transfer_BT2020_2_1
277       Transfer_BT2020_2_2
278       Transfer_SMPTE_ST2084
279       Transfer_ST_428
280       */
281   }
282 
283   switch (color_metadata.range) {
284     case Range_Full:
285       range = Dataspace::RANGE_FULL;
286       break;
287     case Range_Limited:
288       range = Dataspace::RANGE_LIMITED;
289       break;
290     case Range_Extended:
291       range = Dataspace::RANGE_EXTENDED;
292       break;
293     default:
294       return kErrorNotSupported;
295   }
296 
297   *dataspace = (Dataspace)((uint32_t)primaries | (uint32_t)transfer | (uint32_t)range);
298   return kErrorNone;
299 }
300 
301 // Layer operations
HWCLayer(hwc2_display_t display_id,HWCBufferAllocator * buf_allocator)302 HWCLayer::HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator)
303   : id_(next_id_++), display_id_(display_id), buffer_allocator_(buf_allocator) {
304   layer_ = new Layer();
305   // Fences are deferred, so the first time this layer is presented, return -1
306   // TODO(user): Verify that fences are properly obtained on suspend/resume
307   release_fences_.push_back(nullptr);
308 }
309 
~HWCLayer()310 HWCLayer::~HWCLayer() {
311   // Close any fences left for this layer
312   while (!release_fences_.empty()) {
313     release_fences_.pop_front();
314   }
315   if (layer_) {
316     if (buffer_fd_ >= 0) {
317       ::close(buffer_fd_);
318     }
319     delete layer_;
320   }
321 }
322 
SetLayerBuffer(buffer_handle_t buffer,shared_ptr<Fence> acquire_fence)323 HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, shared_ptr<Fence> acquire_fence) {
324   if (!buffer) {
325     if (client_requested_ == HWC2::Composition::Device ||
326         client_requested_ == HWC2::Composition::Cursor) {
327       DLOGW("Invalid buffer handle: %p on layer: %" PRIu64 " client requested comp type %d", buffer,
328             id_, client_requested_);
329       return HWC2::Error::BadParameter;
330     } else {
331       return HWC2::Error::None;
332     }
333   }
334 
335   const private_handle_t *handle = static_cast<const private_handle_t *>(buffer);
336 
337   if (handle->fd < 0) {
338     return HWC2::Error::BadParameter;
339   }
340 
341   LayerBuffer *layer_buffer = &layer_->input_buffer;
342   int aligned_width, aligned_height;
343   buffer_allocator_->GetCustomWidthAndHeight(handle, &aligned_width, &aligned_height);
344 
345   LayerBufferFormat format = GetSDMFormat(handle->format, handle->flags);
346   if ((format != layer_buffer->format) || (UINT32(aligned_width) != layer_buffer->width) ||
347       (UINT32(aligned_height) != layer_buffer->height)) {
348     // Layer buffer geometry has changed.
349     geometry_changes_ |= kBufferGeometry;
350   }
351 
352   layer_buffer->format = format;
353   layer_buffer->width = UINT32(aligned_width);
354   layer_buffer->height = UINT32(aligned_height);
355   layer_buffer->unaligned_width = UINT32(handle->unaligned_width);
356   layer_buffer->unaligned_height = UINT32(handle->unaligned_height);
357 
358   layer_buffer->flags.video = (handle->buffer_type == BUFFER_TYPE_VIDEO) ? true : false;
359   if (SetMetaData(const_cast<private_handle_t *>(handle), layer_) != kErrorNone) {
360     return HWC2::Error::BadLayer;
361   }
362 
363   // TZ Protected Buffer - L1
364   secure_ = (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER);
365   bool secure_camera = secure_ && (handle->flags & private_handle_t::PRIV_FLAGS_CAMERA_WRITE);
366   bool secure_display = (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY);
367   if (secure_ != layer_buffer->flags.secure || secure_camera != layer_buffer->flags.secure_camera ||
368       secure_display != layer_buffer->flags.secure_display) {
369     // Secure attribute of layer buffer has changed.
370     layer_->update_mask.set(kSecurity);
371   }
372 
373   layer_buffer->flags.secure = secure_;
374   layer_buffer->flags.secure_camera = secure_camera;
375   layer_buffer->flags.secure_display = secure_display;
376 
377   layer_buffer->acquire_fence = acquire_fence;
378   if (buffer_fd_ >= 0) {
379     ::close(buffer_fd_);
380   }
381   buffer_fd_ = ::dup(handle->fd);
382   layer_buffer->planes[0].fd = buffer_fd_;
383   layer_buffer->planes[0].offset = handle->offset;
384   layer_buffer->planes[0].stride = UINT32(handle->width);
385   layer_buffer->size = handle->size;
386   buffer_flipped_ = reinterpret_cast<uint64_t>(handle) != layer_buffer->buffer_id;
387   layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
388   layer_buffer->handle_id = handle->id;
389 
390   return HWC2::Error::None;
391 }
392 
SetLayerSurfaceDamage(hwc_region_t damage)393 HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
394   surface_updated_ = true;
395   if ((damage.numRects == 1) && (damage.rects[0].bottom == 0) && (damage.rects[0].right == 0)) {
396     surface_updated_ = false;
397   }
398 
399   if (!layer_->flags.updating && surface_updated_) {
400     layer_->update_mask.set(kSurfaceInvalidate);
401   }
402 
403   if (!partial_update_enabled_) {
404     SetDirtyRegions(damage);
405     return HWC2::Error::None;
406   }
407 
408   // Check if there is an update in SurfaceDamage rects.
409   if (layer_->dirty_regions.size() != damage.numRects) {
410     layer_->update_mask.set(kSurfaceInvalidate);
411   } else {
412     for (uint32_t j = 0; j < damage.numRects; j++) {
413       LayerRect damage_rect;
414       SetRect(damage.rects[j], &damage_rect);
415       if (damage_rect != layer_->dirty_regions.at(j)) {
416         layer_->update_mask.set(kSurfaceDamage);
417         break;
418       }
419     }
420   }
421 
422   SetDirtyRegions(damage);
423   return HWC2::Error::None;
424 }
425 
SetLayerBlendMode(HWC2::BlendMode mode)426 HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) {
427   LayerBlending blending = kBlendingPremultiplied;
428   switch (mode) {
429     case HWC2::BlendMode::Coverage:
430       blending = kBlendingCoverage;
431       break;
432     case HWC2::BlendMode::Premultiplied:
433       blending = kBlendingPremultiplied;
434       break;
435     case HWC2::BlendMode::None:
436       blending = kBlendingOpaque;
437       break;
438     default:
439       return HWC2::Error::BadParameter;
440   }
441 
442   if (layer_->blending != blending) {
443     geometry_changes_ |= kBlendMode;
444     layer_->blending = blending;
445   }
446   return HWC2::Error::None;
447 }
448 
SetLayerColor(hwc_color_t color)449 HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) {
450   if (client_requested_ != HWC2::Composition::SolidColor) {
451     return HWC2::Error::None;
452   }
453   if (layer_->solid_fill_color != GetUint32Color(color)) {
454     layer_->solid_fill_color = GetUint32Color(color);
455     layer_->update_mask.set(kSurfaceInvalidate);
456     surface_updated_ = true;
457   } else {
458     surface_updated_ = false;
459   }
460 
461   layer_->input_buffer.format = kFormatARGB8888;
462   DLOGV_IF(kTagClient, "[%" PRIu64 "][%" PRIu64 "] Layer color set to %x", display_id_, id_,
463            layer_->solid_fill_color);
464   return HWC2::Error::None;
465 }
466 
SetLayerCompositionType(HWC2::Composition type)467 HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) {
468   // Validation is required when the client changes the composition type
469   if (client_requested_ != type) {
470     layer_->update_mask.set(kClientCompRequest);
471   }
472   client_requested_ = type;
473   switch (type) {
474     case HWC2::Composition::Client:
475       break;
476     case HWC2::Composition::Device:
477       // We try and default to this in SDM
478       break;
479     case HWC2::Composition::SolidColor:
480       break;
481     case HWC2::Composition::Cursor:
482       break;
483     case HWC2::Composition::Invalid:
484       return HWC2::Error::BadParameter;
485     default:
486       return HWC2::Error::Unsupported;
487   }
488 
489   return HWC2::Error::None;
490 }
491 
SetLayerDataspace(int32_t dataspace)492 HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) {
493   // Map deprecated dataspace values to appropriate new enums
494   dataspace = TranslateFromLegacyDataspace(dataspace);
495 
496   // cache the dataspace, to be used later to update SDM ColorMetaData
497   if (dataspace_ != dataspace) {
498     geometry_changes_ |= kDataspace;
499     dataspace_ = dataspace;
500     if (layer_->input_buffer.buffer_id) {
501       ValidateAndSetCSC(reinterpret_cast<private_handle_t *>(layer_->input_buffer.buffer_id));
502     }
503   }
504   return HWC2::Error::None;
505 }
506 
SetLayerDisplayFrame(hwc_rect_t frame)507 HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
508   LayerRect dst_rect = {};
509 
510   SetRect(frame, &dst_rect);
511   if (dst_rect_ != dst_rect) {
512     geometry_changes_ |= kDisplayFrame;
513     dst_rect_ = dst_rect;
514   }
515 
516   return HWC2::Error::None;
517 }
518 
ResetPerFrameData()519 void HWCLayer::ResetPerFrameData() {
520   layer_->dst_rect = dst_rect_;
521   layer_->transform = layer_transform_;
522 }
523 
SetCursorPosition(int32_t x,int32_t y)524 HWC2::Error HWCLayer::SetCursorPosition(int32_t x, int32_t y) {
525   hwc_rect_t frame = {};
526   frame.left = x;
527   frame.top = y;
528   frame.right = x + INT(layer_->dst_rect.right - layer_->dst_rect.left);
529   frame.bottom = y + INT(layer_->dst_rect.bottom - layer_->dst_rect.top);
530   SetLayerDisplayFrame(frame);
531 
532   return HWC2::Error::None;
533 }
534 
SetLayerPlaneAlpha(float alpha)535 HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
536   if (alpha < 0.0f || alpha > 1.0f) {
537     return HWC2::Error::BadParameter;
538   }
539 
540   //  Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
541   uint8_t plane_alpha = static_cast<uint8_t>(std::round(255.0f * alpha));
542 
543   if (layer_->plane_alpha != plane_alpha) {
544     geometry_changes_ |= kPlaneAlpha;
545     layer_->plane_alpha = plane_alpha;
546   }
547 
548   return HWC2::Error::None;
549 }
550 
SetLayerSourceCrop(hwc_frect_t crop)551 HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) {
552   LayerRect src_rect = {};
553   SetRect(crop, &src_rect);
554   non_integral_source_crop_ = ((crop.left != roundf(crop.left)) ||
555                               (crop.top != roundf(crop.top)) ||
556                               (crop.right != roundf(crop.right)) ||
557                               (crop.bottom != roundf(crop.bottom)));
558   if (non_integral_source_crop_) {
559     DLOGV_IF(kTagClient, "Crop: LTRB %f %f %f %f", crop.left, crop.top, crop.right, crop.bottom);
560   }
561   if (layer_->src_rect != src_rect) {
562     geometry_changes_ |= kSourceCrop;
563     layer_->src_rect = src_rect;
564   }
565 
566   return HWC2::Error::None;
567 }
568 
SetLayerTransform(HWC2::Transform transform)569 HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
570   LayerTransform layer_transform = {};
571   switch (transform) {
572     case HWC2::Transform::FlipH:
573       layer_transform.flip_horizontal = true;
574       break;
575     case HWC2::Transform::FlipV:
576       layer_transform.flip_vertical = true;
577       break;
578     case HWC2::Transform::Rotate90:
579       layer_transform.rotation = 90.0f;
580       break;
581     case HWC2::Transform::Rotate180:
582       layer_transform.flip_horizontal = true;
583       layer_transform.flip_vertical = true;
584       break;
585     case HWC2::Transform::Rotate270:
586       layer_transform.rotation = 90.0f;
587       layer_transform.flip_horizontal = true;
588       layer_transform.flip_vertical = true;
589       break;
590     case HWC2::Transform::FlipHRotate90:
591       layer_transform.rotation = 90.0f;
592       layer_transform.flip_horizontal = true;
593       break;
594     case HWC2::Transform::FlipVRotate90:
595       layer_transform.rotation = 90.0f;
596       layer_transform.flip_vertical = true;
597       break;
598     case HWC2::Transform::None:
599       break;
600     default:
601       //  bad transform
602       return HWC2::Error::BadParameter;
603   }
604 
605   if (layer_transform_ != layer_transform) {
606     geometry_changes_ |= kTransform;
607     layer_transform_ = layer_transform;
608   }
609 
610   return HWC2::Error::None;
611 }
612 
SetLayerVisibleRegion(hwc_region_t visible)613 HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
614   layer_->visible_regions.clear();
615   for (uint32_t i = 0; i < visible.numRects; i++) {
616     LayerRect rect;
617     SetRect(visible.rects[i], &rect);
618     layer_->visible_regions.push_back(rect);
619   }
620 
621   return HWC2::Error::None;
622 }
623 
SetLayerZOrder(uint32_t z)624 HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
625   if (z_ != z) {
626     geometry_changes_ |= kZOrder;
627     z_ = z;
628   }
629 
630   return HWC2::Error::None;
631 }
632 
SetLayerType(IQtiComposerClient::LayerType type)633 HWC2::Error HWCLayer::SetLayerType(IQtiComposerClient::LayerType type) {
634   LayerTypes layer_type = kLayerUnknown;
635   switch (type) {
636     case IQtiComposerClient::LayerType::UNKNOWN:
637       layer_type = kLayerUnknown;
638       break;
639     case IQtiComposerClient::LayerType::APP:
640       layer_type = kLayerApp;
641       break;
642     case IQtiComposerClient::LayerType::GAME:
643       layer_type = kLayerGame;
644       break;
645     case IQtiComposerClient::LayerType::BROWSER:
646       layer_type = kLayerBrowser;
647       break;
648     default:
649       DLOGW("Unsupported layer type %d", layer_type);
650       break;
651   }
652 
653   type_ = layer_type;
654   return HWC2::Error::None;
655 }
656 
SetLayerColorTransform(const float * matrix)657 HWC2::Error HWCLayer::SetLayerColorTransform(const float *matrix) {
658   if (std::memcmp(matrix, layer_->color_transform_matrix, sizeof(layer_->color_transform_matrix))) {
659     std::memcpy(layer_->color_transform_matrix, matrix, sizeof(layer_->color_transform_matrix));
660     layer_->update_mask.set(kColorTransformUpdate);
661     color_transform_matrix_set_ = true;
662     if (!std::memcmp(matrix, kIdentityMatrix, sizeof(kIdentityMatrix))) {
663       color_transform_matrix_set_ = false;
664     }
665   }
666   return HWC2::Error::None;
667 }
668 
SetLayerPerFrameMetadata(uint32_t num_elements,const PerFrameMetadataKey * keys,const float * metadata)669 HWC2::Error HWCLayer::SetLayerPerFrameMetadata(uint32_t num_elements,
670                                                const PerFrameMetadataKey *keys,
671                                                const float *metadata) {
672   auto old_mastering_display = layer_->input_buffer.color_metadata.masteringDisplayInfo;
673   auto old_content_light = layer_->input_buffer.color_metadata.contentLightLevel;
674   auto &mastering_display = layer_->input_buffer.color_metadata.masteringDisplayInfo;
675   auto &content_light = layer_->input_buffer.color_metadata.contentLightLevel;
676   for (uint32_t i = 0; i < num_elements; i++) {
677     switch (keys[i]) {
678       case PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X:
679         mastering_display.colorVolumeSEIEnabled = true;
680         mastering_display.primaries.rgbPrimaries[0][0] = UINT32(metadata[i] * 50000);
681         break;
682       case PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y:
683         mastering_display.primaries.rgbPrimaries[0][1] = UINT32(metadata[i] * 50000);
684         break;
685       case PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X:
686         mastering_display.primaries.rgbPrimaries[1][0] = UINT32(metadata[i] * 50000);
687         break;
688       case PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y:
689         mastering_display.primaries.rgbPrimaries[1][1] = UINT32(metadata[i] * 50000);
690         break;
691       case PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X:
692         mastering_display.primaries.rgbPrimaries[2][0] = UINT32(metadata[i] * 50000);
693         break;
694       case PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y:
695         mastering_display.primaries.rgbPrimaries[2][1] = UINT32(metadata[i] * 50000);
696         break;
697       case PerFrameMetadataKey::WHITE_POINT_X:
698         mastering_display.primaries.whitePoint[0] = UINT32(metadata[i] * 50000);
699         break;
700       case PerFrameMetadataKey::WHITE_POINT_Y:
701         mastering_display.primaries.whitePoint[1] = UINT32(metadata[i] * 50000);
702         break;
703       case PerFrameMetadataKey::MAX_LUMINANCE:
704         mastering_display.maxDisplayLuminance = UINT32(metadata[i]);
705         break;
706       case PerFrameMetadataKey::MIN_LUMINANCE:
707         mastering_display.minDisplayLuminance = UINT32(metadata[i] * 10000);
708         break;
709       case PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL:
710         content_light.lightLevelSEIEnabled = true;
711         content_light.maxContentLightLevel = UINT32(metadata[i]);
712         break;
713       case PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL:
714         content_light.minPicAverageLightLevel = UINT32(metadata[i]);
715         break;
716       default:
717        break;
718     }
719   }
720   if ((!SameConfig(&old_mastering_display, &mastering_display, UINT32(sizeof(MasteringDisplay)))) ||
721       (!SameConfig(&old_content_light, &content_light, UINT32(sizeof(ContentLightLevel))))) {
722     layer_->update_mask.set(kMetadataUpdate);
723     geometry_changes_ |= kDataspace;
724   }
725   return HWC2::Error::None;
726 }
727 
SetLayerPerFrameMetadataBlobs(uint32_t num_elements,const PerFrameMetadataKey * keys,const uint32_t * sizes,const uint8_t * metadata)728 HWC2::Error HWCLayer::SetLayerPerFrameMetadataBlobs(uint32_t num_elements,
729                                                     const PerFrameMetadataKey *keys,
730                                                     const uint32_t *sizes,
731                                                     const uint8_t* metadata) {
732   if (!keys || !sizes || !metadata) {
733     DLOGE("metadata or sizes or keys is null");
734     return HWC2::Error::BadParameter;
735   }
736 
737   ColorMetaData &color_metadata = layer_->input_buffer.color_metadata;
738   for (uint32_t i = 0; i < num_elements; i++) {
739     switch (keys[i]) {
740       case PerFrameMetadataKey::HDR10_PLUS_SEI:
741         if (sizes[i] > HDR_DYNAMIC_META_DATA_SZ) {
742           DLOGE("Size of HDR10_PLUS_SEI = %d", sizes[i]);
743           return HWC2::Error::BadParameter;
744         }
745         // if dynamic metadata changes, store and set needs validate
746         if (!SameConfig(static_cast<const uint8_t*>(color_metadata.dynamicMetaDataPayload),
747                         metadata, sizes[i])) {
748           geometry_changes_ |= kDataspace;
749           color_metadata.dynamicMetaDataValid = true;
750           color_metadata.dynamicMetaDataLen = sizes[i];
751           std::memcpy(color_metadata.dynamicMetaDataPayload, metadata, sizes[i]);
752           layer_->update_mask.set(kMetadataUpdate);
753         }
754         break;
755       default:
756         DLOGW("Invalid key = %d", keys[i]);
757         return HWC2::Error::BadParameter;
758     }
759   }
760   return HWC2::Error::None;
761 }
762 
SetRect(const hwc_rect_t & source,LayerRect * target)763 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
764   target->left = FLOAT(source.left);
765   target->top = FLOAT(source.top);
766   target->right = FLOAT(source.right);
767   target->bottom = FLOAT(source.bottom);
768 }
769 
SetRect(const hwc_frect_t & source,LayerRect * target)770 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
771   // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
772   target->left = std::ceil(source.left);
773   target->top = std::ceil(source.top);
774   target->right = std::floor(source.right);
775   target->bottom = std::floor(source.bottom);
776 }
777 
GetUint32Color(const hwc_color_t & source)778 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
779   // Returns 32 bit ARGB
780   uint32_t a = UINT32(source.a) << 24;
781   uint32_t r = UINT32(source.r) << 16;
782   uint32_t g = UINT32(source.g) << 8;
783   uint32_t b = UINT32(source.b);
784   uint32_t color = a | r | g | b;
785   return color;
786 }
787 
GetSDMFormat(const int32_t & source,const int flags)788 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
789   LayerBufferFormat format = kFormatInvalid;
790   if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
791     switch (source) {
792       case HAL_PIXEL_FORMAT_RGBA_8888:
793         format = kFormatRGBA8888Ubwc;
794         break;
795       case HAL_PIXEL_FORMAT_RGBX_8888:
796         format = kFormatRGBX8888Ubwc;
797         break;
798       case HAL_PIXEL_FORMAT_BGR_565:
799         format = kFormatBGR565Ubwc;
800         break;
801       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
802       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
803       case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
804         format = kFormatYCbCr420SPVenusUbwc;
805         break;
806       case HAL_PIXEL_FORMAT_RGBA_1010102:
807         format = kFormatRGBA1010102Ubwc;
808         break;
809       case HAL_PIXEL_FORMAT_RGBX_1010102:
810         format = kFormatRGBX1010102Ubwc;
811         break;
812       case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
813         format = kFormatYCbCr420TP10Ubwc;
814         break;
815       case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
816         format = kFormatYCbCr420P010Ubwc;
817         break;
818       default:
819         DLOGW("Unsupported format type for UBWC %s", qdutils::GetHALPixelFormatString(source));
820         return kFormatInvalid;
821     }
822     return format;
823   }
824 
825   switch (source) {
826     case HAL_PIXEL_FORMAT_RGBA_8888:
827       format = kFormatRGBA8888;
828       break;
829     case HAL_PIXEL_FORMAT_RGBA_5551:
830       format = kFormatRGBA5551;
831       break;
832     case HAL_PIXEL_FORMAT_RGBA_4444:
833       format = kFormatRGBA4444;
834       break;
835     case HAL_PIXEL_FORMAT_BGRA_8888:
836       format = kFormatBGRA8888;
837       break;
838     case HAL_PIXEL_FORMAT_RGBX_8888:
839       format = kFormatRGBX8888;
840       break;
841     case HAL_PIXEL_FORMAT_BGRX_8888:
842       format = kFormatBGRX8888;
843       break;
844     case HAL_PIXEL_FORMAT_RGB_888:
845       format = kFormatRGB888;
846       break;
847     case HAL_PIXEL_FORMAT_BGR_888:
848       format = kFormatBGR888;
849       break;
850     case HAL_PIXEL_FORMAT_RGB_565:
851       format = kFormatRGB565;
852       break;
853     case HAL_PIXEL_FORMAT_BGR_565:
854       format = kFormatBGR565;
855       break;
856     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
857     case HAL_PIXEL_FORMAT_NV12_LINEAR_FLEX:
858     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
859       format = kFormatYCbCr420SemiPlanarVenus;
860       break;
861     case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
862       format = kFormatYCrCb420SemiPlanarVenus;
863       break;
864     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
865       format = kFormatYCbCr420SPVenusUbwc;
866       break;
867     case HAL_PIXEL_FORMAT_YV12:
868       format = kFormatYCrCb420PlanarStride16;
869       break;
870     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
871     case HAL_PIXEL_FORMAT_NV21_ZSL:
872       format = kFormatYCrCb420SemiPlanar;
873       break;
874     case HAL_PIXEL_FORMAT_YCbCr_420_SP:
875       format = kFormatYCbCr420SemiPlanar;
876       break;
877     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
878       format = kFormatYCbCr422H2V1SemiPlanar;
879       break;
880     case HAL_PIXEL_FORMAT_YCbCr_422_I:
881       format = kFormatYCbCr422H2V1Packed;
882       break;
883     case HAL_PIXEL_FORMAT_CbYCrY_422_I:
884       format = kFormatCbYCrY422H2V1Packed;
885       break;
886     case HAL_PIXEL_FORMAT_RGBA_1010102:
887       format = kFormatRGBA1010102;
888       break;
889     case HAL_PIXEL_FORMAT_ARGB_2101010:
890       format = kFormatARGB2101010;
891       break;
892     case HAL_PIXEL_FORMAT_RGBX_1010102:
893       format = kFormatRGBX1010102;
894       break;
895     case HAL_PIXEL_FORMAT_XRGB_2101010:
896       format = kFormatXRGB2101010;
897       break;
898     case HAL_PIXEL_FORMAT_BGRA_1010102:
899       format = kFormatBGRA1010102;
900       break;
901     case HAL_PIXEL_FORMAT_ABGR_2101010:
902       format = kFormatABGR2101010;
903       break;
904     case HAL_PIXEL_FORMAT_BGRX_1010102:
905       format = kFormatBGRX1010102;
906       break;
907     case HAL_PIXEL_FORMAT_XBGR_2101010:
908       format = kFormatXBGR2101010;
909       break;
910     case HAL_PIXEL_FORMAT_YCbCr_420_P010:
911       format = kFormatYCbCr420P010;
912       break;
913     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
914       format = kFormatYCbCr420TP10Ubwc;
915       break;
916     case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
917       format = kFormatYCbCr420P010Ubwc;
918       break;
919     case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
920       format = kFormatYCbCr420P010Venus;
921       break;
922     case HAL_PIXEL_FORMAT_RGBA_FP16:
923       format = kFormatInvalid;
924       break;
925     default:
926       DLOGW("Unsupported format type = %s", qdutils::GetHALPixelFormatString(source));
927       return kFormatInvalid;
928   }
929 
930   return format;
931 }
932 
GetUBWCStatsFromMetaData(UBWCStats * cr_stats,UbwcCrStatsVector * cr_vec)933 void HWCLayer::GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec) {
934   // TODO(user): Check if we can use UBWCStats directly
935   // in layer_buffer or copy directly to Vector
936   if (cr_stats->bDataValid) {
937     switch (cr_stats->version) {
938       case UBWC_4_0:
939       case UBWC_3_0:
940       case UBWC_2_0:
941         cr_vec->push_back(std::make_pair(32, cr_stats->ubwc_stats.nCRStatsTile32));
942         cr_vec->push_back(std::make_pair(64, cr_stats->ubwc_stats.nCRStatsTile64));
943         cr_vec->push_back(std::make_pair(96, cr_stats->ubwc_stats.nCRStatsTile96));
944         cr_vec->push_back(std::make_pair(128, cr_stats->ubwc_stats.nCRStatsTile128));
945         cr_vec->push_back(std::make_pair(160, cr_stats->ubwc_stats.nCRStatsTile160));
946         cr_vec->push_back(std::make_pair(192, cr_stats->ubwc_stats.nCRStatsTile192));
947         cr_vec->push_back(std::make_pair(256, cr_stats->ubwc_stats.nCRStatsTile256));
948         break;
949       default:
950         DLOGW("Invalid UBWC Version %d", cr_stats->version);
951         break;
952     }  // switch(cr_stats->version)
953   }  // if (cr_stats->bDatvalid)
954 }
955 
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)956 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
957   LayerBuffer *layer_buffer = &layer->input_buffer;
958   private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
959 
960   float fps = 0;
961   uint32_t frame_rate = layer->frame_rate;
962   if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
963     frame_rate = (fps != 0) ? RoundToStandardFPS(fps) : layer->frame_rate;
964     has_metadata_refresh_rate_ = true;
965   }
966 
967   int32_t interlaced = 0;
968   getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced);
969   bool interlace = interlaced ? true : false;
970 
971   if (interlace != layer_buffer->flags.interlace) {
972     DLOGI("Layer buffer interlaced metadata has changed. old=%d, new=%d",
973           layer_buffer->flags.interlace, interlace);
974   }
975 
976   uint32_t linear_format = 0;
977   if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
978     layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
979   }
980 
981   if ((interlace != layer_buffer->flags.interlace) || (frame_rate != layer->frame_rate)) {
982     // Layer buffer metadata has changed.
983     layer->frame_rate = frame_rate;
984     layer_buffer->flags.interlace = interlace;
985     layer_->update_mask.set(kMetadataUpdate);
986   }
987 
988   // Check if metadata is set
989   struct UBWCStats cr_stats[NUM_UBWC_CR_STATS_LAYERS] = {};
990 
991   for (int i = 0; i < NUM_UBWC_CR_STATS_LAYERS; i++) {
992     layer_buffer->ubwc_crstats[i].clear();
993   }
994 
995   if (getMetaData(handle, GET_UBWC_CR_STATS_INFO, cr_stats) == 0) {
996   // Only copy top layer for now as only top field for interlaced is used
997     GetUBWCStatsFromMetaData(&cr_stats[0], &(layer_buffer->ubwc_crstats[0]));
998   }  // if (getMetaData)
999 
1000   uint32_t single_buffer = 0;
1001   getMetaData(const_cast<private_handle_t *>(handle), GET_SINGLE_BUFFER_MODE, &single_buffer);
1002   single_buffer_ = (single_buffer == 1);
1003 
1004   // Handle colorMetaData / Dataspace handling now
1005   ValidateAndSetCSC(handle);
1006 
1007   return kErrorNone;
1008 }
1009 
IsDataSpaceSupported()1010 bool HWCLayer::IsDataSpaceSupported() {
1011   if (client_requested_ != HWC2::Composition::Device &&
1012       client_requested_ != HWC2::Composition::Cursor) {
1013     // Layers marked for GPU can have any dataspace
1014     return true;
1015   }
1016 
1017   return dataspace_supported_;
1018 }
1019 
ValidateAndSetCSC(const private_handle_t * handle)1020 void HWCLayer::ValidateAndSetCSC(const private_handle_t *handle) {
1021   LayerBuffer *layer_buffer = &layer_->input_buffer;
1022   bool use_color_metadata = true;
1023   ColorMetaData csc = {};
1024   if (dataspace_ != HAL_DATASPACE_UNKNOWN) {
1025     use_color_metadata = false;
1026     bool valid_csc = GetSDMColorSpace(dataspace_, &csc);
1027     if (!valid_csc) {
1028       dataspace_supported_ = false;
1029       return;
1030     }
1031 
1032     if (layer_buffer->color_metadata.transfer != csc.transfer ||
1033        layer_buffer->color_metadata.colorPrimaries != csc.colorPrimaries ||
1034        layer_buffer->color_metadata.range != csc.range) {
1035         // ColorMetadata updated. Needs validate.
1036         layer_->update_mask.set(kMetadataUpdate);
1037         // if we are here here, update the sdm layer csc.
1038         layer_buffer->color_metadata.transfer = csc.transfer;
1039         layer_buffer->color_metadata.colorPrimaries = csc.colorPrimaries;
1040         layer_buffer->color_metadata.range = csc.range;
1041     }
1042   }
1043 
1044   if (IsBT2020(layer_buffer->color_metadata.colorPrimaries)) {
1045      // android_dataspace_t doesnt support mastering display and light levels
1046      // so retrieve it from metadata for BT2020(HDR)
1047      use_color_metadata = true;
1048   }
1049 
1050   if (use_color_metadata) {
1051     ColorMetaData new_metadata = layer_buffer->color_metadata;
1052     if (sdm::SetCSC(handle, &new_metadata) == kErrorNone) {
1053       // If dataspace is KNOWN, overwrite the gralloc metadata CSC using the previously derived CSC
1054       // from dataspace.
1055       if (dataspace_ != HAL_DATASPACE_UNKNOWN) {
1056         new_metadata.colorPrimaries = layer_buffer->color_metadata.colorPrimaries;
1057         new_metadata.transfer = layer_buffer->color_metadata.transfer;
1058         new_metadata.range = layer_buffer->color_metadata.range;
1059       }
1060       if ((layer_buffer->color_metadata.colorPrimaries != new_metadata.colorPrimaries) ||
1061           (layer_buffer->color_metadata.transfer != new_metadata.transfer) ||
1062           (layer_buffer->color_metadata.range != new_metadata.range)) {
1063         layer_buffer->color_metadata.colorPrimaries = new_metadata.colorPrimaries;
1064         layer_buffer->color_metadata.transfer = new_metadata.transfer;
1065         layer_buffer->color_metadata.range = new_metadata.range;
1066         layer_->update_mask.set(kMetadataUpdate);
1067       }
1068       if (layer_buffer->color_metadata.matrixCoefficients != new_metadata.matrixCoefficients) {
1069         layer_buffer->color_metadata.matrixCoefficients = new_metadata.matrixCoefficients;
1070         layer_->update_mask.set(kMetadataUpdate);
1071       }
1072       DLOGV_IF(kTagClient, "Layer id = %" PRIu64 " ColorVolEnabled = %d ContentLightLevelEnabled = %d "
1073                "cRIEnabled = %d Dynamic Metadata valid = %d size = %d", id_,
1074                new_metadata.masteringDisplayInfo.colorVolumeSEIEnabled,
1075                new_metadata.contentLightLevel.lightLevelSEIEnabled,
1076                new_metadata.cRI.criEnabled, new_metadata.dynamicMetaDataValid,
1077                new_metadata.dynamicMetaDataLen);
1078       // Read color metadata from gralloc handle if it's enabled by clients, this will override the
1079       // values set using the Composer API's(SetLayerPerFrameMetaData)
1080       if (new_metadata.masteringDisplayInfo.colorVolumeSEIEnabled &&
1081           !SameConfig(&new_metadata.masteringDisplayInfo,
1082           &layer_buffer->color_metadata.masteringDisplayInfo, UINT32(sizeof(MasteringDisplay)))) {
1083         layer_buffer->color_metadata.masteringDisplayInfo = new_metadata.masteringDisplayInfo;
1084         layer_->update_mask.set(kMetadataUpdate);
1085       }
1086       if (new_metadata.contentLightLevel.lightLevelSEIEnabled &&
1087           !SameConfig(&new_metadata.contentLightLevel,
1088           &layer_buffer->color_metadata.contentLightLevel, UINT32(sizeof(ContentLightLevel)))) {
1089         layer_buffer->color_metadata.contentLightLevel = new_metadata.contentLightLevel;
1090         layer_->update_mask.set(kMetadataUpdate);
1091       }
1092       if (new_metadata.cRI.criEnabled &&
1093           !SameConfig(&new_metadata.cRI, &layer_buffer->color_metadata.cRI,
1094           UINT32(sizeof(ColorRemappingInfo)))) {
1095         layer_buffer->color_metadata.cRI = new_metadata.cRI;
1096         layer_->update_mask.set(kMetadataUpdate);
1097       }
1098       if (new_metadata.dynamicMetaDataValid &&
1099           !SameConfig(layer_buffer->color_metadata.dynamicMetaDataPayload,
1100           new_metadata.dynamicMetaDataPayload, HDR_DYNAMIC_META_DATA_SZ)) {
1101           layer_buffer->color_metadata.dynamicMetaDataValid = true;
1102           layer_buffer->color_metadata.dynamicMetaDataLen = new_metadata.dynamicMetaDataLen;
1103           std::memcpy(layer_buffer->color_metadata.dynamicMetaDataPayload,
1104                       new_metadata.dynamicMetaDataPayload, new_metadata.dynamicMetaDataLen);
1105         layer_->update_mask.set(kMetadataUpdate);
1106       }
1107     } else {
1108       dataspace_supported_ = false;
1109       return;
1110     }
1111   }
1112 
1113   dataspace_supported_ = true;
1114 }
1115 
1116 
RoundToStandardFPS(float fps)1117 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
1118   static const int32_t standard_fps[4] = {24, 30, 48, 60};
1119   int32_t frame_rate = (uint32_t)(fps);
1120 
1121   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
1122   for (int i = 0; i < count; i++) {
1123     if ((standard_fps[i] - frame_rate) < 2) {
1124       // Most likely used for video, the fps can fluctuate
1125       // Ex: b/w 29 and 30 for 30 fps clip
1126       return standard_fps[i];
1127     }
1128   }
1129 
1130   return frame_rate;
1131 }
1132 
SetComposition(const LayerComposition & sdm_composition)1133 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
1134   auto hwc_composition = HWC2::Composition::Invalid;
1135   switch (sdm_composition) {
1136     case kCompositionGPU:
1137       hwc_composition = HWC2::Composition::Client;
1138       break;
1139     case kCompositionCursor:
1140       hwc_composition = HWC2::Composition::Cursor;
1141       break;
1142     default:
1143       hwc_composition = HWC2::Composition::Device;
1144       break;
1145   }
1146   // Update solid fill composition
1147   if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
1148     hwc_composition = HWC2::Composition::SolidColor;
1149   }
1150   device_selected_ = hwc_composition;
1151 
1152   return;
1153 }
1154 
PushBackReleaseFence(const shared_ptr<Fence> & fence)1155 void HWCLayer::PushBackReleaseFence(const shared_ptr<Fence> &fence) {
1156   release_fences_.push_back(fence);
1157 }
1158 
PopBackReleaseFence(shared_ptr<Fence> * fence)1159 void HWCLayer::PopBackReleaseFence(shared_ptr<Fence> *fence) {
1160   if (release_fences_.empty()) {
1161     return;
1162   }
1163 
1164   *fence = release_fences_.back();
1165   release_fences_.pop_back();
1166 }
1167 
PopFrontReleaseFence(shared_ptr<Fence> * fence)1168 void HWCLayer::PopFrontReleaseFence(shared_ptr<Fence> *fence) {
1169   if (release_fences_.empty()) {
1170     return;
1171   }
1172 
1173   *fence = release_fences_.front();
1174   release_fences_.pop_front();
1175 }
1176 
IsRotationPresent()1177 bool HWCLayer::IsRotationPresent() {
1178   return ((layer_->transform.rotation != 0.0f) ||
1179          layer_->transform.flip_horizontal ||
1180          layer_->transform.flip_vertical);
1181 }
1182 
IsScalingPresent()1183 bool HWCLayer::IsScalingPresent() {
1184   uint32_t src_width  = static_cast<uint32_t>(layer_->src_rect.right - layer_->src_rect.left);
1185   uint32_t src_height = static_cast<uint32_t>(layer_->src_rect.bottom - layer_->src_rect.top);
1186   uint32_t dst_width  = static_cast<uint32_t>(layer_->dst_rect.right - layer_->dst_rect.left);
1187   uint32_t dst_height = static_cast<uint32_t>(layer_->dst_rect.bottom - layer_->dst_rect.top);
1188 
1189   if ((layer_->transform.rotation == 90.0) || (layer_->transform.rotation == 270.0)) {
1190     std::swap(src_width, src_height);
1191   }
1192 
1193   return ((src_width != dst_width) || (dst_height != src_height));
1194 }
1195 
SetDirtyRegions(hwc_region_t surface_damage)1196 void HWCLayer::SetDirtyRegions(hwc_region_t surface_damage) {
1197   layer_->dirty_regions.clear();
1198   for (uint32_t i = 0; i < surface_damage.numRects; i++) {
1199     LayerRect rect;
1200     SetRect(surface_damage.rects[i], &rect);
1201     layer_->dirty_regions.push_back(rect);
1202   }
1203 }
1204 
SetLayerAsMask()1205 void HWCLayer::SetLayerAsMask() {
1206   layer_->input_buffer.flags.mask_layer = true;
1207   DLOGV_IF(kTagClient, " Layer Id: ""[%" PRIu64 "]", id_);
1208 }
1209 
1210 }  // namespace sdm
1211