blob: c68081a552bcc448c6aa6fad07fd77337c7a3d4c [file] [log] [blame]
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001/*
Varun Arora465c1f72018-02-08 16:18:50 -08002* Copyright (c) 2014 - 2018, The Linux Foundation. All rights reserved.
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05003*
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
Arun Kumar K.R29cd6582016-05-10 19:12:45 -070037#include <map>
38#include <string>
39#include <vector>
40
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050041#include "hwc_display_primary.h"
42#include "hwc_debugger.h"
43
44#define __CLASS__ "HWCDisplayPrimary"
45
46namespace sdm {
47
48int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
Varun Arora7c8ee542018-05-01 20:58:16 -070049 HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
50 qService::QService *qservice, HWCDisplay **hwc_display) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050051 int status = 0;
52 uint32_t primary_width = 0;
53 uint32_t primary_height = 0;
54
55 HWCDisplay *hwc_display_primary =
Varun Arora7c8ee542018-05-01 20:58:16 -070056 new HWCDisplayPrimary(core_intf, buffer_allocator, callbacks, event_handler, qservice);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050057 status = hwc_display_primary->Init();
58 if (status) {
59 delete hwc_display_primary;
60 return status;
61 }
62
Arun Kumar K.R8da7f502016-06-07 17:45:50 -070063 hwc_display_primary->GetMixerResolution(&primary_width, &primary_height);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050064 int width = 0, height = 0;
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +053065 HWCDebugHandler::Get()->GetProperty(FB_WIDTH_PROP, &width);
66 HWCDebugHandler::Get()->GetProperty(FB_HEIGHT_PROP, &height);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050067 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
83void HWCDisplayPrimary::Destroy(HWCDisplay *hwc_display) {
84 hwc_display->Deinit();
85 delete hwc_display;
86}
87
88HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
Varun Arora7c8ee542018-05-01 20:58:16 -070089 HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
90 qService::QService *qservice)
91 : HWCDisplay(core_intf, callbacks, event_handler, kPrimary, HWC_DISPLAY_PRIMARY, true,
92 qservice, DISPLAY_CLASS_PRIMARY, buffer_allocator),
93 buffer_allocator_(buffer_allocator), cpu_hint_(NULL) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050094}
95
96int 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;
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530105 HWCDebugHandler::Get()->GetProperty(DISABLE_METADATA_DYNAMIC_FPS_PROP, &disable_metadata_dynfps);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500106 if (disable_metadata_dynfps) {
107 use_metadata_refresh_rate_ = false;
108 }
109
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700110 int status = HWCDisplay::Init();
111 if (status) {
112 return status;
113 }
114 color_mode_ = new HWCColorMode(display_intf_);
Naseer Ahmed61f60952017-05-24 16:48:35 -0400115 color_mode_->Init();
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530116 HWCDebugHandler::Get()->GetProperty(ENABLE_DEFAULT_COLOR_MODE,
Ch Ganesh Kumar86c46512017-06-13 13:09:22 +0530117 &default_mode_status_);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700118
Naseer Ahmed61f60952017-05-24 16:48:35 -0400119 return status;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500120}
121
122void HWCDisplayPrimary::ProcessBootAnimCompleted() {
123 uint32_t numBootUpLayers = 0;
124 // TODO(user): Remove this hack
125
126 numBootUpLayers = static_cast<uint32_t>(Debug::GetBootAnimLayerCount());
127
128 if (numBootUpLayers == 0) {
129 numBootUpLayers = 2;
130 }
131 /* All other checks namely "init.svc.bootanim" or
132 * HWC_GEOMETRY_CHANGED fail in correctly identifying the
133 * exact bootup transition to homescreen
134 */
Naseer Ahmedac442ae2016-06-23 18:58:03 -0400135 char property[PROPERTY_VALUE_MAX];
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500136 bool isEncrypted = false;
137 bool main_class_services_started = false;
Naseer Ahmedac442ae2016-06-23 18:58:03 -0400138 property_get("ro.crypto.state", property, "unencrypted");
139 if (!strcmp(property, "encrypted")) {
140 property_get("ro.crypto.type", property, "block");
141 if (!strcmp(property, "block")) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500142 isEncrypted = true;
Naseer Ahmedac442ae2016-06-23 18:58:03 -0400143 property_get("vold.decrypt", property, "");
144 if (!strcmp(property, "trigger_restart_framework")) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500145 main_class_services_started = true;
Naseer Ahmedac442ae2016-06-23 18:58:03 -0400146 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500147 }
148 }
Naseer Ahmedac442ae2016-06-23 18:58:03 -0400149
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500150 if ((!isEncrypted || (isEncrypted && main_class_services_started)) &&
151 (layer_set_.size() > numBootUpLayers)) {
Naseer Ahmedac442ae2016-06-23 18:58:03 -0400152 DLOGI("Applying default mode");
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500153 boot_animation_completed_ = true;
154 // Applying default mode after bootanimation is finished And
155 // If Data is Encrypted, it is ready for access.
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +0530156 if (display_intf_) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500157 display_intf_->ApplyDefaultDisplayMode();
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +0530158 RestoreColorTransform();
159 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500160 }
161}
162
163HWC2::Error HWCDisplayPrimary::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
164 auto status = HWC2::Error::None;
165 DisplayError error = kErrorNone;
166
Ch Ganesh Kumar86c46512017-06-13 13:09:22 +0530167 if (default_mode_status_ && !boot_animation_completed_) {
168 ProcessBootAnimCompleted();
169 }
170
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500171 if (display_paused_) {
172 MarkLayersForGPUBypass();
173 return status;
174 }
175
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700176 if (color_tranform_failed_) {
177 // Must fall back to client composition
178 MarkLayersForClientComposition();
179 }
180
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500181 // Fill in the remaining blanks in the layers and add them to the SDM layerstack
182 BuildLayerStack();
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700183 // Checks and replaces layer stack for solid fill
184 SolidFillPrepare();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500185
186 bool pending_output_dump = dump_frame_count_ && dump_output_to_file_;
187
188 if (frame_capture_buffer_queued_ || pending_output_dump) {
189 // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up
190 // here in a subsequent draw round.
191 layer_stack_.output_buffer = &output_buffer_;
192 layer_stack_.flags.post_processed_output = post_processed_output_;
193 }
194
Ramakant Singh5db0dfb2017-08-23 12:34:57 +0530195 uint32_t num_updating_layers = GetUpdatingLayersCount();
196 bool one_updating_layer = (num_updating_layers == 1);
197 if (num_updating_layers != 0) {
198 ToggleCPUHint(one_updating_layer);
199 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500200
201 uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer);
Anjaneya Prasad Musunuri4d65df72017-05-18 19:11:28 +0530202 bool final_rate = force_refresh_rate_ ? true : false;
203 error = display_intf_->SetRefreshRate(refresh_rate, final_rate);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500204 if (error == kErrorNone) {
205 // On success, set current refresh rate to new refresh rate
206 current_refresh_rate_ = refresh_rate;
207 }
208
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500209 if (layer_set_.empty()) {
Uday Kiran Pichika8d827732018-01-09 18:08:38 +0530210 // Avoid flush for Command mode panel.
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700211 flush_ = !IsDisplayCommandMode();
212 validated_ = true;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500213 return status;
214 }
215
216 status = PrepareLayerStack(out_num_types, out_num_requests);
217 return status;
218}
219
220HWC2::Error HWCDisplayPrimary::Present(int32_t *out_retire_fence) {
221 auto status = HWC2::Error::None;
222 if (display_paused_) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500223 DisplayError error = display_intf_->Flush();
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700224 validated_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500225 if (error != kErrorNone) {
226 DLOGE("Flush failed. Error = %d", error);
227 }
228 } else {
229 status = HWCDisplay::CommitLayerStack();
230 if (status == HWC2::Error::None) {
231 HandleFrameOutput();
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700232 SolidFillCommit();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500233 status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
234 }
235 }
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700236
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500237 return status;
238}
239
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700240HWC2::Error HWCDisplayPrimary::GetColorModes(uint32_t *out_num_modes,
Naseer Ahmedeae28122016-06-27 15:47:05 -0400241 android_color_mode_t *out_modes) {
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700242 if (out_modes == nullptr) {
243 *out_num_modes = color_mode_->GetColorModeCount();
244 } else {
245 color_mode_->GetColorModes(out_num_modes, out_modes);
246 }
247
248 return HWC2::Error::None;
249}
250
Naseer Ahmedeae28122016-06-27 15:47:05 -0400251HWC2::Error HWCDisplayPrimary::SetColorMode(android_color_mode_t mode) {
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700252 auto status = color_mode_->SetColorMode(mode);
253 if (status != HWC2::Error::None) {
254 DLOGE("failed for mode = %d", mode);
255 return status;
256 }
257
258 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700259 validated_ = false;
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700260
261 return status;
262}
263
Naseer Ahmed6776dae2017-05-09 11:49:41 -0400264HWC2::Error HWCDisplayPrimary::SetColorModeById(int32_t color_mode_id) {
265 auto status = color_mode_->SetColorModeById(color_mode_id);
266 if (status != HWC2::Error::None) {
267 DLOGE("failed for mode = %d", color_mode_id);
268 return status;
269 }
270
271 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700272 validated_ = false;
Naseer Ahmed6776dae2017-05-09 11:49:41 -0400273
274 return status;
275}
276
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +0530277HWC2::Error HWCDisplayPrimary::RestoreColorTransform() {
278 auto status = color_mode_->RestoreColorTransform();
279 if (status != HWC2::Error::None) {
280 DLOGE("failed to RestoreColorTransform");
281 return status;
282 }
283
284 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
285
286 return status;
287}
288
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700289HWC2::Error HWCDisplayPrimary::SetColorTransform(const float *matrix,
290 android_color_transform_t hint) {
291 if (!matrix) {
292 return HWC2::Error::BadParameter;
293 }
294
295 auto status = color_mode_->SetColorTransform(matrix, hint);
296 if (status != HWC2::Error::None) {
297 DLOGE("failed for hint = %d", hint);
298 color_tranform_failed_ = true;
299 return status;
300 }
301
302 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
303 color_tranform_failed_ = false;
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700304 validated_ = false;
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700305
306 return status;
307}
308
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500309int HWCDisplayPrimary::Perform(uint32_t operation, ...) {
310 va_list args;
311 va_start(args, operation);
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700312 int val = 0;
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700313 LayerSolidFill *solid_fill_color;
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700314 LayerRect *rect = NULL;
315
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500316 switch (operation) {
317 case SET_METADATA_DYN_REFRESH_RATE:
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700318 val = va_arg(args, int32_t);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500319 SetMetaDataRefreshRateFlag(val);
320 break;
321 case SET_BINDER_DYN_REFRESH_RATE:
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700322 val = va_arg(args, int32_t);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500323 ForceRefreshRate(UINT32(val));
324 break;
325 case SET_DISPLAY_MODE:
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700326 val = va_arg(args, int32_t);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500327 SetDisplayMode(UINT32(val));
328 break;
329 case SET_QDCM_SOLID_FILL_INFO:
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700330 solid_fill_color = va_arg(args, LayerSolidFill*);
331 SetQDCMSolidFillInfo(true, *solid_fill_color);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500332 break;
333 case UNSET_QDCM_SOLID_FILL_INFO:
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700334 solid_fill_color = va_arg(args, LayerSolidFill*);
335 SetQDCMSolidFillInfo(false, *solid_fill_color);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500336 break;
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700337 case SET_QDCM_SOLID_FILL_RECT:
338 rect = va_arg(args, LayerRect*);
339 solid_fill_rect_ = *rect;
340 break;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500341 default:
342 DLOGW("Invalid operation %d", operation);
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700343 va_end(args);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500344 return -EINVAL;
345 }
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700346 va_end(args);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700347 validated_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500348
349 return 0;
350}
351
352DisplayError HWCDisplayPrimary::SetDisplayMode(uint32_t mode) {
353 DisplayError error = kErrorNone;
354
355 if (display_intf_) {
356 error = display_intf_->SetDisplayMode(mode);
357 }
358
359 return error;
360}
361
362void HWCDisplayPrimary::SetMetaDataRefreshRateFlag(bool enable) {
363 int disable_metadata_dynfps = 0;
364
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530365 HWCDebugHandler::Get()->GetProperty(DISABLE_METADATA_DYNAMIC_FPS_PROP, &disable_metadata_dynfps);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500366 if (disable_metadata_dynfps) {
367 return;
368 }
369 use_metadata_refresh_rate_ = enable;
370}
371
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700372void HWCDisplayPrimary::SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500373 solid_fill_enable_ = enable;
374 solid_fill_color_ = color;
375}
376
377void HWCDisplayPrimary::ToggleCPUHint(bool set) {
378 if (!cpu_hint_) {
379 return;
380 }
381
382 if (set) {
383 cpu_hint_->Set();
384 } else {
385 cpu_hint_->Reset();
386 }
387}
388
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700389int HWCDisplayPrimary::HandleSecureSession(const std::bitset<kSecureMax> &secure_sessions,
390 bool *power_on_pending) {
391 if (!power_on_pending) {
392 return -EINVAL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500393 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700394
395 if (active_secure_sessions_[kSecureDisplay] != secure_sessions[kSecureDisplay]) {
396 SecureEvent secure_event =
397 secure_sessions.test(kSecureDisplay) ? kSecureDisplayStart : kSecureDisplayEnd;
398 DisplayError err = display_intf_->HandleSecureEvent(secure_event);
399 if (err != kErrorNone) {
400 DLOGE("Set secure event failed");
401 return err;
402 }
403
404 DLOGI("SecureDisplay state changed from %d to %d for display %d",
405 active_secure_sessions_.test(kSecureDisplay), secure_sessions.test(kSecureDisplay),
406 type_);
407 }
408 active_secure_sessions_ = secure_sessions;
409 *power_on_pending = false;
410 return 0;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500411}
412
413void HWCDisplayPrimary::ForceRefreshRate(uint32_t refresh_rate) {
414 if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
415 force_refresh_rate_ == refresh_rate) {
416 // Cannot honor force refresh rate, as its beyond the range or new request is same
417 return;
418 }
419
420 force_refresh_rate_ = refresh_rate;
421
422 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
423
424 return;
425}
426
427uint32_t HWCDisplayPrimary::GetOptimalRefreshRate(bool one_updating_layer) {
428 if (force_refresh_rate_) {
429 return force_refresh_rate_;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500430 } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) {
431 return metadata_refresh_rate_;
432 }
433
434 return max_refresh_rate_;
435}
436
437DisplayError HWCDisplayPrimary::Refresh() {
438 DisplayError error = kErrorNone;
439
440 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500441
442 return error;
443}
444
445void HWCDisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) {
446 display_intf_->SetIdleTimeoutMs(timeout_ms);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700447 validated_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500448}
449
450static void SetLayerBuffer(const BufferInfo &output_buffer_info, LayerBuffer *output_buffer) {
Ramkumar Radhakrishnanb27735f2016-08-26 22:37:23 -0700451 const BufferConfig& buffer_config = output_buffer_info.buffer_config;
452 const AllocatedBufferInfo &alloc_buffer_info = output_buffer_info.alloc_buffer_info;
453
454 output_buffer->width = alloc_buffer_info.aligned_width;
455 output_buffer->height = alloc_buffer_info.aligned_height;
456 output_buffer->unaligned_width = buffer_config.width;
457 output_buffer->unaligned_height = buffer_config.height;
458 output_buffer->format = buffer_config.format;
459 output_buffer->planes[0].fd = alloc_buffer_info.fd;
460 output_buffer->planes[0].stride = alloc_buffer_info.stride;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500461}
462
463void HWCDisplayPrimary::HandleFrameOutput() {
464 if (frame_capture_buffer_queued_) {
465 HandleFrameCapture();
466 } else if (dump_output_to_file_) {
467 HandleFrameDump();
468 }
469}
470
471void HWCDisplayPrimary::HandleFrameCapture() {
472 if (output_buffer_.release_fence_fd >= 0) {
473 frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
474 ::close(output_buffer_.release_fence_fd);
475 output_buffer_.release_fence_fd = -1;
476 }
477
478 frame_capture_buffer_queued_ = false;
479 post_processed_output_ = false;
480 output_buffer_ = {};
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500481}
482
483void HWCDisplayPrimary::HandleFrameDump() {
Varun Arora465c1f72018-02-08 16:18:50 -0800484 if (dump_frame_count_) {
485 int ret = 0;
486 if (output_buffer_.release_fence_fd >= 0) {
487 ret = sync_wait(output_buffer_.release_fence_fd, 1000);
488 ::close(output_buffer_.release_fence_fd);
489 output_buffer_.release_fence_fd = -1;
490 if (ret < 0) {
491 DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
492 }
493 }
494 if (ret >= 0) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500495 DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
496 }
497 }
498
499 if (0 == dump_frame_count_) {
500 dump_output_to_file_ = false;
501 // Unmap and Free buffer
502 if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
503 DLOGE("unmap failed with err %d", errno);
504 }
505 if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
506 DLOGE("FreeBuffer failed");
507 }
508
509 post_processed_output_ = false;
510 output_buffer_ = {};
511 output_buffer_info_ = {};
512 output_buffer_base_ = nullptr;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500513 }
514}
515
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530516HWC2::Error HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500517 HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
518 dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
519 DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
520
521 if (!count || !dump_output_to_file_) {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530522 return HWC2::Error::None;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500523 }
524
525 // Allocate and map output buffer
526 output_buffer_info_ = {};
527 // Since we dump DSPP output use Panel resolution.
528 GetPanelResolution(&output_buffer_info_.buffer_config.width,
529 &output_buffer_info_.buffer_config.height);
530 output_buffer_info_.buffer_config.format = kFormatRGB888;
531 output_buffer_info_.buffer_config.buffer_count = 1;
532 if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
533 DLOGE("Buffer allocation failed");
534 output_buffer_info_ = {};
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530535 return HWC2::Error::NoResources;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500536 }
537
538 void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
539 MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
540
541 if (buffer == MAP_FAILED) {
542 DLOGE("mmap failed with err %d", errno);
543 buffer_allocator_->FreeBuffer(&output_buffer_info_);
544 output_buffer_info_ = {};
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530545 return HWC2::Error::NoResources;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500546 }
547
548 output_buffer_base_ = buffer;
549 post_processed_output_ = true;
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700550 DisablePartialUpdateOneFrame();
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700551 validated_ = false;
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530552 return HWC2::Error::None;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500553}
554
555int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo &output_buffer_info,
556 bool post_processed_output) {
557 // Note: This function is called in context of a binder thread and a lock is already held
558 if (output_buffer_info.alloc_buffer_info.fd < 0) {
559 DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
560 return -1;
561 }
562
563 auto panel_width = 0u;
564 auto panel_height = 0u;
565 auto fb_width = 0u;
566 auto fb_height = 0u;
567
568 GetPanelResolution(&panel_width, &panel_height);
569 GetFrameBufferResolution(&fb_width, &fb_height);
570
Ch Ganesh Kumar5c4988b2017-06-07 15:21:02 +0530571 if (post_processed_output && (output_buffer_info.buffer_config.width < panel_width ||
572 output_buffer_info.buffer_config.height < panel_height)) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500573 DLOGE("Buffer dimensions should not be less than panel resolution");
574 return -1;
Ch Ganesh Kumar5c4988b2017-06-07 15:21:02 +0530575 } else if (!post_processed_output && (output_buffer_info.buffer_config.width < fb_width ||
576 output_buffer_info.buffer_config.height < fb_height)) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500577 DLOGE("Buffer dimensions should not be less than FB resolution");
578 return -1;
579 }
580
581 SetLayerBuffer(output_buffer_info, &output_buffer_);
582 post_processed_output_ = post_processed_output;
583 frame_capture_buffer_queued_ = true;
584 // Status is only cleared on a new call to dump and remains valid otherwise
585 frame_capture_status_ = -EAGAIN;
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700586 DisablePartialUpdateOneFrame();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500587
588 return 0;
589}
590
Alan Kwong2e136332016-08-16 07:50:16 -0400591DisplayError HWCDisplayPrimary::SetDetailEnhancerConfig
592 (const DisplayDetailEnhancerData &de_data) {
593 DisplayError error = kErrorNotSupported;
594
595 if (display_intf_) {
596 error = display_intf_->SetDetailEnhancerData(de_data);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700597 validated_ = false;
Alan Kwong2e136332016-08-16 07:50:16 -0400598 }
599 return error;
600}
601
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700602DisplayError HWCDisplayPrimary::ControlPartialUpdate(bool enable, uint32_t *pending) {
603 DisplayError error = kErrorNone;
604
605 if (display_intf_) {
606 error = display_intf_->ControlPartialUpdate(enable, pending);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700607 validated_ = false;
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700608 }
609
610 return error;
611}
612
613DisplayError HWCDisplayPrimary::DisablePartialUpdateOneFrame() {
614 DisplayError error = kErrorNone;
615
616 if (display_intf_) {
617 error = display_intf_->DisablePartialUpdateOneFrame();
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700618 validated_ = false;
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700619 }
620
621 return error;
622}
623
624
Arun Kumar K.R8da7f502016-06-07 17:45:50 -0700625DisplayError HWCDisplayPrimary::SetMixerResolution(uint32_t width, uint32_t height) {
Sushil Chauhan409e8442017-06-12 17:43:25 -0700626 DisplayError error = display_intf_->SetMixerResolution(width, height);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700627 validated_ = false;
Sushil Chauhan409e8442017-06-12 17:43:25 -0700628 return error;
Arun Kumar K.R8da7f502016-06-07 17:45:50 -0700629}
630
631DisplayError HWCDisplayPrimary::GetMixerResolution(uint32_t *width, uint32_t *height) {
632 return display_intf_->GetMixerResolution(width, height);
633}
634
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500635} // namespace sdm