• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *     * Redistributions of source code must retain the above copyright
8 *       notice, this list of conditions and the following disclaimer.
9 *     * Redistributions in binary form must reproduce the above
10 *       copyright notice, this list of conditions and the following
11 *       disclaimer in the documentation and/or other materials provided
12 *       with the distribution.
13 *     * Neither the name of The Linux Foundation nor the names of its
14 *       contributors may be used to endorse or promote products derived
15 *       from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <math.h>
31 #include <errno.h>
32 #include <gralloc_priv.h>
33 #include <gr.h>
34 #include <utils/constants.h>
35 #include <utils/formats.h>
36 #include <utils/rect.h>
37 #include <utils/debug.h>
38 #include <sync/sync.h>
39 #include <cutils/properties.h>
40 #include <map>
41 #include <utility>
42 #include <vector>
43 
44 #include "hwc_display.h"
45 #include "hwc_debugger.h"
46 #include "blit_engine_c2d.h"
47 
48 #ifdef QTI_BSP
49 #include <hardware/display_defs.h>
50 #endif
51 
52 #define __CLASS__ "HWCDisplay"
53 
54 namespace sdm {
55 
ApplyDeInterlaceAdjustment(Layer * layer)56 static void ApplyDeInterlaceAdjustment(Layer *layer) {
57   // De-interlacing adjustment
58   if (layer->input_buffer->flags.interlace) {
59     float height = (layer->src_rect.bottom - layer->src_rect.top) / 2.0f;
60     layer->src_rect.top = ROUND_UP_ALIGN_DOWN(layer->src_rect.top / 2.0f, 2);
61     layer->src_rect.bottom = layer->src_rect.top + floorf(height);
62   }
63 }
64 
HWCDisplay(CoreInterface * core_intf,hwc_procs_t const ** hwc_procs,DisplayType type,int id,bool needs_blit,qService::QService * qservice,DisplayClass display_class)65 HWCDisplay::HWCDisplay(CoreInterface *core_intf, hwc_procs_t const **hwc_procs, DisplayType type,
66                        int id, bool needs_blit, qService::QService *qservice,
67                        DisplayClass display_class)
68   : core_intf_(core_intf), hwc_procs_(hwc_procs), type_(type), id_(id), needs_blit_(needs_blit),
69     qservice_(qservice), display_class_(display_class) {
70 }
71 
Init()72 int HWCDisplay::Init() {
73   DisplayError error = core_intf_->CreateDisplay(type_, this, &display_intf_);
74   if (error != kErrorNone) {
75     DLOGE("Display create failed. Error = %d display_type %d event_handler %p disp_intf %p",
76       error, type_, this, &display_intf_);
77     return -EINVAL;
78   }
79 
80   int property_swap_interval = 1;
81   HWCDebugHandler::Get()->GetProperty("debug.egl.swapinterval", &property_swap_interval);
82   if (property_swap_interval == 0) {
83     swap_interval_zero_ = true;
84   }
85 
86   int blit_enabled = 0;
87   HWCDebugHandler::Get()->GetProperty("persist.hwc.blit.comp", &blit_enabled);
88   if (needs_blit_ && blit_enabled) {
89     blit_engine_ = new BlitEngineC2d();
90     if (!blit_engine_) {
91       DLOGI("Create Blit Engine C2D failed");
92     } else {
93       if (blit_engine_->Init() < 0) {
94         DLOGI("Blit Engine Init failed, Blit Composition will not be used!!");
95         delete blit_engine_;
96         blit_engine_ = NULL;
97       }
98     }
99   }
100 
101   display_intf_->GetRefreshRateRange(&min_refresh_rate_, &max_refresh_rate_);
102   current_refresh_rate_ = max_refresh_rate_;
103 
104   s3d_format_hwc_to_sdm_.insert(std::pair<int, LayerBufferS3DFormat>(HAL_NO_3D, kS3dFormatNone));
105   s3d_format_hwc_to_sdm_.insert(std::pair<int, LayerBufferS3DFormat>(HAL_3D_SIDE_BY_SIDE_L_R,
106                                 kS3dFormatLeftRight));
107   s3d_format_hwc_to_sdm_.insert(std::pair<int, LayerBufferS3DFormat>(HAL_3D_SIDE_BY_SIDE_R_L,
108                                 kS3dFormatRightLeft));
109   s3d_format_hwc_to_sdm_.insert(std::pair<int, LayerBufferS3DFormat>(HAL_3D_TOP_BOTTOM,
110                                 kS3dFormatTopBottom));
111 
112   return 0;
113 }
114 
Deinit()115 int HWCDisplay::Deinit() {
116   DisplayError error = core_intf_->DestroyDisplay(display_intf_);
117   if (error != kErrorNone) {
118     DLOGE("Display destroy failed. Error = %d", error);
119     return -EINVAL;
120   }
121 
122   if (blit_engine_) {
123     blit_engine_->DeInit();
124     delete blit_engine_;
125     blit_engine_ = NULL;
126   }
127 
128   return 0;
129 }
130 
EventControl(int event,int enable)131 int HWCDisplay::EventControl(int event, int enable) {
132   DisplayError error = kErrorNone;
133 
134   if (shutdown_pending_) {
135     return 0;
136   }
137 
138   switch (event) {
139   case HWC_EVENT_VSYNC:
140     error = display_intf_->SetVSyncState(enable);
141     break;
142   default:
143     DLOGW("Unsupported event = %d", event);
144   }
145 
146   if (error != kErrorNone) {
147     if (error == kErrorShutDown) {
148       shutdown_pending_ = true;
149       return 0;
150     }
151     DLOGE("Failed. event = %d, enable = %d, error = %d", event, enable, error);
152     return -EINVAL;
153   }
154 
155   return 0;
156 }
157 
SetPowerMode(int mode)158 int HWCDisplay::SetPowerMode(int mode) {
159   DLOGI("display = %d, mode = %d", id_, mode);
160   DisplayState state = kStateOff;
161   bool flush_on_error = flush_on_error_;
162 
163   if (shutdown_pending_) {
164     return 0;
165   }
166 
167   switch (mode) {
168   case HWC_POWER_MODE_OFF:
169     // During power off, all of the buffers are released.
170     // Do not flush until a buffer is successfully submitted again.
171     flush_on_error = false;
172     state = kStateOff;
173     break;
174 
175   case HWC_POWER_MODE_NORMAL:
176     state = kStateOn;
177     last_power_mode_ = HWC_POWER_MODE_NORMAL;
178     break;
179 
180   case HWC_POWER_MODE_DOZE:
181     state = kStateDoze;
182     last_power_mode_ = HWC_POWER_MODE_DOZE;
183     break;
184 
185   case HWC_POWER_MODE_DOZE_SUSPEND:
186     state = kStateDozeSuspend;
187     last_power_mode_ = HWC_POWER_MODE_DOZE_SUSPEND;
188     break;
189 
190   default:
191     return -EINVAL;
192   }
193 
194   DisplayError error = display_intf_->SetDisplayState(state);
195   if (error == kErrorNone) {
196     flush_on_error_ = flush_on_error;
197   } else {
198     if (error == kErrorShutDown) {
199       shutdown_pending_ = true;
200       return 0;
201     }
202     DLOGE("Set state failed. Error = %d", error);
203     return -EINVAL;
204   }
205 
206   return 0;
207 }
208 
GetDisplayConfigs(uint32_t * configs,size_t * num_configs)209 int HWCDisplay::GetDisplayConfigs(uint32_t *configs, size_t *num_configs) {
210   if (*num_configs > 0) {
211     configs[0] = 0;
212     *num_configs = 1;
213   }
214 
215   return 0;
216 }
217 
GetDisplayAttributes(uint32_t config,const uint32_t * display_attributes,int32_t * values)218 int HWCDisplay::GetDisplayAttributes(uint32_t config, const uint32_t *display_attributes,
219                                      int32_t *values) {
220   DisplayConfigVariableInfo variable_config;
221   DisplayError error = display_intf_->GetFrameBufferConfig(&variable_config);
222   if (error != kErrorNone) {
223     DLOGV("Get variable config failed. Error = %d", error);
224     return -EINVAL;
225   }
226 
227   for (int i = 0; display_attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++) {
228     switch (display_attributes[i]) {
229     case HWC_DISPLAY_VSYNC_PERIOD:
230       values[i] = INT32(variable_config.vsync_period_ns);
231       break;
232     case HWC_DISPLAY_WIDTH:
233       values[i] = INT32(variable_config.x_pixels);
234       break;
235     case HWC_DISPLAY_HEIGHT:
236       values[i] = INT32(variable_config.y_pixels);
237       break;
238     case HWC_DISPLAY_DPI_X:
239       values[i] = INT32(variable_config.x_dpi * 1000.0f);
240       break;
241     case HWC_DISPLAY_DPI_Y:
242       values[i] = INT32(variable_config.y_dpi * 1000.0f);
243       break;
244     default:
245       DLOGW("Spurious attribute type = %d", display_attributes[i]);
246       return -EINVAL;
247     }
248   }
249 
250   return 0;
251 }
252 
GetActiveConfig()253 int HWCDisplay::GetActiveConfig() {
254   return 0;
255 }
256 
SetActiveConfig(int index)257 int HWCDisplay::SetActiveConfig(int index) {
258   return -1;
259 }
260 
SetMixerResolution(uint32_t width,uint32_t height)261 DisplayError HWCDisplay::SetMixerResolution(uint32_t width, uint32_t height) {
262   return kErrorNotSupported;
263 }
264 
SetFrameDumpConfig(uint32_t count,uint32_t bit_mask_layer_type)265 void HWCDisplay::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
266   dump_frame_count_ = count;
267   dump_frame_index_ = 0;
268   dump_input_layers_ = ((bit_mask_layer_type & (1 << INPUT_LAYER_DUMP)) != 0);
269 
270   if (blit_engine_) {
271     blit_engine_->SetFrameDumpConfig(count);
272   }
273 
274   DLOGI("num_frame_dump %d, input_layer_dump_enable %d", dump_frame_count_, dump_input_layers_);
275 }
276 
GetLastPowerMode()277 uint32_t HWCDisplay::GetLastPowerMode() {
278   return last_power_mode_;
279 }
280 
VSync(const DisplayEventVSync & vsync)281 DisplayError HWCDisplay::VSync(const DisplayEventVSync &vsync) {
282   const hwc_procs_t *hwc_procs = *hwc_procs_;
283 
284   if (!hwc_procs) {
285     return kErrorParameters;
286   }
287 
288   hwc_procs->vsync(hwc_procs, id_, vsync.timestamp);
289 
290   return kErrorNone;
291 }
292 
Refresh()293 DisplayError HWCDisplay::Refresh() {
294   return kErrorNotSupported;
295 }
296 
CECMessage(char * message)297 DisplayError HWCDisplay::CECMessage(char *message) {
298   if (qservice_) {
299     qservice_->onCECMessageReceived(message, 0);
300   } else {
301     DLOGW("Qservice instance not available.");
302   }
303 
304   return kErrorNone;
305 }
306 
AllocateLayerStack(hwc_display_contents_1_t * content_list)307 int HWCDisplay::AllocateLayerStack(hwc_display_contents_1_t *content_list) {
308   if (!content_list || !content_list->numHwLayers) {
309     DLOGW("Invalid content list");
310     return -EINVAL;
311   }
312 
313   size_t num_hw_layers = content_list->numHwLayers;
314   uint32_t blit_target_count = 0;
315 
316   if (blit_engine_) {
317     blit_target_count = kMaxBlitTargetLayers;
318   }
319 
320   FreeLayerStack();
321 
322   for (size_t i = 0; i < num_hw_layers + blit_target_count; i++) {
323     Layer *layer = new Layer();
324     LayerBuffer *layer_buffer = new LayerBuffer();
325     layer->input_buffer = layer_buffer;
326     layer_stack_.layers.push_back(layer);
327   }
328 
329   return 0;
330 }
331 
FreeLayerStack()332 void HWCDisplay::FreeLayerStack() {
333   for (Layer *layer : layer_stack_.layers) {
334     delete layer->input_buffer;
335     delete layer;
336   }
337   layer_stack_ = {};
338 }
339 
PrepareLayerParams(hwc_layer_1_t * hwc_layer,Layer * layer)340 int HWCDisplay::PrepareLayerParams(hwc_layer_1_t *hwc_layer, Layer* layer) {
341   const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer->handle);
342 
343   LayerBuffer *layer_buffer = layer->input_buffer;
344 
345   if (pvt_handle) {
346     layer_buffer->format = GetSDMFormat(pvt_handle->format, pvt_handle->flags);
347     layer_buffer->width = UINT32(pvt_handle->width);
348     layer_buffer->height = UINT32(pvt_handle->height);
349 
350     if (SetMetaData(pvt_handle, layer) != kErrorNone) {
351       return -EINVAL;
352     }
353 
354     if (pvt_handle->bufferType == BUFFER_TYPE_VIDEO) {
355       layer_stack_.flags.video_present = true;
356       layer_buffer->flags.video = true;
357     }
358     // TZ Protected Buffer - L1
359     if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
360       layer_stack_.flags.secure_present = true;
361       layer_buffer->flags.secure = true;
362     }
363     // Gralloc Usage Protected Buffer - L3 - which needs to be treated as Secure & avoid fallback
364     if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER) {
365       layer_stack_.flags.secure_present = true;
366     }
367     if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) {
368       layer_buffer->flags.secure_display = true;
369     }
370 
371     // check if this is special solid_fill layer without input_buffer.
372     if (solid_fill_enable_ && pvt_handle->fd == -1) {
373       layer->flags.solid_fill = true;
374       layer->solid_fill_color = solid_fill_color_;
375     }
376   } else {
377     // for FBT layer
378     if (hwc_layer->compositionType == HWC_FRAMEBUFFER_TARGET) {
379       uint32_t x_pixels;
380       uint32_t y_pixels;
381       int aligned_width;
382       int aligned_height;
383       int usage = GRALLOC_USAGE_HW_FB;
384       int format = HAL_PIXEL_FORMAT_RGBA_8888;
385       int ubwc_enabled = 0;
386       int flags = 0;
387       HWCDebugHandler::Get()->GetProperty("debug.gralloc.enable_fb_ubwc", &ubwc_enabled);
388       if (ubwc_enabled == 1) {
389         usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
390         flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
391       }
392 
393       GetFrameBufferResolution(&x_pixels, &y_pixels);
394 
395       AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(INT(x_pixels), INT(y_pixels), format,
396                                                             usage, aligned_width, aligned_height);
397       layer_buffer->width = UINT32(aligned_width);
398       layer_buffer->height = UINT32(aligned_height);
399       layer_buffer->format = GetSDMFormat(format, flags);
400     }
401   }
402 
403   return 0;
404 }
405 
CommitLayerParams(hwc_layer_1_t * hwc_layer,Layer * layer)406 void HWCDisplay::CommitLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer) {
407   const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer->handle);
408   LayerBuffer *layer_buffer = layer->input_buffer;
409 
410   if (pvt_handle) {
411     layer_buffer->planes[0].fd = pvt_handle->fd;
412     layer_buffer->planes[0].offset = pvt_handle->offset;
413     layer_buffer->planes[0].stride = UINT32(pvt_handle->width);
414     layer_buffer->size = pvt_handle->size;
415   }
416 
417   // if swapinterval property is set to 0 then close and reset the acquireFd
418   if (swap_interval_zero_ && hwc_layer->acquireFenceFd >= 0) {
419     close(hwc_layer->acquireFenceFd);
420     hwc_layer->acquireFenceFd = -1;
421   }
422   layer_buffer->acquire_fence_fd = hwc_layer->acquireFenceFd;
423 }
424 
PrePrepareLayerStack(hwc_display_contents_1_t * content_list)425 int HWCDisplay::PrePrepareLayerStack(hwc_display_contents_1_t *content_list) {
426   if (shutdown_pending_) {
427     return 0;
428   }
429 
430   size_t num_hw_layers = content_list->numHwLayers;
431 
432   use_blit_comp_ = false;
433   metadata_refresh_rate_ = 0;
434   display_rect_ = LayerRect();
435 
436   // Configure each layer
437   for (size_t i = 0; i < num_hw_layers; i++) {
438     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
439 
440     Layer *layer = layer_stack_.layers.at(i);
441     int ret = PrepareLayerParams(&content_list->hwLayers[i], layer);
442 
443     if (ret != kErrorNone) {
444       return ret;
445     }
446 
447     layer->flags.skip = ((hwc_layer.flags & HWC_SKIP_LAYER) > 0);
448     layer->flags.solid_fill = (hwc_layer.flags & kDimLayer) || solid_fill_enable_;
449     if (layer->flags.skip || layer->flags.solid_fill) {
450       layer->dirty_regions.clear();
451     }
452 
453     hwc_rect_t scaled_display_frame = hwc_layer.displayFrame;
454     ApplyScanAdjustment(&scaled_display_frame);
455 
456     SetRect(scaled_display_frame, &layer->dst_rect);
457     SetRect(hwc_layer.sourceCropf, &layer->src_rect);
458     ApplyDeInterlaceAdjustment(layer);
459 
460     uint32_t num_visible_rects = UINT32(hwc_layer.visibleRegionScreen.numRects);
461     uint32_t num_dirty_rects = UINT32(hwc_layer.surfaceDamage.numRects);
462 
463     for (uint32_t j = 0; j < num_visible_rects; j++) {
464       LayerRect visible_rect = {};
465       SetRect(hwc_layer.visibleRegionScreen.rects[j], &visible_rect);
466       layer->visible_regions.push_back(visible_rect);
467     }
468 
469     for (uint32_t j = 0; j < num_dirty_rects; j++) {
470       LayerRect dirty_rect = {};
471       SetRect(hwc_layer.surfaceDamage.rects[j], &dirty_rect);
472       layer->dirty_regions.push_back(dirty_rect);
473     }
474 
475     if (blit_engine_) {
476       for (uint32_t j = 0; j < kMaxBlitTargetLayers; j++) {
477         LayerRect blit_rect = {};
478         layer->blit_regions.push_back(blit_rect);
479       }
480     }
481 
482     SetComposition(hwc_layer.compositionType, &layer->composition);
483     if (hwc_layer.compositionType != HWC_FRAMEBUFFER_TARGET) {
484       display_rect_ = Union(display_rect_, layer->dst_rect);
485     }
486 
487     // For dim layers, SurfaceFlinger
488     //    - converts planeAlpha to per pixel alpha,
489     //    - sets RGB color to 000,
490     //    - sets planeAlpha to 0xff,
491     //    - blending to Premultiplied.
492     // This can be achieved at hardware by
493     //    - solid fill ARGB to 0xff000000,
494     //    - incoming planeAlpha,
495     //    - blending to Coverage.
496     if (hwc_layer.flags & kDimLayer) {
497       layer->input_buffer->format = kFormatARGB8888;
498       layer->solid_fill_color = 0xff000000;
499       SetBlending(HWC_BLENDING_COVERAGE, &layer->blending);
500     } else {
501       SetBlending(hwc_layer.blending, &layer->blending);
502       LayerTransform &layer_transform = layer->transform;
503       uint32_t &hwc_transform = hwc_layer.transform;
504       layer_transform.flip_horizontal = ((hwc_transform & HWC_TRANSFORM_FLIP_H) > 0);
505       layer_transform.flip_vertical = ((hwc_transform & HWC_TRANSFORM_FLIP_V) > 0);
506       layer_transform.rotation = ((hwc_transform & HWC_TRANSFORM_ROT_90) ? 90.0f : 0.0f);
507     }
508 
509     // TODO(user): Remove below block.
510     // For solid fill, only dest rect need to be specified.
511     if (layer->flags.solid_fill) {
512       LayerBuffer *input_buffer = layer->input_buffer;
513       input_buffer->width = UINT32(layer->dst_rect.right - layer->dst_rect.left);
514       input_buffer->height = UINT32(layer->dst_rect.bottom - layer->dst_rect.top);
515       layer->src_rect.left = 0;
516       layer->src_rect.top = 0;
517       layer->src_rect.right = input_buffer->width;
518       layer->src_rect.bottom = input_buffer->height;
519     }
520 
521     layer->plane_alpha = hwc_layer.planeAlpha;
522     layer->flags.cursor = ((hwc_layer.flags & HWC_IS_CURSOR_LAYER) > 0);
523     layer->flags.updating = true;
524 
525     if (num_hw_layers <= kMaxLayerCount) {
526       layer->flags.updating = IsLayerUpdating(content_list, layer);
527     }
528 #ifdef QTI_BSP
529     if (hwc_layer.flags & HWC_SCREENSHOT_ANIMATOR_LAYER) {
530       layer_stack_.flags.animating = true;
531     }
532 #endif
533     if (layer->flags.skip) {
534       layer_stack_.flags.skip_present = true;
535     }
536 
537     if (layer->flags.cursor) {
538       layer_stack_.flags.cursor_present = true;
539     }
540 
541     PrepareDynamicRefreshRate(layer);
542 
543     layer->input_buffer->buffer_id = reinterpret_cast<uint64_t>(hwc_layer.handle);
544   }
545 
546   // Prepare the Blit Target
547   if (blit_engine_) {
548   // TODO(user): Fix this to enable BLIT
549 #if 0
550     int ret = blit_engine_->Prepare(&layer_stack_);
551     if (ret) {
552       // Blit engine cannot handle this layer stack, hence set the layer stack
553       // count to num_hw_layers
554       layer_stack_.layer_count -= kMaxBlitTargetLayers;
555     } else {
556       use_blit_comp_ = true;
557     }
558 #endif
559   }
560 
561   // Configure layer stack
562   layer_stack_.flags.geometry_changed = ((content_list->flags & HWC_GEOMETRY_CHANGED) > 0);
563 
564   return 0;
565 }
566 
PrepareLayerStack(hwc_display_contents_1_t * content_list)567 int HWCDisplay::PrepareLayerStack(hwc_display_contents_1_t *content_list) {
568   if (shutdown_pending_) {
569     return 0;
570   }
571 
572   size_t num_hw_layers = content_list->numHwLayers;
573 
574   if (!skip_prepare_) {
575     DisplayError error = display_intf_->Prepare(&layer_stack_);
576     if (error != kErrorNone) {
577       if (error == kErrorShutDown) {
578         shutdown_pending_ = true;
579       } else if (error != kErrorPermission) {
580         DLOGE("Prepare failed. Error = %d", error);
581         // To prevent surfaceflinger infinite wait, flush the previous frame during Commit()
582         // so that previous buffer and fences are released, and override the error.
583         flush_ = true;
584       }
585 
586       return 0;
587     }
588   } else {
589     // Skip is not set
590     MarkLayersForGPUBypass(content_list);
591     skip_prepare_ = false;
592     DLOGI("SecureDisplay %s, Skip Prepare/Commit and Flush", secure_display_active_ ? "Starting" :
593           "Stopping");
594     flush_ = true;
595   }
596 
597   for (size_t i = 0; i < num_hw_layers; i++) {
598     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
599     Layer *layer = layer_stack_.layers.at(i);
600     LayerComposition composition = layer->composition;
601 
602     if ((composition == kCompositionSDE) || (composition == kCompositionHybrid) ||
603         (composition == kCompositionBlit)) {
604       hwc_layer.hints |= HWC_HINT_CLEAR_FB;
605     }
606     SetComposition(composition, &hwc_layer.compositionType);
607   }
608 
609   return 0;
610 }
611 
CommitLayerStack(hwc_display_contents_1_t * content_list)612 int HWCDisplay::CommitLayerStack(hwc_display_contents_1_t *content_list) {
613   if (!content_list || !content_list->numHwLayers) {
614     DLOGW("Invalid content list");
615     return -EINVAL;
616   }
617 
618   if (shutdown_pending_) {
619     return 0;
620   }
621 
622   int status = 0;
623 
624   size_t num_hw_layers = content_list->numHwLayers;
625 
626   DumpInputBuffers(content_list);
627 
628   if (!flush_) {
629     for (size_t i = 0; i < num_hw_layers; i++) {
630       CommitLayerParams(&content_list->hwLayers[i], layer_stack_.layers.at(i));
631     }
632 
633     if (use_blit_comp_) {
634       status = blit_engine_->PreCommit(content_list, &layer_stack_);
635       if (status == 0) {
636         status = blit_engine_->Commit(content_list, &layer_stack_);
637         if (status != 0) {
638           DLOGE("Blit Comp Failed!");
639         }
640       }
641     }
642 
643     DisplayError error = kErrorUndefined;
644     if (status == 0) {
645       error = display_intf_->Commit(&layer_stack_);
646       status = 0;
647     }
648 
649     if (error == kErrorNone) {
650       // A commit is successfully submitted, start flushing on failure now onwards.
651       flush_on_error_ = true;
652     } else {
653       if (error == kErrorShutDown) {
654         shutdown_pending_ = true;
655         return status;
656       } else if (error != kErrorPermission) {
657         DLOGE("Commit failed. Error = %d", error);
658         // To prevent surfaceflinger infinite wait, flush the previous frame during Commit()
659         // so that previous buffer and fences are released, and override the error.
660         flush_ = true;
661       }
662     }
663   }
664 
665   return status;
666 }
667 
PostCommitLayerStack(hwc_display_contents_1_t * content_list)668 int HWCDisplay::PostCommitLayerStack(hwc_display_contents_1_t *content_list) {
669   size_t num_hw_layers = content_list->numHwLayers;
670   int status = 0;
671 
672   // Do no call flush on errors, if a successful buffer is never submitted.
673   if (flush_ && flush_on_error_) {
674     display_intf_->Flush();
675   }
676 
677   // Set the release fence fd to the blit engine
678   if (use_blit_comp_ && blit_engine_->BlitActive()) {
679     blit_engine_->PostCommit(&layer_stack_);
680   }
681 
682   for (size_t i = 0; i < num_hw_layers; i++) {
683     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
684     Layer *layer = layer_stack_.layers.at(i);
685     LayerBuffer *layer_buffer = layer->input_buffer;
686 
687     if (!flush_) {
688       // If swapinterval property is set to 0 or for single buffer layers, do not update f/w
689       // release fences and discard fences from driver
690       if (swap_interval_zero_ || layer->flags.single_buffer) {
691         hwc_layer.releaseFenceFd = -1;
692         close(layer_buffer->release_fence_fd);
693         layer_buffer->release_fence_fd = -1;
694       } else if (layer->composition != kCompositionGPU) {
695         hwc_layer.releaseFenceFd = layer_buffer->release_fence_fd;
696       }
697 
698       // During animation on external/virtual display, SDM will use the cached
699       // framebuffer layer throughout animation and do not allow framework to do eglswapbuffer on
700       // framebuffer target. So graphics doesn't close the release fence fd of framebuffer target,
701       // Hence close the release fencefd of framebuffer target here.
702       if (layer->composition == kCompositionGPUTarget && animating_) {
703         close(hwc_layer.releaseFenceFd);
704         hwc_layer.releaseFenceFd = -1;
705       }
706     }
707 
708     if (hwc_layer.acquireFenceFd >= 0) {
709       close(hwc_layer.acquireFenceFd);
710       hwc_layer.acquireFenceFd = -1;
711     }
712   }
713 
714   if (!flush_) {
715     animating_ = layer_stack_.flags.animating;
716     // if swapinterval property is set to 0 then close and reset the list retire fence
717     if (swap_interval_zero_) {
718       close(layer_stack_.retire_fence_fd);
719       layer_stack_.retire_fence_fd = -1;
720     }
721     content_list->retireFenceFd = layer_stack_.retire_fence_fd;
722 
723     if (dump_frame_count_) {
724       dump_frame_count_--;
725       dump_frame_index_++;
726     }
727   }
728 
729   flush_ = false;
730 
731   return status;
732 }
733 
IsLayerUpdating(hwc_display_contents_1_t * content_list,const Layer * layer)734 bool HWCDisplay::IsLayerUpdating(hwc_display_contents_1_t *content_list, const Layer *layer) {
735   // Layer should be considered updating if
736   //   a) layer is in single buffer mode, or
737   //   b) valid dirty_regions(android specific hint for updating status), or
738   //   c) layer stack geometry has changed
739   return (layer->flags.single_buffer || IsSurfaceUpdated(layer->dirty_regions) ||
740          (layer_stack_.flags.geometry_changed));
741 }
742 
SetRect(const hwc_rect_t & source,LayerRect * target)743 void HWCDisplay::SetRect(const hwc_rect_t &source, LayerRect *target) {
744   target->left = FLOAT(source.left);
745   target->top = FLOAT(source.top);
746   target->right = FLOAT(source.right);
747   target->bottom = FLOAT(source.bottom);
748 }
749 
SetRect(const hwc_frect_t & source,LayerRect * target)750 void HWCDisplay::SetRect(const hwc_frect_t &source, LayerRect *target) {
751   target->left = floorf(source.left);
752   target->top = floorf(source.top);
753   target->right = ceilf(source.right);
754   target->bottom = ceilf(source.bottom);
755 }
756 
SetComposition(const int32_t & source,LayerComposition * target)757 void HWCDisplay::SetComposition(const int32_t &source, LayerComposition *target) {
758   switch (source) {
759   case HWC_FRAMEBUFFER_TARGET:  *target = kCompositionGPUTarget;  break;
760   default:                      *target = kCompositionGPU;        break;
761   }
762 }
763 
SetComposition(const LayerComposition & source,int32_t * target)764 void HWCDisplay::SetComposition(const LayerComposition &source, int32_t *target) {
765   switch (source) {
766   case kCompositionGPUTarget:   *target = HWC_FRAMEBUFFER_TARGET; break;
767   case kCompositionGPU:         *target = HWC_FRAMEBUFFER;        break;
768   case kCompositionHWCursor:    *target = HWC_CURSOR_OVERLAY;     break;
769   default:                      *target = HWC_OVERLAY;            break;
770   }
771 }
772 
SetBlending(const int32_t & source,LayerBlending * target)773 void HWCDisplay::SetBlending(const int32_t &source, LayerBlending *target) {
774   switch (source) {
775   case HWC_BLENDING_PREMULT:    *target = kBlendingPremultiplied;   break;
776   case HWC_BLENDING_COVERAGE:   *target = kBlendingCoverage;        break;
777   default:                      *target = kBlendingOpaque;          break;
778   }
779 }
780 
SetIdleTimeoutMs(uint32_t timeout_ms)781 void HWCDisplay::SetIdleTimeoutMs(uint32_t timeout_ms) {
782   return;
783 }
784 
SetMaxMixerStages(uint32_t max_mixer_stages)785 DisplayError HWCDisplay::SetMaxMixerStages(uint32_t max_mixer_stages) {
786   DisplayError error = kErrorNone;
787 
788   if (display_intf_) {
789     error = display_intf_->SetMaxMixerStages(max_mixer_stages);
790   }
791 
792   return error;
793 }
794 
GetSDMFormat(const int32_t & source,const int flags)795 LayerBufferFormat HWCDisplay::GetSDMFormat(const int32_t &source, const int flags) {
796   LayerBufferFormat format = kFormatInvalid;
797   if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
798     switch (source) {
799     case HAL_PIXEL_FORMAT_RGBA_8888:           format = kFormatRGBA8888Ubwc;            break;
800     case HAL_PIXEL_FORMAT_RGBX_8888:           format = kFormatRGBX8888Ubwc;            break;
801     case HAL_PIXEL_FORMAT_BGR_565:             format = kFormatBGR565Ubwc;              break;
802     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
803     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
804     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:     format = kFormatYCbCr420SPVenusUbwc;     break;
805     case HAL_PIXEL_FORMAT_RGBA_1010102:        format = kFormatRGBA1010102Ubwc;         break;
806     case HAL_PIXEL_FORMAT_RGBX_1010102:        format = kFormatRGBX1010102Ubwc;         break;
807     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: format = kFormatYCbCr420TP10Ubwc;        break;
808     default:
809       DLOGE("Unsupported format type for UBWC %d", source);
810       return kFormatInvalid;
811     }
812     return format;
813   }
814 
815   switch (source) {
816   case HAL_PIXEL_FORMAT_RGBA_8888:                format = kFormatRGBA8888;                 break;
817   case HAL_PIXEL_FORMAT_RGBA_5551:                format = kFormatRGBA5551;                 break;
818   case HAL_PIXEL_FORMAT_RGBA_4444:                format = kFormatRGBA4444;                 break;
819   case HAL_PIXEL_FORMAT_BGRA_8888:                format = kFormatBGRA8888;                 break;
820   case HAL_PIXEL_FORMAT_RGBX_8888:                format = kFormatRGBX8888;                 break;
821   case HAL_PIXEL_FORMAT_BGRX_8888:                format = kFormatBGRX8888;                 break;
822   case HAL_PIXEL_FORMAT_RGB_888:                  format = kFormatRGB888;                   break;
823   case HAL_PIXEL_FORMAT_RGB_565:                  format = kFormatRGB565;                   break;
824   case HAL_PIXEL_FORMAT_BGR_565:                  format = kFormatBGR565;                   break;
825   case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
826   case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:       format = kFormatYCbCr420SemiPlanarVenus;  break;
827   case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:       format = kFormatYCrCb420SemiPlanarVenus;  break;
828   case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:  format = kFormatYCbCr420SPVenusUbwc;      break;
829   case HAL_PIXEL_FORMAT_YV12:                     format = kFormatYCrCb420PlanarStride16;   break;
830   case HAL_PIXEL_FORMAT_YCrCb_420_SP:             format = kFormatYCrCb420SemiPlanar;       break;
831   case HAL_PIXEL_FORMAT_YCbCr_420_SP:             format = kFormatYCbCr420SemiPlanar;       break;
832   case HAL_PIXEL_FORMAT_YCbCr_422_SP:             format = kFormatYCbCr422H2V1SemiPlanar;   break;
833   case HAL_PIXEL_FORMAT_YCbCr_422_I:              format = kFormatYCbCr422H2V1Packed;       break;
834   case HAL_PIXEL_FORMAT_RGBA_1010102:             format = kFormatRGBA1010102;              break;
835   case HAL_PIXEL_FORMAT_ARGB_2101010:             format = kFormatARGB2101010;              break;
836   case HAL_PIXEL_FORMAT_RGBX_1010102:             format = kFormatRGBX1010102;              break;
837   case HAL_PIXEL_FORMAT_XRGB_2101010:             format = kFormatXRGB2101010;              break;
838   case HAL_PIXEL_FORMAT_BGRA_1010102:             format = kFormatBGRA1010102;              break;
839   case HAL_PIXEL_FORMAT_ABGR_2101010:             format = kFormatABGR2101010;              break;
840   case HAL_PIXEL_FORMAT_BGRX_1010102:             format = kFormatBGRX1010102;              break;
841   case HAL_PIXEL_FORMAT_XBGR_2101010:             format = kFormatXBGR2101010;              break;
842   case HAL_PIXEL_FORMAT_YCbCr_420_P010:           format = kFormatYCbCr420P010;             break;
843   case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:      format = kFormatYCbCr420TP10Ubwc;         break;
844   default:
845     DLOGW("Unsupported format type = %d", source);
846     return kFormatInvalid;
847   }
848 
849   return format;
850 }
851 
DumpInputBuffers(hwc_display_contents_1_t * content_list)852 void HWCDisplay::DumpInputBuffers(hwc_display_contents_1_t *content_list) {
853   size_t num_hw_layers = content_list->numHwLayers;
854   char dir_path[PATH_MAX];
855 
856   if (!dump_frame_count_ || flush_ || !dump_input_layers_) {
857     return;
858   }
859 
860   snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString());
861 
862   if (mkdir(dir_path, 0777) != 0 && errno != EEXIST) {
863     DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno));
864     return;
865   }
866 
867   // if directory exists already, need to explicitly change the permission.
868   if (errno == EEXIST && chmod(dir_path, 0777) != 0) {
869     DLOGW("Failed to change permissions on %s directory", dir_path);
870     return;
871   }
872 
873   for (uint32_t i = 0; i < num_hw_layers; i++) {
874     hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
875     const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer.handle);
876 
877     if (hwc_layer.acquireFenceFd >= 0) {
878       int error = sync_wait(hwc_layer.acquireFenceFd, 1000);
879       if (error < 0) {
880         DLOGW("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
881         return;
882       }
883     }
884 
885     if (pvt_handle && pvt_handle->base) {
886       char dump_file_name[PATH_MAX];
887       size_t result = 0;
888 
889       snprintf(dump_file_name, sizeof(dump_file_name), "%s/input_layer%d_%dx%d_%s_frame%d.raw",
890                dir_path, i, pvt_handle->width, pvt_handle->height,
891                GetHALPixelFormatString(pvt_handle->format), dump_frame_index_);
892 
893       FILE* fp = fopen(dump_file_name, "w+");
894       if (fp) {
895         result = fwrite(reinterpret_cast<void *>(pvt_handle->base), pvt_handle->size, 1, fp);
896         fclose(fp);
897       }
898 
899       DLOGI("Frame Dump %s: is %s", dump_file_name, result ? "Successful" : "Failed");
900     }
901   }
902 }
903 
DumpOutputBuffer(const BufferInfo & buffer_info,void * base,int fence)904 void HWCDisplay::DumpOutputBuffer(const BufferInfo& buffer_info, void *base, int fence) {
905   char dir_path[PATH_MAX];
906 
907   snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString());
908 
909   if (mkdir(dir_path, 777) != 0 && errno != EEXIST) {
910     DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno));
911     return;
912   }
913 
914   // if directory exists already, need to explicitly change the permission.
915   if (errno == EEXIST && chmod(dir_path, 0777) != 0) {
916     DLOGW("Failed to change permissions on %s directory", dir_path);
917     return;
918   }
919 
920   if (base) {
921     char dump_file_name[PATH_MAX];
922     size_t result = 0;
923 
924     if (fence >= 0) {
925       int error = sync_wait(fence, 1000);
926       if (error < 0) {
927         DLOGW("sync_wait error errno = %d, desc = %s", errno,  strerror(errno));
928         return;
929       }
930     }
931 
932     snprintf(dump_file_name, sizeof(dump_file_name), "%s/output_layer_%dx%d_%s_frame%d.raw",
933              dir_path, buffer_info.buffer_config.width, buffer_info.buffer_config.height,
934              GetFormatString(buffer_info.buffer_config.format), dump_frame_index_);
935 
936     FILE* fp = fopen(dump_file_name, "w+");
937     if (fp) {
938       result = fwrite(base, buffer_info.alloc_buffer_info.size, 1, fp);
939       fclose(fp);
940     }
941 
942     DLOGI("Frame Dump of %s is %s", dump_file_name, result ? "Successful" : "Failed");
943   }
944 }
945 
GetHALPixelFormatString(int format)946 const char *HWCDisplay::GetHALPixelFormatString(int format) {
947   switch (format) {
948   case HAL_PIXEL_FORMAT_RGBA_8888:
949     return "RGBA_8888";
950   case HAL_PIXEL_FORMAT_RGBX_8888:
951     return "RGBX_8888";
952   case HAL_PIXEL_FORMAT_RGB_888:
953     return "RGB_888";
954   case HAL_PIXEL_FORMAT_RGB_565:
955     return "RGB_565";
956   case HAL_PIXEL_FORMAT_BGR_565:
957     return "BGR_565";
958   case HAL_PIXEL_FORMAT_BGRA_8888:
959     return "BGRA_8888";
960   case HAL_PIXEL_FORMAT_RGBA_5551:
961     return "RGBA_5551";
962   case HAL_PIXEL_FORMAT_RGBA_4444:
963     return "RGBA_4444";
964   case HAL_PIXEL_FORMAT_YV12:
965     return "YV12";
966   case HAL_PIXEL_FORMAT_YCbCr_422_SP:
967     return "YCbCr_422_SP_NV16";
968   case HAL_PIXEL_FORMAT_YCrCb_420_SP:
969     return "YCrCb_420_SP_NV21";
970   case HAL_PIXEL_FORMAT_YCbCr_422_I:
971     return "YCbCr_422_I_YUY2";
972   case HAL_PIXEL_FORMAT_YCrCb_422_I:
973     return "YCrCb_422_I_YVYU";
974   case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
975     return "NV12_ENCODEABLE";
976   case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
977     return "YCbCr_420_SP_TILED_TILE_4x2";
978   case HAL_PIXEL_FORMAT_YCbCr_420_SP:
979     return "YCbCr_420_SP";
980   case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
981     return "YCrCb_420_SP_ADRENO";
982   case HAL_PIXEL_FORMAT_YCrCb_422_SP:
983     return "YCrCb_422_SP";
984   case HAL_PIXEL_FORMAT_R_8:
985     return "R_8";
986   case HAL_PIXEL_FORMAT_RG_88:
987     return "RG_88";
988   case HAL_PIXEL_FORMAT_INTERLACE:
989     return "INTERLACE";
990   case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
991     return "YCbCr_420_SP_VENUS";
992   case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
993     return "YCrCb_420_SP_VENUS";
994   case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
995     return "YCbCr_420_SP_VENUS_UBWC";
996   case HAL_PIXEL_FORMAT_RGBA_1010102:
997     return "RGBA_1010102";
998   case HAL_PIXEL_FORMAT_ARGB_2101010:
999     return "ARGB_2101010";
1000   case HAL_PIXEL_FORMAT_RGBX_1010102:
1001     return "RGBX_1010102";
1002   case HAL_PIXEL_FORMAT_XRGB_2101010:
1003     return "XRGB_2101010";
1004   case HAL_PIXEL_FORMAT_BGRA_1010102:
1005     return "BGRA_1010102";
1006   case HAL_PIXEL_FORMAT_ABGR_2101010:
1007     return "ABGR_2101010";
1008   case HAL_PIXEL_FORMAT_BGRX_1010102:
1009     return "BGRX_1010102";
1010   case HAL_PIXEL_FORMAT_XBGR_2101010:
1011     return "XBGR_2101010";
1012   case HAL_PIXEL_FORMAT_YCbCr_420_P010:
1013     return "YCbCr_420_P010";
1014   case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
1015     return "YCbCr_420_TP10_UBWC";
1016   default:
1017     return "Unknown_format";
1018   }
1019 }
1020 
GetDisplayString()1021 const char *HWCDisplay::GetDisplayString() {
1022   switch (type_) {
1023   case kPrimary:
1024     return "primary";
1025   case kHDMI:
1026     return "hdmi";
1027   case kVirtual:
1028     return "virtual";
1029   default:
1030     return "invalid";
1031   }
1032 }
1033 
SetFrameBufferResolution(uint32_t x_pixels,uint32_t y_pixels)1034 int HWCDisplay::SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels) {
1035   DisplayConfigVariableInfo fb_config;
1036   DisplayError error = display_intf_->GetFrameBufferConfig(&fb_config);
1037   if (error != kErrorNone) {
1038     DLOGV("Get frame buffer config failed. Error = %d", error);
1039     return -EINVAL;
1040   }
1041 
1042   fb_config.x_pixels = x_pixels;
1043   fb_config.y_pixels = y_pixels;
1044 
1045   error = display_intf_->SetFrameBufferConfig(fb_config);
1046   if (error != kErrorNone) {
1047     DLOGV("Set frame buffer config failed. Error = %d", error);
1048     return -EINVAL;
1049   }
1050 
1051   DLOGI("New framebuffer resolution (%dx%d)", x_pixels, y_pixels);
1052 
1053   return 0;
1054 }
1055 
GetFrameBufferResolution(uint32_t * x_pixels,uint32_t * y_pixels)1056 void HWCDisplay::GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
1057   DisplayConfigVariableInfo fb_config;
1058   display_intf_->GetFrameBufferConfig(&fb_config);
1059 
1060   *x_pixels = fb_config.x_pixels;
1061   *y_pixels = fb_config.y_pixels;
1062 }
1063 
GetMixerResolution(uint32_t * x_pixels,uint32_t * y_pixels)1064 DisplayError HWCDisplay::GetMixerResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
1065   return display_intf_->GetMixerResolution(x_pixels, y_pixels);
1066 }
1067 
1068 
GetPanelResolution(uint32_t * x_pixels,uint32_t * y_pixels)1069 void HWCDisplay::GetPanelResolution(uint32_t *x_pixels, uint32_t *y_pixels) {
1070   DisplayConfigVariableInfo display_config;
1071   uint32_t active_index = 0;
1072 
1073   display_intf_->GetActiveConfig(&active_index);
1074   display_intf_->GetConfig(active_index, &display_config);
1075 
1076   *x_pixels = display_config.x_pixels;
1077   *y_pixels = display_config.y_pixels;
1078 }
1079 
SetDisplayStatus(uint32_t display_status)1080 int HWCDisplay::SetDisplayStatus(uint32_t display_status) {
1081   int status = 0;
1082   const hwc_procs_t *hwc_procs = *hwc_procs_;
1083 
1084   switch (display_status) {
1085   case kDisplayStatusResume:
1086     display_paused_ = false;
1087   case kDisplayStatusOnline:
1088     status = SetPowerMode(HWC_POWER_MODE_NORMAL);
1089     break;
1090   case kDisplayStatusPause:
1091     display_paused_ = true;
1092   case kDisplayStatusOffline:
1093     status = SetPowerMode(HWC_POWER_MODE_OFF);
1094     break;
1095   default:
1096     DLOGW("Invalid display status %d", display_status);
1097     return -EINVAL;
1098   }
1099 
1100   if (display_status == kDisplayStatusResume ||
1101       display_status == kDisplayStatusPause) {
1102     hwc_procs->invalidate(hwc_procs);
1103   }
1104 
1105   return status;
1106 }
1107 
SetCursorPosition(int x,int y)1108 int HWCDisplay::SetCursorPosition(int x, int y) {
1109   DisplayError error = kErrorNone;
1110 
1111   if (shutdown_pending_) {
1112     return 0;
1113   }
1114 
1115   error = display_intf_->SetCursorPosition(x, y);
1116   if (error != kErrorNone) {
1117     if (error == kErrorShutDown) {
1118       shutdown_pending_ = true;
1119       return 0;
1120     }
1121     DLOGE("Failed for x = %d y = %d, Error = %d", x, y, error);
1122     return -1;
1123   }
1124 
1125   return 0;
1126 }
1127 
OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level)1128 int HWCDisplay::OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level) {
1129   DisplayError error = display_intf_->OnMinHdcpEncryptionLevelChange(min_enc_level);
1130   if (error != kErrorNone) {
1131     DLOGE("Failed. Error = %d", error);
1132     return -1;
1133   }
1134 
1135   return 0;
1136 }
1137 
MarkLayersForGPUBypass(hwc_display_contents_1_t * content_list)1138 void HWCDisplay::MarkLayersForGPUBypass(hwc_display_contents_1_t *content_list) {
1139   for (size_t i = 0 ; i < (content_list->numHwLayers - 1); i++) {
1140     hwc_layer_1_t *layer = &content_list->hwLayers[i];
1141     layer->compositionType = HWC_OVERLAY;
1142   }
1143 }
1144 
ApplyScanAdjustment(hwc_rect_t * display_frame)1145 void HWCDisplay::ApplyScanAdjustment(hwc_rect_t *display_frame) {
1146 }
1147 
SetCSC(ColorSpace_t source,LayerCSC * target)1148 DisplayError HWCDisplay::SetCSC(ColorSpace_t source, LayerCSC *target) {
1149   switch (source) {
1150   case ITU_R_601:       *target = kCSCLimitedRange601;   break;
1151   case ITU_R_601_FR:    *target = kCSCFullRange601;      break;
1152   case ITU_R_709:       *target = kCSCLimitedRange709;   break;
1153   default:
1154     DLOGE("Unsupported CSC: %d", source);
1155     return kErrorNotSupported;
1156   }
1157 
1158   return kErrorNone;
1159 }
1160 
SetIGC(IGC_t source,LayerIGC * target)1161 DisplayError HWCDisplay::SetIGC(IGC_t source, LayerIGC *target) {
1162   switch (source) {
1163   case IGC_NotSpecified:    *target = kIGCNotSpecified; break;
1164   case IGC_sRGB:            *target = kIGCsRGB;   break;
1165   default:
1166     DLOGE("Unsupported IGC: %d", source);
1167     return kErrorNotSupported;
1168   }
1169 
1170   return kErrorNone;
1171 }
1172 
SetMetaData(const private_handle_t * pvt_handle,Layer * layer)1173 DisplayError HWCDisplay::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) {
1174   const MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
1175   LayerBuffer *layer_buffer = layer->input_buffer;
1176 
1177   if (!meta_data) {
1178     return kErrorNone;
1179   }
1180 
1181   if (meta_data->operation & UPDATE_COLOR_SPACE) {
1182     if (SetCSC(meta_data->colorSpace, &layer_buffer->csc) != kErrorNone) {
1183       return kErrorNotSupported;
1184     }
1185   }
1186 
1187   if (meta_data->operation & SET_IGC) {
1188     if (SetIGC(meta_data->igc, &layer_buffer->igc) != kErrorNone) {
1189       return kErrorNotSupported;
1190     }
1191   }
1192 
1193   if (meta_data->operation & UPDATE_REFRESH_RATE) {
1194     layer->frame_rate = RoundToStandardFPS(meta_data->refreshrate);
1195   }
1196 
1197   if ((meta_data->operation & PP_PARAM_INTERLACED) && meta_data->interlaced) {
1198     layer_buffer->flags.interlace = true;
1199   }
1200 
1201   if (meta_data->operation & LINEAR_FORMAT) {
1202     layer_buffer->format = GetSDMFormat(INT32(meta_data->linearFormat), 0);
1203   }
1204 
1205   if (meta_data->operation & UPDATE_BUFFER_GEOMETRY) {
1206     int actual_width = pvt_handle->width;
1207     int actual_height = pvt_handle->height;
1208     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(pvt_handle, actual_width, actual_height);
1209     layer_buffer->width = UINT32(actual_width);
1210     layer_buffer->height = UINT32(actual_height);
1211   }
1212 
1213   if (meta_data->operation & SET_SINGLE_BUFFER_MODE) {
1214     layer->flags.single_buffer = meta_data->isSingleBufferMode;
1215     // Graphics can set this operation on all types of layers including FB and set the actual value
1216     // to 0. To protect against SET operations of 0 value, we need to do a logical OR.
1217     layer_stack_.flags.single_buffered_layer_present |= meta_data->isSingleBufferMode;
1218   }
1219 
1220   if (meta_data->operation & S3D_FORMAT) {
1221     std::map<int, LayerBufferS3DFormat>::iterator it =
1222         s3d_format_hwc_to_sdm_.find(INT32(meta_data->s3dFormat));
1223     if (it != s3d_format_hwc_to_sdm_.end()) {
1224       layer->input_buffer->s3d_format = it->second;
1225     } else {
1226       DLOGW("Invalid S3D format %d", meta_data->s3dFormat);
1227     }
1228   }
1229 
1230   return kErrorNone;
1231 }
1232 
SetPanelBrightness(int level)1233 int HWCDisplay::SetPanelBrightness(int level) {
1234   int ret = 0;
1235   if (display_intf_)
1236     ret = display_intf_->SetPanelBrightness(level);
1237   else
1238     ret = -EINVAL;
1239 
1240   return ret;
1241 }
1242 
GetPanelBrightness(int * level)1243 int HWCDisplay::GetPanelBrightness(int *level) {
1244   return display_intf_->GetPanelBrightness(level);
1245 }
1246 
ToggleScreenUpdates(bool enable)1247 int HWCDisplay::ToggleScreenUpdates(bool enable) {
1248   const hwc_procs_t *hwc_procs = *hwc_procs_;
1249   display_paused_ = enable ? false : true;
1250   hwc_procs->invalidate(hwc_procs);
1251   return 0;
1252 }
1253 
ColorSVCRequestRoute(const PPDisplayAPIPayload & in_payload,PPDisplayAPIPayload * out_payload,PPPendingParams * pending_action)1254 int HWCDisplay::ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload,
1255                                      PPDisplayAPIPayload *out_payload,
1256                                      PPPendingParams *pending_action) {
1257   int ret = 0;
1258 
1259   if (display_intf_)
1260     ret = display_intf_->ColorSVCRequestRoute(in_payload, out_payload, pending_action);
1261   else
1262     ret = -EINVAL;
1263 
1264   return ret;
1265 }
1266 
GetVisibleDisplayRect(hwc_rect_t * visible_rect)1267 int HWCDisplay::GetVisibleDisplayRect(hwc_rect_t* visible_rect) {
1268   if (!IsValid(display_rect_)) {
1269     return -EINVAL;
1270   }
1271 
1272   visible_rect->left = INT(display_rect_.left);
1273   visible_rect->top = INT(display_rect_.top);
1274   visible_rect->right = INT(display_rect_.right);
1275   visible_rect->bottom = INT(display_rect_.bottom);
1276   DLOGI("Dpy = %d Visible Display Rect(%d %d %d %d)", visible_rect->left, visible_rect->top,
1277         visible_rect->right, visible_rect->bottom);
1278 
1279   return 0;
1280 }
1281 
SetSecureDisplay(bool secure_display_active)1282 void HWCDisplay::SetSecureDisplay(bool secure_display_active) {
1283   secure_display_active_ = secure_display_active;
1284   return;
1285 }
1286 
SetActiveDisplayConfig(int config)1287 int HWCDisplay::SetActiveDisplayConfig(int config) {
1288   return display_intf_->SetActiveConfig(UINT32(config)) == kErrorNone ? 0 : -1;
1289 }
1290 
GetActiveDisplayConfig(uint32_t * config)1291 int HWCDisplay::GetActiveDisplayConfig(uint32_t *config) {
1292   return display_intf_->GetActiveConfig(config) == kErrorNone ? 0 : -1;
1293 }
1294 
GetDisplayConfigCount(uint32_t * count)1295 int HWCDisplay::GetDisplayConfigCount(uint32_t *count) {
1296   return display_intf_->GetNumVariableInfoConfigs(count) == kErrorNone ? 0 : -1;
1297 }
1298 
GetDisplayAttributesForConfig(int config,DisplayConfigVariableInfo * display_attributes)1299 int HWCDisplay::GetDisplayAttributesForConfig(int config,
1300                                             DisplayConfigVariableInfo *display_attributes) {
1301   return display_intf_->GetConfig(UINT32(config), display_attributes) == kErrorNone ? 0 : -1;
1302 }
1303 
1304 // TODO(user): HWC needs to know updating for dyn_fps, cpu hint features,
1305 // once the features are moved to SDM, the two functions below can be removed.
SingleLayerUpdating(uint32_t app_layer_count)1306 bool HWCDisplay::SingleLayerUpdating(uint32_t app_layer_count) {
1307   uint32_t updating_count = 0;
1308 
1309   for (uint i = 0; i < app_layer_count; i++) {
1310     Layer *layer = layer_stack_.layers.at(i);
1311     if (layer->flags.updating) {
1312       updating_count++;
1313     }
1314   }
1315 
1316   return (updating_count == 1);
1317 }
1318 
SingleVideoLayerUpdating(uint32_t app_layer_count)1319 bool HWCDisplay::SingleVideoLayerUpdating(uint32_t app_layer_count) {
1320   uint32_t updating_count = 0;
1321 
1322   for (uint i = 0; i < app_layer_count; i++) {
1323     Layer *layer = layer_stack_.layers[i];
1324     if (layer->flags.updating && (layer->input_buffer->flags.video == true)) {
1325       updating_count++;
1326     }
1327   }
1328 
1329   return (updating_count == 1);
1330 }
1331 
RoundToStandardFPS(float fps)1332 uint32_t HWCDisplay::RoundToStandardFPS(float fps) {
1333   static const uint32_t standard_fps[4] = {30, 24, 48, 60};
1334   uint32_t frame_rate = (uint32_t)(fps);
1335 
1336   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
1337   for (int i = 0; i < count; i++) {
1338     if ((standard_fps[i] - frame_rate) < 2) {
1339       // Most likely used for video, the fps can fluctuate
1340       // Ex: b/w 29 and 30 for 30 fps clip
1341       return standard_fps[i];
1342     }
1343   }
1344 
1345   return frame_rate;
1346 }
1347 
SanitizeRefreshRate(uint32_t req_refresh_rate)1348 uint32_t HWCDisplay::SanitizeRefreshRate(uint32_t req_refresh_rate) {
1349   uint32_t refresh_rate = req_refresh_rate;
1350 
1351   if (refresh_rate < min_refresh_rate_) {
1352     // Pick the next multiple of request which is within the range
1353     refresh_rate = (((min_refresh_rate_ / refresh_rate) +
1354                      ((min_refresh_rate_ % refresh_rate) ? 1 : 0)) * refresh_rate);
1355   }
1356 
1357   if (refresh_rate > max_refresh_rate_) {
1358     refresh_rate = max_refresh_rate_;
1359   }
1360 
1361   return refresh_rate;
1362 }
1363 
GetDisplayClass()1364 DisplayClass HWCDisplay::GetDisplayClass() {
1365   return display_class_;
1366 }
1367 
PrepareDynamicRefreshRate(Layer * layer)1368 void HWCDisplay::PrepareDynamicRefreshRate(Layer *layer) {
1369   if (layer->frame_rate > metadata_refresh_rate_) {
1370     metadata_refresh_rate_ = SanitizeRefreshRate(layer->frame_rate);
1371   } else {
1372     layer->frame_rate = current_refresh_rate_;
1373   }
1374 }
1375 
IsSurfaceUpdated(const std::vector<LayerRect> & dirty_regions)1376 bool HWCDisplay::IsSurfaceUpdated(const std::vector<LayerRect> &dirty_regions) {
1377   // based on dirty_regions determine if its updating
1378   // dirty_rect count = 0 - whole layer - updating.
1379   // dirty_rect count = 1 or more valid rects - updating.
1380   // dirty_rect count = 1 with (0,0,0,0) - not updating.
1381   return (dirty_regions.empty() || IsValid(dirty_regions.at(0)));
1382 }
1383 
1384 }  // namespace sdm
1385