blob: 4068e71cdb649ae6b6c8deb22b7bbf992128fc95 [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
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "HwcDisplay.h"
21
Manasi Navare3f0c01a2024-10-04 18:01:55 +000022#include <cinttypes>
23
Drew Davenport76c17a82025-01-15 15:02:45 -070024#include <xf86drmMode.h>
25
Drew Davenport97b5abc2024-11-07 10:43:54 -070026#include <hardware/gralloc.h>
27#include <ui/GraphicBufferAllocator.h>
28#include <ui/GraphicBufferMapper.h>
29#include <ui/PixelFormat.h>
30
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020031#include "backend/Backend.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020032#include "backend/BackendManager.h"
33#include "bufferinfo/BufferInfoGetter.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060034#include "compositor/DisplayInfo.h"
35#include "drm/DrmConnector.h"
36#include "drm/DrmDisplayPipeline.h"
Drew Davenport93443182023-12-14 09:25:45 +000037#include "drm/DrmHwc.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020038#include "utils/log.h"
39#include "utils/properties.h"
40
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060041using ::android::DrmDisplayPipeline;
42
Roman Stratiienko3627beb2022-01-04 16:02:55 +020043namespace android {
44
Drew Davenport97b5abc2024-11-07 10:43:54 -070045namespace {
Drew Davenport76c17a82025-01-15 15:02:45 -070046
47constexpr int kCtmRows = 3;
48constexpr int kCtmCols = 3;
49
50constexpr std::array<float, 16> kIdentityMatrix = {
51 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F,
52 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F,
53};
54
55uint64_t To3132FixPt(float in) {
56 constexpr uint64_t kSignMask = (1ULL << 63);
57 constexpr uint64_t kValueMask = ~(1ULL << 63);
58 constexpr auto kValueScale = static_cast<float>(1ULL << 32);
59 if (in < 0)
60 return (static_cast<uint64_t>(-in * kValueScale) & kValueMask) | kSignMask;
61 return static_cast<uint64_t>(in * kValueScale) & kValueMask;
62}
63
64auto ToColorTransform(const std::array<float, 16> &color_transform_matrix) {
65 /* HAL provides a 4x4 float type matrix:
66 * | 0 1 2 3|
67 * | 4 5 6 7|
68 * | 8 9 10 11|
69 * |12 13 14 15|
70 *
71 * R_out = R*0 + G*4 + B*8 + 12
72 * G_out = R*1 + G*5 + B*9 + 13
73 * B_out = R*2 + G*6 + B*10 + 14
74 *
75 * DRM expects a 3x3 s31.32 fixed point matrix:
76 * out matrix in
77 * |R| |0 1 2| |R|
78 * |G| = |3 4 5| x |G|
79 * |B| |6 7 8| |B|
80 *
81 * R_out = R*0 + G*1 + B*2
82 * G_out = R*3 + G*4 + B*5
83 * B_out = R*6 + G*7 + B*8
84 */
85 auto color_matrix = std::make_shared<drm_color_ctm>();
86 for (int i = 0; i < kCtmCols; i++) {
87 for (int j = 0; j < kCtmRows; j++) {
88 constexpr int kInCtmRows = 4;
89 color_matrix->matrix[i * kCtmRows + j] = To3132FixPt(
90 color_transform_matrix[j * kInCtmRows + i]);
91 }
92 }
93 return color_matrix;
94}
95
Drew Davenport97b5abc2024-11-07 10:43:54 -070096// Allocate a black buffer that can be used for an initial modeset when there.
97// is no appropriate client buffer available to be used.
98// Caller must free the returned buffer with GraphicBufferAllocator::free.
99auto GetModesetBuffer(uint32_t width, uint32_t height) -> buffer_handle_t {
100 constexpr PixelFormat format = PIXEL_FORMAT_RGBA_8888;
101 constexpr uint64_t usage = GRALLOC_USAGE_SW_READ_OFTEN |
102 GRALLOC_USAGE_SW_WRITE_OFTEN |
103 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB;
104
105 constexpr uint32_t layer_count = 1;
106 const std::string name = "drm-hwcomposer";
107
108 buffer_handle_t handle = nullptr;
109 uint32_t stride = 0;
110 status_t status = GraphicBufferAllocator::get().allocate(width, height,
111 format, layer_count,
112 usage, &handle,
113 &stride, name);
114 if (status != OK) {
115 ALOGE("Failed to allocate modeset buffer.");
116 return nullptr;
117 }
118
119 void *data = nullptr;
120 Rect bounds = {0, 0, static_cast<int32_t>(width),
121 static_cast<int32_t>(height)};
122 status = GraphicBufferMapper::get().lock(handle, usage, bounds, &data);
123 if (status != OK) {
124 ALOGE("Failed to map modeset buffer.");
125 GraphicBufferAllocator::get().free(handle);
126 return nullptr;
127 }
128
129 // Cast one of the multiplicands to ensure that the multiplication happens
130 // in a wider type (size_t).
131 const size_t buffer_size = static_cast<size_t>(height) * stride *
132 bytesPerPixel(format);
133 memset(data, 0, buffer_size);
134 status = GraphicBufferMapper::get().unlock(handle);
135 ALOGW_IF(status != OK, "Failed to unmap buffer.");
136 return handle;
137}
138
139auto GetModesetLayerProperties(buffer_handle_t buffer, uint32_t width,
140 uint32_t height) -> HwcLayer::LayerProperties {
141 HwcLayer::LayerProperties properties;
142 properties.buffer = {.buffer_handle = buffer, .acquire_fence = {}};
143 properties.display_frame = {
144 .left = 0,
145 .top = 0,
146 .right = int(width),
147 .bottom = int(height),
148 };
149 properties.source_crop = (hwc_frect_t){
150 .left = 0.0F,
151 .top = 0.0F,
152 .right = static_cast<float>(width),
153 .bottom = static_cast<float>(height),
154 };
155 properties.blend_mode = BufferBlendMode::kNone;
156 return properties;
157}
158} // namespace
159
Roman Stratiienko44b95772025-01-22 18:03:36 +0200160static BufferColorSpace Hwc2ToColorSpace(int32_t dataspace) {
161 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
162 case HAL_DATASPACE_STANDARD_BT709:
163 return BufferColorSpace::kItuRec709;
164 case HAL_DATASPACE_STANDARD_BT601_625:
165 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
166 case HAL_DATASPACE_STANDARD_BT601_525:
167 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
168 return BufferColorSpace::kItuRec601;
169 case HAL_DATASPACE_STANDARD_BT2020:
170 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
171 return BufferColorSpace::kItuRec2020;
172 default:
173 return BufferColorSpace::kUndefined;
174 }
175}
176
177static BufferSampleRange Hwc2ToSampleRange(int32_t dataspace) {
178 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
179 case HAL_DATASPACE_RANGE_FULL:
180 return BufferSampleRange::kFullRange;
181 case HAL_DATASPACE_RANGE_LIMITED:
182 return BufferSampleRange::kLimitedRange;
183 default:
184 return BufferSampleRange::kUndefined;
185 }
186}
187
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200188std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
189 if (delta.total_pixops_ == 0)
190 return "No stats yet";
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300191 auto ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200192
193 std::stringstream ss;
194 ss << " Total frames count: " << delta.total_frames_ << "\n"
195 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
196 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
197 << ((delta.failed_kms_present_ > 0)
198 ? " !!! Internal failure, FIX it please\n"
199 : "")
200 << " Flattened frames: " << delta.frames_flattened_ << "\n"
201 << " Pixel operations (free units)"
202 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
203 << "]\n"
204 << " Composition efficiency: " << ratio;
205
206 return ss.str();
207}
208
209std::string HwcDisplay::Dump() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300210 auto connector_name = IsInHeadlessMode()
211 ? std::string("NULL-DISPLAY")
212 : GetPipe().connector->Get()->GetName();
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200213
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200214 std::stringstream ss;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200215 ss << "- Display on: " << connector_name << "\n"
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216 << "Statistics since system boot:\n"
217 << DumpDelta(total_stats_) << "\n\n"
218 << "Statistics since last dumpsys request:\n"
219 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
220
221 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
222 return ss.str();
223}
224
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200225HwcDisplay::HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type,
Drew Davenport93443182023-12-14 09:25:45 +0000226 DrmHwc *hwc)
227 : hwc_(hwc), handle_(handle), type_(type), client_layer_(this) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300228 if (type_ == HWC2::DisplayType::Virtual) {
229 writeback_layer_ = std::make_unique<HwcLayer>(this);
230 }
231}
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200232
Drew Davenport76c17a82025-01-15 15:02:45 -0700233void HwcDisplay::SetColorTransformMatrix(
234 const std::array<float, 16> &color_transform_matrix) {
235 auto almost_equal = [](auto a, auto b) {
236 const float epsilon = 0.001F;
237 return std::abs(a - b) < epsilon;
238 };
239 const bool is_identity = std::equal(color_transform_matrix.begin(),
240 color_transform_matrix.end(),
241 kIdentityMatrix.begin(), almost_equal);
242 color_transform_hint_ = is_identity ? HAL_COLOR_TRANSFORM_IDENTITY
243 : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
244 if (color_transform_hint_ == is_identity) {
245 SetColorMatrixToIdentity();
246 } else {
247 color_matrix_ = ToColorTransform(color_transform_matrix);
248 }
249}
250
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400251void HwcDisplay::SetColorMatrixToIdentity() {
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200252 color_matrix_ = std::make_shared<drm_color_ctm>();
253 for (int i = 0; i < kCtmCols; i++) {
254 for (int j = 0; j < kCtmRows; j++) {
Yongqin Liu152bc622023-01-29 00:48:10 +0800255 constexpr uint64_t kOne = (1ULL << 32); /* 1.0 in s31.32 format */
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200256 color_matrix_->matrix[i * kCtmRows + j] = (i == j) ? kOne : 0;
257 }
258 }
259
260 color_transform_hint_ = HAL_COLOR_TRANSFORM_IDENTITY;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200261}
262
Normunds Rieksts545096d2024-03-11 16:37:45 +0000263HwcDisplay::~HwcDisplay() {
264 Deinit();
265};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200266
Drew Davenportfe70c802024-11-07 13:02:21 -0700267auto HwcDisplay::GetConfig(hwc2_config_t config_id) const
268 -> const HwcDisplayConfig * {
269 auto config_iter = configs_.hwc_configs.find(config_id);
Drew Davenport9799ab82024-10-23 10:15:45 -0600270 if (config_iter == configs_.hwc_configs.end()) {
271 return nullptr;
272 }
273 return &config_iter->second;
274}
275
Drew Davenportfe70c802024-11-07 13:02:21 -0700276auto HwcDisplay::GetCurrentConfig() const -> const HwcDisplayConfig * {
277 return GetConfig(configs_.active_config_id);
278}
279
Drew Davenport8998f8b2024-10-24 10:15:12 -0600280auto HwcDisplay::GetLastRequestedConfig() const -> const HwcDisplayConfig * {
Drew Davenportfe70c802024-11-07 13:02:21 -0700281 return GetConfig(staged_mode_config_id_.value_or(configs_.active_config_id));
Drew Davenport85be25d2024-10-23 10:26:34 -0600282}
283
Drew Davenport97b5abc2024-11-07 10:43:54 -0700284HwcDisplay::ConfigError HwcDisplay::SetConfig(hwc2_config_t config) {
285 const HwcDisplayConfig *new_config = GetConfig(config);
286 if (new_config == nullptr) {
287 ALOGE("Could not find active mode for %u", config);
288 return ConfigError::kBadConfig;
289 }
290
291 const HwcDisplayConfig *current_config = GetCurrentConfig();
292
293 const uint32_t width = new_config->mode.GetRawMode().hdisplay;
Drew Davenport8cfa7ff2024-12-06 13:42:04 -0700294 const uint32_t height = new_config->mode.GetRawMode().vdisplay;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700295
296 std::optional<LayerData> modeset_layer_data;
297 // If a client layer has already been provided, and its size matches the
298 // new config, use it for the modeset.
299 if (client_layer_.IsLayerUsableAsDevice() && current_config &&
300 current_config->mode.GetRawMode().hdisplay == width &&
301 current_config->mode.GetRawMode().vdisplay == height) {
302 ALOGV("Use existing client_layer for blocking config.");
303 modeset_layer_data = client_layer_.GetLayerData();
304 } else {
305 ALOGV("Allocate modeset buffer.");
306 buffer_handle_t modeset_buffer = GetModesetBuffer(width, height);
307 if (modeset_buffer != nullptr) {
308 auto modeset_layer = std::make_unique<HwcLayer>(this);
309 modeset_layer->SetLayerProperties(
310 GetModesetLayerProperties(modeset_buffer, width, height));
311 modeset_layer->PopulateLayerData();
312 modeset_layer_data = modeset_layer->GetLayerData();
313 GraphicBufferAllocator::get().free(modeset_buffer);
314 }
315 }
316
317 ALOGV("Create modeset commit.");
318 // Create atomic commit args for a blocking modeset. There's no need to do a
319 // separate test commit, since the commit does a test anyways.
320 AtomicCommitArgs commit_args = CreateModesetCommit(new_config,
321 modeset_layer_data);
322 commit_args.blocking = true;
323 int ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(commit_args);
324
325 if (ret) {
326 ALOGE("Blocking config failed: %d", ret);
327 return HwcDisplay::ConfigError::kBadConfig;
328 }
329
330 ALOGV("Blocking config succeeded.");
331 configs_.active_config_id = config;
Drew Davenport53da3712024-12-04 13:31:07 -0700332 staged_mode_config_id_.reset();
Drew Davenport59833182024-12-13 10:02:15 -0700333 vsync_worker_->SetVsyncPeriodNs(new_config->mode.GetVSyncPeriodNs());
334 // set new vsync period
Drew Davenport97b5abc2024-11-07 10:43:54 -0700335 return ConfigError::kNone;
336}
337
Drew Davenport8998f8b2024-10-24 10:15:12 -0600338auto HwcDisplay::QueueConfig(hwc2_config_t config, int64_t desired_time,
339 bool seamless, QueuedConfigTiming *out_timing)
340 -> ConfigError {
341 if (configs_.hwc_configs.count(config) == 0) {
342 ALOGE("Could not find active mode for %u", config);
343 return ConfigError::kBadConfig;
344 }
345
346 // TODO: Add support for seamless configuration changes.
347 if (seamless) {
348 return ConfigError::kSeamlessNotAllowed;
349 }
350
351 // Request a refresh from the client one vsync period before the desired
352 // time, or simply at the desired time if there is no active configuration.
353 const HwcDisplayConfig *current_config = GetCurrentConfig();
354 out_timing->refresh_time_ns = desired_time -
355 (current_config
356 ? current_config->mode.GetVSyncPeriodNs()
357 : 0);
358 out_timing->new_vsync_time_ns = desired_time;
359
360 // Queue the config change timing to be consistent with the requested
361 // refresh time.
Drew Davenport8998f8b2024-10-24 10:15:12 -0600362 staged_mode_change_time_ = out_timing->refresh_time_ns;
363 staged_mode_config_id_ = config;
364
365 // Enable vsync events until the mode has been applied.
Drew Davenport33121b72024-12-13 14:59:35 -0700366 vsync_worker_->SetVsyncTimestampTracking(true);
Drew Davenport8998f8b2024-10-24 10:15:12 -0600367
368 return ConfigError::kNone;
369}
370
Roman Stratiienko63762a92023-09-18 22:33:45 +0300371void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200372 Deinit();
373
Roman Stratiienko63762a92023-09-18 22:33:45 +0300374 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200375
Roman Stratiienko63762a92023-09-18 22:33:45 +0300376 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200377 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000378 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200379 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000380 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200381 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200382}
383
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200384void HwcDisplay::Deinit() {
385 if (pipeline_ != nullptr) {
386 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200387 a_args.composition = std::make_shared<DrmKmsPlan>();
388 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300389 a_args.composition = {};
390 a_args.active = false;
391 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200392
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200393 current_plan_.reset();
394 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200395 if (flatcon_) {
396 flatcon_->StopThread();
397 flatcon_.reset();
398 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200399 }
400
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200401 if (vsync_worker_) {
402 vsync_worker_->StopThread();
403 vsync_worker_ = {};
404 }
405
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200406 SetClientTarget(nullptr, -1, 0, {});
407}
408
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200409HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200410 ChosePreferredConfig();
411
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300412 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700413 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300414 if (!vsync_worker_) {
415 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
416 return HWC2::Error::BadDisplay;
417 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200418 }
419
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200420 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200421 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200422 if (ret) {
423 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
424 return HWC2::Error::BadDisplay;
425 }
Drew Davenport93443182023-12-14 09:25:45 +0000426 auto flatcbk = (struct FlatConCallbacks){
427 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200428 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200429 }
430
Roman Stratiienko44b95772025-01-22 18:03:36 +0200431 HwcLayer::LayerProperties lp;
432 lp.blend_mode = BufferBlendMode::kPreMult;
433 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200434
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400435 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200436
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200437 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200438}
439
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600440std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
441 if (IsInHeadlessMode()) {
442 // The pipeline can be nullptr in headless mode, so return the default
443 // "normal" mode.
444 return PanelOrientation::kModePanelOrientationNormal;
445 }
446
447 DrmDisplayPipeline &pipeline = GetPipe();
448 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
449 ALOGW(
450 "No display pipeline present to query the panel orientation property.");
451 return {};
452 }
453
454 return pipeline.connector->Get()->GetPanelOrientation();
455}
456
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200457HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200458 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300459 if (type_ == HWC2::DisplayType::Virtual) {
460 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
461 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200462 err = configs_.Update(*pipeline_->connector->Get());
463 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300464 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200465 }
466 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200467 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200468 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200469
Roman Stratiienko0137f862022-01-04 18:27:40 +0200470 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200471}
472
473HWC2::Error HwcDisplay::AcceptDisplayChanges() {
474 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
475 l.second.AcceptTypeChange();
476 return HWC2::Error::None;
477}
478
479HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200480 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200481 *layer = static_cast<hwc2_layer_t>(layer_idx_);
482 ++layer_idx_;
483 return HWC2::Error::None;
484}
485
486HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200487 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200488 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200489 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490
491 layers_.erase(layer);
492 return HWC2::Error::None;
493}
494
495HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700496 // If a config has been queued, it is considered the "active" config.
497 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
498 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200499 return HWC2::Error::BadConfig;
500
Drew Davenportfe70c802024-11-07 13:02:21 -0700501 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502 return HWC2::Error::None;
503}
504
505HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
506 hwc2_layer_t *layers,
507 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200508 if (IsInHeadlessMode()) {
509 *num_elements = 0;
510 return HWC2::Error::None;
511 }
512
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200513 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300514 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200515 if (l.second.IsTypeChanged()) {
516 if (layers && num_changes < *num_elements)
517 layers[num_changes] = l.first;
518 if (types && num_changes < *num_elements)
519 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
520 ++num_changes;
521 }
522 }
523 if (!layers && !types)
524 *num_elements = num_changes;
525 return HWC2::Error::None;
526}
527
528HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
529 int32_t /*format*/,
530 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200531 if (IsInHeadlessMode()) {
532 return HWC2::Error::None;
533 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200534
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300535 auto min = pipeline_->device->GetMinResolution();
536 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200537
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200538 if (width < min.first || height < min.second)
539 return HWC2::Error::Unsupported;
540
541 if (width > max.first || height > max.second)
542 return HWC2::Error::Unsupported;
543
544 if (dataspace != HAL_DATASPACE_UNKNOWN)
545 return HWC2::Error::Unsupported;
546
547 // TODO(nobody): Validate format can be handled by either GL or planes
548 return HWC2::Error::None;
549}
550
551HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
552 if (!modes)
553 *num_modes = 1;
554
555 if (modes)
556 *modes = HAL_COLOR_MODE_NATIVE;
557
558 return HWC2::Error::None;
559}
560
561HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
562 int32_t attribute_in,
563 int32_t *value) {
564 int conf = static_cast<int>(config);
565
Roman Stratiienko0137f862022-01-04 18:27:40 +0200566 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200567 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200568 return HWC2::Error::BadConfig;
569 }
570
Roman Stratiienko0137f862022-01-04 18:27:40 +0200571 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200572
573 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300574 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200575 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
576 switch (attribute) {
577 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200578 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200579 break;
580 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200581 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200582 break;
583 case HWC2::Attribute::VsyncPeriod:
584 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600585 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200586 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000587 case HWC2::Attribute::DpiY:
588 // ideally this should be vdisplay/mm_heigth, however mm_height
589 // comes from edid parsing and is highly unreliable. Viewing the
590 // rarity of anisotropic displays, falling back to a single value
591 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200592 case HWC2::Attribute::DpiX:
593 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200594 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
595 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200596 : -1;
597 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200598#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200599 case HWC2::Attribute::ConfigGroup:
600 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
601 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200602 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200603 break;
604#endif
605 default:
606 *value = -1;
607 return HWC2::Error::BadConfig;
608 }
609 return HWC2::Error::None;
610}
611
Drew Davenportf7e88332024-09-06 12:54:38 -0600612HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
613 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200614 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200615 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200616 if (hwc_config.second.disabled) {
617 continue;
618 }
619
620 if (configs != nullptr) {
621 if (idx >= *num_configs) {
622 break;
623 }
624 configs[idx] = hwc_config.second.id;
625 }
626
627 idx++;
628 }
629 *num_configs = idx;
630 return HWC2::Error::None;
631}
632
633HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
634 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200635 if (IsInHeadlessMode()) {
636 stream << "null-display";
637 } else {
638 stream << "display-" << GetPipe().connector->Get()->GetId();
639 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300640 auto string = stream.str();
641 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200642 if (!name) {
643 *size = length;
644 return HWC2::Error::None;
645 }
646
647 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
648 strncpy(name, string.c_str(), *size);
649 return HWC2::Error::None;
650}
651
652HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
653 uint32_t *num_elements,
654 hwc2_layer_t * /*layers*/,
655 int32_t * /*layer_requests*/) {
656 // TODO(nobody): I think virtual display should request
657 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
658 *num_elements = 0;
659 return HWC2::Error::None;
660}
661
662HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
663 *type = static_cast<int32_t>(type_);
664 return HWC2::Error::None;
665}
666
667HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
668 *support = 0;
669 return HWC2::Error::None;
670}
671
672HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
673 int32_t * /*types*/,
674 float * /*max_luminance*/,
675 float * /*max_average_luminance*/,
676 float * /*min_luminance*/) {
677 *num_types = 0;
678 return HWC2::Error::None;
679}
680
681/* Find API details at:
682 * 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 +0300683 *
684 * Called after PresentDisplay(), CLIENT is expecting release fence for the
685 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200686 */
687HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
688 hwc2_layer_t *layers,
689 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200690 if (IsInHeadlessMode()) {
691 *num_elements = 0;
692 return HWC2::Error::None;
693 }
694
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200695 uint32_t num_layers = 0;
696
Roman Stratiienkodd214942022-05-03 18:24:49 +0300697 for (auto &l : layers_) {
698 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
699 continue;
700 }
701
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200702 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300703
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200704 if (layers == nullptr || fences == nullptr)
705 continue;
706
707 if (num_layers > *num_elements) {
708 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
709 return HWC2::Error::None;
710 }
711
712 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200713 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200714 }
715 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300716
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200717 return HWC2::Error::None;
718}
719
Drew Davenport97b5abc2024-11-07 10:43:54 -0700720AtomicCommitArgs HwcDisplay::CreateModesetCommit(
721 const HwcDisplayConfig *config,
722 const std::optional<LayerData> &modeset_layer) {
723 AtomicCommitArgs args{};
724
725 args.color_matrix = color_matrix_;
726 args.content_type = content_type_;
727 args.colorspace = colorspace_;
728
729 std::vector<LayerData> composition_layers;
730 if (modeset_layer) {
731 composition_layers.emplace_back(modeset_layer.value());
732 }
733
734 if (composition_layers.empty()) {
735 ALOGW("Attempting to create a modeset commit without a layer.");
736 }
737
738 args.display_mode = config->mode;
739 args.active = true;
740 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
741 std::move(
742 composition_layers));
743 ALOGW_IF(!args.composition, "No composition for blocking modeset");
744
745 return args;
746}
747
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200749 if (IsInHeadlessMode()) {
750 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
751 return HWC2::Error::None;
752 }
753
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200754 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400755 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400756 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200757
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200758 uint32_t prev_vperiod_ns = 0;
759 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200760
Drew Davenportd387c842024-12-16 16:57:24 -0700761 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700762 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200763 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700764 const HwcDisplayConfig *staged_config = GetConfig(
765 staged_mode_config_id_.value());
766 if (staged_config == nullptr) {
767 return HWC2::Error::BadConfig;
768 }
Roman Stratiienko44b95772025-01-22 18:03:36 +0200769 HwcLayer::LayerProperties lp;
770 lp.display_frame = {
771 .left = 0,
772 .top = 0,
773 .right = int(staged_config->mode.GetRawMode().hdisplay),
774 .bottom = int(staged_config->mode.GetRawMode().vdisplay),
775 };
776 client_layer_.SetLayerProperties(lp);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200777
Drew Davenportfe70c802024-11-07 13:02:21 -0700778 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700779 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200780 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700781 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200782 }
783 }
784
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200785 // order the layers by z-order
786 bool use_client_layer = false;
787 uint32_t client_z_order = UINT32_MAX;
788 std::map<uint32_t, HwcLayer *> z_map;
789 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
790 switch (l.second.GetValidatedType()) {
791 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300792 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200793 break;
794 case HWC2::Composition::Client:
795 // Place it at the z_order of the lowest client layer
796 use_client_layer = true;
797 client_z_order = std::min(client_z_order, l.second.GetZOrder());
798 break;
799 default:
800 continue;
801 }
802 }
803 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300804 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200805
806 if (z_map.empty())
807 return HWC2::Error::BadLayer;
808
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200809 std::vector<LayerData> composition_layers;
810
811 /* Import & populate */
812 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200813 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200814 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200815
816 // now that they're ordered by z, add them to the composition
817 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200818 if (!l.second->IsLayerUsableAsDevice()) {
819 /* This will be normally triggered on validation of the first frame
820 * containing CLIENT layer. At this moment client buffer is not yet
821 * provided by the CLIENT.
822 * This may be triggered once in HwcLayer lifecycle in case FB can't be
823 * imported. For example when non-contiguous buffer is imported into
824 * contiguous-only DRM/KMS driver.
825 */
826 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200827 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200828 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200829 }
830
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200831 /* Store plan to ensure shared planes won't be stolen by other display
832 * in between of ValidateDisplay() and PresentDisplay() calls
833 */
834 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
835 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300836
837 if (type_ == HWC2::DisplayType::Virtual) {
838 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
839 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
840 .acquire_fence;
841 }
842
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200843 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700844 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200845 return HWC2::Error::BadConfig;
846 }
847
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200848 a_args.composition = current_plan_;
849
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300850 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200851
852 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700853 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200854 return HWC2::Error::BadParameter;
855 }
856
Drew Davenportd387c842024-12-16 16:57:24 -0700857 if (new_vsync_period_ns) {
858 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700859 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700860
861 vsync_worker_->SetVsyncTimestampTracking(false);
862 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
863 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000864 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700865 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000866 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200867 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200868 }
869
870 return HWC2::Error::None;
871}
872
873/* Find API details at:
874 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
875 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300876HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200877 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300878 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200879 return HWC2::Error::None;
880 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200881 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200882
883 ++total_stats_.total_frames_;
884
885 AtomicCommitArgs a_args{};
886 ret = CreateComposition(a_args);
887
888 if (ret != HWC2::Error::None)
889 ++total_stats_.failed_kms_present_;
890
891 if (ret == HWC2::Error::BadLayer) {
892 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300893 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200894 return HWC2::Error::None;
895 }
896 if (ret != HWC2::Error::None)
897 return ret;
898
Roman Stratiienko76892782023-01-16 17:15:53 +0200899 this->present_fence_ = a_args.out_fence;
900 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200901
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200902 // Reset the color matrix so we don't apply it over and over again.
903 color_matrix_ = {};
904
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200905 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700906
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907 return HWC2::Error::None;
908}
909
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200910HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
911 int64_t change_time) {
912 if (configs_.hwc_configs.count(config) == 0) {
913 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200914 return HWC2::Error::BadConfig;
915 }
916
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200917 staged_mode_change_time_ = change_time;
918 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200919
920 return HWC2::Error::None;
921}
922
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200923HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
924 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
925}
926
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927/* Find API details at:
928 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
929 */
930HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
931 int32_t acquire_fence,
932 int32_t dataspace,
933 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +0200934 HwcLayer::LayerProperties lp;
935 lp.buffer = {.buffer_handle = target,
936 .acquire_fence = MakeSharedFd(acquire_fence)};
937 lp.color_space = Hwc2ToColorSpace(dataspace);
938 lp.sample_range = Hwc2ToSampleRange(dataspace);
939 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200940
941 /*
942 * target can be nullptr, this does mean the Composer Service is calling
943 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
944 * 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
945 */
946 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300947 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200948 return HWC2::Error::None;
949 }
950
Roman Stratiienko5070d512022-05-30 13:41:20 +0300951 if (IsInHeadlessMode()) {
952 return HWC2::Error::None;
953 }
954
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200955 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200956 if (!client_layer_.IsLayerUsableAsDevice()) {
957 ALOGE("Client layer must be always usable by DRM/KMS");
958 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200959 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200960
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200961 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300962 if (!bi) {
963 ALOGE("%s: Invalid state", __func__);
964 return HWC2::Error::BadLayer;
965 }
966
Roman Stratiienko44b95772025-01-22 18:03:36 +0200967 lp = {};
968 lp.source_crop = {.left = 0.0F,
969 .top = 0.0F,
970 .right = float(bi->width),
971 .bottom = float(bi->height)};
972 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200973
974 return HWC2::Error::None;
975}
976
977HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -0400978 /* Maps to the Colorspace DRM connector property:
979 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
980 */
981 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200982 return HWC2::Error::BadParameter;
983
Sasha McIntosh5294f092024-09-18 18:14:54 -0400984 switch (mode) {
985 case HAL_COLOR_MODE_NATIVE:
986 colorspace_ = Colorspace::kDefault;
987 break;
988 case HAL_COLOR_MODE_STANDARD_BT601_625:
989 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
990 case HAL_COLOR_MODE_STANDARD_BT601_525:
991 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
992 // The DP spec does not say whether this is the 525 or the 625 line version.
993 colorspace_ = Colorspace::kBt601Ycc;
994 break;
995 case HAL_COLOR_MODE_STANDARD_BT709:
996 case HAL_COLOR_MODE_SRGB:
997 colorspace_ = Colorspace::kBt709Ycc;
998 break;
999 case HAL_COLOR_MODE_DCI_P3:
1000 case HAL_COLOR_MODE_DISPLAY_P3:
1001 colorspace_ = Colorspace::kDciP3RgbD65;
1002 break;
1003 case HAL_COLOR_MODE_ADOBE_RGB:
1004 default:
1005 return HWC2::Error::Unsupported;
1006 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001007
1008 color_mode_ = mode;
1009 return HWC2::Error::None;
1010}
1011
1012HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1013 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1014 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1015 return HWC2::Error::BadParameter;
1016
1017 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1018 return HWC2::Error::BadParameter;
1019
1020 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001021
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001022 if (IsInHeadlessMode())
1023 return HWC2::Error::None;
1024
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001025 if (!GetPipe().crtc->Get()->GetCtmProperty())
1026 return HWC2::Error::None;
1027
1028 switch (color_transform_hint_) {
1029 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001030 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001031 break;
1032 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001033 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001034 {
1035 for (int i = 12; i < 14; i++) {
1036 if (matrix[i] != 0.F)
1037 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001038 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001039 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1040 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1041 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001042 }
1043 break;
1044 default:
1045 return HWC2::Error::Unsupported;
1046 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001047
1048 return HWC2::Error::None;
1049}
1050
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001051bool HwcDisplay::CtmByGpu() {
1052 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1053 return false;
1054
1055 if (GetPipe().crtc->Get()->GetCtmProperty())
1056 return false;
1057
Drew Davenport93443182023-12-14 09:25:45 +00001058 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001059 return false;
1060
1061 return true;
1062}
1063
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001064HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1065 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001066 HwcLayer::LayerProperties lp;
1067 lp.buffer = {.buffer_handle = buffer,
1068 .acquire_fence = MakeSharedFd(release_fence)};
1069 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001070 writeback_layer_->PopulateLayerData();
1071 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1072 ALOGE("Output layer must be always usable by DRM/KMS");
1073 return HWC2::Error::BadLayer;
1074 }
1075 /* TODO: Check if format is supported by writeback connector */
1076 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001077}
1078
1079HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1080 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001081
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001082 AtomicCommitArgs a_args{};
1083
1084 switch (mode) {
1085 case HWC2::PowerMode::Off:
1086 a_args.active = false;
1087 break;
1088 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001089 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001090 break;
1091 case HWC2::PowerMode::Doze:
1092 case HWC2::PowerMode::DozeSuspend:
1093 return HWC2::Error::Unsupported;
1094 default:
John Stultzffe783c2024-02-14 10:51:27 -08001095 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001096 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001097 }
1098
1099 if (IsInHeadlessMode()) {
1100 return HWC2::Error::None;
1101 }
1102
Jia Ren80566fe2022-11-17 17:26:00 +08001103 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001104 /*
1105 * Setting the display to active before we have a composition
1106 * can break some drivers, so skip setting a_args.active to
1107 * true, as the next composition frame will implicitly activate
1108 * the display
1109 */
1110 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1111 ? HWC2::Error::None
1112 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001113 };
1114
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001115 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001116 if (err) {
1117 ALOGE("Failed to apply the dpms composition err=%d", err);
1118 return HWC2::Error::BadParameter;
1119 }
1120 return HWC2::Error::None;
1121}
1122
1123HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001124 if (type_ == HWC2::DisplayType::Virtual) {
1125 return HWC2::Error::None;
1126 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001127 if (!vsync_worker_) {
1128 return HWC2::Error::NoResources;
1129 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001130
Roman Stratiienko099c3112022-01-20 11:50:54 +02001131 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001132 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001133 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001134 DrmHwc *hwc = hwc_;
1135 hwc2_display_t id = handle_;
1136 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001137 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001138 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1139 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001140 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001141 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001142 return HWC2::Error::None;
1143}
1144
1145HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1146 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001147 if (IsInHeadlessMode()) {
1148 *num_types = *num_requests = 0;
1149 return HWC2::Error::None;
1150 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001151
1152 /* In current drm_hwc design in case previous frame layer was not validated as
1153 * a CLIENT, it is used by display controller (Front buffer). We have to store
1154 * this state to provide the CLIENT with the release fences for such buffers.
1155 */
1156 for (auto &l : layers_) {
1157 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1158 HWC2::Composition::Client);
1159 }
1160
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001161 return backend_->ValidateDisplay(this, num_types, num_requests);
1162}
1163
1164std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1165 std::vector<HwcLayer *> ordered_layers;
1166 ordered_layers.reserve(layers_.size());
1167
1168 for (auto &[handle, layer] : layers_) {
1169 ordered_layers.emplace_back(&layer);
1170 }
1171
1172 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1173 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1174 return lhs->GetZOrder() < rhs->GetZOrder();
1175 });
1176
1177 return ordered_layers;
1178}
1179
Roman Stratiienko099c3112022-01-20 11:50:54 +02001180HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1181 uint32_t *outVsyncPeriod /* ns */) {
1182 return GetDisplayAttribute(configs_.active_config_id,
1183 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1184 (int32_t *)(outVsyncPeriod));
1185}
1186
Roman Stratiienko6b405052022-12-10 19:09:10 +02001187#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001188HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001189 if (IsInHeadlessMode()) {
1190 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1191 return HWC2::Error::None;
1192 }
1193 /* Primary display should be always internal,
1194 * otherwise SF will be unhappy and will crash
1195 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001196 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001197 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001198 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001199 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1200 else
1201 return HWC2::Error::BadConfig;
1202
1203 return HWC2::Error::None;
1204}
1205
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001206HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001207 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001208 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1209 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001210 if (type_ == HWC2::DisplayType::Virtual) {
1211 return HWC2::Error::None;
1212 }
1213
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001214 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1215 return HWC2::Error::BadParameter;
1216 }
1217
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001218 uint32_t current_vsync_period{};
1219 GetDisplayVsyncPeriod(&current_vsync_period);
1220
1221 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1222 return HWC2::Error::SeamlessNotAllowed;
1223 }
1224
1225 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1226 ->desiredTimeNanos -
1227 current_vsync_period;
1228 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1229 if (ret != HWC2::Error::None) {
1230 return ret;
1231 }
1232
1233 outTimeline->refreshRequired = true;
1234 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1235 ->desiredTimeNanos;
1236
Drew Davenport33121b72024-12-13 14:59:35 -07001237 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001238
1239 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001240}
1241
1242HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1243 return HWC2::Error::Unsupported;
1244}
1245
1246HWC2::Error HwcDisplay::GetSupportedContentTypes(
1247 uint32_t *outNumSupportedContentTypes,
1248 const uint32_t *outSupportedContentTypes) {
1249 if (outSupportedContentTypes == nullptr)
1250 *outNumSupportedContentTypes = 0;
1251
1252 return HWC2::Error::None;
1253}
1254
1255HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001256 /* Maps exactly to the content_type DRM connector property:
1257 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001258 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001259 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1260 return HWC2::Error::BadParameter;
1261
1262 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001263
1264 return HWC2::Error::None;
1265}
1266#endif
1267
Roman Stratiienko6b405052022-12-10 19:09:10 +02001268#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001269HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1270 uint32_t *outDataSize,
1271 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001272 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001273 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001274 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001275
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001276 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001277 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001278 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001279 }
1280
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001281 *outPort = handle_; /* TDOD(nobody): What should be here? */
1282
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001283 if (outData) {
1284 *outDataSize = std::min(*outDataSize, blob->length);
1285 memcpy(outData, blob->data, *outDataSize);
1286 } else {
1287 *outDataSize = blob->length;
1288 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001289
1290 return HWC2::Error::None;
1291}
1292
1293HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001294 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001295 if (outNumCapabilities == nullptr) {
1296 return HWC2::Error::BadParameter;
1297 }
1298
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001299 bool skip_ctm = false;
1300
1301 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001302 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001303 skip_ctm = true;
1304
1305 // Skip client CTM if DRM can handle it
1306 if (!skip_ctm && !IsInHeadlessMode() &&
1307 GetPipe().crtc->Get()->GetCtmProperty())
1308 skip_ctm = true;
1309
1310 if (!skip_ctm) {
1311 *outNumCapabilities = 0;
1312 return HWC2::Error::None;
1313 }
1314
1315 *outNumCapabilities = 1;
1316 if (outCapabilities) {
1317 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1318 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001319
1320 return HWC2::Error::None;
1321}
1322
1323HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1324 *supported = false;
1325 return HWC2::Error::None;
1326}
1327
1328HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1329 return HWC2::Error::Unsupported;
1330}
1331
Roman Stratiienko6b405052022-12-10 19:09:10 +02001332#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001333
Roman Stratiienko6b405052022-12-10 19:09:10 +02001334#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001335
1336HWC2::Error HwcDisplay::GetRenderIntents(
1337 int32_t mode, uint32_t *outNumIntents,
1338 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1339 if (mode != HAL_COLOR_MODE_NATIVE) {
1340 return HWC2::Error::BadParameter;
1341 }
1342
1343 if (outIntents == nullptr) {
1344 *outNumIntents = 1;
1345 return HWC2::Error::None;
1346 }
1347 *outNumIntents = 1;
1348 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1349 return HWC2::Error::None;
1350}
1351
1352HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1353 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1354 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1355 return HWC2::Error::BadParameter;
1356
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001357 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1358 return HWC2::Error::Unsupported;
1359
Sasha McIntosh5294f092024-09-18 18:14:54 -04001360 auto err = SetColorMode(mode);
1361 if (err != HWC2::Error::None) return err;
1362
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001363 return HWC2::Error::None;
1364}
1365
Roman Stratiienko6b405052022-12-10 19:09:10 +02001366#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001367
1368const Backend *HwcDisplay::backend() const {
1369 return backend_.get();
1370}
1371
1372void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1373 backend_ = std::move(backend);
1374}
1375
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001376} // namespace android