1 /*
2  * Copyright (c) 2014-2017, 2019-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 <stdint.h>
21 #include <qdMetaData.h>
22 
23 #include "hwc_layers.h"
24 #ifndef USE_GRALLOC1
25 #include <gr.h>
26 #endif
27 #include <utils/debug.h>
28 #include <cmath>
29 
30 #define __CLASS__ "HWCLayer"
31 
32 namespace sdm {
33 
34 std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
35 
SetCSC(const private_handle_t * pvt_handle,ColorMetaData * color_metadata)36 DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata) {
37   if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_METADATA,
38                   color_metadata) != 0) {
39     ColorSpace_t csc = ITU_R_601;
40     if (getMetaData(const_cast<private_handle_t *>(pvt_handle),  GET_COLOR_SPACE,
41                     &csc) == 0) {
42       if (csc == ITU_R_601_FR || csc == ITU_R_2020_FR) {
43         color_metadata->range = Range_Full;
44       }
45 
46       switch (csc) {
47       case ITU_R_601:
48       case ITU_R_601_FR:
49         // video and display driver uses 601_525
50         color_metadata->colorPrimaries = ColorPrimaries_BT601_6_525;
51         break;
52       case ITU_R_709:
53         color_metadata->colorPrimaries = ColorPrimaries_BT709_5;
54         break;
55       case ITU_R_2020:
56       case ITU_R_2020_FR:
57         color_metadata->colorPrimaries = ColorPrimaries_BT2020;
58         break;
59       default:
60         DLOGE("Unsupported CSC: %d", csc);
61         return kErrorNotSupported;
62       }
63     } else {
64       return kErrorNotSupported;
65     }
66   }
67 
68   return kErrorNone;
69 }
70 
71 // Returns true when color primary is supported
GetColorPrimary(const int32_t & dataspace,ColorPrimaries * color_primary)72 bool GetColorPrimary(const int32_t &dataspace, ColorPrimaries *color_primary) {
73   auto standard = dataspace & HAL_DATASPACE_STANDARD_MASK;
74   bool supported_csc = true;
75   switch (standard) {
76     case  HAL_DATASPACE_STANDARD_BT709:
77       *color_primary = ColorPrimaries_BT709_5;
78       break;
79     case HAL_DATASPACE_STANDARD_BT601_525:
80     case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
81       *color_primary = ColorPrimaries_BT601_6_525;
82       break;
83     case HAL_DATASPACE_STANDARD_BT601_625:
84     case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
85       *color_primary = ColorPrimaries_BT601_6_625;
86       break;
87     case HAL_DATASPACE_STANDARD_DCI_P3:
88       *color_primary = ColorPrimaries_DCIP3;
89       break;
90     case HAL_DATASPACE_STANDARD_BT2020:
91       *color_primary = ColorPrimaries_BT2020;
92       break;
93     default:
94       DLOGV_IF(kTagStrategy, "Unsupported Standard Request = %d", standard);
95       supported_csc = false;
96   }
97   return supported_csc;
98 }
99 
GetTransfer(const int32_t & dataspace,GammaTransfer * gamma_transfer)100 bool GetTransfer(const int32_t &dataspace, GammaTransfer *gamma_transfer) {
101   auto transfer = dataspace & HAL_DATASPACE_TRANSFER_MASK;
102   bool supported_transfer = true;
103   switch (transfer) {
104     case HAL_DATASPACE_TRANSFER_SRGB:
105       *gamma_transfer = Transfer_sRGB;
106       break;
107     case HAL_DATASPACE_TRANSFER_SMPTE_170M:
108       *gamma_transfer = Transfer_SMPTE_170M;
109       break;
110     case HAL_DATASPACE_TRANSFER_ST2084:
111       *gamma_transfer = Transfer_SMPTE_ST2084;
112       break;
113     case HAL_DATASPACE_TRANSFER_HLG:
114       *gamma_transfer = Transfer_HLG;
115       break;
116     case HAL_DATASPACE_TRANSFER_LINEAR:
117       *gamma_transfer = Transfer_Linear;
118       break;
119     case HAL_DATASPACE_TRANSFER_GAMMA2_2:
120       *gamma_transfer = Transfer_Gamma2_2;
121       break;
122     case HAL_DATASPACE_TRANSFER_GAMMA2_8:
123       *gamma_transfer = Transfer_Gamma2_8;
124       break;
125     default:
126       DLOGE("Unsupported Transfer Request = %d", transfer);
127       supported_transfer = false;
128   }
129   return supported_transfer;
130 }
131 
GetRange(const int32_t & dataspace,ColorRange * color_range)132 void GetRange(const int32_t &dataspace, ColorRange *color_range) {
133   auto range = dataspace & HAL_DATASPACE_RANGE_MASK;
134   switch (range) {
135     case HAL_DATASPACE_RANGE_FULL:
136       *color_range = Range_Full;
137       break;
138     case HAL_DATASPACE_RANGE_LIMITED:
139       *color_range = Range_Limited;
140       break;
141     default:
142       DLOGV_IF(kTagStrategy, "Unsupported Range Request = %d", range);
143       break;
144   }
145 }
146 
IsBT2020(const ColorPrimaries & color_primary)147 bool IsBT2020(const ColorPrimaries &color_primary) {
148   switch (color_primary) {
149   case ColorPrimaries_BT2020:
150     return true;
151     break;
152   default:
153     return false;
154   }
155 }
156 
157 // Retrieve ColorMetaData from android_data_space_t (STANDARD|TRANSFER|RANGE)
GetSDMColorSpace(const int32_t & dataspace,ColorMetaData * color_metadata)158 bool GetSDMColorSpace(const int32_t &dataspace, ColorMetaData *color_metadata) {
159   bool valid = false;
160   valid = GetColorPrimary(dataspace, &(color_metadata->colorPrimaries));
161   if (!valid) {
162     return valid;
163   }
164   valid = GetTransfer(dataspace, &(color_metadata->transfer));
165   if (!valid) {
166     return valid;
167   }
168   GetRange(dataspace, &(color_metadata->range));
169 
170   return true;
171 }
172 
173 // Layer operations
HWCLayer(hwc2_display_t display_id,HWCBufferAllocator * buf_allocator)174 HWCLayer::HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator)
175   : id_(next_id_++), display_id_(display_id), buffer_allocator_(buf_allocator) {
176   layer_ = new Layer();
177   // Fences are deferred, so the first time this layer is presented, return -1
178   // TODO(user): Verify that fences are properly obtained on suspend/resume
179   release_fences_.push(-1);
180 }
181 
~HWCLayer()182 HWCLayer::~HWCLayer() {
183   // Close any fences left for this layer
184   while (!release_fences_.empty()) {
185     close(release_fences_.front());
186     release_fences_.pop();
187   }
188   close(ion_fd_);
189   if (layer_) {
190     if (layer_->input_buffer.acquire_fence_fd >= 0) {
191       close(layer_->input_buffer.acquire_fence_fd);
192     }
193     delete layer_;
194   }
195 }
196 
SetLayerBuffer(buffer_handle_t buffer,int32_t acquire_fence)197 HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence) {
198   if (!buffer) {
199     if (client_requested_ == HWC2::Composition::Device ||
200         client_requested_ == HWC2::Composition::Cursor) {
201       DLOGE("Invalid buffer handle: %p on layer: %d", buffer, id_);
202       return HWC2::Error::BadParameter;
203     } else {
204       return HWC2::Error::None;
205     }
206   }
207 
208   if (acquire_fence == 0) {
209     DLOGE("acquire_fence is zero");
210     return HWC2::Error::BadParameter;
211   }
212 
213   const private_handle_t *handle = static_cast<const private_handle_t *>(buffer);
214 
215   // Validate and dup ion fd from surfaceflinger
216   // This works around bug 30281222
217   if (handle->fd < 0) {
218     return HWC2::Error::BadParameter;
219   } else {
220     close(ion_fd_);
221     ion_fd_ = dup(handle->fd);
222   }
223 
224   LayerBuffer *layer_buffer = &layer_->input_buffer;
225   int aligned_width, aligned_height;
226 #ifdef USE_GRALLOC1
227   buffer_allocator_->GetCustomWidthAndHeight(handle, &aligned_width, &aligned_height);
228 #else
229   AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(handle, aligned_width, aligned_height);
230 #endif
231 
232   LayerBufferFormat format = GetSDMFormat(handle->format, handle->flags);
233   if ((format != layer_buffer->format) || (UINT32(aligned_width) != layer_buffer->width) ||
234       (UINT32(aligned_height) != layer_buffer->height)) {
235     // Layer buffer geometry has changed.
236     geometry_changes_ |= kBufferGeometry;
237   }
238 
239   layer_buffer->format = format;
240   layer_buffer->width = UINT32(aligned_width);
241   layer_buffer->height = UINT32(aligned_height);
242   layer_buffer->unaligned_width = UINT32(handle->unaligned_width);
243   layer_buffer->unaligned_height = UINT32(handle->unaligned_height);
244 
245   if (SetMetaData(const_cast<private_handle_t *>(handle), layer_) != kErrorNone) {
246     return HWC2::Error::BadLayer;
247   }
248 
249   layer_buffer->flags.video = (handle->buffer_type == BUFFER_TYPE_VIDEO) ? true : false;
250 
251   // TZ Protected Buffer - L1
252   bool secure = (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER);
253   bool secure_camera = secure && (handle->flags & private_handle_t::PRIV_FLAGS_CAMERA_WRITE);
254   bool secure_display = (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY);
255   if (secure != layer_buffer->flags.secure || secure_camera != layer_buffer->flags.secure_camera ||
256       secure_display != layer_buffer->flags.secure_display) {
257     // Secure attribute of layer buffer has changed.
258     needs_validate_ = true;
259   }
260 
261   layer_buffer->flags.secure = secure;
262   layer_buffer->flags.secure_camera = secure_camera;
263   layer_buffer->flags.secure_display = secure_display;
264 
265   if (layer_buffer->acquire_fence_fd >= 0) {
266     close(layer_buffer->acquire_fence_fd);
267   }
268   layer_buffer->acquire_fence_fd = acquire_fence;
269   layer_buffer->planes[0].fd = ion_fd_;
270   layer_buffer->planes[0].offset = handle->offset;
271   layer_buffer->planes[0].stride = UINT32(handle->width);
272   layer_buffer->size = handle->size;
273   layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
274   layer_buffer->fb_id = 0;
275 
276   return HWC2::Error::None;
277 }
278 
SetLayerSurfaceDamage(hwc_region_t damage)279 HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
280   surface_updated_ = true;
281   if ((damage.numRects == 1) && (damage.rects[0].bottom == 0) && (damage.rects[0].right == 0)) {
282     surface_updated_ = false;
283   }
284 
285   if (!layer_->flags.updating && surface_updated_) {
286     needs_validate_ = true;
287   }
288 
289   if (!partial_update_enabled_) {
290     SetDirtyRegions(damage);
291     return HWC2::Error::None;
292   }
293 
294   // Check if there is an update in SurfaceDamage rects.
295   if (layer_->dirty_regions.size() != damage.numRects) {
296     needs_validate_ = true;
297   } else {
298     for (uint32_t j = 0; j < damage.numRects; j++) {
299       LayerRect damage_rect;
300       SetRect(damage.rects[j], &damage_rect);
301       if (damage_rect != layer_->dirty_regions.at(j)) {
302         needs_validate_ = true;
303         break;
304       }
305     }
306   }
307 
308   SetDirtyRegions(damage);
309   return HWC2::Error::None;
310 }
311 
SetLayerBlendMode(HWC2::BlendMode mode)312 HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) {
313   LayerBlending blending = kBlendingPremultiplied;
314   switch (mode) {
315     case HWC2::BlendMode::Coverage:
316       blending = kBlendingCoverage;
317       break;
318     case HWC2::BlendMode::Premultiplied:
319       blending = kBlendingPremultiplied;
320       break;
321     case HWC2::BlendMode::None:
322       blending = kBlendingOpaque;
323       break;
324     default:
325       return HWC2::Error::BadParameter;
326   }
327 
328   if (layer_->blending != blending) {
329     geometry_changes_ |= kBlendMode;
330     layer_->blending = blending;
331   }
332   return HWC2::Error::None;
333 }
334 
SetLayerColor(hwc_color_t color)335 HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) {
336   if (client_requested_ != HWC2::Composition::SolidColor) {
337     return HWC2::Error::None;
338   }
339   layer_->solid_fill_color = GetUint32Color(color);
340   layer_->input_buffer.format = kFormatARGB8888;
341   DLOGV_IF(kTagCompManager, "[%" PRIu64 "][%" PRIu64 "] Layer color set to %x", display_id_, id_,
342            layer_->solid_fill_color);
343   return HWC2::Error::None;
344 }
345 
SetLayerCompositionType(HWC2::Composition type)346 HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) {
347   // Validation is required when the client changes the composition type
348   if (client_requested_ != type) {
349     needs_validate_ = true;
350   }
351   client_requested_ = type;
352   switch (type) {
353     case HWC2::Composition::Client:
354       break;
355     case HWC2::Composition::Device:
356       // We try and default to this in SDM
357       break;
358     case HWC2::Composition::SolidColor:
359       break;
360     case HWC2::Composition::Cursor:
361       break;
362     case HWC2::Composition::Invalid:
363       return HWC2::Error::BadParameter;
364     default:
365       return HWC2::Error::Unsupported;
366   }
367 
368   return HWC2::Error::None;
369 }
370 
SetLayerDataspace(int32_t dataspace)371 HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) {
372   // Map deprecated dataspace values to appropriate
373   // new enums
374   if (dataspace & 0xffff) {
375     switch (dataspace & 0xffff) {
376       case HAL_DATASPACE_SRGB:
377         dataspace = HAL_DATASPACE_V0_SRGB;
378         break;
379       case HAL_DATASPACE_JFIF:
380         dataspace = HAL_DATASPACE_V0_JFIF;
381         break;
382       case HAL_DATASPACE_SRGB_LINEAR:
383         dataspace = HAL_DATASPACE_V0_SRGB_LINEAR;
384         break;
385       case HAL_DATASPACE_BT601_625:
386         dataspace = HAL_DATASPACE_V0_BT601_625;
387         break;
388       case HAL_DATASPACE_BT601_525:
389         dataspace = HAL_DATASPACE_V0_BT601_525;
390         break;
391       case HAL_DATASPACE_BT709:
392         dataspace = HAL_DATASPACE_V0_BT709;
393         break;
394       default:
395         // unknown legacy dataspace
396         DLOGW_IF(kTagQDCM, "Unsupported dataspace type %d", dataspace);
397     }
398   }
399 
400   // cache the dataspace, to be used later to update SDM ColorMetaData
401   if (dataspace_ != dataspace) {
402     geometry_changes_ |= kDataspace;
403     dataspace_ = dataspace;
404   }
405   return HWC2::Error::None;
406 }
407 
SetLayerDisplayFrame(hwc_rect_t frame)408 HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
409   LayerRect dst_rect = {};
410   SetRect(frame, &dst_rect);
411   if (layer_->dst_rect != dst_rect) {
412     geometry_changes_ |= kDisplayFrame;
413     layer_->dst_rect = dst_rect;
414   }
415   return HWC2::Error::None;
416 }
417 
SetLayerPlaneAlpha(float alpha)418 HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
419   // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
420   uint8_t plane_alpha = static_cast<uint8_t>(std::round(255.0f * alpha));
421   if (layer_->plane_alpha != plane_alpha) {
422     geometry_changes_ |= kPlaneAlpha;
423     layer_->plane_alpha = plane_alpha;
424   }
425 
426   return HWC2::Error::None;
427 }
428 
SetLayerSourceCrop(hwc_frect_t crop)429 HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) {
430   LayerRect src_rect = {};
431   SetRect(crop, &src_rect);
432   if (layer_->src_rect != src_rect) {
433     geometry_changes_ |= kSourceCrop;
434     layer_->src_rect = src_rect;
435   }
436 
437   return HWC2::Error::None;
438 }
439 
SetLayerTransform(HWC2::Transform transform)440 HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
441   LayerTransform layer_transform = {};
442   switch (transform) {
443     case HWC2::Transform::FlipH:
444       layer_transform.flip_horizontal = true;
445       break;
446     case HWC2::Transform::FlipV:
447       layer_transform.flip_vertical = true;
448       break;
449     case HWC2::Transform::Rotate90:
450       layer_transform.rotation = 90.0f;
451       break;
452     case HWC2::Transform::Rotate180:
453       layer_transform.flip_horizontal = true;
454       layer_transform.flip_vertical = true;
455       break;
456     case HWC2::Transform::Rotate270:
457       layer_transform.rotation = 90.0f;
458       layer_transform.flip_horizontal = true;
459       layer_transform.flip_vertical = true;
460       break;
461     case HWC2::Transform::FlipHRotate90:
462       layer_transform.rotation = 90.0f;
463       layer_transform.flip_horizontal = true;
464       break;
465     case HWC2::Transform::FlipVRotate90:
466       layer_transform.rotation = 90.0f;
467       layer_transform.flip_vertical = true;
468       break;
469     case HWC2::Transform::None:
470       // do nothing
471       break;
472   }
473 
474   if (layer_->transform != layer_transform) {
475     geometry_changes_ |= kTransform;
476     layer_->transform = layer_transform;
477   }
478   return HWC2::Error::None;
479 }
480 
SetLayerVisibleRegion(hwc_region_t visible)481 HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
482   layer_->visible_regions.clear();
483   for (uint32_t i = 0; i < visible.numRects; i++) {
484     LayerRect rect;
485     SetRect(visible.rects[i], &rect);
486     layer_->visible_regions.push_back(rect);
487   }
488 
489   return HWC2::Error::None;
490 }
491 
SetLayerZOrder(uint32_t z)492 HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
493   if (z_ != z) {
494     geometry_changes_ |= kZOrder;
495     z_ = z;
496   }
497   return HWC2::Error::None;
498 }
499 
SetRect(const hwc_rect_t & source,LayerRect * target)500 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
501   target->left = FLOAT(source.left);
502   target->top = FLOAT(source.top);
503   target->right = FLOAT(source.right);
504   target->bottom = FLOAT(source.bottom);
505 }
506 
SetRect(const hwc_frect_t & source,LayerRect * target)507 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
508   // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
509   target->left = std::ceil(source.left);
510   target->top = std::ceil(source.top);
511   target->right = std::floor(source.right);
512   target->bottom = std::floor(source.bottom);
513 }
514 
GetUint32Color(const hwc_color_t & source)515 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
516   // Returns 32 bit ARGB
517   uint32_t a = UINT32(source.a) << 24;
518   uint32_t r = UINT32(source.r) << 16;
519   uint32_t g = UINT32(source.g) << 8;
520   uint32_t b = UINT32(source.b);
521   uint32_t color = a | r | g | b;
522   return color;
523 }
524 
GetSDMFormat(const int32_t & source,const int flags)525 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
526   LayerBufferFormat format = kFormatInvalid;
527   if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
528     switch (source) {
529       case HAL_PIXEL_FORMAT_RGBA_8888:
530         format = kFormatRGBA8888Ubwc;
531         break;
532       case HAL_PIXEL_FORMAT_RGBX_8888:
533         format = kFormatRGBX8888Ubwc;
534         break;
535       case HAL_PIXEL_FORMAT_BGR_565:
536         format = kFormatBGR565Ubwc;
537         break;
538       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
539       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
540       case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
541         format = kFormatYCbCr420SPVenusUbwc;
542         break;
543       case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
544         format = kFormatYCbCr420TP10Ubwc;
545         break;
546       default:
547         DLOGE("Unsupported format type for UBWC %d", source);
548         return kFormatInvalid;
549     }
550     return format;
551   }
552 
553   switch (source) {
554     case HAL_PIXEL_FORMAT_RGBA_8888:
555       format = kFormatRGBA8888;
556       break;
557     case HAL_PIXEL_FORMAT_RGBA_5551:
558       format = kFormatRGBA5551;
559       break;
560     case HAL_PIXEL_FORMAT_RGBA_4444:
561       format = kFormatRGBA4444;
562       break;
563     case HAL_PIXEL_FORMAT_BGRA_8888:
564       format = kFormatBGRA8888;
565       break;
566     case HAL_PIXEL_FORMAT_RGBX_8888:
567       format = kFormatRGBX8888;
568       break;
569     case HAL_PIXEL_FORMAT_BGRX_8888:
570       format = kFormatBGRX8888;
571       break;
572     case HAL_PIXEL_FORMAT_RGB_888:
573       format = kFormatRGB888;
574       break;
575     case HAL_PIXEL_FORMAT_RGB_565:
576       format = kFormatRGB565;
577       break;
578     case HAL_PIXEL_FORMAT_BGR_565:
579       format = kFormatBGR565;
580       break;
581     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
582     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
583       format = kFormatYCbCr420SemiPlanarVenus;
584       break;
585     case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
586       format = kFormatYCrCb420SemiPlanarVenus;
587       break;
588     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
589       format = kFormatYCbCr420SPVenusUbwc;
590       break;
591     case HAL_PIXEL_FORMAT_YV12:
592       format = kFormatYCrCb420PlanarStride16;
593       break;
594     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
595     case HAL_PIXEL_FORMAT_NV21_ZSL:
596       format = kFormatYCrCb420SemiPlanar;
597       break;
598     case HAL_PIXEL_FORMAT_YCbCr_420_SP:
599       format = kFormatYCbCr420SemiPlanar;
600       break;
601     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
602       format = kFormatYCbCr422H2V1SemiPlanar;
603       break;
604     case HAL_PIXEL_FORMAT_YCbCr_422_I:
605       format = kFormatYCbCr422H2V1Packed;
606       break;
607     case HAL_PIXEL_FORMAT_RGBA_1010102:
608       format = kFormatRGBA1010102;
609       break;
610     case HAL_PIXEL_FORMAT_ARGB_2101010:
611       format = kFormatARGB2101010;
612       break;
613     case HAL_PIXEL_FORMAT_RGBX_1010102:
614       format = kFormatRGBX1010102;
615       break;
616     case HAL_PIXEL_FORMAT_XRGB_2101010:
617       format = kFormatXRGB2101010;
618       break;
619     case HAL_PIXEL_FORMAT_BGRA_1010102:
620       format = kFormatBGRA1010102;
621       break;
622     case HAL_PIXEL_FORMAT_ABGR_2101010:
623       format = kFormatABGR2101010;
624       break;
625     case HAL_PIXEL_FORMAT_BGRX_1010102:
626       format = kFormatBGRX1010102;
627       break;
628     case HAL_PIXEL_FORMAT_XBGR_2101010:
629       format = kFormatXBGR2101010;
630       break;
631     case HAL_PIXEL_FORMAT_YCbCr_420_P010:
632       format = kFormatYCbCr420P010;
633       break;
634     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
635       format = kFormatYCbCr420TP10Ubwc;
636       break;
637     case HAL_PIXEL_FORMAT_RGBA_FP16:
638       format = kFormatInvalid;
639       break;
640     default:
641       DLOGW("Unsupported format type = %d", source);
642       return kFormatInvalid;
643   }
644 
645   return format;
646 }
647 
GetS3DFormat(uint32_t s3d_format)648 LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
649   LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
650   switch (s3d_format) {
651     case HAL_NO_3D:
652       sdm_s3d_format = kS3dFormatNone;
653       break;
654     case HAL_3D_SIDE_BY_SIDE_L_R:
655       sdm_s3d_format = kS3dFormatLeftRight;
656       break;
657     case HAL_3D_SIDE_BY_SIDE_R_L:
658       sdm_s3d_format = kS3dFormatRightLeft;
659       break;
660     case HAL_3D_TOP_BOTTOM:
661       sdm_s3d_format = kS3dFormatTopBottom;
662       break;
663     default:
664       DLOGW("Invalid S3D format %d", s3d_format);
665   }
666   return sdm_s3d_format;
667 }
668 
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)669 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
670   LayerBuffer *layer_buffer = &layer->input_buffer;
671   private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
672   IGC_t igc = {};
673   LayerIGC layer_igc = layer_buffer->igc;
674   if (getMetaData(handle, GET_IGC, &igc) == 0) {
675     if (SetIGC(igc, &layer_igc) != kErrorNone) {
676       return kErrorNotSupported;
677     }
678   }
679 
680   uint32_t fps = 0;
681   uint32_t frame_rate = layer->frame_rate;
682   if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
683     frame_rate = RoundToStandardFPS(FLOAT(fps));
684   }
685 
686   int32_t interlaced = 0;
687   bool interlace = layer_buffer->flags.interlace;
688   if (getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
689     interlace = interlaced ? true : false;
690   }
691 
692   uint32_t linear_format = 0;
693   if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
694     layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
695   }
696 
697   uint32_t s3d = 0;
698   LayerBufferS3DFormat s3d_format = layer_buffer->s3d_format;
699   if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
700     s3d_format = GetS3DFormat(s3d);
701   }
702 
703   if ((layer_igc != layer_buffer->igc) || (interlace != layer_buffer->flags.interlace) ||
704       (frame_rate != layer->frame_rate) || (s3d_format != layer_buffer->s3d_format)) {
705     // Layer buffer metadata has changed.
706     needs_validate_ = true;
707     layer_buffer->igc = layer_igc;
708     layer->frame_rate = frame_rate;
709     layer_buffer->s3d_format = s3d_format;
710     layer_buffer->flags.interlace = interlace;
711   }
712 
713   return kErrorNone;
714 }
715 
SetIGC(IGC_t source,LayerIGC * target)716 DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
717   switch (source) {
718     case IGC_NotSpecified:
719       *target = kIGCNotSpecified;
720       break;
721     case IGC_sRGB:
722       *target = kIGCsRGB;
723       break;
724     default:
725       DLOGE("Unsupported IGC: %d", source);
726       return kErrorNotSupported;
727   }
728 
729   return kErrorNone;
730 }
731 
732 
733 
SupportLocalConversion(ColorPrimaries working_primaries)734 bool HWCLayer::SupportLocalConversion(ColorPrimaries working_primaries) {
735   if (layer_->input_buffer.color_metadata.colorPrimaries <= ColorPrimaries_BT601_6_525 &&
736       working_primaries <= ColorPrimaries_BT601_6_525) {
737     return true;
738   }
739   return false;
740 }
741 
ValidateAndSetCSC()742 bool HWCLayer::ValidateAndSetCSC() {
743   if (client_requested_ != HWC2::Composition::Device &&
744       client_requested_ != HWC2::Composition::Cursor &&
745       client_requested_ != HWC2::Composition::SolidColor) {
746     // Check the layers which are configured to Device
747     return true;
748   }
749 
750   LayerBuffer *layer_buffer = &layer_->input_buffer;
751   bool use_color_metadata = true;
752 #ifdef FEATURE_WIDE_COLOR
753   ColorMetaData csc = {};
754   if (dataspace_ != HAL_DATASPACE_UNKNOWN) {
755     use_color_metadata = false;
756     bool valid_csc = GetSDMColorSpace(dataspace_, &csc);
757     if (!valid_csc) {
758       return false;
759     }
760     // if we are here here, update the sdm layer csc.
761     layer_buffer->color_metadata.transfer = csc.transfer;
762     layer_buffer->color_metadata.colorPrimaries = csc.colorPrimaries;
763     layer_buffer->color_metadata.range = csc.range;
764   }
765 #endif
766 
767   if (IsBT2020(layer_buffer->color_metadata.colorPrimaries)) {
768      // android_dataspace_t doesnt support mastering display and light levels
769      // so retrieve it from metadata for BT2020(HDR)
770      use_color_metadata = true;
771   }
772 
773   if (use_color_metadata && client_requested_ != HWC2::Composition::SolidColor) {
774     const private_handle_t *handle =
775       reinterpret_cast<const private_handle_t *>(layer_buffer->buffer_id);
776     if (sdm::SetCSC(handle, &layer_buffer->color_metadata) != kErrorNone) {
777       return false;
778     }
779   }
780 
781   return true;
782 }
783 
784 
RoundToStandardFPS(float fps)785 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
786   static const uint32_t standard_fps[4] = {24, 30, 48, 60};
787   uint32_t frame_rate = (uint32_t)(fps);
788 
789   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
790   for (int i = 0; i < count; i++) {
791     if ((standard_fps[i] - frame_rate) < 2) {
792       // Most likely used for video, the fps can fluctuate
793       // Ex: b/w 29 and 30 for 30 fps clip
794       return standard_fps[i];
795     }
796   }
797 
798   return frame_rate;
799 }
800 
SetComposition(const LayerComposition & sdm_composition)801 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
802   auto hwc_composition = HWC2::Composition::Invalid;
803   switch (sdm_composition) {
804     case kCompositionGPU:
805       hwc_composition = HWC2::Composition::Client;
806       break;
807     case kCompositionHWCursor:
808       hwc_composition = HWC2::Composition::Cursor;
809       break;
810     default:
811       hwc_composition = HWC2::Composition::Device;
812       break;
813   }
814   // Update solid fill composition
815   if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
816     hwc_composition = HWC2::Composition::SolidColor;
817   }
818   device_selected_ = hwc_composition;
819 
820   return;
821 }
PushReleaseFence(int32_t fence)822 void HWCLayer::PushReleaseFence(int32_t fence) {
823   release_fences_.push(fence);
824 }
PopReleaseFence(void)825 int32_t HWCLayer::PopReleaseFence(void) {
826   if (release_fences_.empty())
827     return -1;
828   auto fence = release_fences_.front();
829   release_fences_.pop();
830   return fence;
831 }
832 
SetDirtyRegions(hwc_region_t surface_damage)833 void HWCLayer::SetDirtyRegions(hwc_region_t surface_damage) {
834   layer_->dirty_regions.clear();
835   for (uint32_t i = 0; i < surface_damage.numRects; i++) {
836     LayerRect rect;
837     SetRect(surface_damage.rects[i], &rect);
838     layer_->dirty_regions.push_back(rect);
839   }
840 }
841 
842 }  // namespace sdm
843