blob: c36fcf64c534db405ad5f75b7e52c2f8855e266c [file] [log] [blame]
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001/*
Gousemoodhin Nadaf087c3102019-01-07 19:34:56 +05302* Copyright (c) 2014-2019, 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>
Sushil Chauhan06521582018-03-19 14:00:23 -070034#include <utils/utils.h>
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050035#include <stdarg.h>
36#include <sys/mman.h>
37
Arun Kumar K.R29cd6582016-05-10 19:12:45 -070038#include <map>
39#include <string>
40#include <vector>
41
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053042#include "hwc_display_builtin.h"
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050043#include "hwc_debugger.h"
Pullakavi Srinivas9189e602018-12-19 16:58:07 +053044#include "hwc_session.h"
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050045
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053046#define __CLASS__ "HWCDisplayBuiltIn"
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050047
48namespace sdm {
49
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +053050static void SetRect(LayerRect &src_rect, GLRect *target) {
51 target->left = src_rect.left;
52 target->top = src_rect.top;
53 target->right = src_rect.right;
54 target->bottom = src_rect.bottom;
55}
56
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053057int HWCDisplayBuiltIn::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
Varun Arora7c8ee542018-05-01 20:58:16 -070058 HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053059 qService::QService *qservice, hwc2_display_t id, int32_t sdm_id,
60 HWCDisplay **hwc_display) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050061 int status = 0;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053062 uint32_t builtin_width = 0;
63 uint32_t builtin_height = 0;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050064
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053065 HWCDisplay *hwc_display_builtin =
66 new HWCDisplayBuiltIn(core_intf, buffer_allocator, callbacks, event_handler, qservice, id,
67 sdm_id);
68 status = hwc_display_builtin->Init();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050069 if (status) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053070 delete hwc_display_builtin;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050071 return status;
72 }
73
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053074 hwc_display_builtin->GetMixerResolution(&builtin_width, &builtin_height);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050075 int width = 0, height = 0;
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +053076 HWCDebugHandler::Get()->GetProperty(FB_WIDTH_PROP, &width);
77 HWCDebugHandler::Get()->GetProperty(FB_HEIGHT_PROP, &height);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050078 if (width > 0 && height > 0) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053079 builtin_width = UINT32(width);
80 builtin_height = UINT32(height);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050081 }
82
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053083 status = hwc_display_builtin->SetFrameBufferResolution(builtin_width, builtin_height);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050084 if (status) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053085 Destroy(hwc_display_builtin);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050086 return status;
87 }
88
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053089 *hwc_display = hwc_display_builtin;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050090
91 return status;
92}
93
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053094void HWCDisplayBuiltIn::Destroy(HWCDisplay *hwc_display) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050095 hwc_display->Deinit();
96 delete hwc_display;
97}
98
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053099HWCDisplayBuiltIn::HWCDisplayBuiltIn(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
Varun Arora7c8ee542018-05-01 20:58:16 -0700100 HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530101 qService::QService *qservice, hwc2_display_t id,
102 int32_t sdm_id)
103 : HWCDisplay(core_intf, buffer_allocator, callbacks, event_handler, qservice, kBuiltIn, id,
Gousemoodhin Nadaf087c3102019-01-07 19:34:56 +0530104 sdm_id, DISPLAY_CLASS_BUILTIN),
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530105 buffer_allocator_(buffer_allocator),
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530106 cpu_hint_(NULL), layer_stitch_task_(*this) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500107}
108
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530109int HWCDisplayBuiltIn::Init() {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500110 cpu_hint_ = new CPUHint();
111 if (cpu_hint_->Init(static_cast<HWCDebugHandler *>(HWCDebugHandler::Get())) != kErrorNone) {
112 delete cpu_hint_;
113 cpu_hint_ = NULL;
114 }
115
116 use_metadata_refresh_rate_ = true;
117 int disable_metadata_dynfps = 0;
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530118 HWCDebugHandler::Get()->GetProperty(DISABLE_METADATA_DYNAMIC_FPS_PROP, &disable_metadata_dynfps);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500119 if (disable_metadata_dynfps) {
120 use_metadata_refresh_rate_ = false;
121 }
122
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700123 int status = HWCDisplay::Init();
124 if (status) {
125 return status;
126 }
127 color_mode_ = new HWCColorMode(display_intf_);
Naseer Ahmed61f60952017-05-24 16:48:35 -0400128 color_mode_->Init();
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700129
Padmanabhan Komanduru866bf6a2019-03-19 14:30:48 +0530130 int optimize_refresh = 0;
131 HWCDebugHandler::Get()->GetProperty(ENABLE_OPTIMIZE_REFRESH, &optimize_refresh);
132 enable_optimize_refresh_ = (optimize_refresh == 1);
133 if (enable_optimize_refresh_) {
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700134 DLOGI("Drop redundant drawcycles %d", id_);
135 }
136
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530137 is_primary_ = display_intf_->IsPrimaryDisplay();
138
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530139 if (!InitLayerStitch()) {
140 DLOGW("Failed to initialize Layer Stitch context");
141 }
142
Naseer Ahmed61f60952017-05-24 16:48:35 -0400143 return status;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500144}
145
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530146HWC2::Error HWCDisplayBuiltIn::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500147 auto status = HWC2::Error::None;
148 DisplayError error = kErrorNone;
149
Gurpreet Singh Dhamia4276882019-04-12 10:30:58 -0700150 DTRACE_SCOPED();
151
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530152 if (display_paused_ || (!is_primary_ && active_secure_sessions_[kSecureDisplay])) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500153 MarkLayersForGPUBypass();
154 return status;
155 }
156
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700157 if (color_tranform_failed_) {
158 // Must fall back to client composition
159 MarkLayersForClientComposition();
160 }
161
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500162 // Fill in the remaining blanks in the layers and add them to the SDM layerstack
163 BuildLayerStack();
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530164
165 // Add stitch layer to layer stack.
166 AppendStitchLayer();
167
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700168 // Checks and replaces layer stack for solid fill
169 SolidFillPrepare();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500170
Sushil Chauhan8008d602018-09-04 14:43:27 -0700171 // Apply current Color Mode and Render Intent.
Xu Yang84e61412018-12-06 14:52:16 +0800172 if (color_mode_->ApplyCurrentColorModeWithRenderIntent(
173 static_cast<bool>(layer_stack_.flags.hdr_present)) != HWC2::Error::None) {
Sushil Chauhan8008d602018-09-04 14:43:27 -0700174 // Fallback to GPU Composition, if Color Mode can't be applied.
175 MarkLayersForClientComposition();
176 }
177
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500178 bool pending_output_dump = dump_frame_count_ && dump_output_to_file_;
179
Sushil Chauhan06521582018-03-19 14:00:23 -0700180 if (readback_buffer_queued_ || pending_output_dump) {
181 CloseFd(&output_buffer_.release_fence_fd);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500182 // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up
Sushil Chauhan06521582018-03-19 14:00:23 -0700183 // here in a subsequent draw round. Readback is not allowed for any secure use case.
184 readback_configured_ = !layer_stack_.flags.secure_present;
185 if (readback_configured_) {
Sushil Chauhanf42d8622018-09-30 23:20:37 -0700186 DisablePartialUpdateOneFrame();
Sushil Chauhan06521582018-03-19 14:00:23 -0700187 layer_stack_.output_buffer = &output_buffer_;
188 layer_stack_.flags.post_processed_output = post_processed_output_;
189 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500190 }
191
Ramakant Singh5db0dfb2017-08-23 12:34:57 +0530192 uint32_t num_updating_layers = GetUpdatingLayersCount();
193 bool one_updating_layer = (num_updating_layers == 1);
194 if (num_updating_layers != 0) {
195 ToggleCPUHint(one_updating_layer);
196 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500197
198 uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer);
Pullakavi Srinivas4619db92019-04-20 15:07:59 +0530199 error = display_intf_->SetRefreshRate(refresh_rate, force_refresh_rate_);
200
201 // Get the refresh rate set.
202 display_intf_->GetRefreshRate(&refresh_rate);
203 bool vsync_source = (callbacks_->GetVsyncSource() == id_);
204
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500205 if (error == kErrorNone) {
Pullakavi Srinivas4619db92019-04-20 15:07:59 +0530206 if (vsync_source && (current_refresh_rate_ < refresh_rate)) {
207 DTRACE_BEGIN("HWC2::Vsync::Enable");
208 // Display is ramping up from idle.
209 // Client realizes need for resync upon change in config.
210 // Since we know config has changed, triggering vsync proactively
211 // can help in reducing pipeline delays to enable events.
212 SetVsyncEnabled(HWC2::Vsync::Enable);
213 DTRACE_END();
214 }
215 // On success, set current refresh rate to new refresh rate.
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500216 current_refresh_rate_ = refresh_rate;
217 }
218
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500219 if (layer_set_.empty()) {
Uday Kiran Pichika8d827732018-01-09 18:08:38 +0530220 // Avoid flush for Command mode panel.
Pullakavi Srinivas2d55f3a2019-07-16 14:49:06 +0530221 flush_ = !client_connected_;
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700222 validated_ = true;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500223 return status;
224 }
225
226 status = PrepareLayerStack(out_num_types, out_num_requests);
Namit Solanki5b428dc2018-02-27 11:39:05 +0530227 pending_commit_ = true;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500228 return status;
229}
230
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700231HWC2::Error HWCDisplayBuiltIn::CommitLayerStack() {
232 skip_commit_ = CanSkipCommit();
233 return HWCDisplay::CommitLayerStack();
234}
235
236bool HWCDisplayBuiltIn::CanSkipCommit() {
237 if (layer_stack_invalid_) {
238 return false;
239 }
240
241 // Reject repeated drawcycle requests if it satisfies all conditions.
242 // 1. None of the layerstack attributes changed.
243 // 2. No new buffer latched.
244 // 3. No refresh request triggered by HWC.
245 // 4. This display is not source of vsync.
246 bool buffers_latched = false;
247 for (auto &hwc_layer : layer_set_) {
248 buffers_latched |= hwc_layer->BufferLatched();
249 hwc_layer->ResetBufferFlip();
250 }
251
Padmanabhan Komanduruf5e6c5d2019-04-04 13:56:31 +0530252 bool vsync_source = (callbacks_->GetVsyncSource() == id_);
Padmanabhan Komanduru866bf6a2019-03-19 14:30:48 +0530253 bool skip_commit = enable_optimize_refresh_ && !pending_commit_ && !buffers_latched &&
Padmanabhan Komanduruf5e6c5d2019-04-04 13:56:31 +0530254 !pending_refresh_ && !vsync_source;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700255 pending_refresh_ = false;
256
257 return skip_commit;
258}
259
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530260HWC2::Error HWCDisplayBuiltIn::CommitStitchLayers() {
261 if (!validated_ || skip_commit_) {
262 return HWC2::Error::None;
263 }
264
265 if (disable_layer_stitch_) {
266 return HWC2::Error::None;
267 }
268
269 for (auto &layer : layer_stack_.layers) {
270 LayerComposition &composition = layer->composition;
271 if (composition != kCompositionStitch) {
272 continue;
273 }
274
275 LayerStitchStitchContext ctx = {};
276 // Stitch target doesn't have an input fence.
277 // Render all layers at specified destination.
278 LayerBuffer &input_buffer = layer->input_buffer;
279 ctx.src_hnd = reinterpret_cast<const private_handle_t *>(input_buffer.buffer_id);
280 Layer *stitch_layer = stitch_target_->GetSDMLayer();
281 LayerBuffer &output_buffer = stitch_layer->input_buffer;
282 ctx.dst_hnd = reinterpret_cast<const private_handle_t *>(output_buffer.buffer_id);
283 SetRect(layer->stitch_dst_rect, &ctx.dst_rect);
284 ctx.src_acquire_fence_fd = input_buffer.acquire_fence_fd;
285
286 layer_stitch_task_.PerformTask(LayerStitchTaskCode::kCodeStitch, &ctx);
287
288 // Merge with current fence and close previous one.
289 int acquire_fence = sync_merge(__CLASS__, output_buffer.acquire_fence_fd, ctx.release_fence_fd);
290 CloseFd(&output_buffer.acquire_fence_fd);
291 CloseFd(&ctx.release_fence_fd);
292
293 output_buffer.acquire_fence_fd = acquire_fence;
294 }
295
296 return HWC2::Error::None;
297}
298
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530299HWC2::Error HWCDisplayBuiltIn::Present(int32_t *out_retire_fence) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500300 auto status = HWC2::Error::None;
Gurpreet Singh Dhamia4276882019-04-12 10:30:58 -0700301
302 DTRACE_SCOPED();
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530303
304 if (!is_primary_ && active_secure_sessions_[kSecureDisplay]) {
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530305 return status;
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530306 } else if (display_paused_) {
Pullakavi Srinivas0a1dba62018-07-02 15:49:11 +0530307 DisplayError error = display_intf_->Flush(&layer_stack_);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700308 validated_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500309 if (error != kErrorNone) {
310 DLOGE("Flush failed. Error = %d", error);
311 }
312 } else {
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530313 status = CommitStitchLayers();
314 if (status != HWC2::Error::None) {
315 DLOGE("Stitch failed: %d", status);
316 return status;
317 }
318
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700319 status = CommitLayerStack();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500320 if (status == HWC2::Error::None) {
321 HandleFrameOutput();
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700322 SolidFillCommit();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500323 status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
324 }
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +0530325
326 if (stitch_target_) {
327 // Close Stitch buffer acquire fence.
328 Layer *stitch_layer = stitch_target_->GetSDMLayer();
329 LayerBuffer &output_buffer = stitch_layer->input_buffer;
330 CloseFd(&output_buffer.acquire_fence_fd);
331 CloseFd(&output_buffer.release_fence_fd);
332 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500333 }
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700334
Sushil Chauhan06521582018-03-19 14:00:23 -0700335 CloseFd(&output_buffer_.acquire_fence_fd);
Namit Solanki5b428dc2018-02-27 11:39:05 +0530336 pending_commit_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500337 return status;
338}
339
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530340HWC2::Error HWCDisplayBuiltIn::GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes) {
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700341 if (out_modes == nullptr) {
342 *out_num_modes = color_mode_->GetColorModeCount();
343 } else {
344 color_mode_->GetColorModes(out_num_modes, out_modes);
345 }
346
347 return HWC2::Error::None;
348}
349
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530350HWC2::Error HWCDisplayBuiltIn::GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
Naseer Ahmede7a77982018-06-04 10:56:04 -0400351 RenderIntent *out_intents) {
352 if (out_intents == nullptr) {
353 *out_num_intents = color_mode_->GetRenderIntentCount(mode);
354 } else {
355 color_mode_->GetRenderIntents(mode, out_num_intents, out_intents);
356 }
357 return HWC2::Error::None;
358}
359
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530360HWC2::Error HWCDisplayBuiltIn::SetColorMode(ColorMode mode) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400361 return SetColorModeWithRenderIntent(mode, RenderIntent::COLORIMETRIC);
362}
363
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530364HWC2::Error HWCDisplayBuiltIn::SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent) {
Sushil Chauhan8008d602018-09-04 14:43:27 -0700365 auto status = color_mode_->CacheColorModeWithRenderIntent(mode, intent);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700366 if (status != HWC2::Error::None) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400367 DLOGE("failed for mode = %d intent = %d", mode, intent);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700368 return status;
369 }
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800370 callbacks_->Refresh(id_);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700371 validated_ = false;
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700372 return status;
373}
374
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530375HWC2::Error HWCDisplayBuiltIn::SetColorModeById(int32_t color_mode_id) {
Naseer Ahmed6776dae2017-05-09 11:49:41 -0400376 auto status = color_mode_->SetColorModeById(color_mode_id);
377 if (status != HWC2::Error::None) {
378 DLOGE("failed for mode = %d", color_mode_id);
379 return status;
380 }
381
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800382 callbacks_->Refresh(id_);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700383 validated_ = false;
Naseer Ahmed6776dae2017-05-09 11:49:41 -0400384
385 return status;
386}
387
Xu Yang84e61412018-12-06 14:52:16 +0800388HWC2::Error HWCDisplayBuiltIn::SetColorModeFromClientApi(int32_t color_mode_id) {
389 DisplayError error = kErrorNone;
390 std::string mode_string;
391
392 error = display_intf_->GetColorModeName(color_mode_id, &mode_string);
393 if (error) {
394 DLOGE("Failed to get mode name for mode %d", color_mode_id);
395 return HWC2::Error::BadParameter;
396 }
397
398 auto status = color_mode_->SetColorModeFromClientApi(mode_string);
399 if (status != HWC2::Error::None) {
400 DLOGE("Failed to set mode = %d", color_mode_id);
401 return status;
402 }
403
404 return status;
405}
406
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530407HWC2::Error HWCDisplayBuiltIn::RestoreColorTransform() {
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +0530408 auto status = color_mode_->RestoreColorTransform();
409 if (status != HWC2::Error::None) {
410 DLOGE("failed to RestoreColorTransform");
411 return status;
412 }
413
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800414 callbacks_->Refresh(id_);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +0530415
416 return status;
417}
418
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530419HWC2::Error HWCDisplayBuiltIn::SetColorTransform(const float *matrix,
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700420 android_color_transform_t hint) {
421 if (!matrix) {
422 return HWC2::Error::BadParameter;
423 }
424
425 auto status = color_mode_->SetColorTransform(matrix, hint);
426 if (status != HWC2::Error::None) {
427 DLOGE("failed for hint = %d", hint);
428 color_tranform_failed_ = true;
429 return status;
430 }
431
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800432 callbacks_->Refresh(id_);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700433 color_tranform_failed_ = false;
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700434 validated_ = false;
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700435
436 return status;
437}
438
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530439HWC2::Error HWCDisplayBuiltIn::SetReadbackBuffer(const native_handle_t *buffer,
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530440 int32_t acquire_fence, bool post_processed_output,
441 CWBClient client) {
442 if (cwb_client_ != client && cwb_client_ != kCWBClientNone) {
443 DLOGE("CWB is in use with client = %d", cwb_client_);
444 return HWC2::Error::NoResources;
445 }
446
Sushil Chauhan06521582018-03-19 14:00:23 -0700447 const private_handle_t *handle = reinterpret_cast<const private_handle_t *>(buffer);
448 if (!handle || (handle->fd < 0)) {
449 return HWC2::Error::BadParameter;
450 }
451
452 // Configure the output buffer as Readback buffer
453 output_buffer_.width = UINT32(handle->width);
454 output_buffer_.height = UINT32(handle->height);
455 output_buffer_.unaligned_width = UINT32(handle->unaligned_width);
456 output_buffer_.unaligned_height = UINT32(handle->unaligned_height);
457 output_buffer_.format = HWCLayer::GetSDMFormat(handle->format, handle->flags);
458 output_buffer_.planes[0].fd = handle->fd;
459 output_buffer_.planes[0].stride = UINT32(handle->width);
460 output_buffer_.acquire_fence_fd = dup(acquire_fence);
461 output_buffer_.release_fence_fd = -1;
Sushil Chauhand0dc03d2018-09-19 07:08:40 -0700462 output_buffer_.handle_id = handle->id;
Sushil Chauhan06521582018-03-19 14:00:23 -0700463
464 post_processed_output_ = post_processed_output;
465 readback_buffer_queued_ = true;
466 readback_configured_ = false;
467 validated_ = false;
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530468 cwb_client_ = client;
Sushil Chauhan06521582018-03-19 14:00:23 -0700469
Sushil Chauhan06521582018-03-19 14:00:23 -0700470 return HWC2::Error::None;
471}
472
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530473HWC2::Error HWCDisplayBuiltIn::GetReadbackBufferFence(int32_t *release_fence) {
Sushil Chauhan06521582018-03-19 14:00:23 -0700474 auto status = HWC2::Error::None;
475
476 if (readback_configured_ && (output_buffer_.release_fence_fd >= 0)) {
477 *release_fence = output_buffer_.release_fence_fd;
478 } else {
479 status = HWC2::Error::Unsupported;
480 *release_fence = -1;
481 }
482
483 post_processed_output_ = false;
484 readback_buffer_queued_ = false;
485 readback_configured_ = false;
486 output_buffer_ = {};
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530487 cwb_client_ = kCWBClientNone;
Sushil Chauhan06521582018-03-19 14:00:23 -0700488
489 return status;
490}
491
Gurpreet Singh Dhami1207e462018-12-27 20:02:02 -0500492DisplayError HWCDisplayBuiltIn::TeardownConcurrentWriteback(void) {
493 DisplayError error = kErrorNotSupported;
494
495 if (output_buffer_.release_fence_fd >= 0) {
496 int32_t release_fence_fd = dup(output_buffer_.release_fence_fd);
497 int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
498 if (ret < 0) {
499 DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
500 }
501
502 ::close(release_fence_fd);
503 if (ret)
504 return kErrorResources;
505 }
506
507 if (display_intf_) {
508 error = display_intf_->TeardownConcurrentWriteback();
509 }
510
511 return error;
512}
513
Yuchao Ma577f0f72018-07-09 11:20:00 +0800514HWC2::Error HWCDisplayBuiltIn::SetDisplayDppsAdROI(uint32_t h_start, uint32_t h_end,
515 uint32_t v_start, uint32_t v_end,
516 uint32_t factor_in, uint32_t factor_out) {
517 DisplayError error = kErrorNone;
518 DisplayDppsAd4RoiCfg dpps_ad4_roi_cfg = {};
519 uint32_t panel_width = 0, panel_height = 0;
520 constexpr uint16_t kMaxFactorVal = 0xffff;
521
522 if (h_start >= h_end || v_start >= v_end || factor_in > kMaxFactorVal ||
523 factor_out > kMaxFactorVal) {
524 DLOGE("Invalid roi region = [%u, %u, %u, %u, %u, %u]",
525 h_start, h_end, v_start, v_end, factor_in, factor_out);
526 return HWC2::Error::BadParameter;
527 }
528
529 GetPanelResolution(&panel_width, &panel_height);
530
531 if (h_start >= panel_width || h_end > panel_width ||
532 v_start >= panel_height || v_end > panel_height) {
533 DLOGE("Invalid roi region = [%u, %u, %u, %u], panel resolution = [%u, %u]",
534 h_start, h_end, v_start, v_end, panel_width, panel_height);
535 return HWC2::Error::BadParameter;
536 }
537
538 dpps_ad4_roi_cfg.h_start = h_start;
539 dpps_ad4_roi_cfg.h_end = h_end;
540 dpps_ad4_roi_cfg.v_start = v_start;
541 dpps_ad4_roi_cfg.v_end = v_end;
542 dpps_ad4_roi_cfg.factor_in = factor_in;
543 dpps_ad4_roi_cfg.factor_out = factor_out;
544
545 error = display_intf_->SetDisplayDppsAdROI(&dpps_ad4_roi_cfg);
546 if (error)
547 return HWC2::Error::BadConfig;
548
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800549 callbacks_->Refresh(id_);
Yuchao Ma577f0f72018-07-09 11:20:00 +0800550
551 return HWC2::Error::None;
552}
553
Xu Yang9dacc872018-12-25 11:04:28 +0800554HWC2::Error HWCDisplayBuiltIn::SetFrameTriggerMode(uint32_t mode) {
555 DisplayError error = kErrorNone;
556 FrameTriggerMode trigger_mode = kFrameTriggerDefault;
557
558 if (mode >= kFrameTriggerMax) {
559 DLOGE("Invalid input mode %d", mode);
560 return HWC2::Error::BadParameter;
561 }
562
563 trigger_mode = static_cast<FrameTriggerMode>(mode);
564 error = display_intf_->SetFrameTriggerMode(trigger_mode);
565 if (error)
566 return HWC2::Error::BadConfig;
567
568 callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
569 validated_ = false;
570
571 return HWC2::Error::None;
572}
573
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530574int HWCDisplayBuiltIn::Perform(uint32_t operation, ...) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500575 va_list args;
576 va_start(args, operation);
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700577 int val = 0;
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700578 LayerSolidFill *solid_fill_color;
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700579 LayerRect *rect = NULL;
580
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500581 switch (operation) {
582 case SET_METADATA_DYN_REFRESH_RATE:
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700583 val = va_arg(args, int32_t);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500584 SetMetaDataRefreshRateFlag(val);
585 break;
586 case SET_BINDER_DYN_REFRESH_RATE:
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700587 val = va_arg(args, int32_t);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500588 ForceRefreshRate(UINT32(val));
589 break;
590 case SET_DISPLAY_MODE:
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700591 val = va_arg(args, int32_t);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500592 SetDisplayMode(UINT32(val));
593 break;
594 case SET_QDCM_SOLID_FILL_INFO:
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700595 solid_fill_color = va_arg(args, LayerSolidFill*);
596 SetQDCMSolidFillInfo(true, *solid_fill_color);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500597 break;
598 case UNSET_QDCM_SOLID_FILL_INFO:
Gopikrishnaiah Anandancc123062017-07-31 17:21:03 -0700599 solid_fill_color = va_arg(args, LayerSolidFill*);
600 SetQDCMSolidFillInfo(false, *solid_fill_color);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500601 break;
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700602 case SET_QDCM_SOLID_FILL_RECT:
603 rect = va_arg(args, LayerRect*);
604 solid_fill_rect_ = *rect;
605 break;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500606 default:
607 DLOGW("Invalid operation %d", operation);
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700608 va_end(args);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500609 return -EINVAL;
610 }
Arun Kumar K.R536c7d62016-06-14 18:47:39 -0700611 va_end(args);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700612 validated_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500613
614 return 0;
615}
616
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530617DisplayError HWCDisplayBuiltIn::SetDisplayMode(uint32_t mode) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500618 DisplayError error = kErrorNone;
619
620 if (display_intf_) {
621 error = display_intf_->SetDisplayMode(mode);
Tharaga Balachandran04192a62018-08-29 16:23:25 -0400622 if (error == kErrorNone) {
623 DisplayConfigFixedInfo fixed_info = {};
624 display_intf_->GetConfig(&fixed_info);
625 is_cmd_mode_ = fixed_info.is_cmdmode;
626 partial_update_enabled_ = fixed_info.partial_update;
627 for (auto hwc_layer : layer_set_) {
628 hwc_layer->SetPartialUpdate(partial_update_enabled_);
629 }
630 client_target_->SetPartialUpdate(partial_update_enabled_);
631 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500632 }
633
634 return error;
635}
636
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530637void HWCDisplayBuiltIn::SetMetaDataRefreshRateFlag(bool enable) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500638 int disable_metadata_dynfps = 0;
639
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530640 HWCDebugHandler::Get()->GetProperty(DISABLE_METADATA_DYNAMIC_FPS_PROP, &disable_metadata_dynfps);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500641 if (disable_metadata_dynfps) {
642 return;
643 }
644 use_metadata_refresh_rate_ = enable;
645}
646
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530647void HWCDisplayBuiltIn::SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500648 solid_fill_enable_ = enable;
649 solid_fill_color_ = color;
650}
651
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530652void HWCDisplayBuiltIn::ToggleCPUHint(bool set) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500653 if (!cpu_hint_) {
654 return;
655 }
656
657 if (set) {
658 cpu_hint_->Set();
659 } else {
660 cpu_hint_->Reset();
661 }
662}
663
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530664int HWCDisplayBuiltIn::HandleSecureSession(const std::bitset<kSecureMax> &secure_sessions,
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700665 bool *power_on_pending) {
666 if (!power_on_pending) {
667 return -EINVAL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500668 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700669
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530670 if (!is_primary_) {
671 return HWCDisplay::HandleSecureSession(secure_sessions, power_on_pending);
672 }
673
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800674 if (current_power_mode_ != HWC2::PowerMode::On) {
675 return 0;
676 }
677
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700678 if (active_secure_sessions_[kSecureDisplay] != secure_sessions[kSecureDisplay]) {
679 SecureEvent secure_event =
680 secure_sessions.test(kSecureDisplay) ? kSecureDisplayStart : kSecureDisplayEnd;
Pullakavi Srinivas0a1dba62018-07-02 15:49:11 +0530681 DisplayError err = display_intf_->HandleSecureEvent(secure_event, &layer_stack_);
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700682 if (err != kErrorNone) {
683 DLOGE("Set secure event failed");
684 return err;
685 }
686
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530687 DLOGI("SecureDisplay state changed from %d to %d for display %d-%d",
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700688 active_secure_sessions_.test(kSecureDisplay), secure_sessions.test(kSecureDisplay),
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530689 id_, type_);
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700690 }
691 active_secure_sessions_ = secure_sessions;
692 *power_on_pending = false;
693 return 0;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500694}
695
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530696void HWCDisplayBuiltIn::ForceRefreshRate(uint32_t refresh_rate) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500697 if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
698 force_refresh_rate_ == refresh_rate) {
699 // Cannot honor force refresh rate, as its beyond the range or new request is same
700 return;
701 }
702
703 force_refresh_rate_ = refresh_rate;
704
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800705 callbacks_->Refresh(id_);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500706
707 return;
708}
709
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530710uint32_t HWCDisplayBuiltIn::GetOptimalRefreshRate(bool one_updating_layer) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500711 if (force_refresh_rate_) {
712 return force_refresh_rate_;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500713 } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) {
714 return metadata_refresh_rate_;
715 }
716
717 return max_refresh_rate_;
718}
719
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530720void HWCDisplayBuiltIn::SetIdleTimeoutMs(uint32_t timeout_ms) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500721 display_intf_->SetIdleTimeoutMs(timeout_ms);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700722 validated_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500723}
724
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530725void HWCDisplayBuiltIn::HandleFrameOutput() {
Sushil Chauhan06521582018-03-19 14:00:23 -0700726 if (readback_buffer_queued_) {
727 validated_ = false;
728 }
729
Sushil Chauhan1d3b90c2018-12-11 16:52:35 -0800730 if (frame_capture_buffer_queued_) {
731 HandleFrameCapture();
732 } else if (dump_output_to_file_) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500733 HandleFrameDump();
734 }
735}
736
Sushil Chauhan1d3b90c2018-12-11 16:52:35 -0800737void HWCDisplayBuiltIn::HandleFrameCapture() {
738 if (readback_configured_ && (output_buffer_.release_fence_fd >= 0)) {
739 frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
740 ::close(output_buffer_.release_fence_fd);
741 output_buffer_.release_fence_fd = -1;
742 }
743
744 frame_capture_buffer_queued_ = false;
745 readback_buffer_queued_ = false;
746 post_processed_output_ = false;
747 readback_configured_ = false;
748 output_buffer_ = {};
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530749 cwb_client_ = kCWBClientNone;
Sushil Chauhan1d3b90c2018-12-11 16:52:35 -0800750}
751
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530752void HWCDisplayBuiltIn::HandleFrameDump() {
Ramkumar Radhakrishnan68331fc2019-06-19 11:24:12 -0700753 if (dump_frame_count_) {
754 int ret = 0;
755 if (output_buffer_.release_fence_fd >= 0) {
756 ret = sync_wait(output_buffer_.release_fence_fd, 1000);
757 if (ret < 0) {
758 DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
759 }
760 ::close(output_buffer_.release_fence_fd);
761 output_buffer_.release_fence_fd = -1;
762 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500763
Ramkumar Radhakrishnan68331fc2019-06-19 11:24:12 -0700764 if (!ret) {
765 DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
766 validated_ = false;
767 }
768
769 if (0 == (dump_frame_count_ - 1)) {
770 dump_output_to_file_ = false;
771 // Unmap and Free buffer
772 if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
773 DLOGE("unmap failed with err %d", errno);
774 }
775 if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
776 DLOGE("FreeBuffer failed");
777 }
778
Sushil Chauhan06521582018-03-19 14:00:23 -0700779 readback_buffer_queued_ = false;
Ramkumar Radhakrishnan68331fc2019-06-19 11:24:12 -0700780 post_processed_output_ = false;
781 readback_configured_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500782
Ramkumar Radhakrishnan68331fc2019-06-19 11:24:12 -0700783 output_buffer_ = {};
784 output_buffer_info_ = {};
785 output_buffer_base_ = nullptr;
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530786 cwb_client_ = kCWBClientNone;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500787 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500788 }
789}
790
Sushil Chauhan267a6192018-06-06 18:41:47 -0700791HWC2::Error HWCDisplayBuiltIn::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
792 int32_t format, bool post_processed) {
793 HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type, format, post_processed);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500794 dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
795 DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
796
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530797 if (dump_output_to_file_) {
798 if (cwb_client_ != kCWBClientNone) {
799 DLOGE("CWB is in use with client = %d", cwb_client_);
800 dump_output_to_file_ = false;
801 return HWC2::Error::NoResources;
802 }
803 }
804
Sushil Chauhand0dc03d2018-09-19 07:08:40 -0700805 if (!count || !dump_output_to_file_ || (output_buffer_info_.alloc_buffer_info.fd >= 0)) {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530806 return HWC2::Error::None;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500807 }
808
809 // Allocate and map output buffer
Sushil Chauhan267a6192018-06-06 18:41:47 -0700810 if (post_processed) {
811 // To dump post-processed (DSPP) output, use Panel resolution.
812 GetPanelResolution(&output_buffer_info_.buffer_config.width,
813 &output_buffer_info_.buffer_config.height);
814 } else {
815 // To dump Layer Mixer output, use FrameBuffer resolution.
816 GetFrameBufferResolution(&output_buffer_info_.buffer_config.width,
817 &output_buffer_info_.buffer_config.height);
818 }
819
820 output_buffer_info_.buffer_config.format = HWCLayer::GetSDMFormat(format, 0);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500821 output_buffer_info_.buffer_config.buffer_count = 1;
822 if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
823 DLOGE("Buffer allocation failed");
824 output_buffer_info_ = {};
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530825 return HWC2::Error::NoResources;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500826 }
827
828 void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
829 MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
830
831 if (buffer == MAP_FAILED) {
832 DLOGE("mmap failed with err %d", errno);
833 buffer_allocator_->FreeBuffer(&output_buffer_info_);
834 output_buffer_info_ = {};
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530835 return HWC2::Error::NoResources;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500836 }
837
838 output_buffer_base_ = buffer;
Sushil Chauhan06521582018-03-19 14:00:23 -0700839 const native_handle_t *handle = static_cast<native_handle_t *>(output_buffer_info_.private_data);
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530840 SetReadbackBuffer(handle, -1, post_processed, kCWBClientFrameDump);
Sushil Chauhan06521582018-03-19 14:00:23 -0700841
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +0530842 return HWC2::Error::None;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500843}
844
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530845int HWCDisplayBuiltIn::FrameCaptureAsync(const BufferInfo &output_buffer_info,
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500846 bool post_processed_output) {
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530847 if (cwb_client_ != kCWBClientNone) {
848 DLOGE("CWB is in use with client = %d", cwb_client_);
849 return -1;
850 }
851
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500852 // Note: This function is called in context of a binder thread and a lock is already held
853 if (output_buffer_info.alloc_buffer_info.fd < 0) {
854 DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
855 return -1;
856 }
857
858 auto panel_width = 0u;
859 auto panel_height = 0u;
860 auto fb_width = 0u;
861 auto fb_height = 0u;
862
863 GetPanelResolution(&panel_width, &panel_height);
864 GetFrameBufferResolution(&fb_width, &fb_height);
865
Ch Ganesh Kumar5c4988b2017-06-07 15:21:02 +0530866 if (post_processed_output && (output_buffer_info.buffer_config.width < panel_width ||
867 output_buffer_info.buffer_config.height < panel_height)) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500868 DLOGE("Buffer dimensions should not be less than panel resolution");
869 return -1;
Ch Ganesh Kumar5c4988b2017-06-07 15:21:02 +0530870 } else if (!post_processed_output && (output_buffer_info.buffer_config.width < fb_width ||
871 output_buffer_info.buffer_config.height < fb_height)) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500872 DLOGE("Buffer dimensions should not be less than FB resolution");
873 return -1;
874 }
875
Sushil Chauhan06521582018-03-19 14:00:23 -0700876 const native_handle_t *buffer = static_cast<native_handle_t *>(output_buffer_info.private_data);
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530877 SetReadbackBuffer(buffer, -1, post_processed_output, kCWBClientColor);
Sushil Chauhan1d3b90c2018-12-11 16:52:35 -0800878 frame_capture_buffer_queued_ = true;
879 frame_capture_status_ = -EAGAIN;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500880
881 return 0;
882}
883
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530884DisplayError HWCDisplayBuiltIn::SetDetailEnhancerConfig
Alan Kwong2e136332016-08-16 07:50:16 -0400885 (const DisplayDetailEnhancerData &de_data) {
886 DisplayError error = kErrorNotSupported;
887
888 if (display_intf_) {
889 error = display_intf_->SetDetailEnhancerData(de_data);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700890 validated_ = false;
Alan Kwong2e136332016-08-16 07:50:16 -0400891 }
892 return error;
893}
894
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530895DisplayError HWCDisplayBuiltIn::ControlPartialUpdate(bool enable, uint32_t *pending) {
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700896 DisplayError error = kErrorNone;
897
898 if (display_intf_) {
899 error = display_intf_->ControlPartialUpdate(enable, pending);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700900 validated_ = false;
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700901 }
902
903 return error;
904}
905
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530906DisplayError HWCDisplayBuiltIn::DisablePartialUpdateOneFrame() {
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700907 DisplayError error = kErrorNone;
908
909 if (display_intf_) {
910 error = display_intf_->DisablePartialUpdateOneFrame();
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700911 validated_ = false;
Arun Kumar K.R17bbd042016-06-07 17:38:02 -0700912 }
913
914 return error;
915}
916
917
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530918DisplayError HWCDisplayBuiltIn::SetMixerResolution(uint32_t width, uint32_t height) {
Sushil Chauhan409e8442017-06-12 17:43:25 -0700919 DisplayError error = display_intf_->SetMixerResolution(width, height);
Ramkumar Radhakrishnanf7f52162019-06-06 19:40:04 -0700920 callbacks_->Refresh(id_);
Saurabh Shaha307e8c2017-09-28 18:05:40 -0700921 validated_ = false;
Sushil Chauhan409e8442017-06-12 17:43:25 -0700922 return error;
Arun Kumar K.R8da7f502016-06-07 17:45:50 -0700923}
924
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530925DisplayError HWCDisplayBuiltIn::GetMixerResolution(uint32_t *width, uint32_t *height) {
Arun Kumar K.R8da7f502016-06-07 17:45:50 -0700926 return display_intf_->GetMixerResolution(width, height);
927}
928
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530929HWC2::Error HWCDisplayBuiltIn::SetQSyncMode(QSyncMode qsync_mode) {
Pullakavi Srinivas24574142019-08-14 12:18:12 +0530930 // Client needs to ensure that config change and qsync mode change
931 // are not triggered in the same drawcycle.
932 if (pending_config_) {
933 DLOGE("Failed to set qsync mode. Pending active config transition");
934 return HWC2::Error::Unsupported;
935 }
936
Sushil Chauhan380a59d2018-03-19 15:47:50 -0700937 auto err = display_intf_->SetQSyncMode(qsync_mode);
938 if (err != kErrorNone) {
939 return HWC2::Error::Unsupported;
940 }
941
Sushil Chauhanec4eb572019-02-07 13:29:42 -0800942 validated_ = false;
Sushil Chauhan380a59d2018-03-19 15:47:50 -0700943 return HWC2::Error::None;
944}
945
Ramkumar Radhakrishnan4faf8a62018-11-15 17:15:37 -0800946DisplayError HWCDisplayBuiltIn::ControlIdlePowerCollapse(bool enable, bool synchronous) {
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -0700947 DisplayError error = kErrorNone;
948
949 if (display_intf_) {
950 error = display_intf_->ControlIdlePowerCollapse(enable, synchronous);
951 validated_ = false;
952 }
Ramkumar Radhakrishnan4faf8a62018-11-15 17:15:37 -0800953 return error;
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -0700954}
955
Pullakavi Srinivas9189e602018-12-19 16:58:07 +0530956DisplayError HWCDisplayBuiltIn::SetDynamicDSIClock(uint64_t bitclk) {
Dileep Marchyaafa45bc2019-06-21 17:05:31 +0530957 DisablePartialUpdateOneFrame();
958 DisplayError error = display_intf_->SetDynamicDSIClock(bitclk);
959 if (error != kErrorNone) {
960 DLOGE(" failed: Clk: %llu Error: %d", bitclk, error);
961 return error;
Pullakavi Srinivas9189e602018-12-19 16:58:07 +0530962 }
963
964 callbacks_->Refresh(id_);
965 validated_ = false;
966
967 return kErrorNone;
968}
969
970DisplayError HWCDisplayBuiltIn::GetDynamicDSIClock(uint64_t *bitclk) {
Pullakavi Srinivas9189e602018-12-19 16:58:07 +0530971 if (display_intf_) {
972 return display_intf_->GetDynamicDSIClock(bitclk);
973 }
974
975 return kErrorNotSupported;
976}
977
978DisplayError HWCDisplayBuiltIn::GetSupportedDSIClock(std::vector<uint64_t> *bitclk_rates) {
979 if (display_intf_) {
980 return display_intf_->GetSupportedDSIClock(bitclk_rates);
981 }
982
983 return kErrorNotSupported;
984}
985
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700986HWC2::Error HWCDisplayBuiltIn::UpdateDisplayId(hwc2_display_t id) {
987 id_ = id;
988 return HWC2::Error::None;
989}
990
991HWC2::Error HWCDisplayBuiltIn::SetPendingRefresh() {
992 pending_refresh_ = true;
993 return HWC2::Error::None;
994}
995
Varun Arora75c05f02019-05-14 14:53:37 -0700996HWC2::Error HWCDisplayBuiltIn::SetPanelBrightness(float brightness) {
997 DisplayError ret = display_intf_->SetPanelBrightness(brightness);
998 if (ret != kErrorNone) {
999 return HWC2::Error::NoResources;
1000 }
1001
1002 return HWC2::Error::None;
1003}
1004
1005HWC2::Error HWCDisplayBuiltIn::GetPanelBrightness(float *brightness) {
1006 DisplayError ret = display_intf_->GetPanelBrightness(brightness);
1007 if (ret != kErrorNone) {
1008 return HWC2::Error::NoResources;
1009 }
1010
1011 return HWC2::Error::None;
1012}
Xu Yang45d0abf2019-07-05 11:34:06 +08001013
1014HWC2::Error HWCDisplayBuiltIn::SetBLScale(uint32_t level) {
1015 DisplayError ret = display_intf_->SetBLScale(level);
1016 if (ret != kErrorNone) {
1017 return HWC2::Error::NoResources;
1018 }
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301019 return HWC2::Error::None;
1020}
Xu Yang45d0abf2019-07-05 11:34:06 +08001021
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301022HWC2::Error HWCDisplayBuiltIn::UpdatePowerMode(HWC2::PowerMode mode) {
1023 current_power_mode_ = mode;
1024 validated_ = false;
Xu Yang45d0abf2019-07-05 11:34:06 +08001025 return HWC2::Error::None;
1026}
1027
Padmanabhan Komanduru9a91e9f2019-09-12 12:33:19 +05301028HWC2::Error HWCDisplayBuiltIn::SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
1029 int32_t dataspace, hwc_region_t damage) {
1030 HWC2::Error error = HWCDisplay::SetClientTarget(target, acquire_fence, dataspace, damage);
1031 if (error != HWC2::Error::None) {
1032 return error;
1033 }
1034
1035 Layer *sdm_layer = client_target_->GetSDMLayer();
Padmanabhan Komandurua4bf6062019-10-13 09:04:49 +05301036 uint32_t fb_width = 0, fb_height = 0;
1037
1038 GetFrameBufferResolution(&fb_width, &fb_height);
1039
1040 if (fb_width != sdm_layer->input_buffer.unaligned_width ||
1041 fb_height != sdm_layer->input_buffer.unaligned_height) {
1042 if (SetFrameBufferConfig(sdm_layer->input_buffer.unaligned_width,
1043 sdm_layer->input_buffer.unaligned_height)) {
1044 return HWC2::Error::BadParameter;
1045 }
1046 }
Padmanabhan Komanduru9a91e9f2019-09-12 12:33:19 +05301047
1048 return HWC2::Error::None;
1049}
Sushil Chauhan1566a132019-10-02 11:59:28 -07001050
1051bool HWCDisplayBuiltIn::IsSmartPanelConfig(uint32_t config_id) {
1052 if (config_id < hwc_config_map_.size()) {
1053 uint32_t index = hwc_config_map_.at(config_id);
1054 return variable_config_map_.at(index).smart_panel;
1055 }
1056
1057 return false;
1058}
1059
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +05301060int HWCDisplayBuiltIn::Deinit() {
1061 // Destory color convert instance. This destroys thread and underlying GL resources.
1062 if (gl_layer_stitch_) {
1063 layer_stitch_task_.PerformTask(LayerStitchTaskCode::kCodeDestroyInstance, nullptr);
1064 }
1065
1066 return HWCDisplay::Deinit();
1067}
1068
1069void HWCDisplayBuiltIn::OnTask(const LayerStitchTaskCode &task_code,
1070 SyncTask<LayerStitchTaskCode>::TaskContext *task_context) {
1071 switch (task_code) {
1072 case LayerStitchTaskCode::kCodeGetInstance: {
1073 gl_layer_stitch_ = GLLayerStitch::GetInstance(false /* Non-secure */);
1074 }
1075 break;
1076 case LayerStitchTaskCode::kCodeStitch: {
1077 DTRACE_SCOPED();
1078 LayerStitchStitchContext* ctx = reinterpret_cast<LayerStitchStitchContext*>(task_context);
1079 gl_layer_stitch_->Blit(ctx->src_hnd, ctx->dst_hnd, ctx->src_rect, ctx->dst_rect,
1080 ctx->src_acquire_fence_fd, ctx->dst_acquire_fence_fd,
1081 &(ctx->release_fence_fd));
1082 }
1083 break;
1084 case LayerStitchTaskCode::kCodeDestroyInstance: {
1085 if (gl_layer_stitch_) {
1086 GLLayerStitch::Destroy(gl_layer_stitch_);
1087 }
1088 }
1089 break;
1090 }
1091}
1092
1093bool HWCDisplayBuiltIn::InitLayerStitch() {
1094 if (!is_primary_) {
1095 // Disable on all non-primary builtins.
1096 DLOGI("Non-primary builtin.");
1097 return true;
1098 }
1099
1100 // Disable by default.
1101 int value = 1;
1102 Debug::Get()->GetProperty(DISABLE_LAYER_STITCH, &value);
1103 disable_layer_stitch_ = (value == 1);
1104
1105 if (disable_layer_stitch_) {
1106 DLOGI("Layer Stitch Disabled !!!");
1107 return true;
1108 }
1109
1110 // Initialize stitch context. This will be non-secure.
1111 layer_stitch_task_.PerformTask(LayerStitchTaskCode::kCodeGetInstance, nullptr);
1112 if (gl_layer_stitch_ == nullptr) {
1113 DLOGE("Failed to get LayerStitch Instance");
1114 return false;
1115 }
1116
1117 if (!AllocateStitchBuffer()) {
1118 return true;
1119 }
1120
1121 stitch_target_ = new HWCLayer(id_, static_cast<HWCBufferAllocator *>(buffer_allocator_));
1122
1123 // Populate buffer params and pvt handle.
1124 InitStitchTarget();
1125
1126 DLOGI("Created LayerStitch instance: %p", gl_layer_stitch_);
1127
1128 return true;
1129}
1130
1131bool HWCDisplayBuiltIn::AllocateStitchBuffer() {
1132 // Buffer dimensions: FB width * (1.5 * height)
1133
1134 DisplayError error = display_intf_->GetFrameBufferConfig(&fb_config_);
1135 if (error != kErrorNone) {
1136 DLOGE("Get frame buffer config failed. Error = %d", error);
1137 return false;
1138 }
1139
1140 BufferConfig &config = buffer_info_.buffer_config;
1141 config.width = fb_config_.x_pixels;
1142 config.height = fb_config_.y_pixels * kBufferHeightFactor;
1143
1144 // By default UBWC is enabled and below property is global enable/disable for all
1145 // buffers allocated through gralloc , including framebuffer targets.
1146 int ubwc_disabled = 0;
1147 HWCDebugHandler::Get()->GetProperty(DISABLE_UBWC_PROP, &ubwc_disabled);
1148 config.format = ubwc_disabled ? kFormatRGBA8888 : kFormatRGBA8888Ubwc;
1149
1150 config.gfx_client = true;
1151
1152 // Populate default params.
1153 config.secure = false;
1154 config.cache = false;
1155 config.secure_camera = false;
1156
1157 error = buffer_allocator_->AllocateBuffer(&buffer_info_);
1158
1159 if (error != kErrorNone) {
1160 DLOGE("Failed to allocate buffer. Error: %d", error);
1161 return false;
1162 }
1163
1164 return true;
1165}
1166
1167void HWCDisplayBuiltIn::InitStitchTarget() {
1168 LayerBuffer buffer = {};
1169 buffer.planes[0].fd = buffer_info_.alloc_buffer_info.fd;
1170 buffer.planes[0].offset = 0;
1171 buffer.planes[0].stride = buffer_info_.alloc_buffer_info.stride;
1172 buffer.size = buffer_info_.alloc_buffer_info.size;
1173 buffer.handle_id = buffer_info_.alloc_buffer_info.id;
1174 buffer.width = buffer_info_.alloc_buffer_info.aligned_width;
1175 buffer.height = buffer_info_.alloc_buffer_info.aligned_height;
1176 buffer.unaligned_width = fb_config_.x_pixels;
1177 buffer.unaligned_height = fb_config_.y_pixels;
1178 buffer.format = buffer_info_.alloc_buffer_info.format;
1179
1180 Layer *sdm_stitch_target = stitch_target_->GetSDMLayer();
1181 sdm_stitch_target->composition = kCompositionStitchTarget;
1182 sdm_stitch_target->input_buffer = buffer;
1183 sdm_stitch_target->input_buffer.buffer_id = reinterpret_cast<uint64_t>(buffer_info_.private_data);
1184}
1185
1186void HWCDisplayBuiltIn::AppendStitchLayer() {
1187 if (disable_layer_stitch_) {
1188 return;
1189 }
1190
1191 // Append stitch target buffer to layer stack.
1192 Layer *sdm_stitch_target = stitch_target_->GetSDMLayer();
1193 sdm_stitch_target->composition = kCompositionStitchTarget;
1194 layer_stack_.layers.push_back(sdm_stitch_target);
1195}
1196
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001197} // namespace sdm