blob: 9fbd6b92334fde6f38ccda4c61d1b1baa2f325ab [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
Drew Davenport7f356c82025-01-17 16:32:06 -0700371auto HwcDisplay::ValidateStagedComposition() -> std::vector<ChangedLayer> {
372 if (IsInHeadlessMode()) {
373 return {};
374 }
375
376 /* In current drm_hwc design in case previous frame layer was not validated as
377 * a CLIENT, it is used by display controller (Front buffer). We have to store
378 * this state to provide the CLIENT with the release fences for such buffers.
379 */
380 for (auto &l : layers_) {
381 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
382 HWC2::Composition::Client);
383 }
384
385 // ValidateDisplay returns the number of layers that may be changed.
386 uint32_t num_types = 0;
387 uint32_t num_requests = 0;
388 backend_->ValidateDisplay(this, &num_types, &num_requests);
389
390 if (num_types == 0) {
391 return {};
392 }
393
394 // Iterate through the layers to find which layers actually changed.
395 std::vector<ChangedLayer> changed_layers;
396 for (auto &l : layers_) {
397 if (l.second.IsTypeChanged()) {
398 changed_layers.emplace_back(l.first, l.second.GetValidatedType());
399 }
400 }
401 return changed_layers;
402}
403
Roman Stratiienko63762a92023-09-18 22:33:45 +0300404void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200405 Deinit();
406
Roman Stratiienko63762a92023-09-18 22:33:45 +0300407 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200408
Roman Stratiienko63762a92023-09-18 22:33:45 +0300409 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200410 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000411 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200412 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000413 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200414 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200415}
416
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200417void HwcDisplay::Deinit() {
418 if (pipeline_ != nullptr) {
419 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200420 a_args.composition = std::make_shared<DrmKmsPlan>();
421 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300422 a_args.composition = {};
423 a_args.active = false;
424 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200425
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200426 current_plan_.reset();
427 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200428 if (flatcon_) {
429 flatcon_->StopThread();
430 flatcon_.reset();
431 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200432 }
433
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200434 if (vsync_worker_) {
435 vsync_worker_->StopThread();
436 vsync_worker_ = {};
437 }
438
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200439 SetClientTarget(nullptr, -1, 0, {});
440}
441
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200442HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200443 ChosePreferredConfig();
444
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300445 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700446 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300447 if (!vsync_worker_) {
448 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
449 return HWC2::Error::BadDisplay;
450 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200451 }
452
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200453 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200454 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200455 if (ret) {
456 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
457 return HWC2::Error::BadDisplay;
458 }
Drew Davenport93443182023-12-14 09:25:45 +0000459 auto flatcbk = (struct FlatConCallbacks){
460 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200461 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200462 }
463
Roman Stratiienko44b95772025-01-22 18:03:36 +0200464 HwcLayer::LayerProperties lp;
465 lp.blend_mode = BufferBlendMode::kPreMult;
466 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200467
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400468 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200469
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200470 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200471}
472
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600473std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
474 if (IsInHeadlessMode()) {
475 // The pipeline can be nullptr in headless mode, so return the default
476 // "normal" mode.
477 return PanelOrientation::kModePanelOrientationNormal;
478 }
479
480 DrmDisplayPipeline &pipeline = GetPipe();
481 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
482 ALOGW(
483 "No display pipeline present to query the panel orientation property.");
484 return {};
485 }
486
487 return pipeline.connector->Get()->GetPanelOrientation();
488}
489
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200491 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300492 if (type_ == HWC2::DisplayType::Virtual) {
493 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
494 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200495 err = configs_.Update(*pipeline_->connector->Get());
496 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300497 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200498 }
499 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200501 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200502
Roman Stratiienko0137f862022-01-04 18:27:40 +0200503 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200504}
505
506HWC2::Error HwcDisplay::AcceptDisplayChanges() {
507 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
508 l.second.AcceptTypeChange();
509 return HWC2::Error::None;
510}
511
512HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200513 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200514 *layer = static_cast<hwc2_layer_t>(layer_idx_);
515 ++layer_idx_;
516 return HWC2::Error::None;
517}
518
519HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200520 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200521 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200522 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200523
524 layers_.erase(layer);
525 return HWC2::Error::None;
526}
527
528HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700529 // If a config has been queued, it is considered the "active" config.
530 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
531 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200532 return HWC2::Error::BadConfig;
533
Drew Davenportfe70c802024-11-07 13:02:21 -0700534 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200535 return HWC2::Error::None;
536}
537
538HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
539 hwc2_layer_t *layers,
540 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200541 if (IsInHeadlessMode()) {
542 *num_elements = 0;
543 return HWC2::Error::None;
544 }
545
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300547 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200548 if (l.second.IsTypeChanged()) {
549 if (layers && num_changes < *num_elements)
550 layers[num_changes] = l.first;
551 if (types && num_changes < *num_elements)
552 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
553 ++num_changes;
554 }
555 }
556 if (!layers && !types)
557 *num_elements = num_changes;
558 return HWC2::Error::None;
559}
560
561HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
562 int32_t /*format*/,
563 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200564 if (IsInHeadlessMode()) {
565 return HWC2::Error::None;
566 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200567
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300568 auto min = pipeline_->device->GetMinResolution();
569 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200570
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200571 if (width < min.first || height < min.second)
572 return HWC2::Error::Unsupported;
573
574 if (width > max.first || height > max.second)
575 return HWC2::Error::Unsupported;
576
577 if (dataspace != HAL_DATASPACE_UNKNOWN)
578 return HWC2::Error::Unsupported;
579
580 // TODO(nobody): Validate format can be handled by either GL or planes
581 return HWC2::Error::None;
582}
583
584HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
585 if (!modes)
586 *num_modes = 1;
587
588 if (modes)
589 *modes = HAL_COLOR_MODE_NATIVE;
590
591 return HWC2::Error::None;
592}
593
594HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
595 int32_t attribute_in,
596 int32_t *value) {
597 int conf = static_cast<int>(config);
598
Roman Stratiienko0137f862022-01-04 18:27:40 +0200599 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200600 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200601 return HWC2::Error::BadConfig;
602 }
603
Roman Stratiienko0137f862022-01-04 18:27:40 +0200604 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200605
606 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300607 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200608 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
609 switch (attribute) {
610 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200611 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200612 break;
613 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200614 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200615 break;
616 case HWC2::Attribute::VsyncPeriod:
617 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600618 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200619 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000620 case HWC2::Attribute::DpiY:
621 // ideally this should be vdisplay/mm_heigth, however mm_height
622 // comes from edid parsing and is highly unreliable. Viewing the
623 // rarity of anisotropic displays, falling back to a single value
624 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200625 case HWC2::Attribute::DpiX:
626 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200627 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
628 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200629 : -1;
630 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200631#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200632 case HWC2::Attribute::ConfigGroup:
633 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
634 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200635 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200636 break;
637#endif
638 default:
639 *value = -1;
640 return HWC2::Error::BadConfig;
641 }
642 return HWC2::Error::None;
643}
644
Drew Davenportf7e88332024-09-06 12:54:38 -0600645HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
646 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200647 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200648 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200649 if (hwc_config.second.disabled) {
650 continue;
651 }
652
653 if (configs != nullptr) {
654 if (idx >= *num_configs) {
655 break;
656 }
657 configs[idx] = hwc_config.second.id;
658 }
659
660 idx++;
661 }
662 *num_configs = idx;
663 return HWC2::Error::None;
664}
665
666HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
667 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200668 if (IsInHeadlessMode()) {
669 stream << "null-display";
670 } else {
671 stream << "display-" << GetPipe().connector->Get()->GetId();
672 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300673 auto string = stream.str();
674 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200675 if (!name) {
676 *size = length;
677 return HWC2::Error::None;
678 }
679
680 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
681 strncpy(name, string.c_str(), *size);
682 return HWC2::Error::None;
683}
684
685HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
686 uint32_t *num_elements,
687 hwc2_layer_t * /*layers*/,
688 int32_t * /*layer_requests*/) {
689 // TODO(nobody): I think virtual display should request
690 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
691 *num_elements = 0;
692 return HWC2::Error::None;
693}
694
695HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
696 *type = static_cast<int32_t>(type_);
697 return HWC2::Error::None;
698}
699
700HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
701 *support = 0;
702 return HWC2::Error::None;
703}
704
705HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
706 int32_t * /*types*/,
707 float * /*max_luminance*/,
708 float * /*max_average_luminance*/,
709 float * /*min_luminance*/) {
710 *num_types = 0;
711 return HWC2::Error::None;
712}
713
714/* Find API details at:
715 * 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 +0300716 *
717 * Called after PresentDisplay(), CLIENT is expecting release fence for the
718 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200719 */
720HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
721 hwc2_layer_t *layers,
722 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200723 if (IsInHeadlessMode()) {
724 *num_elements = 0;
725 return HWC2::Error::None;
726 }
727
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200728 uint32_t num_layers = 0;
729
Roman Stratiienkodd214942022-05-03 18:24:49 +0300730 for (auto &l : layers_) {
731 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
732 continue;
733 }
734
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200735 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300736
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200737 if (layers == nullptr || fences == nullptr)
738 continue;
739
740 if (num_layers > *num_elements) {
741 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
742 return HWC2::Error::None;
743 }
744
745 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200746 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200747 }
748 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300749
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200750 return HWC2::Error::None;
751}
752
Drew Davenport97b5abc2024-11-07 10:43:54 -0700753AtomicCommitArgs HwcDisplay::CreateModesetCommit(
754 const HwcDisplayConfig *config,
755 const std::optional<LayerData> &modeset_layer) {
756 AtomicCommitArgs args{};
757
758 args.color_matrix = color_matrix_;
759 args.content_type = content_type_;
760 args.colorspace = colorspace_;
761
762 std::vector<LayerData> composition_layers;
763 if (modeset_layer) {
764 composition_layers.emplace_back(modeset_layer.value());
765 }
766
767 if (composition_layers.empty()) {
768 ALOGW("Attempting to create a modeset commit without a layer.");
769 }
770
771 args.display_mode = config->mode;
772 args.active = true;
773 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
774 std::move(
775 composition_layers));
776 ALOGW_IF(!args.composition, "No composition for blocking modeset");
777
778 return args;
779}
780
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200781HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200782 if (IsInHeadlessMode()) {
783 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
784 return HWC2::Error::None;
785 }
786
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200787 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400788 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400789 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200790
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200791 uint32_t prev_vperiod_ns = 0;
792 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200793
Drew Davenportd387c842024-12-16 16:57:24 -0700794 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700795 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200796 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700797 const HwcDisplayConfig *staged_config = GetConfig(
798 staged_mode_config_id_.value());
799 if (staged_config == nullptr) {
800 return HWC2::Error::BadConfig;
801 }
Roman Stratiienko44b95772025-01-22 18:03:36 +0200802 HwcLayer::LayerProperties lp;
803 lp.display_frame = {
804 .left = 0,
805 .top = 0,
806 .right = int(staged_config->mode.GetRawMode().hdisplay),
807 .bottom = int(staged_config->mode.GetRawMode().vdisplay),
808 };
809 client_layer_.SetLayerProperties(lp);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200810
Drew Davenportfe70c802024-11-07 13:02:21 -0700811 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700812 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200813 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700814 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200815 }
816 }
817
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200818 // order the layers by z-order
819 bool use_client_layer = false;
820 uint32_t client_z_order = UINT32_MAX;
821 std::map<uint32_t, HwcLayer *> z_map;
822 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
823 switch (l.second.GetValidatedType()) {
824 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300825 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200826 break;
827 case HWC2::Composition::Client:
828 // Place it at the z_order of the lowest client layer
829 use_client_layer = true;
830 client_z_order = std::min(client_z_order, l.second.GetZOrder());
831 break;
832 default:
833 continue;
834 }
835 }
836 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300837 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200838
839 if (z_map.empty())
840 return HWC2::Error::BadLayer;
841
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200842 std::vector<LayerData> composition_layers;
843
844 /* Import & populate */
845 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200846 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200847 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200848
849 // now that they're ordered by z, add them to the composition
850 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200851 if (!l.second->IsLayerUsableAsDevice()) {
852 /* This will be normally triggered on validation of the first frame
853 * containing CLIENT layer. At this moment client buffer is not yet
854 * provided by the CLIENT.
855 * This may be triggered once in HwcLayer lifecycle in case FB can't be
856 * imported. For example when non-contiguous buffer is imported into
857 * contiguous-only DRM/KMS driver.
858 */
859 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200860 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200861 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200862 }
863
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200864 /* Store plan to ensure shared planes won't be stolen by other display
865 * in between of ValidateDisplay() and PresentDisplay() calls
866 */
867 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
868 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300869
870 if (type_ == HWC2::DisplayType::Virtual) {
871 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
872 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
873 .acquire_fence;
874 }
875
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200876 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700877 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200878 return HWC2::Error::BadConfig;
879 }
880
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200881 a_args.composition = current_plan_;
882
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300883 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200884
885 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700886 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200887 return HWC2::Error::BadParameter;
888 }
889
Drew Davenportd387c842024-12-16 16:57:24 -0700890 if (new_vsync_period_ns) {
891 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700892 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700893
894 vsync_worker_->SetVsyncTimestampTracking(false);
895 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
896 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000897 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700898 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000899 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200900 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200901 }
902
903 return HWC2::Error::None;
904}
905
906/* Find API details at:
907 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
908 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300909HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200910 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300911 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200912 return HWC2::Error::None;
913 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200914 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200915
916 ++total_stats_.total_frames_;
917
918 AtomicCommitArgs a_args{};
919 ret = CreateComposition(a_args);
920
921 if (ret != HWC2::Error::None)
922 ++total_stats_.failed_kms_present_;
923
924 if (ret == HWC2::Error::BadLayer) {
925 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300926 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200927 return HWC2::Error::None;
928 }
929 if (ret != HWC2::Error::None)
930 return ret;
931
Roman Stratiienko76892782023-01-16 17:15:53 +0200932 this->present_fence_ = a_args.out_fence;
933 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200934
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200935 // Reset the color matrix so we don't apply it over and over again.
936 color_matrix_ = {};
937
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200938 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700939
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200940 return HWC2::Error::None;
941}
942
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200943HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
944 int64_t change_time) {
945 if (configs_.hwc_configs.count(config) == 0) {
946 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200947 return HWC2::Error::BadConfig;
948 }
949
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200950 staged_mode_change_time_ = change_time;
951 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200952
953 return HWC2::Error::None;
954}
955
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200956HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
957 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
958}
959
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200960/* Find API details at:
961 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
962 */
963HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
964 int32_t acquire_fence,
965 int32_t dataspace,
966 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +0200967 HwcLayer::LayerProperties lp;
968 lp.buffer = {.buffer_handle = target,
969 .acquire_fence = MakeSharedFd(acquire_fence)};
970 lp.color_space = Hwc2ToColorSpace(dataspace);
971 lp.sample_range = Hwc2ToSampleRange(dataspace);
972 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200973
974 /*
975 * target can be nullptr, this does mean the Composer Service is calling
976 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
977 * 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
978 */
979 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300980 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200981 return HWC2::Error::None;
982 }
983
Roman Stratiienko5070d512022-05-30 13:41:20 +0300984 if (IsInHeadlessMode()) {
985 return HWC2::Error::None;
986 }
987
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200988 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200989 if (!client_layer_.IsLayerUsableAsDevice()) {
990 ALOGE("Client layer must be always usable by DRM/KMS");
991 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200992 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200993
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200994 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300995 if (!bi) {
996 ALOGE("%s: Invalid state", __func__);
997 return HWC2::Error::BadLayer;
998 }
999
Roman Stratiienko44b95772025-01-22 18:03:36 +02001000 lp = {};
1001 lp.source_crop = {.left = 0.0F,
1002 .top = 0.0F,
1003 .right = float(bi->width),
1004 .bottom = float(bi->height)};
1005 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001006
1007 return HWC2::Error::None;
1008}
1009
1010HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001011 /* Maps to the Colorspace DRM connector property:
1012 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1013 */
1014 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001015 return HWC2::Error::BadParameter;
1016
Sasha McIntosh5294f092024-09-18 18:14:54 -04001017 switch (mode) {
1018 case HAL_COLOR_MODE_NATIVE:
1019 colorspace_ = Colorspace::kDefault;
1020 break;
1021 case HAL_COLOR_MODE_STANDARD_BT601_625:
1022 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1023 case HAL_COLOR_MODE_STANDARD_BT601_525:
1024 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
1025 // The DP spec does not say whether this is the 525 or the 625 line version.
1026 colorspace_ = Colorspace::kBt601Ycc;
1027 break;
1028 case HAL_COLOR_MODE_STANDARD_BT709:
1029 case HAL_COLOR_MODE_SRGB:
1030 colorspace_ = Colorspace::kBt709Ycc;
1031 break;
1032 case HAL_COLOR_MODE_DCI_P3:
1033 case HAL_COLOR_MODE_DISPLAY_P3:
1034 colorspace_ = Colorspace::kDciP3RgbD65;
1035 break;
1036 case HAL_COLOR_MODE_ADOBE_RGB:
1037 default:
1038 return HWC2::Error::Unsupported;
1039 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001040
1041 color_mode_ = mode;
1042 return HWC2::Error::None;
1043}
1044
1045HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1046 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1047 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1048 return HWC2::Error::BadParameter;
1049
1050 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1051 return HWC2::Error::BadParameter;
1052
1053 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001054
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001055 if (IsInHeadlessMode())
1056 return HWC2::Error::None;
1057
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001058 if (!GetPipe().crtc->Get()->GetCtmProperty())
1059 return HWC2::Error::None;
1060
1061 switch (color_transform_hint_) {
1062 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001063 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001064 break;
1065 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001066 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001067 {
1068 for (int i = 12; i < 14; i++) {
1069 if (matrix[i] != 0.F)
1070 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001071 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001072 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1073 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1074 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001075 }
1076 break;
1077 default:
1078 return HWC2::Error::Unsupported;
1079 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001080
1081 return HWC2::Error::None;
1082}
1083
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001084bool HwcDisplay::CtmByGpu() {
1085 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1086 return false;
1087
1088 if (GetPipe().crtc->Get()->GetCtmProperty())
1089 return false;
1090
Drew Davenport93443182023-12-14 09:25:45 +00001091 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001092 return false;
1093
1094 return true;
1095}
1096
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001097HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1098 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001099 HwcLayer::LayerProperties lp;
1100 lp.buffer = {.buffer_handle = buffer,
1101 .acquire_fence = MakeSharedFd(release_fence)};
1102 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001103 writeback_layer_->PopulateLayerData();
1104 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1105 ALOGE("Output layer must be always usable by DRM/KMS");
1106 return HWC2::Error::BadLayer;
1107 }
1108 /* TODO: Check if format is supported by writeback connector */
1109 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001110}
1111
1112HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1113 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001114
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001115 AtomicCommitArgs a_args{};
1116
1117 switch (mode) {
1118 case HWC2::PowerMode::Off:
1119 a_args.active = false;
1120 break;
1121 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001122 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001123 break;
1124 case HWC2::PowerMode::Doze:
1125 case HWC2::PowerMode::DozeSuspend:
1126 return HWC2::Error::Unsupported;
1127 default:
John Stultzffe783c2024-02-14 10:51:27 -08001128 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001129 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001130 }
1131
1132 if (IsInHeadlessMode()) {
1133 return HWC2::Error::None;
1134 }
1135
Jia Ren80566fe2022-11-17 17:26:00 +08001136 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001137 /*
1138 * Setting the display to active before we have a composition
1139 * can break some drivers, so skip setting a_args.active to
1140 * true, as the next composition frame will implicitly activate
1141 * the display
1142 */
1143 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1144 ? HWC2::Error::None
1145 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001146 };
1147
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001148 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001149 if (err) {
1150 ALOGE("Failed to apply the dpms composition err=%d", err);
1151 return HWC2::Error::BadParameter;
1152 }
1153 return HWC2::Error::None;
1154}
1155
1156HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001157 if (type_ == HWC2::DisplayType::Virtual) {
1158 return HWC2::Error::None;
1159 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001160 if (!vsync_worker_) {
1161 return HWC2::Error::NoResources;
1162 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001163
Roman Stratiienko099c3112022-01-20 11:50:54 +02001164 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001165 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001166 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001167 DrmHwc *hwc = hwc_;
1168 hwc2_display_t id = handle_;
1169 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001170 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001171 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1172 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001173 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001174 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001175 return HWC2::Error::None;
1176}
1177
1178HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1179 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001180 if (IsInHeadlessMode()) {
1181 *num_types = *num_requests = 0;
1182 return HWC2::Error::None;
1183 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001184
1185 /* In current drm_hwc design in case previous frame layer was not validated as
1186 * a CLIENT, it is used by display controller (Front buffer). We have to store
1187 * this state to provide the CLIENT with the release fences for such buffers.
1188 */
1189 for (auto &l : layers_) {
1190 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1191 HWC2::Composition::Client);
1192 }
1193
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001194 return backend_->ValidateDisplay(this, num_types, num_requests);
1195}
1196
1197std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1198 std::vector<HwcLayer *> ordered_layers;
1199 ordered_layers.reserve(layers_.size());
1200
1201 for (auto &[handle, layer] : layers_) {
1202 ordered_layers.emplace_back(&layer);
1203 }
1204
1205 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1206 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1207 return lhs->GetZOrder() < rhs->GetZOrder();
1208 });
1209
1210 return ordered_layers;
1211}
1212
Roman Stratiienko099c3112022-01-20 11:50:54 +02001213HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1214 uint32_t *outVsyncPeriod /* ns */) {
1215 return GetDisplayAttribute(configs_.active_config_id,
1216 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1217 (int32_t *)(outVsyncPeriod));
1218}
1219
Roman Stratiienko6b405052022-12-10 19:09:10 +02001220#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001221HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001222 if (IsInHeadlessMode()) {
1223 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1224 return HWC2::Error::None;
1225 }
1226 /* Primary display should be always internal,
1227 * otherwise SF will be unhappy and will crash
1228 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001229 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001230 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001231 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001232 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1233 else
1234 return HWC2::Error::BadConfig;
1235
1236 return HWC2::Error::None;
1237}
1238
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001239HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001240 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001241 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1242 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001243 if (type_ == HWC2::DisplayType::Virtual) {
1244 return HWC2::Error::None;
1245 }
1246
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001247 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1248 return HWC2::Error::BadParameter;
1249 }
1250
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001251 uint32_t current_vsync_period{};
1252 GetDisplayVsyncPeriod(&current_vsync_period);
1253
1254 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1255 return HWC2::Error::SeamlessNotAllowed;
1256 }
1257
1258 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1259 ->desiredTimeNanos -
1260 current_vsync_period;
1261 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1262 if (ret != HWC2::Error::None) {
1263 return ret;
1264 }
1265
1266 outTimeline->refreshRequired = true;
1267 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1268 ->desiredTimeNanos;
1269
Drew Davenport33121b72024-12-13 14:59:35 -07001270 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001271
1272 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001273}
1274
1275HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1276 return HWC2::Error::Unsupported;
1277}
1278
1279HWC2::Error HwcDisplay::GetSupportedContentTypes(
1280 uint32_t *outNumSupportedContentTypes,
1281 const uint32_t *outSupportedContentTypes) {
1282 if (outSupportedContentTypes == nullptr)
1283 *outNumSupportedContentTypes = 0;
1284
1285 return HWC2::Error::None;
1286}
1287
1288HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001289 /* Maps exactly to the content_type DRM connector property:
1290 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001291 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001292 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1293 return HWC2::Error::BadParameter;
1294
1295 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001296
1297 return HWC2::Error::None;
1298}
1299#endif
1300
Roman Stratiienko6b405052022-12-10 19:09:10 +02001301#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001302HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1303 uint32_t *outDataSize,
1304 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001305 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001306 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001307 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001308
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001309 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001310 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001311 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001312 }
1313
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001314 *outPort = handle_; /* TDOD(nobody): What should be here? */
1315
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001316 if (outData) {
1317 *outDataSize = std::min(*outDataSize, blob->length);
1318 memcpy(outData, blob->data, *outDataSize);
1319 } else {
1320 *outDataSize = blob->length;
1321 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001322
1323 return HWC2::Error::None;
1324}
1325
1326HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001327 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001328 if (outNumCapabilities == nullptr) {
1329 return HWC2::Error::BadParameter;
1330 }
1331
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001332 bool skip_ctm = false;
1333
1334 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001335 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001336 skip_ctm = true;
1337
1338 // Skip client CTM if DRM can handle it
1339 if (!skip_ctm && !IsInHeadlessMode() &&
1340 GetPipe().crtc->Get()->GetCtmProperty())
1341 skip_ctm = true;
1342
1343 if (!skip_ctm) {
1344 *outNumCapabilities = 0;
1345 return HWC2::Error::None;
1346 }
1347
1348 *outNumCapabilities = 1;
1349 if (outCapabilities) {
1350 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1351 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001352
1353 return HWC2::Error::None;
1354}
1355
1356HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1357 *supported = false;
1358 return HWC2::Error::None;
1359}
1360
1361HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1362 return HWC2::Error::Unsupported;
1363}
1364
Roman Stratiienko6b405052022-12-10 19:09:10 +02001365#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001366
Roman Stratiienko6b405052022-12-10 19:09:10 +02001367#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001368
1369HWC2::Error HwcDisplay::GetRenderIntents(
1370 int32_t mode, uint32_t *outNumIntents,
1371 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1372 if (mode != HAL_COLOR_MODE_NATIVE) {
1373 return HWC2::Error::BadParameter;
1374 }
1375
1376 if (outIntents == nullptr) {
1377 *outNumIntents = 1;
1378 return HWC2::Error::None;
1379 }
1380 *outNumIntents = 1;
1381 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1382 return HWC2::Error::None;
1383}
1384
1385HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1386 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1387 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1388 return HWC2::Error::BadParameter;
1389
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001390 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1391 return HWC2::Error::Unsupported;
1392
Sasha McIntosh5294f092024-09-18 18:14:54 -04001393 auto err = SetColorMode(mode);
1394 if (err != HWC2::Error::None) return err;
1395
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001396 return HWC2::Error::None;
1397}
1398
Roman Stratiienko6b405052022-12-10 19:09:10 +02001399#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001400
1401const Backend *HwcDisplay::backend() const {
1402 return backend_.get();
1403}
1404
1405void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1406 backend_ = std::move(backend);
1407}
1408
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001409} // namespace android