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