1 /*
2  * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
3  * Not a Contribution.
4  *
5  * Copyright 2015 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include <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   non_integral_source_crop_ = ((crop.left != roundf(crop.left)) ||
416                               (crop.top != roundf(crop.top)) ||
417                               (crop.right != roundf(crop.right)) ||
418                               (crop.bottom != roundf(crop.bottom)));
419   if (non_integral_source_crop_) {
420     DLOGV_IF(kTagClient, "Crop: LRTB %f %f %f %f", crop.left, crop.top, crop.right, crop.bottom);
421   }
422   if (layer_->src_rect != src_rect) {
423     geometry_changes_ |= kSourceCrop;
424     layer_->src_rect = src_rect;
425   }
426 
427   return HWC2::Error::None;
428 }
429 
SetLayerTransform(HWC2::Transform transform)430 HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
431   LayerTransform layer_transform = {};
432   switch (transform) {
433     case HWC2::Transform::FlipH:
434       layer_transform.flip_horizontal = true;
435       break;
436     case HWC2::Transform::FlipV:
437       layer_transform.flip_vertical = true;
438       break;
439     case HWC2::Transform::Rotate90:
440       layer_transform.rotation = 90.0f;
441       break;
442     case HWC2::Transform::Rotate180:
443       layer_transform.flip_horizontal = true;
444       layer_transform.flip_vertical = true;
445       break;
446     case HWC2::Transform::Rotate270:
447       layer_transform.rotation = 90.0f;
448       layer_transform.flip_horizontal = true;
449       layer_transform.flip_vertical = true;
450       break;
451     case HWC2::Transform::FlipHRotate90:
452       layer_transform.rotation = 90.0f;
453       layer_transform.flip_horizontal = true;
454       break;
455     case HWC2::Transform::FlipVRotate90:
456       layer_transform.rotation = 90.0f;
457       layer_transform.flip_vertical = true;
458       break;
459     case HWC2::Transform::None:
460       // do nothing
461       break;
462   }
463 
464   if (layer_->transform != layer_transform) {
465     geometry_changes_ |= kTransform;
466     layer_->transform = layer_transform;
467   }
468   return HWC2::Error::None;
469 }
470 
SetLayerVisibleRegion(hwc_region_t visible)471 HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
472   layer_->visible_regions.clear();
473   for (uint32_t i = 0; i < visible.numRects; i++) {
474     LayerRect rect;
475     SetRect(visible.rects[i], &rect);
476     layer_->visible_regions.push_back(rect);
477   }
478 
479   return HWC2::Error::None;
480 }
481 
SetLayerZOrder(uint32_t z)482 HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
483   if (z_ != z) {
484     geometry_changes_ |= kZOrder;
485     z_ = z;
486   }
487   return HWC2::Error::None;
488 }
489 
SetRect(const hwc_rect_t & source,LayerRect * target)490 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
491   target->left = FLOAT(source.left);
492   target->top = FLOAT(source.top);
493   target->right = FLOAT(source.right);
494   target->bottom = FLOAT(source.bottom);
495 }
496 
SetRect(const hwc_frect_t & source,LayerRect * target)497 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
498   // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
499   target->left = std::ceil(source.left);
500   target->top = std::ceil(source.top);
501   target->right = std::floor(source.right);
502   target->bottom = std::floor(source.bottom);
503 }
504 
GetUint32Color(const hwc_color_t & source)505 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
506   // Returns 32 bit ARGB
507   uint32_t a = UINT32(source.a) << 24;
508   uint32_t r = UINT32(source.r) << 16;
509   uint32_t g = UINT32(source.g) << 8;
510   uint32_t b = UINT32(source.b);
511   uint32_t color = a | r | g | b;
512   return color;
513 }
514 
GetSDMFormat(const int32_t & source,const int flags)515 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
516   LayerBufferFormat format = kFormatInvalid;
517   if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
518     switch (source) {
519       case HAL_PIXEL_FORMAT_RGBA_8888:
520         format = kFormatRGBA8888Ubwc;
521         break;
522       case HAL_PIXEL_FORMAT_RGBX_8888:
523         format = kFormatRGBX8888Ubwc;
524         break;
525       case HAL_PIXEL_FORMAT_BGR_565:
526         format = kFormatBGR565Ubwc;
527         break;
528       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
529       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
530       case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
531         format = kFormatYCbCr420SPVenusUbwc;
532         break;
533       case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
534         format = kFormatYCbCr420TP10Ubwc;
535         break;
536       case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
537         format = kFormatYCbCr420P010Ubwc;
538         break;
539       default:
540         DLOGE("Unsupported format type for UBWC %d", source);
541         return kFormatInvalid;
542     }
543     return format;
544   }
545 
546   switch (source) {
547     case HAL_PIXEL_FORMAT_RGBA_8888:
548       format = kFormatRGBA8888;
549       break;
550     case HAL_PIXEL_FORMAT_RGBA_5551:
551       format = kFormatRGBA5551;
552       break;
553     case HAL_PIXEL_FORMAT_RGBA_4444:
554       format = kFormatRGBA4444;
555       break;
556     case HAL_PIXEL_FORMAT_BGRA_8888:
557       format = kFormatBGRA8888;
558       break;
559     case HAL_PIXEL_FORMAT_RGBX_8888:
560       format = kFormatRGBX8888;
561       break;
562     case HAL_PIXEL_FORMAT_BGRX_8888:
563       format = kFormatBGRX8888;
564       break;
565     case HAL_PIXEL_FORMAT_RGB_888:
566       format = kFormatRGB888;
567       break;
568     case HAL_PIXEL_FORMAT_BGR_888:
569       format = kFormatBGR888;
570       break;
571     case HAL_PIXEL_FORMAT_RGB_565:
572       format = kFormatRGB565;
573       break;
574     case HAL_PIXEL_FORMAT_BGR_565:
575       format = kFormatBGR565;
576       break;
577     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
578     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
579       format = kFormatYCbCr420SemiPlanarVenus;
580       break;
581     case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
582       format = kFormatYCrCb420SemiPlanarVenus;
583       break;
584     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
585       format = kFormatYCbCr420SPVenusUbwc;
586       break;
587     case HAL_PIXEL_FORMAT_YV12:
588       format = kFormatYCrCb420PlanarStride16;
589       break;
590     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
591       format = kFormatYCrCb420SemiPlanar;
592       break;
593     case HAL_PIXEL_FORMAT_YCbCr_420_SP:
594       format = kFormatYCbCr420SemiPlanar;
595       break;
596     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
597       format = kFormatYCbCr422H2V1SemiPlanar;
598       break;
599     case HAL_PIXEL_FORMAT_YCbCr_422_I:
600       format = kFormatYCbCr422H2V1Packed;
601       break;
602     case HAL_PIXEL_FORMAT_CbYCrY_422_I:
603       format = kFormatCbYCrY422H2V1Packed;
604       break;
605     case HAL_PIXEL_FORMAT_RGBA_1010102:
606       format = kFormatRGBA1010102;
607       break;
608     case HAL_PIXEL_FORMAT_ARGB_2101010:
609       format = kFormatARGB2101010;
610       break;
611     case HAL_PIXEL_FORMAT_RGBX_1010102:
612       format = kFormatRGBX1010102;
613       break;
614     case HAL_PIXEL_FORMAT_XRGB_2101010:
615       format = kFormatXRGB2101010;
616       break;
617     case HAL_PIXEL_FORMAT_BGRA_1010102:
618       format = kFormatBGRA1010102;
619       break;
620     case HAL_PIXEL_FORMAT_ABGR_2101010:
621       format = kFormatABGR2101010;
622       break;
623     case HAL_PIXEL_FORMAT_BGRX_1010102:
624       format = kFormatBGRX1010102;
625       break;
626     case HAL_PIXEL_FORMAT_XBGR_2101010:
627       format = kFormatXBGR2101010;
628       break;
629     case HAL_PIXEL_FORMAT_YCbCr_420_P010:
630       format = kFormatYCbCr420P010;
631       break;
632     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
633       format = kFormatYCbCr420TP10Ubwc;
634       break;
635     case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
636       format = kFormatYCbCr420P010Ubwc;
637       break;
638     default:
639       DLOGW("Unsupported format type = %d", source);
640       return kFormatInvalid;
641   }
642 
643   return format;
644 }
645 
GetS3DFormat(uint32_t s3d_format)646 LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
647   LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
648   switch (s3d_format) {
649     case HAL_NO_3D:
650       sdm_s3d_format = kS3dFormatNone;
651       break;
652     case HAL_3D_SIDE_BY_SIDE_L_R:
653       sdm_s3d_format = kS3dFormatLeftRight;
654       break;
655     case HAL_3D_SIDE_BY_SIDE_R_L:
656       sdm_s3d_format = kS3dFormatRightLeft;
657       break;
658     case HAL_3D_TOP_BOTTOM:
659       sdm_s3d_format = kS3dFormatTopBottom;
660       break;
661     default:
662       DLOGW("Invalid S3D format %d", s3d_format);
663   }
664   return sdm_s3d_format;
665 }
666 
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)667 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
668   LayerBuffer *layer_buffer = &layer->input_buffer;
669   private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
670   IGC_t igc = {};
671   LayerIGC layer_igc = layer_buffer->igc;
672   if (getMetaData(handle, GET_IGC, &igc) == 0) {
673     if (SetIGC(igc, &layer_igc) != kErrorNone) {
674       return kErrorNotSupported;
675     }
676   }
677 
678   float fps = 0;
679   uint32_t frame_rate = layer->frame_rate;
680   if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
681     frame_rate = RoundToStandardFPS(fps);
682   }
683 
684   int32_t interlaced = 0;
685   bool interlace = layer_buffer->flags.interlace;
686   if (getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
687     interlace = interlaced ? true : false;
688   }
689   if (interlace != layer_buffer->flags.interlace) {
690     DLOGI("Layer buffer interlaced metadata has changed. old=%d, new=%d",
691           layer_buffer->flags.interlace, interlace);
692   }
693 
694   uint32_t linear_format = 0;
695   if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
696     layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
697   }
698 
699   uint32_t s3d = 0;
700   LayerBufferS3DFormat s3d_format = layer_buffer->s3d_format;
701   if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
702     s3d_format = GetS3DFormat(s3d);
703   }
704 
705   if ((layer_igc != layer_buffer->igc) || (interlace != layer_buffer->flags.interlace) ||
706       (frame_rate != layer->frame_rate) || (s3d_format != layer_buffer->s3d_format)) {
707     // Layer buffer metadata has changed.
708     needs_validate_ = true;
709     layer_buffer->igc = layer_igc;
710     layer->frame_rate = frame_rate;
711     layer_buffer->s3d_format = s3d_format;
712     layer_buffer->flags.interlace = interlace;
713   }
714 
715   return kErrorNone;
716 }
717 
SetIGC(IGC_t source,LayerIGC * target)718 DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
719   switch (source) {
720     case IGC_NotSpecified:
721       *target = kIGCNotSpecified;
722       break;
723     case IGC_sRGB:
724       *target = kIGCsRGB;
725       break;
726     default:
727       DLOGE("Unsupported IGC: %d", source);
728       return kErrorNotSupported;
729   }
730 
731   return kErrorNone;
732 }
733 
734 
735 
SupportLocalConversion(ColorPrimaries working_primaries)736 bool HWCLayer::SupportLocalConversion(ColorPrimaries working_primaries) {
737   if (layer_->input_buffer.color_metadata.colorPrimaries <= ColorPrimaries_BT601_6_525 &&
738       working_primaries <= ColorPrimaries_BT601_6_525) {
739     return true;
740   }
741   return false;
742 }
743 
ValidateAndSetCSC()744 bool HWCLayer::ValidateAndSetCSC() {
745   if (client_requested_ != HWC2::Composition::Device &&
746       client_requested_ != HWC2::Composition::Cursor) {
747     // Check the layers which are configured to Device
748     return true;
749   }
750 
751   LayerBuffer *layer_buffer = &layer_->input_buffer;
752   bool use_color_metadata = true;
753 #ifdef FEATURE_WIDE_COLOR
754   ColorMetaData csc = {};
755   if (dataspace_ != HAL_DATASPACE_UNKNOWN) {
756     use_color_metadata = false;
757     bool valid_csc = GetSDMColorSpace(dataspace_, &csc);
758     if (!valid_csc) {
759       return false;
760     }
761     // if we are here here, update the sdm layer csc.
762     layer_buffer->color_metadata.transfer = csc.transfer;
763     layer_buffer->color_metadata.colorPrimaries = csc.colorPrimaries;
764     layer_buffer->color_metadata.range = csc.range;
765   }
766 #endif
767 
768   if (IsBT2020(layer_buffer->color_metadata.colorPrimaries)) {
769      // android_dataspace_t doesnt support mastering display and light levels
770      // so retrieve it from metadata for BT2020(HDR)
771      use_color_metadata = true;
772   }
773 
774   if (use_color_metadata) {
775     const private_handle_t *handle =
776       reinterpret_cast<const private_handle_t *>(layer_buffer->buffer_id);
777     if (sdm::SetCSC(handle, &layer_buffer->color_metadata) != kErrorNone) {
778       return false;
779     }
780   }
781 
782   return true;
783 }
784 
785 
RoundToStandardFPS(float fps)786 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
787   static const uint32_t standard_fps[4] = {24, 30, 48, 60};
788   uint32_t frame_rate = (uint32_t)(fps);
789 
790   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
791   for (int i = 0; i < count; i++) {
792     if ((standard_fps[i] - frame_rate) < 2) {
793       // Most likely used for video, the fps can fluctuate
794       // Ex: b/w 29 and 30 for 30 fps clip
795       return standard_fps[i];
796     }
797   }
798 
799   return frame_rate;
800 }
801 
SetComposition(const LayerComposition & sdm_composition)802 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
803   auto hwc_composition = HWC2::Composition::Invalid;
804   switch (sdm_composition) {
805     case kCompositionGPU:
806       hwc_composition = HWC2::Composition::Client;
807       break;
808     case kCompositionHWCursor:
809       hwc_composition = HWC2::Composition::Cursor;
810       break;
811     default:
812       hwc_composition = HWC2::Composition::Device;
813       break;
814   }
815   // Update solid fill composition
816   if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
817     hwc_composition = HWC2::Composition::SolidColor;
818   }
819   device_selected_ = hwc_composition;
820 
821   return;
822 }
PushReleaseFence(int32_t fence)823 void HWCLayer::PushReleaseFence(int32_t fence) {
824   release_fences_.push(fence);
825 }
PopReleaseFence(void)826 int32_t HWCLayer::PopReleaseFence(void) {
827   if (release_fences_.empty())
828     return -1;
829   auto fence = release_fences_.front();
830   release_fences_.pop();
831   return fence;
832 }
833 
834 }  // namespace sdm
835