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