• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 // Layer operations
HWCLayer(hwc2_display_t display_id,HWCBufferAllocator * buf_allocator)37 HWCLayer::HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator)
38   : id_(next_id_++), display_id_(display_id), buffer_allocator_(buf_allocator) {
39   layer_ = new Layer();
40   layer_->input_buffer = new LayerBuffer();
41   // Fences are deferred, so the first time this layer is presented, return -1
42   // TODO(user): Verify that fences are properly obtained on suspend/resume
43   release_fences_.push(-1);
44 }
45 
~HWCLayer()46 HWCLayer::~HWCLayer() {
47   // Close any fences left for this layer
48   while (!release_fences_.empty()) {
49     close(release_fences_.front());
50     release_fences_.pop();
51   }
52   close(ion_fd_);
53   if (layer_) {
54     if (layer_->input_buffer) {
55       delete (layer_->input_buffer);
56     }
57     delete layer_;
58   }
59 }
60 
SetLayerBuffer(buffer_handle_t buffer,int32_t acquire_fence)61 HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence) {
62   if (!buffer) {
63     DLOGE("Invalid buffer handle: %p on layer: %d", buffer, id_);
64     return HWC2::Error::BadParameter;
65   }
66 
67   if (acquire_fence == 0) {
68     DLOGE("acquire_fence is zero");
69     return HWC2::Error::BadParameter;
70   }
71 
72   const private_handle_t *handle = static_cast<const private_handle_t *>(buffer);
73 
74   // Validate and dup ion fd from surfaceflinger
75   // This works around bug 30281222
76   if (handle->fd < 0) {
77     return HWC2::Error::BadParameter;
78   } else {
79     close(ion_fd_);
80     ion_fd_ = dup(handle->fd);
81   }
82 
83   LayerBuffer *layer_buffer = layer_->input_buffer;
84 
85   layer_buffer->width = UINT32(handle->width);
86   layer_buffer->height = UINT32(handle->height);
87   auto format = layer_buffer->format;
88   layer_buffer->format = GetSDMFormat(handle->format, handle->flags);
89   if (format != layer_buffer->format) {
90     needs_validate_ = true;
91   }
92   if (SetMetaData(handle, layer_) != kErrorNone) {
93     return HWC2::Error::BadLayer;
94   }
95 
96   layer_buffer->flags.video = (handle->buffer_type == BUFFER_TYPE_VIDEO) ? true : false;
97   // TZ Protected Buffer - L1
98   layer_buffer->flags.secure =
99       (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) ? true: false;
100   layer_buffer->flags.secure_display =
101       (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) ? true : false;
102 
103   layer_buffer->planes[0].fd = ion_fd_;
104   layer_buffer->planes[0].offset = handle->offset;
105   layer_buffer->planes[0].stride = UINT32(handle->width);
106   layer_buffer->acquire_fence_fd = acquire_fence;
107   layer_buffer->buffer_id = reinterpret_cast<uint64_t>(handle);
108 
109   return HWC2::Error::None;
110 }
111 
SetLayerSurfaceDamage(hwc_region_t damage)112 HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
113   // Check if there is an update in SurfaceDamage rects
114   if (layer_->dirty_regions.size() != damage.numRects) {
115     needs_validate_ = true;
116   } else {
117     for (uint32_t j = 0; j < damage.numRects; j++) {
118       LayerRect damage_rect;
119       SetRect(damage.rects[j], &damage_rect);
120       if (damage_rect != layer_->dirty_regions.at(j)) {
121         needs_validate_ = true;
122         break;
123       }
124     }
125   }
126 
127   layer_->dirty_regions.clear();
128   for (uint32_t i = 0; i < damage.numRects; i++) {
129     LayerRect rect;
130     SetRect(damage.rects[i], &rect);
131     layer_->dirty_regions.push_back(rect);
132   }
133   return HWC2::Error::None;
134 }
135 
SetLayerBlendMode(HWC2::BlendMode mode)136 HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) {
137   LayerBlending blending = kBlendingPremultiplied;
138   switch (mode) {
139     case HWC2::BlendMode::Coverage:
140       blending = kBlendingCoverage;
141       break;
142     case HWC2::BlendMode::Premultiplied:
143       blending = kBlendingPremultiplied;
144       break;
145     case HWC2::BlendMode::None:
146       blending = kBlendingOpaque;
147       break;
148     default:
149       return HWC2::Error::BadParameter;
150   }
151 
152   if (layer_->blending != blending) {
153     geometry_changes_ |= kBlendMode;
154     layer_->blending = blending;
155   }
156   return HWC2::Error::None;
157 }
158 
SetLayerColor(hwc_color_t color)159 HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) {
160   layer_->solid_fill_color = GetUint32Color(color);
161   layer_->input_buffer->format = kFormatARGB8888;
162   DLOGV_IF(kTagCompManager, "[%" PRIu64 "][%" PRIu64 "] Layer color set to %x", display_id_, id_,
163            layer_->solid_fill_color);
164   return HWC2::Error::None;
165 }
166 
SetLayerCompositionType(HWC2::Composition type)167 HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) {
168   // Validation is required when the client changes the composition type
169   if (client_requested_ != type) {
170     needs_validate_ = true;
171   }
172   client_requested_ = type;
173   switch (type) {
174     case HWC2::Composition::Client:
175       break;
176     case HWC2::Composition::Device:
177       // We try and default to this in SDM
178       break;
179     case HWC2::Composition::SolidColor:
180       break;
181     case HWC2::Composition::Cursor:
182       break;
183     case HWC2::Composition::Invalid:
184       return HWC2::Error::BadParameter;
185     default:
186       return HWC2::Error::Unsupported;
187   }
188 
189   return HWC2::Error::None;
190 }
191 
SetLayerDataspace(int32_t dataspace)192 HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) {
193   if (dataspace != dataspace_) {
194     dataspace_ = dataspace;
195     geometry_changes_ |= kDataspace;
196   }
197   return HWC2::Error::None;
198 }
199 
SetLayerDisplayFrame(hwc_rect_t frame)200 HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
201   LayerRect dst_rect = {};
202   SetRect(frame, &dst_rect);
203   if (layer_->dst_rect != dst_rect) {
204     geometry_changes_ |= kDisplayFrame;
205     layer_->dst_rect = dst_rect;
206   }
207   return HWC2::Error::None;
208 }
209 
SetLayerPlaneAlpha(float alpha)210 HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
211   // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
212   uint8_t plane_alpha = static_cast<uint8_t>(std::round(255.0f * alpha));
213   if (layer_->plane_alpha != plane_alpha) {
214     geometry_changes_ |= kPlaneAlpha;
215     layer_->plane_alpha = plane_alpha;
216   }
217 
218   return HWC2::Error::None;
219 }
220 
SetLayerSourceCrop(hwc_frect_t crop)221 HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) {
222   LayerRect src_rect = {};
223   SetRect(crop, &src_rect);
224   if (layer_->src_rect != src_rect) {
225     geometry_changes_ |= kSourceCrop;
226     layer_->src_rect = src_rect;
227   }
228 
229   return HWC2::Error::None;
230 }
231 
SetLayerTransform(HWC2::Transform transform)232 HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) {
233   LayerTransform layer_transform = {};
234   switch (transform) {
235     case HWC2::Transform::FlipH:
236       layer_transform.flip_horizontal = true;
237       break;
238     case HWC2::Transform::FlipV:
239       layer_transform.flip_vertical = true;
240       break;
241     case HWC2::Transform::Rotate90:
242       layer_transform.rotation = 90.0f;
243       break;
244     case HWC2::Transform::Rotate180:
245       layer_transform.flip_horizontal = true;
246       layer_transform.flip_vertical = true;
247       break;
248     case HWC2::Transform::Rotate270:
249       layer_transform.rotation = 90.0f;
250       layer_transform.flip_horizontal = true;
251       layer_transform.flip_vertical = true;
252       break;
253     case HWC2::Transform::FlipHRotate90:
254       layer_transform.rotation = 90.0f;
255       layer_transform.flip_horizontal = true;
256       break;
257     case HWC2::Transform::FlipVRotate90:
258       layer_transform.rotation = 90.0f;
259       layer_transform.flip_vertical = true;
260       break;
261     case HWC2::Transform::None:
262       // do nothing
263       break;
264   }
265 
266   if (layer_->transform != layer_transform) {
267     geometry_changes_ |= kTransform;
268     layer_->transform = layer_transform;
269   }
270   return HWC2::Error::None;
271 }
272 
SetLayerVisibleRegion(hwc_region_t visible)273 HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) {
274   layer_->visible_regions.clear();
275   for (uint32_t i = 0; i < visible.numRects; i++) {
276     LayerRect rect;
277     SetRect(visible.rects[i], &rect);
278     layer_->visible_regions.push_back(rect);
279   }
280 
281   return HWC2::Error::None;
282 }
283 
SetLayerZOrder(uint32_t z)284 HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) {
285   if (z_ != z) {
286     geometry_changes_ |= kZOrder;
287     z_ = z;
288   }
289   return HWC2::Error::None;
290 }
291 
SetRect(const hwc_rect_t & source,LayerRect * target)292 void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) {
293   target->left = FLOAT(source.left);
294   target->top = FLOAT(source.top);
295   target->right = FLOAT(source.right);
296   target->bottom = FLOAT(source.bottom);
297 }
298 
SetRect(const hwc_frect_t & source,LayerRect * target)299 void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) {
300   // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop
301   target->left = std::ceil(source.left);
302   target->top = std::ceil(source.top);
303   target->right = std::floor(source.right);
304   target->bottom = std::floor(source.bottom);
305 }
306 
GetUint32Color(const hwc_color_t & source)307 uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) {
308   // Returns 32 bit ARGB
309   uint32_t a = UINT32(source.a) << 24;
310   uint32_t r = UINT32(source.r) << 16;
311   uint32_t g = UINT32(source.g) << 8;
312   uint32_t b = UINT32(source.b);
313   uint32_t color = a | r | g | b;
314   return color;
315 }
316 
GetSDMFormat(const int32_t & source,const int flags)317 LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) {
318   LayerBufferFormat format = kFormatInvalid;
319   if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
320     switch (source) {
321       case HAL_PIXEL_FORMAT_RGBA_8888:
322         format = kFormatRGBA8888Ubwc;
323         break;
324       case HAL_PIXEL_FORMAT_RGBX_8888:
325         format = kFormatRGBX8888Ubwc;
326         break;
327       case HAL_PIXEL_FORMAT_BGR_565:
328         format = kFormatBGR565Ubwc;
329         break;
330       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
331       case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
332       case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
333         format = kFormatYCbCr420SPVenusUbwc;
334         break;
335       case HAL_PIXEL_FORMAT_RGBA_1010102:
336         format = kFormatRGBA1010102Ubwc;
337         break;
338       case HAL_PIXEL_FORMAT_RGBX_1010102:
339         format = kFormatRGBX1010102Ubwc;
340         break;
341       default:
342         DLOGE("Unsupported format type for UBWC %d", source);
343         return kFormatInvalid;
344     }
345     return format;
346   }
347 
348   switch (source) {
349     case HAL_PIXEL_FORMAT_RGBA_8888:
350       format = kFormatRGBA8888;
351       break;
352     case HAL_PIXEL_FORMAT_RGBA_5551:
353       format = kFormatRGBA5551;
354       break;
355     case HAL_PIXEL_FORMAT_RGBA_4444:
356       format = kFormatRGBA4444;
357       break;
358     case HAL_PIXEL_FORMAT_BGRA_8888:
359       format = kFormatBGRA8888;
360       break;
361     case HAL_PIXEL_FORMAT_RGBX_8888:
362       format = kFormatRGBX8888;
363       break;
364     case HAL_PIXEL_FORMAT_BGRX_8888:
365       format = kFormatBGRX8888;
366       break;
367     case HAL_PIXEL_FORMAT_RGB_888:
368       format = kFormatRGB888;
369       break;
370     case HAL_PIXEL_FORMAT_RGB_565:
371       format = kFormatRGB565;
372       break;
373     case HAL_PIXEL_FORMAT_BGR_565:
374       format = kFormatBGR565;
375       break;
376     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
377     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
378       format = kFormatYCbCr420SemiPlanarVenus;
379       break;
380     case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
381       format = kFormatYCrCb420SemiPlanarVenus;
382       break;
383     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
384       format = kFormatYCbCr420SPVenusUbwc;
385       break;
386     case HAL_PIXEL_FORMAT_YV12:
387       format = kFormatYCrCb420PlanarStride16;
388       break;
389     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
390       format = kFormatYCrCb420SemiPlanar;
391       break;
392     case HAL_PIXEL_FORMAT_YCbCr_420_SP:
393       format = kFormatYCbCr420SemiPlanar;
394       break;
395     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
396       format = kFormatYCbCr422H2V1SemiPlanar;
397       break;
398     case HAL_PIXEL_FORMAT_YCbCr_422_I:
399       format = kFormatYCbCr422H2V1Packed;
400       break;
401     case HAL_PIXEL_FORMAT_RGBA_1010102:
402       format = kFormatRGBA1010102;
403       break;
404     case HAL_PIXEL_FORMAT_ARGB_2101010:
405       format = kFormatARGB2101010;
406       break;
407     case HAL_PIXEL_FORMAT_RGBX_1010102:
408       format = kFormatRGBX1010102;
409       break;
410     case HAL_PIXEL_FORMAT_XRGB_2101010:
411       format = kFormatXRGB2101010;
412       break;
413     case HAL_PIXEL_FORMAT_BGRA_1010102:
414       format = kFormatBGRA1010102;
415       break;
416     case HAL_PIXEL_FORMAT_ABGR_2101010:
417       format = kFormatABGR2101010;
418       break;
419     case HAL_PIXEL_FORMAT_BGRX_1010102:
420       format = kFormatBGRX1010102;
421       break;
422     case HAL_PIXEL_FORMAT_XBGR_2101010:
423       format = kFormatXBGR2101010;
424       break;
425     case HAL_PIXEL_FORMAT_YCbCr_420_P010:
426       format = kFormatYCbCr420P010;
427       break;
428     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
429       format = kFormatYCbCr420TP10Ubwc;
430       break;
431     default:
432       DLOGW("Unsupported format type = %d", source);
433       return kFormatInvalid;
434   }
435 
436   return format;
437 }
438 
GetS3DFormat(uint32_t s3d_format)439 LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) {
440   LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone;
441   switch (s3d_format) {
442     case HAL_NO_3D:
443       sdm_s3d_format = kS3dFormatNone;
444       break;
445     case HAL_3D_SIDE_BY_SIDE_L_R:
446       sdm_s3d_format = kS3dFormatLeftRight;
447       break;
448     case HAL_3D_SIDE_BY_SIDE_R_L:
449       sdm_s3d_format = kS3dFormatRightLeft;
450       break;
451     case HAL_3D_TOP_BOTTOM:
452       sdm_s3d_format = kS3dFormatTopBottom;
453       break;
454     default:
455       DLOGW("Invalid S3D format %d", s3d_format);
456   }
457   return sdm_s3d_format;
458 }
459 
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)460 DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
461   LayerBuffer *layer_buffer = layer->input_buffer;
462   private_handle_t *handle = const_cast<private_handle_t *>(pvt_handle);
463 
464   BufferDim_t  buffer_dim = {};
465   buffer_dim.sliceWidth = pvt_handle->width;
466   buffer_dim.sliceHeight = pvt_handle->height;
467   if (getMetaData(handle, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
468 #ifdef USE_GRALLOC1
469     buffer_allocator_->GetCustomWidthAndHeight(pvt_handle, &buffer_dim.sliceWidth,
470                                                &buffer_dim.sliceHeight);
471 #else
472     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(handle, &buffer_dim.sliceWidth,
473                                                           &buffer_dim.sliceHeight);
474 #endif
475     layer_buffer->width = UINT32(buffer_dim.sliceWidth);
476     layer_buffer->height = UINT32(buffer_dim.sliceHeight);
477   }
478 
479   ColorSpace_t csc = ITU_R_601;
480   if (getMetaData(const_cast<private_handle_t *>(pvt_handle), GET_COLOR_SPACE, &csc) == 0) {
481     if (SetCSC(csc, &layer_buffer->csc) != kErrorNone) {
482       return kErrorNotSupported;
483     }
484   }
485 
486   IGC_t igc = {};
487   if (getMetaData(handle, GET_IGC, &igc) == 0) {
488     if (SetIGC(igc, &layer_buffer->igc) != kErrorNone) {
489       return kErrorNotSupported;
490     }
491   }
492 
493   uint32_t fps = 0;
494   if (getMetaData(handle, GET_REFRESH_RATE, &fps) == 0) {
495     layer->frame_rate = RoundToStandardFPS(fps);
496   }
497 
498   int32_t interlaced = 0;
499   if (getMetaData(handle, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
500     layer_buffer->flags.interlace = interlaced ? true : false;
501   }
502 
503   uint32_t linear_format = 0;
504   if (getMetaData(handle, GET_LINEAR_FORMAT, &linear_format) == 0) {
505     layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
506   }
507 
508   uint32_t s3d = 0;
509   if (getMetaData(handle, GET_S3D_FORMAT, &s3d) == 0) {
510     layer_buffer->s3d_format = GetS3DFormat(s3d);
511   }
512 
513   return kErrorNone;
514 }
515 
SetCSC(ColorSpace_t source,LayerCSC * target)516 DisplayError HWCLayer::SetCSC(ColorSpace_t source, LayerCSC *target) {
517   switch (source) {
518     case ITU_R_601:
519       *target = kCSCLimitedRange601;
520       break;
521     case ITU_R_601_FR:
522       *target = kCSCFullRange601;
523       break;
524     case ITU_R_709:
525       *target = kCSCLimitedRange709;
526       break;
527     default:
528       DLOGE("Unsupported CSC: %d", source);
529       return kErrorNotSupported;
530   }
531 
532   return kErrorNone;
533 }
534 
SetIGC(IGC_t source,LayerIGC * target)535 DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) {
536   switch (source) {
537     case IGC_NotSpecified:
538       *target = kIGCNotSpecified;
539       break;
540     case IGC_sRGB:
541       *target = kIGCsRGB;
542       break;
543     default:
544       DLOGE("Unsupported IGC: %d", source);
545       return kErrorNotSupported;
546   }
547 
548   return kErrorNone;
549 }
550 
RoundToStandardFPS(float fps)551 uint32_t HWCLayer::RoundToStandardFPS(float fps) {
552   static const uint32_t standard_fps[4] = {24, 30, 48, 60};
553   uint32_t frame_rate = (uint32_t)(fps);
554 
555   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
556   for (int i = 0; i < count; i++) {
557     if ((standard_fps[i] - frame_rate) < 2) {
558       // Most likely used for video, the fps can fluctuate
559       // Ex: b/w 29 and 30 for 30 fps clip
560       return standard_fps[i];
561     }
562   }
563 
564   return frame_rate;
565 }
566 
SetComposition(const LayerComposition & sdm_composition)567 void HWCLayer::SetComposition(const LayerComposition &sdm_composition) {
568   auto hwc_composition = HWC2::Composition::Invalid;
569   switch (sdm_composition) {
570     case kCompositionGPU:
571       hwc_composition = HWC2::Composition::Client;
572       break;
573     case kCompositionHWCursor:
574       hwc_composition = HWC2::Composition::Cursor;
575       break;
576     default:
577       hwc_composition = HWC2::Composition::Device;
578       break;
579   }
580   // Update solid fill composition
581   if (sdm_composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
582     hwc_composition = HWC2::Composition::SolidColor;
583   }
584   device_selected_ = hwc_composition;
585 
586   return;
587 }
PushReleaseFence(int32_t fence)588 void HWCLayer::PushReleaseFence(int32_t fence) {
589   release_fences_.push(fence);
590 }
PopReleaseFence(void)591 int32_t HWCLayer::PopReleaseFence(void) {
592   if (release_fences_.empty())
593     return -1;
594   auto fence = release_fences_.front();
595   release_fences_.pop();
596   return fence;
597 }
598 
599 }  // namespace sdm
600