blob: d335d720d6da76c2e02135857d65a0365c624020 [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() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030053 auto connector_name = IsInHeadlessMode()
54 ? std::string("NULL-DISPLAY")
55 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +020056
Roman Stratiienko3627beb2022-01-04 16:02:55 +020057 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020058 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020059 << "Statistics since system boot:\n"
60 << DumpDelta(total_stats_) << "\n\n"
61 << "Statistics since last dumpsys request:\n"
62 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
63
64 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
65 return ss.str();
66}
67
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020068HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
69 DrmHwcTwo *hwc2)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020070 : hwc2_(hwc2), handle_(handle), type_(type), client_layer_(this){};
71
72void HwcDisplay::SetColorMarixToIdentity() {
73 color_matrix_ = std::make_shared<drm_color_ctm>();
74 for (int i = 0; i < kCtmCols; i++) {
75 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +080076 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020077 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
78 }
79 }
80
81 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020082}
83
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020084HwcDisplay::~HwcDisplay() = default;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020085
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020086void HwcDisplay::SetPipeline(DrmDisplayPipeline *pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +020087 Deinit();
88
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020089 pipeline_ = pipeline;
90
Roman Stratiienkod0494d92022-03-15 18:02:04 +020091 if (pipeline != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020092 Init();
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020093 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ true);
94 } else {
Roman Stratiienkod0494d92022-03-15 18:02:04 +020095 hwc2_->ScheduleHotplugEvent(handle_, /*connected = */ false);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020096 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +020097}
98
Roman Stratiienkod0494d92022-03-15 18:02:04 +020099void HwcDisplay::Deinit() {
100 if (pipeline_ != nullptr) {
101 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200102 a_args.composition = std::make_shared<DrmKmsPlan>();
103 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000104/*
105 * TODO:
106 * Unfortunately the following causes regressions on db845c
107 * with VtsHalGraphicsComposerV2_3TargetTest due to the display
108 * never coming back. Patches to avoiding that issue on the
109 * the kernel side unfortunately causes further crashes in
110 * drm_hwcomposer, because the client detach takes longer then the
111 * 1 second max VTS expects. So for now as a workaround, lets skip
112 * deactivating the display on deinit, which matches previous
113 * behavior prior to commit d0494d9b8097
114 */
115#if 0
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300116 a_args.composition = {};
117 a_args.active = false;
118 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
John Stultz799e8c72022-06-30 19:49:42 +0000119#endif
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200120
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200121 current_plan_.reset();
122 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200123 if (flatcon_) {
124 flatcon_->StopThread();
125 flatcon_.reset();
126 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200127 }
128
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200129 if (vsync_worker_) {
130 vsync_worker_->StopThread();
131 vsync_worker_ = {};
132 }
133
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200134 SetClientTarget(nullptr, -1, 0, {});
135}
136
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200137HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200138 ChosePreferredConfig();
139
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200140 auto vsw_callbacks = (VSyncWorkerCallbacks){
141 .out_event =
142 [this](int64_t timestamp) {
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +0200143 const std::unique_lock lock(hwc2_->GetResMan().GetMainLock());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200144 if (vsync_event_en_) {
145 uint32_t period_ns{};
146 GetDisplayVsyncPeriod(&period_ns);
147 hwc2_->SendVsyncEventToClient(handle_, timestamp, period_ns);
148 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200149 if (vsync_tracking_en_) {
150 last_vsync_ts_ = timestamp;
151 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200152 if (!vsync_event_en_ && !vsync_tracking_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200153 vsync_worker_->VSyncControl(false);
154 }
155 },
156 .get_vperiod_ns = [this]() -> uint32_t {
157 uint32_t outVsyncPeriod = 0;
158 GetDisplayVsyncPeriod(&outVsyncPeriod);
159 return outVsyncPeriod;
160 },
161 };
162
163 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_, vsw_callbacks);
164 if (!vsync_worker_) {
165 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200166 return HWC2::Error::BadDisplay;
167 }
168
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200169 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200170 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200171 if (ret) {
172 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
173 return HWC2::Error::BadDisplay;
174 }
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200175 auto flatcbk = (struct FlatConCallbacks){.trigger = [this]() {
176 if (hwc2_->refresh_callback_.first != nullptr &&
177 hwc2_->refresh_callback_.second != nullptr)
178 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second,
179 handle_);
180 }};
181 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200182 }
183
184 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
185
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200186 SetColorMarixToIdentity();
187
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200188 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200189}
190
191HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200192 HWC2::Error err{};
193 if (!IsInHeadlessMode()) {
194 err = configs_.Update(*pipeline_->connector->Get());
195 } else {
196 configs_.FillHeadless();
197 }
198 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200200 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200201
Roman Stratiienko0137f862022-01-04 18:27:40 +0200202 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200203}
204
205HWC2::Error HwcDisplay::AcceptDisplayChanges() {
206 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
207 l.second.AcceptTypeChange();
208 return HWC2::Error::None;
209}
210
211HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200212 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200213 *layer = static_cast<hwc2_layer_t>(layer_idx_);
214 ++layer_idx_;
215 return HWC2::Error::None;
216}
217
218HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200219 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200220 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200221 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222
223 layers_.erase(layer);
224 return HWC2::Error::None;
225}
226
227HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200228 if (configs_.hwc_configs.count(staged_mode_config_id_) == 0)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200229 return HWC2::Error::BadConfig;
230
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200231 *config = staged_mode_config_id_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232 return HWC2::Error::None;
233}
234
235HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
236 hwc2_layer_t *layers,
237 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200238 if (IsInHeadlessMode()) {
239 *num_elements = 0;
240 return HWC2::Error::None;
241 }
242
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200243 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300244 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200245 if (l.second.IsTypeChanged()) {
246 if (layers && num_changes < *num_elements)
247 layers[num_changes] = l.first;
248 if (types && num_changes < *num_elements)
249 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
250 ++num_changes;
251 }
252 }
253 if (!layers && !types)
254 *num_elements = num_changes;
255 return HWC2::Error::None;
256}
257
258HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
259 int32_t /*format*/,
260 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200261 if (IsInHeadlessMode()) {
262 return HWC2::Error::None;
263 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200264
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300265 auto min = pipeline_->device->GetMinResolution();
266 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200267
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200268 if (width < min.first || height < min.second)
269 return HWC2::Error::Unsupported;
270
271 if (width > max.first || height > max.second)
272 return HWC2::Error::Unsupported;
273
274 if (dataspace != HAL_DATASPACE_UNKNOWN)
275 return HWC2::Error::Unsupported;
276
277 // TODO(nobody): Validate format can be handled by either GL or planes
278 return HWC2::Error::None;
279}
280
281HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
282 if (!modes)
283 *num_modes = 1;
284
285 if (modes)
286 *modes = HAL_COLOR_MODE_NATIVE;
287
288 return HWC2::Error::None;
289}
290
291HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
292 int32_t attribute_in,
293 int32_t *value) {
294 int conf = static_cast<int>(config);
295
Roman Stratiienko0137f862022-01-04 18:27:40 +0200296 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200297 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200298 return HWC2::Error::BadConfig;
299 }
300
Roman Stratiienko0137f862022-01-04 18:27:40 +0200301 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200302
303 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300304 auto mm_width = configs_.mm_width;
305 auto mm_height = configs_.mm_height;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200306 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
307 switch (attribute) {
308 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200309 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200310 break;
311 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200312 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200313 break;
314 case HWC2::Attribute::VsyncPeriod:
315 // in nanoseconds
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200316 *value = static_cast<int>(1E9 / hwc_config.mode.GetVRefresh());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200317 break;
318 case HWC2::Attribute::DpiX:
319 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200320 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
321 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200322 : -1;
323 break;
324 case HWC2::Attribute::DpiY:
325 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200326 *value = mm_height ? int(hwc_config.mode.GetRawMode().vdisplay *
327 kUmPerInch / mm_height)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200328 : -1;
329 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200330#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200331 case HWC2::Attribute::ConfigGroup:
332 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
333 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200334 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200335 break;
336#endif
337 default:
338 *value = -1;
339 return HWC2::Error::BadConfig;
340 }
341 return HWC2::Error::None;
342}
343
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200344HWC2::Error HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
345 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200346 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200347 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200348 if (hwc_config.second.disabled) {
349 continue;
350 }
351
352 if (configs != nullptr) {
353 if (idx >= *num_configs) {
354 break;
355 }
356 configs[idx] = hwc_config.second.id;
357 }
358
359 idx++;
360 }
361 *num_configs = idx;
362 return HWC2::Error::None;
363}
364
365HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
366 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200367 if (IsInHeadlessMode()) {
368 stream << "null-display";
369 } else {
370 stream << "display-" << GetPipe().connector->Get()->GetId();
371 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300372 auto string = stream.str();
373 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200374 if (!name) {
375 *size = length;
376 return HWC2::Error::None;
377 }
378
379 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
380 strncpy(name, string.c_str(), *size);
381 return HWC2::Error::None;
382}
383
384HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
385 uint32_t *num_elements,
386 hwc2_layer_t * /*layers*/,
387 int32_t * /*layer_requests*/) {
388 // TODO(nobody): I think virtual display should request
389 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
390 *num_elements = 0;
391 return HWC2::Error::None;
392}
393
394HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
395 *type = static_cast<int32_t>(type_);
396 return HWC2::Error::None;
397}
398
399HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
400 *support = 0;
401 return HWC2::Error::None;
402}
403
404HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
405 int32_t * /*types*/,
406 float * /*max_luminance*/,
407 float * /*max_average_luminance*/,
408 float * /*min_luminance*/) {
409 *num_types = 0;
410 return HWC2::Error::None;
411}
412
413/* Find API details at:
414 * 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 +0300415 *
416 * Called after PresentDisplay(), CLIENT is expecting release fence for the
417 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200418 */
419HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
420 hwc2_layer_t *layers,
421 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200422 if (IsInHeadlessMode()) {
423 *num_elements = 0;
424 return HWC2::Error::None;
425 }
426
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200427 uint32_t num_layers = 0;
428
Roman Stratiienkodd214942022-05-03 18:24:49 +0300429 for (auto &l : layers_) {
430 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
431 continue;
432 }
433
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200434 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300435
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200436 if (layers == nullptr || fences == nullptr)
437 continue;
438
439 if (num_layers > *num_elements) {
440 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
441 return HWC2::Error::None;
442 }
443
444 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200445 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200446 }
447 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300448
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200449 return HWC2::Error::None;
450}
451
452HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200453 if (IsInHeadlessMode()) {
454 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
455 return HWC2::Error::None;
456 }
457
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200458 a_args.color_matrix = color_matrix_;
459
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200460 uint32_t prev_vperiod_ns = 0;
461 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200462
463 auto mode_update_commited_ = false;
464 if (staged_mode_ &&
465 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
466 client_layer_.SetLayerDisplayFrame(
467 (hwc_rect_t){.left = 0,
468 .top = 0,
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200469 .right = int(staged_mode_->GetRawMode().hdisplay),
470 .bottom = int(staged_mode_->GetRawMode().vdisplay)});
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200471
472 configs_.active_config_id = staged_mode_config_id_;
473
474 a_args.display_mode = *staged_mode_;
475 if (!a_args.test_only) {
476 mode_update_commited_ = true;
477 }
478 }
479
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200480 // order the layers by z-order
481 bool use_client_layer = false;
482 uint32_t client_z_order = UINT32_MAX;
483 std::map<uint32_t, HwcLayer *> z_map;
484 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
485 switch (l.second.GetValidatedType()) {
486 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300487 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488 break;
489 case HWC2::Composition::Client:
490 // Place it at the z_order of the lowest client layer
491 use_client_layer = true;
492 client_z_order = std::min(client_z_order, l.second.GetZOrder());
493 break;
494 default:
495 continue;
496 }
497 }
498 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300499 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500
501 if (z_map.empty())
502 return HWC2::Error::BadLayer;
503
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200504 std::vector<LayerData> composition_layers;
505
506 /* Import & populate */
507 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200508 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200509 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200510
511 // now that they're ordered by z, add them to the composition
512 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200513 if (!l.second->IsLayerUsableAsDevice()) {
514 /* This will be normally triggered on validation of the first frame
515 * containing CLIENT layer. At this moment client buffer is not yet
516 * provided by the CLIENT.
517 * This may be triggered once in HwcLayer lifecycle in case FB can't be
518 * imported. For example when non-contiguous buffer is imported into
519 * contiguous-only DRM/KMS driver.
520 */
521 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200522 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200523 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200524 }
525
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200526 /* Store plan to ensure shared planes won't be stolen by other display
527 * in between of ValidateDisplay() and PresentDisplay() calls
528 */
529 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
530 std::move(composition_layers));
531 if (!current_plan_) {
532 if (!a_args.test_only) {
533 ALOGE("Failed to create DrmKmsPlan");
534 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535 return HWC2::Error::BadConfig;
536 }
537
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200538 a_args.composition = current_plan_;
539
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300540 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200541
542 if (ret) {
543 if (!a_args.test_only)
544 ALOGE("Failed to apply the frame composition ret=%d", ret);
545 return HWC2::Error::BadParameter;
546 }
547
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200548 if (mode_update_commited_) {
549 staged_mode_.reset();
550 vsync_tracking_en_ = false;
551 if (last_vsync_ts_ != 0) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200552 hwc2_->SendVsyncPeriodTimingChangedEventToClient(handle_,
553 last_vsync_ts_ +
554 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200555 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200556 }
557
558 return HWC2::Error::None;
559}
560
561/* Find API details at:
562 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
563 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300564HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200565 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300566 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200567 return HWC2::Error::None;
568 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200569 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200570
571 ++total_stats_.total_frames_;
572
573 AtomicCommitArgs a_args{};
574 ret = CreateComposition(a_args);
575
576 if (ret != HWC2::Error::None)
577 ++total_stats_.failed_kms_present_;
578
579 if (ret == HWC2::Error::BadLayer) {
580 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300581 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200582 return HWC2::Error::None;
583 }
584 if (ret != HWC2::Error::None)
585 return ret;
586
Roman Stratiienko76892782023-01-16 17:15:53 +0200587 this->present_fence_ = a_args.out_fence;
588 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200589
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200590 // Reset the color matrix so we don't apply it over and over again.
591 color_matrix_ = {};
592
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200593 ++frame_no_;
594 return HWC2::Error::None;
595}
596
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200597HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
598 int64_t change_time) {
599 if (configs_.hwc_configs.count(config) == 0) {
600 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200601 return HWC2::Error::BadConfig;
602 }
603
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200604 staged_mode_ = configs_.hwc_configs[config].mode;
605 staged_mode_change_time_ = change_time;
606 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200607
608 return HWC2::Error::None;
609}
610
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200611HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
612 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
613}
614
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200615/* Find API details at:
616 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
617 */
618HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
619 int32_t acquire_fence,
620 int32_t dataspace,
621 hwc_region_t /*damage*/) {
622 client_layer_.SetLayerBuffer(target, acquire_fence);
623 client_layer_.SetLayerDataspace(dataspace);
624
625 /*
626 * target can be nullptr, this does mean the Composer Service is calling
627 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
628 * 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
629 */
630 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300631 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200632 return HWC2::Error::None;
633 }
634
Roman Stratiienko5070d512022-05-30 13:41:20 +0300635 if (IsInHeadlessMode()) {
636 return HWC2::Error::None;
637 }
638
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200639 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200640 if (!client_layer_.IsLayerUsableAsDevice()) {
641 ALOGE("Client layer must be always usable by DRM/KMS");
642 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200643 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200644
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200645 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300646 if (!bi) {
647 ALOGE("%s: Invalid state", __func__);
648 return HWC2::Error::BadLayer;
649 }
650
651 auto source_crop = (hwc_frect_t){.left = 0.0F,
652 .top = 0.0F,
653 .right = static_cast<float>(bi->width),
654 .bottom = static_cast<float>(bi->height)};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200655 client_layer_.SetLayerSourceCrop(source_crop);
656
657 return HWC2::Error::None;
658}
659
660HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
661 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
662 return HWC2::Error::BadParameter;
663
664 if (mode != HAL_COLOR_MODE_NATIVE)
665 return HWC2::Error::Unsupported;
666
667 color_mode_ = mode;
668 return HWC2::Error::None;
669}
670
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200671#include <xf86drmMode.h>
672
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200673HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
674 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
675 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
676 return HWC2::Error::BadParameter;
677
678 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
679 return HWC2::Error::BadParameter;
680
681 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200682
683 if (!GetPipe().crtc->Get()->GetCtmProperty())
684 return HWC2::Error::None;
685
686 switch (color_transform_hint_) {
687 case HAL_COLOR_TRANSFORM_IDENTITY:
688 SetColorMarixToIdentity();
689 break;
690 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
691 color_matrix_ = std::make_shared<drm_color_ctm>();
692 /* DRM expects a 3x3 matrix, but the HAL provides a 4x4 matrix. */
693 for (int i = 0; i < kCtmCols; i++) {
694 for (int j = 0; j < kCtmRows; j++) {
695 constexpr int kInCtmRows = 4;
696 /* HAL matrix type is float, but DRM expects a s31.32 fix point */
Yongqin Liu152bc622023-01-29 00:48:10 +0800697 auto value = uint64_t(matrix[i * kInCtmRows + j] * float(1ULL << 32));
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200698 color_matrix_->matrix[i * kCtmRows + j] = value;
699 }
700 }
701 break;
702 default:
703 return HWC2::Error::Unsupported;
704 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200705
706 return HWC2::Error::None;
707}
708
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200709bool HwcDisplay::CtmByGpu() {
710 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
711 return false;
712
713 if (GetPipe().crtc->Get()->GetCtmProperty())
714 return false;
715
716 if (GetHwc2()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
717 return false;
718
719 return true;
720}
721
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200722HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
723 int32_t /*release_fence*/) {
724 // TODO(nobody): Need virtual display support
725 return HWC2::Error::Unsupported;
726}
727
728HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
729 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300730
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200731 AtomicCommitArgs a_args{};
732
733 switch (mode) {
734 case HWC2::PowerMode::Off:
735 a_args.active = false;
736 break;
737 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300738 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200739 break;
740 case HWC2::PowerMode::Doze:
741 case HWC2::PowerMode::DozeSuspend:
742 return HWC2::Error::Unsupported;
743 default:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300744 ALOGE("Incorrect power mode value (%d)\n", mode);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200745 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300746 }
747
748 if (IsInHeadlessMode()) {
749 return HWC2::Error::None;
750 }
751
Jia Ren80566fe2022-11-17 17:26:00 +0800752 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +0300753 /*
754 * Setting the display to active before we have a composition
755 * can break some drivers, so skip setting a_args.active to
756 * true, as the next composition frame will implicitly activate
757 * the display
758 */
759 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
760 ? HWC2::Error::None
761 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200762 };
763
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300764 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200765 if (err) {
766 ALOGE("Failed to apply the dpms composition err=%d", err);
767 return HWC2::Error::BadParameter;
768 }
769 return HWC2::Error::None;
770}
771
772HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienko099c3112022-01-20 11:50:54 +0200773 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
774 if (vsync_event_en_) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200775 vsync_worker_->VSyncControl(true);
Roman Stratiienko099c3112022-01-20 11:50:54 +0200776 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200777 return HWC2::Error::None;
778}
779
780HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
781 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200782 if (IsInHeadlessMode()) {
783 *num_types = *num_requests = 0;
784 return HWC2::Error::None;
785 }
Roman Stratiienkodd214942022-05-03 18:24:49 +0300786
787 /* In current drm_hwc design in case previous frame layer was not validated as
788 * a CLIENT, it is used by display controller (Front buffer). We have to store
789 * this state to provide the CLIENT with the release fences for such buffers.
790 */
791 for (auto &l : layers_) {
792 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
793 HWC2::Composition::Client);
794 }
795
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200796 return backend_->ValidateDisplay(this, num_types, num_requests);
797}
798
799std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
800 std::vector<HwcLayer *> ordered_layers;
801 ordered_layers.reserve(layers_.size());
802
803 for (auto &[handle, layer] : layers_) {
804 ordered_layers.emplace_back(&layer);
805 }
806
807 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
808 [](const HwcLayer *lhs, const HwcLayer *rhs) {
809 return lhs->GetZOrder() < rhs->GetZOrder();
810 });
811
812 return ordered_layers;
813}
814
Roman Stratiienko099c3112022-01-20 11:50:54 +0200815HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
816 uint32_t *outVsyncPeriod /* ns */) {
817 return GetDisplayAttribute(configs_.active_config_id,
818 HWC2_ATTRIBUTE_VSYNC_PERIOD,
819 (int32_t *)(outVsyncPeriod));
820}
821
Roman Stratiienko6b405052022-12-10 19:09:10 +0200822#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200823HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200824 if (IsInHeadlessMode()) {
825 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
826 return HWC2::Error::None;
827 }
828 /* Primary display should be always internal,
829 * otherwise SF will be unhappy and will crash
830 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200831 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200832 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200833 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200834 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
835 else
836 return HWC2::Error::BadConfig;
837
838 return HWC2::Error::None;
839}
840
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200841HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200842 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200843 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
844 hwc_vsync_period_change_timeline_t *outTimeline) {
845 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
846 return HWC2::Error::BadParameter;
847 }
848
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200849 uint32_t current_vsync_period{};
850 GetDisplayVsyncPeriod(&current_vsync_period);
851
852 if (vsyncPeriodChangeConstraints->seamlessRequired) {
853 return HWC2::Error::SeamlessNotAllowed;
854 }
855
856 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
857 ->desiredTimeNanos -
858 current_vsync_period;
859 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
860 if (ret != HWC2::Error::None) {
861 return ret;
862 }
863
864 outTimeline->refreshRequired = true;
865 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
866 ->desiredTimeNanos;
867
868 last_vsync_ts_ = 0;
869 vsync_tracking_en_ = true;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200870 vsync_worker_->VSyncControl(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200871
872 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200873}
874
875HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
876 return HWC2::Error::Unsupported;
877}
878
879HWC2::Error HwcDisplay::GetSupportedContentTypes(
880 uint32_t *outNumSupportedContentTypes,
881 const uint32_t *outSupportedContentTypes) {
882 if (outSupportedContentTypes == nullptr)
883 *outNumSupportedContentTypes = 0;
884
885 return HWC2::Error::None;
886}
887
888HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
889 if (contentType != HWC2_CONTENT_TYPE_NONE)
890 return HWC2::Error::Unsupported;
891
892 /* TODO: Map to the DRM Connector property:
893 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
894 */
895
896 return HWC2::Error::None;
897}
898#endif
899
Roman Stratiienko6b405052022-12-10 19:09:10 +0200900#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200901HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
902 uint32_t *outDataSize,
903 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200904 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300905 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200906 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300907
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200908 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200909 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300910 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200911 }
912
Roman Stratiienkof87d8082022-05-06 11:33:56 +0300913 *outPort = handle_; /* TDOD(nobody): What should be here? */
914
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200915 if (outData) {
916 *outDataSize = std::min(*outDataSize, blob->length);
917 memcpy(outData, blob->data, *outDataSize);
918 } else {
919 *outDataSize = blob->length;
920 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200921
922 return HWC2::Error::None;
923}
924
925HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200926 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927 if (outNumCapabilities == nullptr) {
928 return HWC2::Error::BadParameter;
929 }
930
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200931 bool skip_ctm = false;
932
933 // Skip client CTM if user requested DRM_OR_IGNORE
934 if (GetHwc2()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
935 skip_ctm = true;
936
937 // Skip client CTM if DRM can handle it
938 if (!skip_ctm && !IsInHeadlessMode() &&
939 GetPipe().crtc->Get()->GetCtmProperty())
940 skip_ctm = true;
941
942 if (!skip_ctm) {
943 *outNumCapabilities = 0;
944 return HWC2::Error::None;
945 }
946
947 *outNumCapabilities = 1;
948 if (outCapabilities) {
949 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
950 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200951
952 return HWC2::Error::None;
953}
954
955HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
956 *supported = false;
957 return HWC2::Error::None;
958}
959
960HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
961 return HWC2::Error::Unsupported;
962}
963
Roman Stratiienko6b405052022-12-10 19:09:10 +0200964#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200965
Roman Stratiienko6b405052022-12-10 19:09:10 +0200966#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200967
968HWC2::Error HwcDisplay::GetRenderIntents(
969 int32_t mode, uint32_t *outNumIntents,
970 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
971 if (mode != HAL_COLOR_MODE_NATIVE) {
972 return HWC2::Error::BadParameter;
973 }
974
975 if (outIntents == nullptr) {
976 *outNumIntents = 1;
977 return HWC2::Error::None;
978 }
979 *outNumIntents = 1;
980 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
981 return HWC2::Error::None;
982}
983
984HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
985 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
986 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
987 return HWC2::Error::BadParameter;
988
989 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
990 return HWC2::Error::BadParameter;
991
992 if (mode != HAL_COLOR_MODE_NATIVE)
993 return HWC2::Error::Unsupported;
994
995 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
996 return HWC2::Error::Unsupported;
997
998 color_mode_ = mode;
999 return HWC2::Error::None;
1000}
1001
Roman Stratiienko6b405052022-12-10 19:09:10 +02001002#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001003
1004const Backend *HwcDisplay::backend() const {
1005 return backend_.get();
1006}
1007
1008void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1009 backend_ = std::move(backend);
1010}
1011
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001012} // namespace android