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 <cutils/properties.h>
31 #include <sync/sync.h>
32 #include <utils/constants.h>
33 #include <utils/debug.h>
34 #include <stdarg.h>
35 #include <sys/mman.h>
36
37 #include <map>
38 #include <string>
39 #include <vector>
40
41 #include "hwc_display_primary.h"
42 #include "hwc_debugger.h"
43
44 #define __CLASS__ "HWCDisplayPrimary"
45
46 namespace sdm {
47
Create(CoreInterface * core_intf,BufferAllocator * buffer_allocator,HWCCallbacks * callbacks,qService::QService * qservice,HWCDisplay ** hwc_display)48 int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
49 HWCCallbacks *callbacks, qService::QService *qservice,
50 HWCDisplay **hwc_display) {
51 int status = 0;
52 uint32_t primary_width = 0;
53 uint32_t primary_height = 0;
54
55 HWCDisplay *hwc_display_primary =
56 new HWCDisplayPrimary(core_intf, buffer_allocator, callbacks, qservice);
57 status = hwc_display_primary->Init();
58 if (status) {
59 delete hwc_display_primary;
60 return status;
61 }
62
63 hwc_display_primary->GetMixerResolution(&primary_width, &primary_height);
64 int width = 0, height = 0;
65 HWCDebugHandler::Get()->GetProperty("sdm.fb_size_width", &width);
66 HWCDebugHandler::Get()->GetProperty("sdm.fb_size_height", &height);
67 if (width > 0 && height > 0) {
68 primary_width = UINT32(width);
69 primary_height = UINT32(height);
70 }
71
72 status = hwc_display_primary->SetFrameBufferResolution(primary_width, primary_height);
73 if (status) {
74 Destroy(hwc_display_primary);
75 return status;
76 }
77
78 *hwc_display = hwc_display_primary;
79
80 return status;
81 }
82
Destroy(HWCDisplay * hwc_display)83 void HWCDisplayPrimary::Destroy(HWCDisplay *hwc_display) {
84 hwc_display->Deinit();
85 delete hwc_display;
86 }
87
HWCDisplayPrimary(CoreInterface * core_intf,BufferAllocator * buffer_allocator,HWCCallbacks * callbacks,qService::QService * qservice)88 HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
89 HWCCallbacks *callbacks, qService::QService *qservice)
90 : HWCDisplay(core_intf, callbacks, kPrimary, HWC_DISPLAY_PRIMARY, true, qservice,
91 DISPLAY_CLASS_PRIMARY, buffer_allocator),
92 buffer_allocator_(buffer_allocator),
93 cpu_hint_(NULL) {
94 }
95
Init()96 int HWCDisplayPrimary::Init() {
97 cpu_hint_ = new CPUHint();
98 if (cpu_hint_->Init(static_cast<HWCDebugHandler *>(HWCDebugHandler::Get())) != kErrorNone) {
99 delete cpu_hint_;
100 cpu_hint_ = NULL;
101 }
102
103 use_metadata_refresh_rate_ = true;
104 int disable_metadata_dynfps = 0;
105 HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps);
106 if (disable_metadata_dynfps) {
107 use_metadata_refresh_rate_ = false;
108 }
109
110 int status = HWCDisplay::Init();
111 if (status) {
112 return status;
113 }
114 color_mode_ = new HWCColorMode(display_intf_);
115
116 return INT(color_mode_->Init());
117 }
118
ProcessBootAnimCompleted()119 void HWCDisplayPrimary::ProcessBootAnimCompleted() {
120 uint32_t numBootUpLayers = 0;
121 // TODO(user): Remove this hack
122
123 numBootUpLayers = static_cast<uint32_t>(Debug::GetBootAnimLayerCount());
124
125 if (numBootUpLayers == 0) {
126 numBootUpLayers = 2;
127 }
128 /* All other checks namely "init.svc.bootanim" or
129 * HWC_GEOMETRY_CHANGED fail in correctly identifying the
130 * exact bootup transition to homescreen
131 */
132 char property[PROPERTY_VALUE_MAX];
133 bool isEncrypted = false;
134 bool main_class_services_started = false;
135 property_get("ro.crypto.state", property, "unencrypted");
136 if (!strcmp(property, "encrypted")) {
137 property_get("ro.crypto.type", property, "block");
138 if (!strcmp(property, "block")) {
139 isEncrypted = true;
140 property_get("vold.decrypt", property, "");
141 if (!strcmp(property, "trigger_restart_framework")) {
142 main_class_services_started = true;
143 }
144 }
145 }
146
147 if ((!isEncrypted || (isEncrypted && main_class_services_started)) &&
148 (layer_set_.size() > numBootUpLayers)) {
149 DLOGI("Applying default mode");
150 boot_animation_completed_ = true;
151 // Applying default mode after bootanimation is finished And
152 // If Data is Encrypted, it is ready for access.
153 if (display_intf_)
154 display_intf_->ApplyDefaultDisplayMode();
155 }
156 }
157
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)158 HWC2::Error HWCDisplayPrimary::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
159 auto status = HWC2::Error::None;
160 DisplayError error = kErrorNone;
161
162 if (display_paused_) {
163 MarkLayersForGPUBypass();
164 return status;
165 }
166
167 if (color_tranform_failed_) {
168 // Must fall back to client composition
169 MarkLayersForClientComposition();
170 }
171
172 // Fill in the remaining blanks in the layers and add them to the SDM layerstack
173 BuildLayerStack();
174 // Checks and replaces layer stack for solid fill
175 SolidFillPrepare();
176
177 bool pending_output_dump = dump_frame_count_ && dump_output_to_file_;
178
179 if (frame_capture_buffer_queued_ || pending_output_dump) {
180 // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up
181 // here in a subsequent draw round.
182 layer_stack_.output_buffer = &output_buffer_;
183 layer_stack_.flags.post_processed_output = post_processed_output_;
184 }
185
186 bool one_updating_layer = SingleLayerUpdating();
187 ToggleCPUHint(one_updating_layer);
188
189 uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer);
190 if (current_refresh_rate_ != refresh_rate) {
191 error = display_intf_->SetRefreshRate(refresh_rate);
192 }
193
194 if (error == kErrorNone) {
195 // On success, set current refresh rate to new refresh rate
196 current_refresh_rate_ = refresh_rate;
197 }
198
199 if (handle_idle_timeout_) {
200 handle_idle_timeout_ = false;
201 }
202
203 if (layer_set_.empty()) {
204 flush_ = true;
205 validated_ = true;
206 return status;
207 }
208
209 status = PrepareLayerStack(out_num_types, out_num_requests);
210 return status;
211 }
212
Present(int32_t * out_retire_fence)213 HWC2::Error HWCDisplayPrimary::Present(int32_t *out_retire_fence) {
214 auto status = HWC2::Error::None;
215 if (display_paused_) {
216 // TODO(user): From old HWC implementation
217 // If we do not handle the frame set retireFenceFd to outbufAcquireFenceFd
218 // Revisit this when validating display_paused
219 DisplayError error = display_intf_->Flush();
220 if (error != kErrorNone) {
221 DLOGE("Flush failed. Error = %d", error);
222 }
223 } else {
224 status = HWCDisplay::CommitLayerStack();
225 if (status == HWC2::Error::None) {
226 HandleFrameOutput();
227 SolidFillCommit();
228 status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
229 }
230 }
231
232 return status;
233 }
234
GetColorModes(uint32_t * out_num_modes,android_color_mode_t * out_modes)235 HWC2::Error HWCDisplayPrimary::GetColorModes(uint32_t *out_num_modes,
236 android_color_mode_t *out_modes) {
237 if (out_modes == nullptr) {
238 *out_num_modes = color_mode_->GetColorModeCount();
239 } else {
240 color_mode_->GetColorModes(out_num_modes, out_modes);
241 }
242
243 return HWC2::Error::None;
244 }
245
SetColorMode(android_color_mode_t mode)246 HWC2::Error HWCDisplayPrimary::SetColorMode(android_color_mode_t mode) {
247 validated_ = false;
248 auto status = color_mode_->SetColorMode(mode);
249 if (status != HWC2::Error::None) {
250 DLOGE("failed for mode = %d", mode);
251 return status;
252 }
253
254 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
255
256 return status;
257 }
258
SetColorModeById(int32_t color_mode_id)259 HWC2::Error HWCDisplayPrimary::SetColorModeById(int32_t color_mode_id) {
260 auto status = color_mode_->SetColorModeById(color_mode_id);
261 if (status != HWC2::Error::None) {
262 DLOGE("failed for mode = %d", color_mode_id);
263 return status;
264 }
265
266 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
267
268 return status;
269 }
270
SetColorTransform(const float * matrix,android_color_transform_t hint)271 HWC2::Error HWCDisplayPrimary::SetColorTransform(const float *matrix,
272 android_color_transform_t hint) {
273 validated_ = false;
274 if (!matrix) {
275 return HWC2::Error::BadParameter;
276 }
277
278 auto status = color_mode_->SetColorTransform(matrix, hint);
279 if (status != HWC2::Error::None) {
280 DLOGE("failed for hint = %d", hint);
281 color_tranform_failed_ = true;
282 return status;
283 }
284
285 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
286 color_tranform_failed_ = false;
287
288 return status;
289 }
290
Perform(uint32_t operation,...)291 int HWCDisplayPrimary::Perform(uint32_t operation, ...) {
292 va_list args;
293 va_start(args, operation);
294 int val = 0;
295 LayerRect *rect = NULL;
296
297 switch (operation) {
298 case SET_METADATA_DYN_REFRESH_RATE:
299 val = va_arg(args, int32_t);
300 SetMetaDataRefreshRateFlag(val);
301 break;
302 case SET_BINDER_DYN_REFRESH_RATE:
303 val = va_arg(args, int32_t);
304 ForceRefreshRate(UINT32(val));
305 break;
306 case SET_DISPLAY_MODE:
307 val = va_arg(args, int32_t);
308 SetDisplayMode(UINT32(val));
309 break;
310 case SET_QDCM_SOLID_FILL_INFO:
311 val = va_arg(args, int32_t);
312 SetQDCMSolidFillInfo(true, UINT32(val));
313 break;
314 case UNSET_QDCM_SOLID_FILL_INFO:
315 val = va_arg(args, int32_t);
316 SetQDCMSolidFillInfo(false, UINT32(val));
317 break;
318 case SET_QDCM_SOLID_FILL_RECT:
319 rect = va_arg(args, LayerRect*);
320 solid_fill_rect_ = *rect;
321 break;
322 default:
323 DLOGW("Invalid operation %d", operation);
324 va_end(args);
325 return -EINVAL;
326 }
327 va_end(args);
328
329 return 0;
330 }
331
SetDisplayMode(uint32_t mode)332 DisplayError HWCDisplayPrimary::SetDisplayMode(uint32_t mode) {
333 DisplayError error = kErrorNone;
334
335 if (display_intf_) {
336 error = display_intf_->SetDisplayMode(mode);
337 }
338
339 return error;
340 }
341
SetMetaDataRefreshRateFlag(bool enable)342 void HWCDisplayPrimary::SetMetaDataRefreshRateFlag(bool enable) {
343 int disable_metadata_dynfps = 0;
344
345 HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps);
346 if (disable_metadata_dynfps) {
347 return;
348 }
349 use_metadata_refresh_rate_ = enable;
350 }
351
SetQDCMSolidFillInfo(bool enable,uint32_t color)352 void HWCDisplayPrimary::SetQDCMSolidFillInfo(bool enable, uint32_t color) {
353 solid_fill_enable_ = enable;
354 solid_fill_color_ = color;
355 }
356
ToggleCPUHint(bool set)357 void HWCDisplayPrimary::ToggleCPUHint(bool set) {
358 if (!cpu_hint_) {
359 return;
360 }
361
362 if (set) {
363 cpu_hint_->Set();
364 } else {
365 cpu_hint_->Reset();
366 }
367 }
368
SetSecureDisplay(bool secure_display_active)369 void HWCDisplayPrimary::SetSecureDisplay(bool secure_display_active) {
370 if (secure_display_active_ != secure_display_active) {
371 // Skip Prepare and call Flush for null commit
372 DLOGI("SecureDisplay state changed from %d to %d Needs Flush!!", secure_display_active_,
373 secure_display_active);
374 secure_display_active_ = secure_display_active;
375 skip_prepare_ = true;
376 }
377 return;
378 }
379
ForceRefreshRate(uint32_t refresh_rate)380 void HWCDisplayPrimary::ForceRefreshRate(uint32_t refresh_rate) {
381 if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
382 force_refresh_rate_ == refresh_rate) {
383 // Cannot honor force refresh rate, as its beyond the range or new request is same
384 return;
385 }
386
387 force_refresh_rate_ = refresh_rate;
388
389 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
390
391 return;
392 }
393
GetOptimalRefreshRate(bool one_updating_layer)394 uint32_t HWCDisplayPrimary::GetOptimalRefreshRate(bool one_updating_layer) {
395 if (force_refresh_rate_) {
396 return force_refresh_rate_;
397 } else if (handle_idle_timeout_) {
398 return min_refresh_rate_;
399 } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) {
400 return metadata_refresh_rate_;
401 }
402
403 return max_refresh_rate_;
404 }
405
Refresh()406 DisplayError HWCDisplayPrimary::Refresh() {
407 DisplayError error = kErrorNone;
408
409 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
410 handle_idle_timeout_ = true;
411 validated_ = false;
412
413 return error;
414 }
415
SetIdleTimeoutMs(uint32_t timeout_ms)416 void HWCDisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) {
417 display_intf_->SetIdleTimeoutMs(timeout_ms);
418 }
419
SetLayerBuffer(const BufferInfo & output_buffer_info,LayerBuffer * output_buffer)420 static void SetLayerBuffer(const BufferInfo &output_buffer_info, LayerBuffer *output_buffer) {
421 const BufferConfig& buffer_config = output_buffer_info.buffer_config;
422 const AllocatedBufferInfo &alloc_buffer_info = output_buffer_info.alloc_buffer_info;
423
424 output_buffer->width = alloc_buffer_info.aligned_width;
425 output_buffer->height = alloc_buffer_info.aligned_height;
426 output_buffer->unaligned_width = buffer_config.width;
427 output_buffer->unaligned_height = buffer_config.height;
428 output_buffer->format = buffer_config.format;
429 output_buffer->planes[0].fd = alloc_buffer_info.fd;
430 output_buffer->planes[0].stride = alloc_buffer_info.stride;
431 }
432
HandleFrameOutput()433 void HWCDisplayPrimary::HandleFrameOutput() {
434 if (frame_capture_buffer_queued_) {
435 HandleFrameCapture();
436 } else if (dump_output_to_file_) {
437 HandleFrameDump();
438 }
439 }
440
HandleFrameCapture()441 void HWCDisplayPrimary::HandleFrameCapture() {
442 if (output_buffer_.release_fence_fd >= 0) {
443 frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
444 ::close(output_buffer_.release_fence_fd);
445 output_buffer_.release_fence_fd = -1;
446 }
447
448 frame_capture_buffer_queued_ = false;
449 post_processed_output_ = false;
450 output_buffer_ = {};
451 }
452
HandleFrameDump()453 void HWCDisplayPrimary::HandleFrameDump() {
454 if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) {
455 int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
456 ::close(output_buffer_.release_fence_fd);
457 output_buffer_.release_fence_fd = -1;
458 if (ret < 0) {
459 DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
460 } else {
461 DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
462 }
463 }
464
465 if (0 == dump_frame_count_) {
466 dump_output_to_file_ = false;
467 // Unmap and Free buffer
468 if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
469 DLOGE("unmap failed with err %d", errno);
470 }
471 if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
472 DLOGE("FreeBuffer failed");
473 }
474
475 post_processed_output_ = false;
476 output_buffer_ = {};
477 output_buffer_info_ = {};
478 output_buffer_base_ = nullptr;
479 }
480 }
481
SetFrameDumpConfig(uint32_t count,uint32_t bit_mask_layer_type)482 void HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
483 HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
484 dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
485 DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
486
487 if (!count || !dump_output_to_file_) {
488 return;
489 }
490
491 // Allocate and map output buffer
492 output_buffer_info_ = {};
493 // Since we dump DSPP output use Panel resolution.
494 GetPanelResolution(&output_buffer_info_.buffer_config.width,
495 &output_buffer_info_.buffer_config.height);
496 output_buffer_info_.buffer_config.format = kFormatRGB888;
497 output_buffer_info_.buffer_config.buffer_count = 1;
498 if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
499 DLOGE("Buffer allocation failed");
500 output_buffer_info_ = {};
501 return;
502 }
503
504 void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
505 MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
506
507 if (buffer == MAP_FAILED) {
508 DLOGE("mmap failed with err %d", errno);
509 buffer_allocator_->FreeBuffer(&output_buffer_info_);
510 output_buffer_info_ = {};
511 return;
512 }
513
514 output_buffer_base_ = buffer;
515 post_processed_output_ = true;
516 DisablePartialUpdateOneFrame();
517 }
518
FrameCaptureAsync(const BufferInfo & output_buffer_info,bool post_processed_output)519 int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo &output_buffer_info,
520 bool post_processed_output) {
521 // Note: This function is called in context of a binder thread and a lock is already held
522 if (output_buffer_info.alloc_buffer_info.fd < 0) {
523 DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
524 return -1;
525 }
526
527 auto panel_width = 0u;
528 auto panel_height = 0u;
529 auto fb_width = 0u;
530 auto fb_height = 0u;
531
532 GetPanelResolution(&panel_width, &panel_height);
533 GetFrameBufferResolution(&fb_width, &fb_height);
534
535 if (post_processed_output && (output_buffer_info_.buffer_config.width < panel_width ||
536 output_buffer_info_.buffer_config.height < panel_height)) {
537 DLOGE("Buffer dimensions should not be less than panel resolution");
538 return -1;
539 } else if (!post_processed_output && (output_buffer_info_.buffer_config.width < fb_width ||
540 output_buffer_info_.buffer_config.height < fb_height)) {
541 DLOGE("Buffer dimensions should not be less than FB resolution");
542 return -1;
543 }
544
545 SetLayerBuffer(output_buffer_info, &output_buffer_);
546 post_processed_output_ = post_processed_output;
547 frame_capture_buffer_queued_ = true;
548 // Status is only cleared on a new call to dump and remains valid otherwise
549 frame_capture_status_ = -EAGAIN;
550 DisablePartialUpdateOneFrame();
551
552 return 0;
553 }
554
SetDetailEnhancerConfig(const DisplayDetailEnhancerData & de_data)555 DisplayError HWCDisplayPrimary::SetDetailEnhancerConfig
556 (const DisplayDetailEnhancerData &de_data) {
557 DisplayError error = kErrorNotSupported;
558
559 if (display_intf_) {
560 error = display_intf_->SetDetailEnhancerData(de_data);
561 }
562 return error;
563 }
564
ControlPartialUpdate(bool enable,uint32_t * pending)565 DisplayError HWCDisplayPrimary::ControlPartialUpdate(bool enable, uint32_t *pending) {
566 DisplayError error = kErrorNone;
567
568 if (display_intf_) {
569 error = display_intf_->ControlPartialUpdate(enable, pending);
570 }
571
572 return error;
573 }
574
DisablePartialUpdateOneFrame()575 DisplayError HWCDisplayPrimary::DisablePartialUpdateOneFrame() {
576 DisplayError error = kErrorNone;
577
578 if (display_intf_) {
579 error = display_intf_->DisablePartialUpdateOneFrame();
580 }
581
582 return error;
583 }
584
585
SetMixerResolution(uint32_t width,uint32_t height)586 DisplayError HWCDisplayPrimary::SetMixerResolution(uint32_t width, uint32_t height) {
587 validated_ = false;
588 return display_intf_->SetMixerResolution(width, height);
589 }
590
GetMixerResolution(uint32_t * width,uint32_t * height)591 DisplayError HWCDisplayPrimary::GetMixerResolution(uint32_t *width, uint32_t *height) {
592 return display_intf_->GetMixerResolution(width, height);
593 }
594
595 } // namespace sdm
596