blob: a0c200e7f2bb303e97e14cfc3390e250e68dc8da [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;
Roman Stratiienko88bd6a22025-01-24 23:55:44 +020089 color_matrix->matrix[(i * kCtmRows) + j] = To3132FixPt(
90 color_transform_matrix[(j * kInCtmRows) + i]);
Drew Davenport76c17a82025-01-15 15:02:45 -070091 }
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 Stratiienko88bd6a22025-01-24 23:55:44 +0200191 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 Stratiienko88bd6a22025-01-24 23:55:44 +0200256 color_matrix_->matrix[(i * kCtmRows) + j] = (i == j) ? kOne : 0;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200257 }
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
Drew Davenportb864ccf2025-01-22 14:57:36 -0700404auto HwcDisplay::AcceptValidatedComposition() -> void {
405 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
406 l.second.AcceptTypeChange();
407 }
408}
409
410auto HwcDisplay::PresentStagedComposition(
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200411 SharedFd &out_present_fence, std::vector<ReleaseFence> &out_release_fences)
412 -> bool {
413 int out_fd = -1;
414 auto error = PresentDisplay(&out_fd);
415 out_present_fence = MakeSharedFd(out_fd);
416 if (error != HWC2::Error::None) {
417 return false;
418 }
419
420 if (!out_present_fence) {
421 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700422 }
423
424 for (auto &l : layers_) {
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200425 if (l.second.GetPriorBufferScanOutFlag()) {
426 out_release_fences.emplace_back(l.first, out_present_fence);
Drew Davenportb864ccf2025-01-22 14:57:36 -0700427 }
Drew Davenportb864ccf2025-01-22 14:57:36 -0700428 }
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200429
430 return true;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700431}
432
Roman Stratiienko63762a92023-09-18 22:33:45 +0300433void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200434 Deinit();
435
Roman Stratiienko63762a92023-09-18 22:33:45 +0300436 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200437
Roman Stratiienko63762a92023-09-18 22:33:45 +0300438 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200439 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000440 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200441 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000442 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200443 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200444}
445
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200446void HwcDisplay::Deinit() {
447 if (pipeline_ != nullptr) {
448 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200449 a_args.composition = std::make_shared<DrmKmsPlan>();
450 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300451 a_args.composition = {};
452 a_args.active = false;
453 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200454
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200455 current_plan_.reset();
456 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200457 if (flatcon_) {
458 flatcon_->StopThread();
459 flatcon_.reset();
460 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200461 }
462
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200463 if (vsync_worker_) {
464 vsync_worker_->StopThread();
465 vsync_worker_ = {};
466 }
467
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200468 SetClientTarget(nullptr, -1, 0, {});
469}
470
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200471HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200472 ChosePreferredConfig();
473
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300474 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700475 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300476 if (!vsync_worker_) {
477 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
478 return HWC2::Error::BadDisplay;
479 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200480 }
481
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200482 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200483 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200484 if (ret) {
485 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
486 return HWC2::Error::BadDisplay;
487 }
Drew Davenport93443182023-12-14 09:25:45 +0000488 auto flatcbk = (struct FlatConCallbacks){
489 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200490 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200491 }
492
Roman Stratiienko44b95772025-01-22 18:03:36 +0200493 HwcLayer::LayerProperties lp;
494 lp.blend_mode = BufferBlendMode::kPreMult;
495 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200496
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400497 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200498
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200499 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200500}
501
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600502std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
503 if (IsInHeadlessMode()) {
504 // The pipeline can be nullptr in headless mode, so return the default
505 // "normal" mode.
506 return PanelOrientation::kModePanelOrientationNormal;
507 }
508
509 DrmDisplayPipeline &pipeline = GetPipe();
510 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
511 ALOGW(
512 "No display pipeline present to query the panel orientation property.");
513 return {};
514 }
515
516 return pipeline.connector->Get()->GetPanelOrientation();
517}
518
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200519HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200520 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300521 if (type_ == HWC2::DisplayType::Virtual) {
522 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
523 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200524 err = configs_.Update(*pipeline_->connector->Get());
525 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300526 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200527 }
528 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200529 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200530 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200531
Roman Stratiienko0137f862022-01-04 18:27:40 +0200532 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200533}
534
535HWC2::Error HwcDisplay::AcceptDisplayChanges() {
536 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
537 l.second.AcceptTypeChange();
538 return HWC2::Error::None;
539}
540
541HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200542 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200543 *layer = static_cast<hwc2_layer_t>(layer_idx_);
544 ++layer_idx_;
545 return HWC2::Error::None;
546}
547
548HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200549 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200550 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200551 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200552
553 layers_.erase(layer);
554 return HWC2::Error::None;
555}
556
557HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700558 // If a config has been queued, it is considered the "active" config.
559 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
560 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200561 return HWC2::Error::BadConfig;
562
Drew Davenportfe70c802024-11-07 13:02:21 -0700563 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200564 return HWC2::Error::None;
565}
566
567HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
568 hwc2_layer_t *layers,
569 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200570 if (IsInHeadlessMode()) {
571 *num_elements = 0;
572 return HWC2::Error::None;
573 }
574
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200575 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300576 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200577 if (l.second.IsTypeChanged()) {
578 if (layers && num_changes < *num_elements)
579 layers[num_changes] = l.first;
580 if (types && num_changes < *num_elements)
581 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
582 ++num_changes;
583 }
584 }
585 if (!layers && !types)
586 *num_elements = num_changes;
587 return HWC2::Error::None;
588}
589
590HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
591 int32_t /*format*/,
592 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200593 if (IsInHeadlessMode()) {
594 return HWC2::Error::None;
595 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200596
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300597 auto min = pipeline_->device->GetMinResolution();
598 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200599
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200600 if (width < min.first || height < min.second)
601 return HWC2::Error::Unsupported;
602
603 if (width > max.first || height > max.second)
604 return HWC2::Error::Unsupported;
605
606 if (dataspace != HAL_DATASPACE_UNKNOWN)
607 return HWC2::Error::Unsupported;
608
609 // TODO(nobody): Validate format can be handled by either GL or planes
610 return HWC2::Error::None;
611}
612
613HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
614 if (!modes)
615 *num_modes = 1;
616
617 if (modes)
618 *modes = HAL_COLOR_MODE_NATIVE;
619
620 return HWC2::Error::None;
621}
622
623HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
624 int32_t attribute_in,
625 int32_t *value) {
626 int conf = static_cast<int>(config);
627
Roman Stratiienko0137f862022-01-04 18:27:40 +0200628 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200629 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200630 return HWC2::Error::BadConfig;
631 }
632
Roman Stratiienko0137f862022-01-04 18:27:40 +0200633 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200634
635 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300636 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200637 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
638 switch (attribute) {
639 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200640 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200641 break;
642 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200643 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200644 break;
645 case HWC2::Attribute::VsyncPeriod:
646 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600647 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200648 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000649 case HWC2::Attribute::DpiY:
650 // ideally this should be vdisplay/mm_heigth, however mm_height
651 // comes from edid parsing and is highly unreliable. Viewing the
652 // rarity of anisotropic displays, falling back to a single value
653 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200654 case HWC2::Attribute::DpiX:
655 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200656 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
657 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200658 : -1;
659 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200660#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200661 case HWC2::Attribute::ConfigGroup:
662 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
663 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200664 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200665 break;
666#endif
667 default:
668 *value = -1;
669 return HWC2::Error::BadConfig;
670 }
671 return HWC2::Error::None;
672}
673
Drew Davenportf7e88332024-09-06 12:54:38 -0600674HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
675 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200676 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200677 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200678 if (hwc_config.second.disabled) {
679 continue;
680 }
681
682 if (configs != nullptr) {
683 if (idx >= *num_configs) {
684 break;
685 }
686 configs[idx] = hwc_config.second.id;
687 }
688
689 idx++;
690 }
691 *num_configs = idx;
692 return HWC2::Error::None;
693}
694
695HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
696 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200697 if (IsInHeadlessMode()) {
698 stream << "null-display";
699 } else {
700 stream << "display-" << GetPipe().connector->Get()->GetId();
701 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300702 auto string = stream.str();
703 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200704 if (!name) {
705 *size = length;
706 return HWC2::Error::None;
707 }
708
709 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
710 strncpy(name, string.c_str(), *size);
711 return HWC2::Error::None;
712}
713
714HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
715 uint32_t *num_elements,
716 hwc2_layer_t * /*layers*/,
717 int32_t * /*layer_requests*/) {
718 // TODO(nobody): I think virtual display should request
719 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
720 *num_elements = 0;
721 return HWC2::Error::None;
722}
723
724HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
725 *type = static_cast<int32_t>(type_);
726 return HWC2::Error::None;
727}
728
729HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
730 *support = 0;
731 return HWC2::Error::None;
732}
733
734HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
735 int32_t * /*types*/,
736 float * /*max_luminance*/,
737 float * /*max_average_luminance*/,
738 float * /*min_luminance*/) {
739 *num_types = 0;
740 return HWC2::Error::None;
741}
742
743/* Find API details at:
744 * 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 +0300745 *
746 * Called after PresentDisplay(), CLIENT is expecting release fence for the
747 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200748 */
749HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
750 hwc2_layer_t *layers,
751 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200752 if (IsInHeadlessMode()) {
753 *num_elements = 0;
754 return HWC2::Error::None;
755 }
756
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200757 uint32_t num_layers = 0;
758
Roman Stratiienkodd214942022-05-03 18:24:49 +0300759 for (auto &l : layers_) {
760 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
761 continue;
762 }
763
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200764 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300765
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200766 if (layers == nullptr || fences == nullptr)
767 continue;
768
769 if (num_layers > *num_elements) {
770 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
771 return HWC2::Error::None;
772 }
773
774 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200775 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200776 }
777 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300778
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200779 return HWC2::Error::None;
780}
781
Drew Davenport97b5abc2024-11-07 10:43:54 -0700782AtomicCommitArgs HwcDisplay::CreateModesetCommit(
783 const HwcDisplayConfig *config,
784 const std::optional<LayerData> &modeset_layer) {
785 AtomicCommitArgs args{};
786
787 args.color_matrix = color_matrix_;
788 args.content_type = content_type_;
789 args.colorspace = colorspace_;
790
791 std::vector<LayerData> composition_layers;
792 if (modeset_layer) {
793 composition_layers.emplace_back(modeset_layer.value());
794 }
795
796 if (composition_layers.empty()) {
797 ALOGW("Attempting to create a modeset commit without a layer.");
798 }
799
800 args.display_mode = config->mode;
801 args.active = true;
802 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
803 std::move(
804 composition_layers));
805 ALOGW_IF(!args.composition, "No composition for blocking modeset");
806
807 return args;
808}
809
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200810HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200811 if (IsInHeadlessMode()) {
812 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
813 return HWC2::Error::None;
814 }
815
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200816 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400817 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400818 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200819
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200820 uint32_t prev_vperiod_ns = 0;
821 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200822
Drew Davenportd387c842024-12-16 16:57:24 -0700823 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700824 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200825 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700826 const HwcDisplayConfig *staged_config = GetConfig(
827 staged_mode_config_id_.value());
828 if (staged_config == nullptr) {
829 return HWC2::Error::BadConfig;
830 }
Roman Stratiienko44b95772025-01-22 18:03:36 +0200831 HwcLayer::LayerProperties lp;
832 lp.display_frame = {
833 .left = 0,
834 .top = 0,
835 .right = int(staged_config->mode.GetRawMode().hdisplay),
836 .bottom = int(staged_config->mode.GetRawMode().vdisplay),
837 };
838 client_layer_.SetLayerProperties(lp);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200839
Drew Davenportfe70c802024-11-07 13:02:21 -0700840 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700841 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200842 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700843 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200844 }
845 }
846
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200847 // order the layers by z-order
848 bool use_client_layer = false;
849 uint32_t client_z_order = UINT32_MAX;
850 std::map<uint32_t, HwcLayer *> z_map;
851 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
852 switch (l.second.GetValidatedType()) {
853 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300854 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200855 break;
856 case HWC2::Composition::Client:
857 // Place it at the z_order of the lowest client layer
858 use_client_layer = true;
859 client_z_order = std::min(client_z_order, l.second.GetZOrder());
860 break;
861 default:
862 continue;
863 }
864 }
865 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300866 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200867
868 if (z_map.empty())
869 return HWC2::Error::BadLayer;
870
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200871 std::vector<LayerData> composition_layers;
872
873 /* Import & populate */
874 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200875 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200876 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200877
878 // now that they're ordered by z, add them to the composition
879 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200880 if (!l.second->IsLayerUsableAsDevice()) {
881 /* This will be normally triggered on validation of the first frame
882 * containing CLIENT layer. At this moment client buffer is not yet
883 * provided by the CLIENT.
884 * This may be triggered once in HwcLayer lifecycle in case FB can't be
885 * imported. For example when non-contiguous buffer is imported into
886 * contiguous-only DRM/KMS driver.
887 */
888 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200889 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200890 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200891 }
892
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200893 /* Store plan to ensure shared planes won't be stolen by other display
894 * in between of ValidateDisplay() and PresentDisplay() calls
895 */
896 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
897 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300898
899 if (type_ == HWC2::DisplayType::Virtual) {
900 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
901 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
902 .acquire_fence;
903 }
904
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200905 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700906 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907 return HWC2::Error::BadConfig;
908 }
909
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200910 a_args.composition = current_plan_;
911
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300912 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200913
914 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700915 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200916 return HWC2::Error::BadParameter;
917 }
918
Drew Davenportd387c842024-12-16 16:57:24 -0700919 if (new_vsync_period_ns) {
920 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700921 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700922
923 vsync_worker_->SetVsyncTimestampTracking(false);
924 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
925 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000926 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700927 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000928 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200929 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200930 }
931
932 return HWC2::Error::None;
933}
934
935/* Find API details at:
936 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
937 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300938HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200939 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300940 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200941 return HWC2::Error::None;
942 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200943 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200944
945 ++total_stats_.total_frames_;
946
947 AtomicCommitArgs a_args{};
948 ret = CreateComposition(a_args);
949
950 if (ret != HWC2::Error::None)
951 ++total_stats_.failed_kms_present_;
952
953 if (ret == HWC2::Error::BadLayer) {
954 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300955 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200956 return HWC2::Error::None;
957 }
958 if (ret != HWC2::Error::None)
959 return ret;
960
Roman Stratiienko76892782023-01-16 17:15:53 +0200961 this->present_fence_ = a_args.out_fence;
962 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200963
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200964 // Reset the color matrix so we don't apply it over and over again.
965 color_matrix_ = {};
966
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200967 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700968
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200969 return HWC2::Error::None;
970}
971
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200972HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
973 int64_t change_time) {
974 if (configs_.hwc_configs.count(config) == 0) {
975 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200976 return HWC2::Error::BadConfig;
977 }
978
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200979 staged_mode_change_time_ = change_time;
980 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200981
982 return HWC2::Error::None;
983}
984
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200985HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
986 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
987}
988
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200989/* Find API details at:
990 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
991 */
992HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
993 int32_t acquire_fence,
994 int32_t dataspace,
995 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +0200996 HwcLayer::LayerProperties lp;
997 lp.buffer = {.buffer_handle = target,
998 .acquire_fence = MakeSharedFd(acquire_fence)};
999 lp.color_space = Hwc2ToColorSpace(dataspace);
1000 lp.sample_range = Hwc2ToSampleRange(dataspace);
1001 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001002
1003 /*
1004 * target can be nullptr, this does mean the Composer Service is calling
1005 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
1006 * 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
1007 */
1008 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +03001009 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001010 return HWC2::Error::None;
1011 }
1012
Roman Stratiienko5070d512022-05-30 13:41:20 +03001013 if (IsInHeadlessMode()) {
1014 return HWC2::Error::None;
1015 }
1016
Roman Stratiienko359a9d32023-01-16 17:41:07 +02001017 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001018 if (!client_layer_.IsLayerUsableAsDevice()) {
1019 ALOGE("Client layer must be always usable by DRM/KMS");
1020 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +02001021 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001022
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001023 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001024 if (!bi) {
1025 ALOGE("%s: Invalid state", __func__);
1026 return HWC2::Error::BadLayer;
1027 }
1028
Roman Stratiienko44b95772025-01-22 18:03:36 +02001029 lp = {};
1030 lp.source_crop = {.left = 0.0F,
1031 .top = 0.0F,
1032 .right = float(bi->width),
1033 .bottom = float(bi->height)};
1034 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001035
1036 return HWC2::Error::None;
1037}
1038
1039HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001040 /* Maps to the Colorspace DRM connector property:
1041 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1042 */
1043 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001044 return HWC2::Error::BadParameter;
1045
Sasha McIntosh5294f092024-09-18 18:14:54 -04001046 switch (mode) {
1047 case HAL_COLOR_MODE_NATIVE:
1048 colorspace_ = Colorspace::kDefault;
1049 break;
1050 case HAL_COLOR_MODE_STANDARD_BT601_625:
1051 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1052 case HAL_COLOR_MODE_STANDARD_BT601_525:
1053 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
1054 // The DP spec does not say whether this is the 525 or the 625 line version.
1055 colorspace_ = Colorspace::kBt601Ycc;
1056 break;
1057 case HAL_COLOR_MODE_STANDARD_BT709:
1058 case HAL_COLOR_MODE_SRGB:
1059 colorspace_ = Colorspace::kBt709Ycc;
1060 break;
1061 case HAL_COLOR_MODE_DCI_P3:
1062 case HAL_COLOR_MODE_DISPLAY_P3:
1063 colorspace_ = Colorspace::kDciP3RgbD65;
1064 break;
1065 case HAL_COLOR_MODE_ADOBE_RGB:
1066 default:
1067 return HWC2::Error::Unsupported;
1068 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001069
1070 color_mode_ = mode;
1071 return HWC2::Error::None;
1072}
1073
1074HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1075 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1076 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1077 return HWC2::Error::BadParameter;
1078
1079 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1080 return HWC2::Error::BadParameter;
1081
1082 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001083
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001084 if (IsInHeadlessMode())
1085 return HWC2::Error::None;
1086
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001087 if (!GetPipe().crtc->Get()->GetCtmProperty())
1088 return HWC2::Error::None;
1089
1090 switch (color_transform_hint_) {
1091 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001092 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001093 break;
1094 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001095 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001096 {
1097 for (int i = 12; i < 14; i++) {
1098 if (matrix[i] != 0.F)
1099 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001100 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001101 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1102 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1103 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001104 }
1105 break;
1106 default:
1107 return HWC2::Error::Unsupported;
1108 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001109
1110 return HWC2::Error::None;
1111}
1112
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001113bool HwcDisplay::CtmByGpu() {
1114 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1115 return false;
1116
1117 if (GetPipe().crtc->Get()->GetCtmProperty())
1118 return false;
1119
Drew Davenport93443182023-12-14 09:25:45 +00001120 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001121 return false;
1122
1123 return true;
1124}
1125
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001126HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1127 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001128 HwcLayer::LayerProperties lp;
1129 lp.buffer = {.buffer_handle = buffer,
1130 .acquire_fence = MakeSharedFd(release_fence)};
1131 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001132 writeback_layer_->PopulateLayerData();
1133 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1134 ALOGE("Output layer must be always usable by DRM/KMS");
1135 return HWC2::Error::BadLayer;
1136 }
1137 /* TODO: Check if format is supported by writeback connector */
1138 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001139}
1140
1141HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1142 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001143
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001144 AtomicCommitArgs a_args{};
1145
1146 switch (mode) {
1147 case HWC2::PowerMode::Off:
1148 a_args.active = false;
1149 break;
1150 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001151 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001152 break;
1153 case HWC2::PowerMode::Doze:
1154 case HWC2::PowerMode::DozeSuspend:
1155 return HWC2::Error::Unsupported;
1156 default:
John Stultzffe783c2024-02-14 10:51:27 -08001157 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001158 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001159 }
1160
1161 if (IsInHeadlessMode()) {
1162 return HWC2::Error::None;
1163 }
1164
Jia Ren80566fe2022-11-17 17:26:00 +08001165 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001166 /*
1167 * Setting the display to active before we have a composition
1168 * can break some drivers, so skip setting a_args.active to
1169 * true, as the next composition frame will implicitly activate
1170 * the display
1171 */
1172 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1173 ? HWC2::Error::None
1174 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001175 };
1176
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001177 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001178 if (err) {
1179 ALOGE("Failed to apply the dpms composition err=%d", err);
1180 return HWC2::Error::BadParameter;
1181 }
1182 return HWC2::Error::None;
1183}
1184
1185HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001186 if (type_ == HWC2::DisplayType::Virtual) {
1187 return HWC2::Error::None;
1188 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001189 if (!vsync_worker_) {
1190 return HWC2::Error::NoResources;
1191 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001192
Roman Stratiienko099c3112022-01-20 11:50:54 +02001193 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001194 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001195 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001196 DrmHwc *hwc = hwc_;
1197 hwc2_display_t id = handle_;
1198 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001199 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001200 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1201 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001202 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001203 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001204 return HWC2::Error::None;
1205}
1206
1207HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1208 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001209 if (IsInHeadlessMode()) {
1210 *num_types = *num_requests = 0;
1211 return HWC2::Error::None;
1212 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001213
1214 /* In current drm_hwc design in case previous frame layer was not validated as
1215 * a CLIENT, it is used by display controller (Front buffer). We have to store
1216 * this state to provide the CLIENT with the release fences for such buffers.
1217 */
1218 for (auto &l : layers_) {
1219 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1220 HWC2::Composition::Client);
1221 }
1222
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001223 return backend_->ValidateDisplay(this, num_types, num_requests);
1224}
1225
1226std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1227 std::vector<HwcLayer *> ordered_layers;
1228 ordered_layers.reserve(layers_.size());
1229
1230 for (auto &[handle, layer] : layers_) {
1231 ordered_layers.emplace_back(&layer);
1232 }
1233
1234 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1235 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1236 return lhs->GetZOrder() < rhs->GetZOrder();
1237 });
1238
1239 return ordered_layers;
1240}
1241
Roman Stratiienko099c3112022-01-20 11:50:54 +02001242HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1243 uint32_t *outVsyncPeriod /* ns */) {
1244 return GetDisplayAttribute(configs_.active_config_id,
1245 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1246 (int32_t *)(outVsyncPeriod));
1247}
1248
Roman Stratiienko6b405052022-12-10 19:09:10 +02001249#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001250HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001251 if (IsInHeadlessMode()) {
1252 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1253 return HWC2::Error::None;
1254 }
1255 /* Primary display should be always internal,
1256 * otherwise SF will be unhappy and will crash
1257 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001258 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001259 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001260 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001261 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1262 else
1263 return HWC2::Error::BadConfig;
1264
1265 return HWC2::Error::None;
1266}
1267
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001268HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001269 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001270 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1271 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001272 if (type_ == HWC2::DisplayType::Virtual) {
1273 return HWC2::Error::None;
1274 }
1275
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001276 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1277 return HWC2::Error::BadParameter;
1278 }
1279
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001280 uint32_t current_vsync_period{};
1281 GetDisplayVsyncPeriod(&current_vsync_period);
1282
1283 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1284 return HWC2::Error::SeamlessNotAllowed;
1285 }
1286
1287 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1288 ->desiredTimeNanos -
1289 current_vsync_period;
1290 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1291 if (ret != HWC2::Error::None) {
1292 return ret;
1293 }
1294
1295 outTimeline->refreshRequired = true;
1296 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1297 ->desiredTimeNanos;
1298
Drew Davenport33121b72024-12-13 14:59:35 -07001299 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001300
1301 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001302}
1303
1304HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1305 return HWC2::Error::Unsupported;
1306}
1307
1308HWC2::Error HwcDisplay::GetSupportedContentTypes(
1309 uint32_t *outNumSupportedContentTypes,
1310 const uint32_t *outSupportedContentTypes) {
1311 if (outSupportedContentTypes == nullptr)
1312 *outNumSupportedContentTypes = 0;
1313
1314 return HWC2::Error::None;
1315}
1316
1317HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001318 /* Maps exactly to the content_type DRM connector property:
1319 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001320 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001321 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1322 return HWC2::Error::BadParameter;
1323
1324 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001325
1326 return HWC2::Error::None;
1327}
1328#endif
1329
Roman Stratiienko6b405052022-12-10 19:09:10 +02001330#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001331HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1332 uint32_t *outDataSize,
1333 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001334 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001335 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001336 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001337
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001338 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001339 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001340 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001341 }
1342
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001343 *outPort = handle_; /* TDOD(nobody): What should be here? */
1344
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001345 if (outData) {
1346 *outDataSize = std::min(*outDataSize, blob->length);
1347 memcpy(outData, blob->data, *outDataSize);
1348 } else {
1349 *outDataSize = blob->length;
1350 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001351
1352 return HWC2::Error::None;
1353}
1354
1355HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001356 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001357 if (outNumCapabilities == nullptr) {
1358 return HWC2::Error::BadParameter;
1359 }
1360
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001361 bool skip_ctm = false;
1362
1363 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001364 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001365 skip_ctm = true;
1366
1367 // Skip client CTM if DRM can handle it
1368 if (!skip_ctm && !IsInHeadlessMode() &&
1369 GetPipe().crtc->Get()->GetCtmProperty())
1370 skip_ctm = true;
1371
1372 if (!skip_ctm) {
1373 *outNumCapabilities = 0;
1374 return HWC2::Error::None;
1375 }
1376
1377 *outNumCapabilities = 1;
1378 if (outCapabilities) {
1379 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1380 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001381
1382 return HWC2::Error::None;
1383}
1384
1385HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1386 *supported = false;
1387 return HWC2::Error::None;
1388}
1389
1390HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1391 return HWC2::Error::Unsupported;
1392}
1393
Roman Stratiienko6b405052022-12-10 19:09:10 +02001394#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001395
Roman Stratiienko6b405052022-12-10 19:09:10 +02001396#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001397
1398HWC2::Error HwcDisplay::GetRenderIntents(
1399 int32_t mode, uint32_t *outNumIntents,
1400 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1401 if (mode != HAL_COLOR_MODE_NATIVE) {
1402 return HWC2::Error::BadParameter;
1403 }
1404
1405 if (outIntents == nullptr) {
1406 *outNumIntents = 1;
1407 return HWC2::Error::None;
1408 }
1409 *outNumIntents = 1;
1410 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1411 return HWC2::Error::None;
1412}
1413
1414HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1415 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1416 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1417 return HWC2::Error::BadParameter;
1418
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001419 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1420 return HWC2::Error::Unsupported;
1421
Sasha McIntosh5294f092024-09-18 18:14:54 -04001422 auto err = SetColorMode(mode);
1423 if (err != HWC2::Error::None) return err;
1424
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001425 return HWC2::Error::None;
1426}
1427
Roman Stratiienko6b405052022-12-10 19:09:10 +02001428#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001429
1430const Backend *HwcDisplay::backend() const {
1431 return backend_.get();
1432}
1433
1434void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1435 backend_ = std::move(backend);
1436}
1437
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001438} // namespace android