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