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