blob: 194889e23109736ec454ce9620d15f87b1b537ae [file] [log] [blame]
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "hwc-display"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
22#include "DrmHwcTwo.h"
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020023#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020024#include "backend/BackendManager.h"
25#include "bufferinfo/BufferInfoGetter.h"
26#include "utils/log.h"
27#include "utils/properties.h"
28
29namespace android {
30
31std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
32 if (delta.total_pixops_ == 0)
33 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030034 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020035
36 std::stringstream ss;
37 ss << " Total frames count: " << delta.total_frames_ << "\n"
38 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
39 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
40 << ((delta.failed_kms_present_ > 0)
41 ? " !!! Internal failure, FIX it please\n"
42 : "")
43 << " Flattened frames: " << delta.frames_flattened_ << "\n"
44 << " Pixel operations (free units)"
45 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
46 << "]\n"
47 << " Composition efficiency: " << ratio;
48
49 return ss.str();
50}
51
52std::string HwcDisplay::Dump() {
53 std::string flattening_state_str;
54 switch (flattenning_state_) {
55 case ClientFlattenningState::Disabled:
56 flattening_state_str = "Disabled";
57 break;
58 case ClientFlattenningState::NotRequired:
59 flattening_state_str = "Not needed";
60 break;
61 case ClientFlattenningState::Flattened:
62 flattening_state_str = "Active";
63 break;
64 case ClientFlattenningState::ClientRefreshRequested:
65 flattening_state_str = "Refresh requested";
66 break;
67 default:
68 flattening_state_str = std::to_string(flattenning_state_) +
69 " VSync remains";
70 }
71
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030072 auto connector_name = IsInHeadlessMode()
73 ? std::string("NULL-DISPLAY")
74 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020075
Roman Stratiienko3627beb2022-01-04 16:02:55 +020076 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020077 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020078 << " Flattening state: " << flattening_state_str << "\n"
79 << "Statistics since system boot:\n"
80 << DumpDelta(total_stats_) << "\n\n"
81 << "Statistics since last dumpsys request:\n"
82 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
83
84 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
85 return ss.str();
86}
87
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020088HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
89 DrmHwcTwo *hwc2)
Roman Stratiienko3627beb2022-01-04 16:02:55 +020090 : hwc2_(hwc2),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020091 handle_(handle),
92 type_(type),
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020093 client_layer_(this),
Roman Stratiienko3627beb2022-01-04 16:02:55 +020094 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
95 // clang-format off
96 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
97 0.0, 1.0, 0.0, 0.0,
98 0.0, 0.0, 1.0, 0.0,
99 0.0, 0.0, 0.0, 1.0};
100 // clang-format on
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200101}
102
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200103HwcDisplay::~HwcDisplay() = default;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200104
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200105void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200106 Deinit();
107
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200108 pipeline_ = pipeline;
109
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200110 if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200111 Init();
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200112 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
113 } else {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200114 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200115 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200116}
117
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200118void HwcDisplay::Deinit() {
119 if (pipeline_ != nullptr) {
120 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200121 a_args.composition = std::make_shared<DrmKmsPlan>();
122 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000123/*
124 * TODO:
125 * Unfortunately the following causes regressions on db845c
126 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
127 * never coming back. Patches to avoiding that issue on the
128 * the kernel side unfortunately causes further crashes in
129 * drm_hwcomposer, because the client detach takes longer then the
130 * 1 second max VTS expects. So for now as a workaround, lets skip
131 * deactivating the display on deinit, which matches previous
132 * behavior prior to commit d0494d9b8097
133 */
134#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300135 a_args.composition = {};
136 a_args.active = false;
137 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000138#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200139
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200140 current_plan_.reset();
141 backend_.reset();
142 }
143
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200144 if (vsync_worker_) {
145 vsync_worker_->StopThread();
146 vsync_worker_ = {};
147 }
148
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200149 SetClientTarget(nullptr, -1, 0, {});
150}
151
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200152HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200153 ChosePreferredConfig();
154
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200155 auto vsw_callbacks = (VSyncWorkerCallbacks){
156 .out_event =
157 [this](int64_t timestamp) {
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +0200158 const std::unique_lock lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200159 if (vsync_event_en_) {
160 uint32_t period_ns{};
161 GetDisplayVsyncPeriod(&period_ns);
162 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
163 }
164 if (vsync_flattening_en_) {
165 ProcessFlatenningVsyncInternal();
166 }
167 if (vsync_tracking_en_) {
168 last_vsync_ts_ = timestamp;
169 }
170 if (!vsync_event_en_ && !vsync_flattening_en_ &&
171 !vsync_tracking_en_) {
172 vsync_worker_->VSyncControl(false);
173 }
174 },
175 .get_vperiod_ns = [this]() -> uint32_t {
176 uint32_t outVsyncPeriod = 0;
177 GetDisplayVsyncPeriod(&outVsyncPeriod);
178 return outVsyncPeriod;
179 },
180 };
181
182 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
183 if (!vsync_worker_) {
184 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200185 return HWC2::Error::BadDisplay;
186 }
187
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200188 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200189 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200190 if (ret) {
191 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
192 return HWC2::Error::BadDisplay;
193 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194 }
195
196 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
197
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200198 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199}
200
201HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200202 HWC2::Error err{};
203 if (!IsInHeadlessMode()) {
204 err = configs_.Update(*pipeline_->connector->Get());
205 } else {
206 configs_.FillHeadless();
207 }
208 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200209 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200210 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211
Roman Stratiienko0137f862022-01-04 18:27:40 +0200212 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200213}
214
215HWC2::Error HwcDisplay::AcceptDisplayChanges() {
216 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
217 l.second.AcceptTypeChange();
218 return HWC2::Error::None;
219}
220
221HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200222 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200223 *layer = static_cast<hwc2_layer_t>(layer_idx_);
224 ++layer_idx_;
225 return HWC2::Error::None;
226}
227
228HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200229 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200230 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200231 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232
233 layers_.erase(layer);
234 return HWC2::Error::None;
235}
236
237HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200238 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200239 return HWC2::Error::BadConfig;
240
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200241 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200242 return HWC2::Error::None;
243}
244
245HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
246 hwc2_layer_t *layers,
247 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200248 if (IsInHeadlessMode()) {
249 *num_elements = 0;
250 return HWC2::Error::None;
251 }
252
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200253 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300254 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200255 if (l.second.IsTypeChanged()) {
256 if (layers && num_changes < *num_elements)
257 layers[num_changes] = l.first;
258 if (types && num_changes < *num_elements)
259 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
260 ++num_changes;
261 }
262 }
263 if (!layers && !types)
264 *num_elements = num_changes;
265 return HWC2::Error::None;
266}
267
268HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
269 int32_t /*format*/,
270 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200271 if (IsInHeadlessMode()) {
272 return HWC2::Error::None;
273 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200274
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300275 auto min = pipeline_->device->GetMinResolution();
276 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200277
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200278 if (width < min.first || height < min.second)
279 return HWC2::Error::Unsupported;
280
281 if (width > max.first || height > max.second)
282 return HWC2::Error::Unsupported;
283
284 if (dataspace != HAL_DATASPACE_UNKNOWN)
285 return HWC2::Error::Unsupported;
286
287 // TODO(nobody): Validate format can be handled by either GL or planes
288 return HWC2::Error::None;
289}
290
291HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
292 if (!modes)
293 *num_modes = 1;
294
295 if (modes)
296 *modes = HAL_COLOR_MODE_NATIVE;
297
298 return HWC2::Error::None;
299}
300
301HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
302 int32_t attribute_in,
303 int32_t *value) {
304 int conf = static_cast<int>(config);
305
Roman Stratiienko0137f862022-01-04 18:27:40 +0200306 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200307 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200308 return HWC2::Error::BadConfig;
309 }
310
Roman Stratiienko0137f862022-01-04 18:27:40 +0200311 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200312
313 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300314 auto mm_width = configs_.mm_width;
315 auto mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200316 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
317 switch (attribute) {
318 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200319 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200320 break;
321 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200322 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200323 break;
324 case HWC2::Attribute::VsyncPeriod:
325 // in nanoseconds
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200326 *value = static_cast<int>(1E9 / hwc_config.mode.GetVRefresh());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200327 break;
328 case HWC2::Attribute::DpiX:
329 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200330 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
331 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200332 : -1;
333 break;
334 case HWC2::Attribute::DpiY:
335 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200336 *value = mm_height ? int(hwc_config.mode.GetRawMode().vdisplay *
337 kUmPerInch / mm_height)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200338 : -1;
339 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200340#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200341 case HWC2::Attribute::ConfigGroup:
342 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
343 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200344 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200345 break;
346#endif
347 default:
348 *value = -1;
349 return HWC2::Error::BadConfig;
350 }
351 return HWC2::Error::None;
352}
353
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200354HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
355 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200356 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200357 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200358 if (hwc_config.second.disabled) {
359 continue;
360 }
361
362 if (configs != nullptr) {
363 if (idx >= *num_configs) {
364 break;
365 }
366 configs[idx] = hwc_config.second.id;
367 }
368
369 idx++;
370 }
371 *num_configs = idx;
372 return HWC2::Error::None;
373}
374
375HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
376 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200377 if (IsInHeadlessMode()) {
378 stream << "null-display";
379 } else {
380 stream << "display-" << GetPipe().connector->Get()->GetId();
381 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300382 auto string = stream.str();
383 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200384 if (!name) {
385 *size = length;
386 return HWC2::Error::None;
387 }
388
389 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
390 strncpy(name, string.c_str(), *size);
391 return HWC2::Error::None;
392}
393
394HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
395 uint32_t *num_elements,
396 hwc2_layer_t * /*layers*/,
397 int32_t * /*layer_requests*/) {
398 // TODO(nobody): I think virtual display should request
399 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
400 *num_elements = 0;
401 return HWC2::Error::None;
402}
403
404HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
405 *type = static_cast<int32_t>(type_);
406 return HWC2::Error::None;
407}
408
409HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
410 *support = 0;
411 return HWC2::Error::None;
412}
413
414HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
415 int32_t * /*types*/,
416 float * /*max_luminance*/,
417 float * /*max_average_luminance*/,
418 float * /*min_luminance*/) {
419 *num_types = 0;
420 return HWC2::Error::None;
421}
422
423/* Find API details at:
424 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
Roman Stratiienkodd214942022-05-03 18:24:49 +0300425 *
426 * Called after PresentDisplay(), CLIENT is expecting release fence for the
427 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200428 */
429HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
430 hwc2_layer_t *layers,
431 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200432 if (IsInHeadlessMode()) {
433 *num_elements = 0;
434 return HWC2::Error::None;
435 }
436
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200437 uint32_t num_layers = 0;
438
Roman Stratiienkodd214942022-05-03 18:24:49 +0300439 for (auto &l : layers_) {
440 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
441 continue;
442 }
443
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200444 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300445
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200446 if (layers == nullptr || fences == nullptr)
447 continue;
448
449 if (num_layers > *num_elements) {
450 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
451 return HWC2::Error::None;
452 }
453
454 layers[num_layers - 1] = l.first;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300455 fences[num_layers - 1] = UniqueFd::Dup(present_fence_.Get()).Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200456 }
457 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300458
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200459 return HWC2::Error::None;
460}
461
462HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200463 if (IsInHeadlessMode()) {
464 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
465 return HWC2::Error::None;
466 }
467
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200468 uint32_t prev_vperiod_ns = 0;
469 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200470
471 auto mode_update_commited_ = false;
472 if (staged_mode_ &&
473 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
474 client_layer_.SetLayerDisplayFrame(
475 (hwc_rect_t){.left = 0,
476 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200477 .right = int(staged_mode_->GetRawMode().hdisplay),
478 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200479
480 configs_.active_config_id = staged_mode_config_id_;
481
482 a_args.display_mode = *staged_mode_;
483 if (!a_args.test_only) {
484 mode_update_commited_ = true;
485 }
486 }
487
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488 // order the layers by z-order
489 bool use_client_layer = false;
490 uint32_t client_z_order = UINT32_MAX;
491 std::map<uint32_t, HwcLayer *> z_map;
492 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
493 switch (l.second.GetValidatedType()) {
494 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300495 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496 break;
497 case HWC2::Composition::Client:
498 // Place it at the z_order of the lowest client layer
499 use_client_layer = true;
500 client_z_order = std::min(client_z_order, l.second.GetZOrder());
501 break;
502 default:
503 continue;
504 }
505 }
506 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300507 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200508
509 if (z_map.empty())
510 return HWC2::Error::BadLayer;
511
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200512 std::vector<LayerData> composition_layers;
513
514 /* Import & populate */
515 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
516 l.second->PopulateLayerData(a_args.test_only);
517 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200518
519 // now that they're ordered by z, add them to the composition
520 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200521 if (!l.second->IsLayerUsableAsDevice()) {
522 /* This will be normally triggered on validation of the first frame
523 * containing CLIENT layer. At this moment client buffer is not yet
524 * provided by the CLIENT.
525 * This may be triggered once in HwcLayer lifecycle in case FB can't be
526 * imported. For example when non-contiguous buffer is imported into
527 * contiguous-only DRM/KMS driver.
528 */
529 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200530 }
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200531 composition_layers.emplace_back(l.second->GetLayerData().Clone());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532 }
533
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200534 /* Store plan to ensure shared planes won't be stolen by other display
535 * in between of ValidateDisplay() and PresentDisplay() calls
536 */
537 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
538 std::move(composition_layers));
539 if (!current_plan_) {
540 if (!a_args.test_only) {
541 ALOGE("Failed to create DrmKmsPlan");
542 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200543 return HWC2::Error::BadConfig;
544 }
545
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200546 a_args.composition = current_plan_;
547
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300548 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200549
550 if (ret) {
551 if (!a_args.test_only)
552 ALOGE("Failed to apply the frame composition ret=%d", ret);
553 return HWC2::Error::BadParameter;
554 }
555
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200556 if (mode_update_commited_) {
557 staged_mode_.reset();
558 vsync_tracking_en_ = false;
559 if (last_vsync_ts_ != 0) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200560 hwc2_->SendVsyncPeriodTimingChangedEventToClient(handle_,
561 last_vsync_ts_ +
562 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200563 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200564 }
565
566 return HWC2::Error::None;
567}
568
569/* Find API details at:
570 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
571 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300572HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200573 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300574 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200575 return HWC2::Error::None;
576 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200577 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200578
579 ++total_stats_.total_frames_;
580
581 AtomicCommitArgs a_args{};
582 ret = CreateComposition(a_args);
583
584 if (ret != HWC2::Error::None)
585 ++total_stats_.failed_kms_present_;
586
587 if (ret == HWC2::Error::BadLayer) {
588 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300589 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200590 return HWC2::Error::None;
591 }
592 if (ret != HWC2::Error::None)
593 return ret;
594
Roman Stratiienkodd214942022-05-03 18:24:49 +0300595 this->present_fence_ = UniqueFd::Dup(a_args.out_fence.Get());
596 *out_present_fence = a_args.out_fence.Release();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200597
598 ++frame_no_;
599 return HWC2::Error::None;
600}
601
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200602HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
603 int64_t change_time) {
604 if (configs_.hwc_configs.count(config) == 0) {
605 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200606 return HWC2::Error::BadConfig;
607 }
608
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200609 staged_mode_ = configs_.hwc_configs[config].mode;
610 staged_mode_change_time_ = change_time;
611 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200612
613 return HWC2::Error::None;
614}
615
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200616HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
617 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
618}
619
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200620/* Find API details at:
621 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
622 */
623HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
624 int32_t acquire_fence,
625 int32_t dataspace,
626 hwc_region_t /*damage*/) {
627 client_layer_.SetLayerBuffer(target, acquire_fence);
628 client_layer_.SetLayerDataspace(dataspace);
629
630 /*
631 * target can be nullptr, this does mean the Composer Service is calling
632 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
633 * https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/graphics/composer/2.1/utils/hal/include/composer-hal/2.1/ComposerClient.h;l=350;drc=944b68180b008456ed2eb4d4d329e33b19bd5166
634 */
635 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300636 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200637 return HWC2::Error::None;
638 }
639
Roman Stratiienko5070d512022-05-30 13:41:20 +0300640 if (IsInHeadlessMode()) {
641 return HWC2::Error::None;
642 }
643
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200644 client_layer_.PopulateLayerData(/*test = */ true);
645 if (!client_layer_.IsLayerUsableAsDevice()) {
646 ALOGE("Client layer must be always usable by DRM/KMS");
647 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200648 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200649
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200650 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300651 if (!bi) {
652 ALOGE("%s: Invalid state", __func__);
653 return HWC2::Error::BadLayer;
654 }
655
656 auto source_crop = (hwc_frect_t){.left = 0.0F,
657 .top = 0.0F,
658 .right = static_cast<float>(bi->width),
659 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200660 client_layer_.SetLayerSourceCrop(source_crop);
661
662 return HWC2::Error::None;
663}
664
665HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
666 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
667 return HWC2::Error::BadParameter;
668
669 if (mode != HAL_COLOR_MODE_NATIVE)
670 return HWC2::Error::Unsupported;
671
672 color_mode_ = mode;
673 return HWC2::Error::None;
674}
675
676HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
677 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
678 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
679 return HWC2::Error::BadParameter;
680
681 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
682 return HWC2::Error::BadParameter;
683
684 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
685 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
686 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
687
688 return HWC2::Error::None;
689}
690
691HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
692 int32_t /*release_fence*/) {
693 // TODO(nobody): Need virtual display support
694 return HWC2::Error::Unsupported;
695}
696
697HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
698 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300699
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200700 AtomicCommitArgs a_args{};
701
702 switch (mode) {
703 case HWC2::PowerMode::Off:
704 a_args.active = false;
705 break;
706 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300707 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200708 break;
709 case HWC2::PowerMode::Doze:
710 case HWC2::PowerMode::DozeSuspend:
711 return HWC2::Error::Unsupported;
712 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300713 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200714 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300715 }
716
717 if (IsInHeadlessMode()) {
718 return HWC2::Error::None;
719 }
720
Jia Ren80566fe2022-11-17 17:26:00 +0800721 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300722 /*
723 * Setting the display to active before we have a composition
724 * can break some drivers, so skip setting a_args.active to
725 * true, as the next composition frame will implicitly activate
726 * the display
727 */
728 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
729 ? HWC2::Error::None
730 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200731 };
732
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300733 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200734 if (err) {
735 ALOGE("Failed to apply the dpms composition err=%d", err);
736 return HWC2::Error::BadParameter;
737 }
738 return HWC2::Error::None;
739}
740
741HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200742 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
743 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200744 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200745 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200746 return HWC2::Error::None;
747}
748
749HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
750 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200751 if (IsInHeadlessMode()) {
752 *num_types = *num_requests = 0;
753 return HWC2::Error::None;
754 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300755
756 /* In current drm_hwc design in case previous frame layer was not validated as
757 * a CLIENT, it is used by display controller (Front buffer). We have to store
758 * this state to provide the CLIENT with the release fences for such buffers.
759 */
760 for (auto &l : layers_) {
761 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
762 HWC2::Composition::Client);
763 }
764
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200765 return backend_->ValidateDisplay(this, num_types, num_requests);
766}
767
768std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
769 std::vector<HwcLayer *> ordered_layers;
770 ordered_layers.reserve(layers_.size());
771
772 for (auto &[handle, layer] : layers_) {
773 ordered_layers.emplace_back(&layer);
774 }
775
776 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
777 [](const HwcLayer *lhs, const HwcLayer *rhs) {
778 return lhs->GetZOrder() < rhs->GetZOrder();
779 });
780
781 return ordered_layers;
782}
783
Roman Stratiienko099c3112022-01-20 11:50:54 +0200784HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
785 uint32_t *outVsyncPeriod /* ns */) {
786 return GetDisplayAttribute(configs_.active_config_id,
787 HWC2_ATTRIBUTE_VSYNC_PERIOD,
788 (int32_t *)(outVsyncPeriod));
789}
790
Roman Stratiienko6b405052022-12-10 19:09:10 +0200791#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200792HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200793 if (IsInHeadlessMode()) {
794 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
795 return HWC2::Error::None;
796 }
797 /* Primary display should be always internal,
798 * otherwise SF will be unhappy and will crash
799 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200800 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200801 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200802 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200803 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
804 else
805 return HWC2::Error::BadConfig;
806
807 return HWC2::Error::None;
808}
809
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200810HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200811 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200812 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
813 hwc_vsync_period_change_timeline_t *outTimeline) {
814 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
815 return HWC2::Error::BadParameter;
816 }
817
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200818 uint32_t current_vsync_period{};
819 GetDisplayVsyncPeriod(&current_vsync_period);
820
821 if (vsyncPeriodChangeConstraints->seamlessRequired) {
822 return HWC2::Error::SeamlessNotAllowed;
823 }
824
825 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
826 ->desiredTimeNanos -
827 current_vsync_period;
828 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
829 if (ret != HWC2::Error::None) {
830 return ret;
831 }
832
833 outTimeline->refreshRequired = true;
834 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
835 ->desiredTimeNanos;
836
837 last_vsync_ts_ = 0;
838 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200839 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200840
841 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200842}
843
844HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
845 return HWC2::Error::Unsupported;
846}
847
848HWC2::Error HwcDisplay::GetSupportedContentTypes(
849 uint32_t *outNumSupportedContentTypes,
850 const uint32_t *outSupportedContentTypes) {
851 if (outSupportedContentTypes == nullptr)
852 *outNumSupportedContentTypes = 0;
853
854 return HWC2::Error::None;
855}
856
857HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
858 if (contentType != HWC2_CONTENT_TYPE_NONE)
859 return HWC2::Error::Unsupported;
860
861 /* TODO: Map to the DRM Connector property:
862 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
863 */
864
865 return HWC2::Error::None;
866}
867#endif
868
Roman Stratiienko6b405052022-12-10 19:09:10 +0200869#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200870HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
871 uint32_t *outDataSize,
872 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200873 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300874 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200875 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300876
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200877 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200878 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300879 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200880 }
881
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300882 *outPort = handle_; /* TDOD(nobody): What should be here? */
883
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200884 if (outData) {
885 *outDataSize = std::min(*outDataSize, blob->length);
886 memcpy(outData, blob->data, *outDataSize);
887 } else {
888 *outDataSize = blob->length;
889 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200890
891 return HWC2::Error::None;
892}
893
894HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
895 uint32_t * /*outCapabilities*/) {
896 if (outNumCapabilities == nullptr) {
897 return HWC2::Error::BadParameter;
898 }
899
900 *outNumCapabilities = 0;
901
902 return HWC2::Error::None;
903}
904
905HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
906 *supported = false;
907 return HWC2::Error::None;
908}
909
910HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
911 return HWC2::Error::Unsupported;
912}
913
Roman Stratiienko6b405052022-12-10 19:09:10 +0200914#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200915
Roman Stratiienko6b405052022-12-10 19:09:10 +0200916#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200917
918HWC2::Error HwcDisplay::GetRenderIntents(
919 int32_t mode, uint32_t *outNumIntents,
920 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
921 if (mode != HAL_COLOR_MODE_NATIVE) {
922 return HWC2::Error::BadParameter;
923 }
924
925 if (outIntents == nullptr) {
926 *outNumIntents = 1;
927 return HWC2::Error::None;
928 }
929 *outNumIntents = 1;
930 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
931 return HWC2::Error::None;
932}
933
934HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
935 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
936 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
937 return HWC2::Error::BadParameter;
938
939 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
940 return HWC2::Error::BadParameter;
941
942 if (mode != HAL_COLOR_MODE_NATIVE)
943 return HWC2::Error::Unsupported;
944
945 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
946 return HWC2::Error::Unsupported;
947
948 color_mode_ = mode;
949 return HWC2::Error::None;
950}
951
Roman Stratiienko6b405052022-12-10 19:09:10 +0200952#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200953
954const Backend *HwcDisplay::backend() const {
955 return backend_.get();
956}
957
958void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
959 backend_ = std::move(backend);
960}
961
Roman Stratiienko099c3112022-01-20 11:50:54 +0200962/* returns true if composition should be sent to client */
963bool HwcDisplay::ProcessClientFlatteningState(bool skip) {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300964 const int flattenning_state = flattenning_state_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200965 if (flattenning_state == ClientFlattenningState::Disabled) {
966 return false;
967 }
968
969 if (skip) {
970 flattenning_state_ = ClientFlattenningState::NotRequired;
971 return false;
972 }
973
974 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
975 flattenning_state_ = ClientFlattenningState::Flattened;
976 return true;
977 }
978
979 vsync_flattening_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200980 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200981 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
982 return false;
983}
984
985void HwcDisplay::ProcessFlatenningVsyncInternal() {
986 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
987 --flattenning_state_ == ClientFlattenningState::ClientRefreshRequested &&
988 hwc2_->refresh_callback_.first != nullptr &&
989 hwc2_->refresh_callback_.second != nullptr) {
990 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
991 vsync_flattening_en_ = false;
992 }
993}
994
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200995} // namespace android