blob: c0fc3c5c48db2b8b9c6f1a4a0c0a05a52ecf2957 [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
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(
411 int32_t *out_present_fence, std::vector<ReleaseFence> *out_release_fences)
412 -> HWC2::Error {
413 auto error = PresentDisplay(out_present_fence);
414 if (error != HWC2::Error::None || *out_present_fence == -1) {
415 return error;
416 }
417
418 for (auto &l : layers_) {
419 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
420 continue;
421 }
422 out_release_fences->emplace_back(l.first, DupFd(present_fence_));
423 }
424 return error;
425}
426
Roman Stratiienko63762a92023-09-18 22:33:45 +0300427void HwcDisplay::SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline) {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200428 Deinit();
429
Roman Stratiienko63762a92023-09-18 22:33:45 +0300430 pipeline_ = std::move(pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200431
Roman Stratiienko63762a92023-09-18 22:33:45 +0300432 if (pipeline_ != nullptr || handle_ == kPrimaryDisplay) {
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200433 Init();
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000434 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kConnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200435 } else {
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000436 hwc_->ScheduleHotplugEvent(handle_, DrmHwc::kDisconnected);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200437 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200438}
439
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200440void HwcDisplay::Deinit() {
441 if (pipeline_ != nullptr) {
442 AtomicCommitArgs a_args{};
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200443 a_args.composition = std::make_shared<DrmKmsPlan>();
444 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkoaf862a52022-06-22 12:14:22 +0300445 a_args.composition = {};
446 a_args.active = false;
447 GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200448
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200449 current_plan_.reset();
450 backend_.reset();
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200451 if (flatcon_) {
452 flatcon_->StopThread();
453 flatcon_.reset();
454 }
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200455 }
456
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200457 if (vsync_worker_) {
458 vsync_worker_->StopThread();
459 vsync_worker_ = {};
460 }
461
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200462 SetClientTarget(nullptr, -1, 0, {});
463}
464
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200465HWC2::Error HwcDisplay::Init() {
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200466 ChosePreferredConfig();
467
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300468 if (type_ != HWC2::DisplayType::Virtual) {
Drew Davenport15016c42024-12-13 15:13:28 -0700469 vsync_worker_ = VSyncWorker::CreateInstance(pipeline_);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300470 if (!vsync_worker_) {
471 ALOGE("Failed to create event worker for d=%d\n", int(handle_));
472 return HWC2::Error::BadDisplay;
473 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200474 }
475
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200476 if (!IsInHeadlessMode()) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200477 auto ret = BackendManager::GetInstance().SetBackendForDisplay(this);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200478 if (ret) {
479 ALOGE("Failed to set backend for d=%d %d\n", int(handle_), ret);
480 return HWC2::Error::BadDisplay;
481 }
Drew Davenport93443182023-12-14 09:25:45 +0000482 auto flatcbk = (struct FlatConCallbacks){
483 .trigger = [this]() { hwc_->SendRefreshEventToClient(handle_); }};
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200484 flatcon_ = FlatteningController::CreateInstance(flatcbk);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200485 }
486
Roman Stratiienko44b95772025-01-22 18:03:36 +0200487 HwcLayer::LayerProperties lp;
488 lp.blend_mode = BufferBlendMode::kPreMult;
489 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200490
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400491 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200492
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200493 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200494}
495
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600496std::optional<PanelOrientation> HwcDisplay::getDisplayPhysicalOrientation() {
497 if (IsInHeadlessMode()) {
498 // The pipeline can be nullptr in headless mode, so return the default
499 // "normal" mode.
500 return PanelOrientation::kModePanelOrientationNormal;
501 }
502
503 DrmDisplayPipeline &pipeline = GetPipe();
504 if (pipeline.connector == nullptr || pipeline.connector->Get() == nullptr) {
505 ALOGW(
506 "No display pipeline present to query the panel orientation property.");
507 return {};
508 }
509
510 return pipeline.connector->Get()->GetPanelOrientation();
511}
512
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200513HWC2::Error HwcDisplay::ChosePreferredConfig() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200514 HWC2::Error err{};
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300515 if (type_ == HWC2::DisplayType::Virtual) {
516 configs_.GenFakeMode(virtual_disp_width_, virtual_disp_height_);
517 } else if (!IsInHeadlessMode()) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200518 err = configs_.Update(*pipeline_->connector->Get());
519 } else {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300520 configs_.GenFakeMode(0, 0);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200521 }
522 if (!IsInHeadlessMode() && err != HWC2::Error::None) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200523 return HWC2::Error::BadDisplay;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200524 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200525
Roman Stratiienko0137f862022-01-04 18:27:40 +0200526 return SetActiveConfig(configs_.preferred_config_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200527}
528
529HWC2::Error HwcDisplay::AcceptDisplayChanges() {
530 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
531 l.second.AcceptTypeChange();
532 return HWC2::Error::None;
533}
534
535HWC2::Error HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200536 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer(this));
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200537 *layer = static_cast<hwc2_layer_t>(layer_idx_);
538 ++layer_idx_;
539 return HWC2::Error::None;
540}
541
542HWC2::Error HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200543 if (!get_layer(layer)) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200544 return HWC2::Error::BadLayer;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200545 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200546
547 layers_.erase(layer);
548 return HWC2::Error::None;
549}
550
551HWC2::Error HwcDisplay::GetActiveConfig(hwc2_config_t *config) const {
Drew Davenportfe70c802024-11-07 13:02:21 -0700552 // If a config has been queued, it is considered the "active" config.
553 const HwcDisplayConfig *hwc_config = GetLastRequestedConfig();
554 if (hwc_config == nullptr)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200555 return HWC2::Error::BadConfig;
556
Drew Davenportfe70c802024-11-07 13:02:21 -0700557 *config = hwc_config->id;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200558 return HWC2::Error::None;
559}
560
561HWC2::Error HwcDisplay::GetChangedCompositionTypes(uint32_t *num_elements,
562 hwc2_layer_t *layers,
563 int32_t *types) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200564 if (IsInHeadlessMode()) {
565 *num_elements = 0;
566 return HWC2::Error::None;
567 }
568
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200569 uint32_t num_changes = 0;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300570 for (auto &l : layers_) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200571 if (l.second.IsTypeChanged()) {
572 if (layers && num_changes < *num_elements)
573 layers[num_changes] = l.first;
574 if (types && num_changes < *num_elements)
575 types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
576 ++num_changes;
577 }
578 }
579 if (!layers && !types)
580 *num_elements = num_changes;
581 return HWC2::Error::None;
582}
583
584HWC2::Error HwcDisplay::GetClientTargetSupport(uint32_t width, uint32_t height,
585 int32_t /*format*/,
586 int32_t dataspace) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200587 if (IsInHeadlessMode()) {
588 return HWC2::Error::None;
589 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200590
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300591 auto min = pipeline_->device->GetMinResolution();
592 auto max = pipeline_->device->GetMaxResolution();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200593
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200594 if (width < min.first || height < min.second)
595 return HWC2::Error::Unsupported;
596
597 if (width > max.first || height > max.second)
598 return HWC2::Error::Unsupported;
599
600 if (dataspace != HAL_DATASPACE_UNKNOWN)
601 return HWC2::Error::Unsupported;
602
603 // TODO(nobody): Validate format can be handled by either GL or planes
604 return HWC2::Error::None;
605}
606
607HWC2::Error HwcDisplay::GetColorModes(uint32_t *num_modes, int32_t *modes) {
608 if (!modes)
609 *num_modes = 1;
610
611 if (modes)
612 *modes = HAL_COLOR_MODE_NATIVE;
613
614 return HWC2::Error::None;
615}
616
617HWC2::Error HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
618 int32_t attribute_in,
619 int32_t *value) {
620 int conf = static_cast<int>(config);
621
Roman Stratiienko0137f862022-01-04 18:27:40 +0200622 if (configs_.hwc_configs.count(conf) == 0) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200623 ALOGE("Could not find mode #%d", conf);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200624 return HWC2::Error::BadConfig;
625 }
626
Roman Stratiienko0137f862022-01-04 18:27:40 +0200627 auto &hwc_config = configs_.hwc_configs[conf];
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200628
629 static const int32_t kUmPerInch = 25400;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300630 auto mm_width = configs_.mm_width;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200631 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
632 switch (attribute) {
633 case HWC2::Attribute::Width:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200634 *value = static_cast<int>(hwc_config.mode.GetRawMode().hdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200635 break;
636 case HWC2::Attribute::Height:
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200637 *value = static_cast<int>(hwc_config.mode.GetRawMode().vdisplay);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200638 break;
639 case HWC2::Attribute::VsyncPeriod:
640 // in nanoseconds
Drew Davenport8053f2e2024-10-02 13:44:41 -0600641 *value = hwc_config.mode.GetVSyncPeriodNs();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200642 break;
Lucas Berthoudf686aa2024-08-28 16:15:38 +0000643 case HWC2::Attribute::DpiY:
644 // ideally this should be vdisplay/mm_heigth, however mm_height
645 // comes from edid parsing and is highly unreliable. Viewing the
646 // rarity of anisotropic displays, falling back to a single value
647 // for dpi yield more correct output.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200648 case HWC2::Attribute::DpiX:
649 // Dots per 1000 inches
Roman Stratiienkodf3120f2022-12-07 23:10:55 +0200650 *value = mm_width ? int(hwc_config.mode.GetRawMode().hdisplay *
651 kUmPerInch / mm_width)
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200652 : -1;
653 break;
Roman Stratiienko6b405052022-12-10 19:09:10 +0200654#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200655 case HWC2::Attribute::ConfigGroup:
656 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
657 * able to request it even if service @2.1 is used */
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200658 *value = int(hwc_config.group_id);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200659 break;
660#endif
661 default:
662 *value = -1;
663 return HWC2::Error::BadConfig;
664 }
665 return HWC2::Error::None;
666}
667
Drew Davenportf7e88332024-09-06 12:54:38 -0600668HWC2::Error HwcDisplay::LegacyGetDisplayConfigs(uint32_t *num_configs,
669 hwc2_config_t *configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200670 uint32_t idx = 0;
Roman Stratiienko0137f862022-01-04 18:27:40 +0200671 for (auto &hwc_config : configs_.hwc_configs) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200672 if (hwc_config.second.disabled) {
673 continue;
674 }
675
676 if (configs != nullptr) {
677 if (idx >= *num_configs) {
678 break;
679 }
680 configs[idx] = hwc_config.second.id;
681 }
682
683 idx++;
684 }
685 *num_configs = idx;
686 return HWC2::Error::None;
687}
688
689HWC2::Error HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
690 std::ostringstream stream;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200691 if (IsInHeadlessMode()) {
692 stream << "null-display";
693 } else {
694 stream << "display-" << GetPipe().connector->Get()->GetId();
695 }
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300696 auto string = stream.str();
697 auto length = string.length();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200698 if (!name) {
699 *size = length;
700 return HWC2::Error::None;
701 }
702
703 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
704 strncpy(name, string.c_str(), *size);
705 return HWC2::Error::None;
706}
707
708HWC2::Error HwcDisplay::GetDisplayRequests(int32_t * /*display_requests*/,
709 uint32_t *num_elements,
710 hwc2_layer_t * /*layers*/,
711 int32_t * /*layer_requests*/) {
712 // TODO(nobody): I think virtual display should request
713 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
714 *num_elements = 0;
715 return HWC2::Error::None;
716}
717
718HWC2::Error HwcDisplay::GetDisplayType(int32_t *type) {
719 *type = static_cast<int32_t>(type_);
720 return HWC2::Error::None;
721}
722
723HWC2::Error HwcDisplay::GetDozeSupport(int32_t *support) {
724 *support = 0;
725 return HWC2::Error::None;
726}
727
728HWC2::Error HwcDisplay::GetHdrCapabilities(uint32_t *num_types,
729 int32_t * /*types*/,
730 float * /*max_luminance*/,
731 float * /*max_average_luminance*/,
732 float * /*min_luminance*/) {
733 *num_types = 0;
734 return HWC2::Error::None;
735}
736
737/* Find API details at:
738 * 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 +0300739 *
740 * Called after PresentDisplay(), CLIENT is expecting release fence for the
741 * prior buffer (not the one assigned to the layer at the moment).
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200742 */
743HWC2::Error HwcDisplay::GetReleaseFences(uint32_t *num_elements,
744 hwc2_layer_t *layers,
745 int32_t *fences) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200746 if (IsInHeadlessMode()) {
747 *num_elements = 0;
748 return HWC2::Error::None;
749 }
750
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200751 uint32_t num_layers = 0;
752
Roman Stratiienkodd214942022-05-03 18:24:49 +0300753 for (auto &l : layers_) {
754 if (!l.second.GetPriorBufferScanOutFlag() || !present_fence_) {
755 continue;
756 }
757
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200758 ++num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300759
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200760 if (layers == nullptr || fences == nullptr)
761 continue;
762
763 if (num_layers > *num_elements) {
764 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
765 return HWC2::Error::None;
766 }
767
768 layers[num_layers - 1] = l.first;
Roman Stratiienko76892782023-01-16 17:15:53 +0200769 fences[num_layers - 1] = DupFd(present_fence_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200770 }
771 *num_elements = num_layers;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300772
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200773 return HWC2::Error::None;
774}
775
Drew Davenport97b5abc2024-11-07 10:43:54 -0700776AtomicCommitArgs HwcDisplay::CreateModesetCommit(
777 const HwcDisplayConfig *config,
778 const std::optional<LayerData> &modeset_layer) {
779 AtomicCommitArgs args{};
780
781 args.color_matrix = color_matrix_;
782 args.content_type = content_type_;
783 args.colorspace = colorspace_;
784
785 std::vector<LayerData> composition_layers;
786 if (modeset_layer) {
787 composition_layers.emplace_back(modeset_layer.value());
788 }
789
790 if (composition_layers.empty()) {
791 ALOGW("Attempting to create a modeset commit without a layer.");
792 }
793
794 args.display_mode = config->mode;
795 args.active = true;
796 args.composition = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
797 std::move(
798 composition_layers));
799 ALOGW_IF(!args.composition, "No composition for blocking modeset");
800
801 return args;
802}
803
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200804HWC2::Error HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200805 if (IsInHeadlessMode()) {
806 ALOGE("%s: Display is in headless mode, should never reach here", __func__);
807 return HWC2::Error::None;
808 }
809
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200810 a_args.color_matrix = color_matrix_;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400811 a_args.content_type = content_type_;
Sasha McIntosh5294f092024-09-18 18:14:54 -0400812 a_args.colorspace = colorspace_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200813
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200814 uint32_t prev_vperiod_ns = 0;
815 GetDisplayVsyncPeriod(&prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200816
Drew Davenportd387c842024-12-16 16:57:24 -0700817 std::optional<uint32_t> new_vsync_period_ns;
Drew Davenportfe70c802024-11-07 13:02:21 -0700818 if (staged_mode_config_id_ &&
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200819 staged_mode_change_time_ <= ResourceManager::GetTimeMonotonicNs()) {
Drew Davenportfe70c802024-11-07 13:02:21 -0700820 const HwcDisplayConfig *staged_config = GetConfig(
821 staged_mode_config_id_.value());
822 if (staged_config == nullptr) {
823 return HWC2::Error::BadConfig;
824 }
Roman Stratiienko44b95772025-01-22 18:03:36 +0200825 HwcLayer::LayerProperties lp;
826 lp.display_frame = {
827 .left = 0,
828 .top = 0,
829 .right = int(staged_config->mode.GetRawMode().hdisplay),
830 .bottom = int(staged_config->mode.GetRawMode().vdisplay),
831 };
832 client_layer_.SetLayerProperties(lp);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200833
Drew Davenportfe70c802024-11-07 13:02:21 -0700834 configs_.active_config_id = staged_mode_config_id_.value();
Drew Davenportfe70c802024-11-07 13:02:21 -0700835 a_args.display_mode = staged_config->mode;
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200836 if (!a_args.test_only) {
Drew Davenportd387c842024-12-16 16:57:24 -0700837 new_vsync_period_ns = staged_config->mode.GetVSyncPeriodNs();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200838 }
839 }
840
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200841 // order the layers by z-order
842 bool use_client_layer = false;
843 uint32_t client_z_order = UINT32_MAX;
844 std::map<uint32_t, HwcLayer *> z_map;
845 for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
846 switch (l.second.GetValidatedType()) {
847 case HWC2::Composition::Device:
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300848 z_map.emplace(l.second.GetZOrder(), &l.second);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200849 break;
850 case HWC2::Composition::Client:
851 // Place it at the z_order of the lowest client layer
852 use_client_layer = true;
853 client_z_order = std::min(client_z_order, l.second.GetZOrder());
854 break;
855 default:
856 continue;
857 }
858 }
859 if (use_client_layer)
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300860 z_map.emplace(client_z_order, &client_layer_);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200861
862 if (z_map.empty())
863 return HWC2::Error::BadLayer;
864
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200865 std::vector<LayerData> composition_layers;
866
867 /* Import & populate */
868 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200869 l.second->PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200870 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200871
872 // now that they're ordered by z, add them to the composition
873 for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200874 if (!l.second->IsLayerUsableAsDevice()) {
875 /* This will be normally triggered on validation of the first frame
876 * containing CLIENT layer. At this moment client buffer is not yet
877 * provided by the CLIENT.
878 * This may be triggered once in HwcLayer lifecycle in case FB can't be
879 * imported. For example when non-contiguous buffer is imported into
880 * contiguous-only DRM/KMS driver.
881 */
882 return HWC2::Error::BadLayer;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200883 }
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200884 composition_layers.emplace_back(l.second->GetLayerData());
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200885 }
886
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200887 /* Store plan to ensure shared planes won't be stolen by other display
888 * in between of ValidateDisplay() and PresentDisplay() calls
889 */
890 current_plan_ = DrmKmsPlan::CreateDrmKmsPlan(GetPipe(),
891 std::move(composition_layers));
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300892
893 if (type_ == HWC2::DisplayType::Virtual) {
894 a_args.writeback_fb = writeback_layer_->GetLayerData().fb;
895 a_args.writeback_release_fence = writeback_layer_->GetLayerData()
896 .acquire_fence;
897 }
898
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200899 if (!current_plan_) {
Drew Davenport897a7092024-11-12 12:14:01 -0700900 ALOGE_IF(!a_args.test_only, "Failed to create DrmKmsPlan");
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200901 return HWC2::Error::BadConfig;
902 }
903
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200904 a_args.composition = current_plan_;
905
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300906 auto ret = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200907
908 if (ret) {
Drew Davenport897a7092024-11-12 12:14:01 -0700909 ALOGE_IF(!a_args.test_only, "Failed to apply the frame composition ret=%d", ret);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200910 return HWC2::Error::BadParameter;
911 }
912
Drew Davenportd387c842024-12-16 16:57:24 -0700913 if (new_vsync_period_ns) {
914 vsync_worker_->SetVsyncPeriodNs(new_vsync_period_ns.value());
Drew Davenportfe70c802024-11-07 13:02:21 -0700915 staged_mode_config_id_.reset();
Drew Davenport33121b72024-12-13 14:59:35 -0700916
917 vsync_worker_->SetVsyncTimestampTracking(false);
918 uint32_t last_vsync_ts = vsync_worker_->GetLastVsyncTimestamp();
919 if (last_vsync_ts != 0) {
Drew Davenport93443182023-12-14 09:25:45 +0000920 hwc_->SendVsyncPeriodTimingChangedEventToClient(handle_,
Drew Davenport33121b72024-12-13 14:59:35 -0700921 last_vsync_ts +
Drew Davenport93443182023-12-14 09:25:45 +0000922 prev_vperiod_ns);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200923 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200924 }
925
926 return HWC2::Error::None;
927}
928
929/* Find API details at:
930 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
931 */
Roman Stratiienkodd214942022-05-03 18:24:49 +0300932HWC2::Error HwcDisplay::PresentDisplay(int32_t *out_present_fence) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200933 if (IsInHeadlessMode()) {
Roman Stratiienkodd214942022-05-03 18:24:49 +0300934 *out_present_fence = -1;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200935 return HWC2::Error::None;
936 }
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200937 HWC2::Error ret{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200938
939 ++total_stats_.total_frames_;
940
941 AtomicCommitArgs a_args{};
942 ret = CreateComposition(a_args);
943
944 if (ret != HWC2::Error::None)
945 ++total_stats_.failed_kms_present_;
946
947 if (ret == HWC2::Error::BadLayer) {
948 // Can we really have no client or device layers?
Roman Stratiienkodd214942022-05-03 18:24:49 +0300949 *out_present_fence = -1;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200950 return HWC2::Error::None;
951 }
952 if (ret != HWC2::Error::None)
953 return ret;
954
Roman Stratiienko76892782023-01-16 17:15:53 +0200955 this->present_fence_ = a_args.out_fence;
956 *out_present_fence = DupFd(a_args.out_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200957
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200958 // Reset the color matrix so we don't apply it over and over again.
959 color_matrix_ = {};
960
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200961 ++frame_no_;
Drew Davenport97b5abc2024-11-07 10:43:54 -0700962
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200963 return HWC2::Error::None;
964}
965
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200966HWC2::Error HwcDisplay::SetActiveConfigInternal(uint32_t config,
967 int64_t change_time) {
968 if (configs_.hwc_configs.count(config) == 0) {
969 ALOGE("Could not find active mode for %u", config);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200970 return HWC2::Error::BadConfig;
971 }
972
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200973 staged_mode_change_time_ = change_time;
974 staged_mode_config_id_ = config;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200975
976 return HWC2::Error::None;
977}
978
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200979HWC2::Error HwcDisplay::SetActiveConfig(hwc2_config_t config) {
980 return SetActiveConfigInternal(config, ResourceManager::GetTimeMonotonicNs());
981}
982
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200983/* Find API details at:
984 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
985 */
986HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
987 int32_t acquire_fence,
988 int32_t dataspace,
989 hwc_region_t /*damage*/) {
Roman Stratiienko44b95772025-01-22 18:03:36 +0200990 HwcLayer::LayerProperties lp;
991 lp.buffer = {.buffer_handle = target,
992 .acquire_fence = MakeSharedFd(acquire_fence)};
993 lp.color_space = Hwc2ToColorSpace(dataspace);
994 lp.sample_range = Hwc2ToSampleRange(dataspace);
995 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200996
997 /*
998 * target can be nullptr, this does mean the Composer Service is calling
999 * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
1000 * 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
1001 */
1002 if (target == nullptr) {
Roman Stratiienkoa32f9072022-05-13 12:12:20 +03001003 client_layer_.SwChainClearCache();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001004 return HWC2::Error::None;
1005 }
1006
Roman Stratiienko5070d512022-05-30 13:41:20 +03001007 if (IsInHeadlessMode()) {
1008 return HWC2::Error::None;
1009 }
1010
Roman Stratiienko359a9d32023-01-16 17:41:07 +02001011 client_layer_.PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001012 if (!client_layer_.IsLayerUsableAsDevice()) {
1013 ALOGE("Client layer must be always usable by DRM/KMS");
1014 return HWC2::Error::BadLayer;
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +02001015 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001016
Roman Stratiienko4b2cc482022-02-21 14:53:58 +02001017 auto &bi = client_layer_.GetLayerData().bi;
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001018 if (!bi) {
1019 ALOGE("%s: Invalid state", __func__);
1020 return HWC2::Error::BadLayer;
1021 }
1022
Roman Stratiienko44b95772025-01-22 18:03:36 +02001023 lp = {};
1024 lp.source_crop = {.left = 0.0F,
1025 .top = 0.0F,
1026 .right = float(bi->width),
1027 .bottom = float(bi->height)};
1028 client_layer_.SetLayerProperties(lp);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001029
1030 return HWC2::Error::None;
1031}
1032
1033HWC2::Error HwcDisplay::SetColorMode(int32_t mode) {
Sasha McIntosh5294f092024-09-18 18:14:54 -04001034 /* Maps to the Colorspace DRM connector property:
1035 * https://elixir.bootlin.com/linux/v6.11/source/include/drm/drm_connector.h#L538
1036 */
1037 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_DISPLAY_P3)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001038 return HWC2::Error::BadParameter;
1039
Sasha McIntosh5294f092024-09-18 18:14:54 -04001040 switch (mode) {
1041 case HAL_COLOR_MODE_NATIVE:
1042 colorspace_ = Colorspace::kDefault;
1043 break;
1044 case HAL_COLOR_MODE_STANDARD_BT601_625:
1045 case HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED:
1046 case HAL_COLOR_MODE_STANDARD_BT601_525:
1047 case HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED:
1048 // The DP spec does not say whether this is the 525 or the 625 line version.
1049 colorspace_ = Colorspace::kBt601Ycc;
1050 break;
1051 case HAL_COLOR_MODE_STANDARD_BT709:
1052 case HAL_COLOR_MODE_SRGB:
1053 colorspace_ = Colorspace::kBt709Ycc;
1054 break;
1055 case HAL_COLOR_MODE_DCI_P3:
1056 case HAL_COLOR_MODE_DISPLAY_P3:
1057 colorspace_ = Colorspace::kDciP3RgbD65;
1058 break;
1059 case HAL_COLOR_MODE_ADOBE_RGB:
1060 default:
1061 return HWC2::Error::Unsupported;
1062 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001063
1064 color_mode_ = mode;
1065 return HWC2::Error::None;
1066}
1067
1068HWC2::Error HwcDisplay::SetColorTransform(const float *matrix, int32_t hint) {
1069 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
1070 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
1071 return HWC2::Error::BadParameter;
1072
1073 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
1074 return HWC2::Error::BadParameter;
1075
1076 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001077
Roman Stratiienko5de61b52023-02-01 16:29:45 +02001078 if (IsInHeadlessMode())
1079 return HWC2::Error::None;
1080
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001081 if (!GetPipe().crtc->Get()->GetCtmProperty())
1082 return HWC2::Error::None;
1083
1084 switch (color_transform_hint_) {
1085 case HAL_COLOR_TRANSFORM_IDENTITY:
Sasha McIntosha37df7c2024-09-20 12:31:08 -04001086 SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001087 break;
1088 case HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX:
Sasha McIntosh921c1cd2024-10-09 19:50:52 -04001089 // Without HW support, we cannot correctly process matrices with an offset.
Drew Davenport76c17a82025-01-15 15:02:45 -07001090 {
1091 for (int i = 12; i < 14; i++) {
1092 if (matrix[i] != 0.F)
1093 return HWC2::Error::Unsupported;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001094 }
Drew Davenport76c17a82025-01-15 15:02:45 -07001095 std::array<float, 16> aidl_matrix = kIdentityMatrix;
1096 memcpy(aidl_matrix.data(), matrix, aidl_matrix.size() * sizeof(float));
1097 color_matrix_ = ToColorTransform(aidl_matrix);
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001098 }
1099 break;
1100 default:
1101 return HWC2::Error::Unsupported;
1102 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001103
1104 return HWC2::Error::None;
1105}
1106
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001107bool HwcDisplay::CtmByGpu() {
1108 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_IDENTITY)
1109 return false;
1110
1111 if (GetPipe().crtc->Get()->GetCtmProperty())
1112 return false;
1113
Drew Davenport93443182023-12-14 09:25:45 +00001114 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001115 return false;
1116
1117 return true;
1118}
1119
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001120HWC2::Error HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
1121 int32_t release_fence) {
Roman Stratiienko44b95772025-01-22 18:03:36 +02001122 HwcLayer::LayerProperties lp;
1123 lp.buffer = {.buffer_handle = buffer,
1124 .acquire_fence = MakeSharedFd(release_fence)};
1125 writeback_layer_->SetLayerProperties(lp);
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001126 writeback_layer_->PopulateLayerData();
1127 if (!writeback_layer_->IsLayerUsableAsDevice()) {
1128 ALOGE("Output layer must be always usable by DRM/KMS");
1129 return HWC2::Error::BadLayer;
1130 }
1131 /* TODO: Check if format is supported by writeback connector */
1132 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001133}
1134
1135HWC2::Error HwcDisplay::SetPowerMode(int32_t mode_in) {
1136 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001137
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001138 AtomicCommitArgs a_args{};
1139
1140 switch (mode) {
1141 case HWC2::PowerMode::Off:
1142 a_args.active = false;
1143 break;
1144 case HWC2::PowerMode::On:
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001145 a_args.active = true;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001146 break;
1147 case HWC2::PowerMode::Doze:
1148 case HWC2::PowerMode::DozeSuspend:
1149 return HWC2::Error::Unsupported;
1150 default:
John Stultzffe783c2024-02-14 10:51:27 -08001151 ALOGE("Incorrect power mode value (%d)\n", mode_in);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001152 return HWC2::Error::BadParameter;
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001153 }
1154
1155 if (IsInHeadlessMode()) {
1156 return HWC2::Error::None;
1157 }
1158
Jia Ren80566fe2022-11-17 17:26:00 +08001159 if (a_args.active && *a_args.active) {
Roman Stratiienkoccaf5162022-04-01 19:26:30 +03001160 /*
1161 * Setting the display to active before we have a composition
1162 * can break some drivers, so skip setting a_args.active to
1163 * true, as the next composition frame will implicitly activate
1164 * the display
1165 */
1166 return GetPipe().atomic_state_manager->ActivateDisplayUsingDPMS() == 0
1167 ? HWC2::Error::None
1168 : HWC2::Error::BadParameter;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001169 };
1170
Roman Stratiienkoa7913de2022-10-20 13:18:57 +03001171 auto err = GetPipe().atomic_state_manager->ExecuteAtomicCommit(a_args);
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001172 if (err) {
1173 ALOGE("Failed to apply the dpms composition err=%d", err);
1174 return HWC2::Error::BadParameter;
1175 }
1176 return HWC2::Error::None;
1177}
1178
1179HWC2::Error HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001180 if (type_ == HWC2::DisplayType::Virtual) {
1181 return HWC2::Error::None;
1182 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001183 if (!vsync_worker_) {
1184 return HWC2::Error::NoResources;
1185 }
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001186
Roman Stratiienko099c3112022-01-20 11:50:54 +02001187 vsync_event_en_ = HWC2_VSYNC_ENABLE == enabled;
Lucas Berthoua2928992025-01-07 22:48:28 +00001188 std::optional<VSyncWorker::VsyncTimestampCallback> callback = std::nullopt;
Roman Stratiienko099c3112022-01-20 11:50:54 +02001189 if (vsync_event_en_) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001190 DrmHwc *hwc = hwc_;
1191 hwc2_display_t id = handle_;
1192 // Callback will be called from the vsync thread.
Lucas Berthoua2928992025-01-07 22:48:28 +00001193 callback = [hwc, id](int64_t timestamp, uint32_t period_ns) {
Drew Davenport63a699e2024-12-13 15:00:00 -07001194 hwc->SendVsyncEventToClient(id, timestamp, period_ns);
1195 };
Roman Stratiienko099c3112022-01-20 11:50:54 +02001196 }
Lucas Berthoua2928992025-01-07 22:48:28 +00001197 vsync_worker_->SetTimestampCallback(std::move(callback));
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001198 return HWC2::Error::None;
1199}
1200
1201HWC2::Error HwcDisplay::ValidateDisplay(uint32_t *num_types,
1202 uint32_t *num_requests) {
Roman Stratiienkof0c507f2022-01-17 18:29:24 +02001203 if (IsInHeadlessMode()) {
1204 *num_types = *num_requests = 0;
1205 return HWC2::Error::None;
1206 }
Roman Stratiienkodd214942022-05-03 18:24:49 +03001207
1208 /* In current drm_hwc design in case previous frame layer was not validated as
1209 * a CLIENT, it is used by display controller (Front buffer). We have to store
1210 * this state to provide the CLIENT with the release fences for such buffers.
1211 */
1212 for (auto &l : layers_) {
1213 l.second.SetPriorBufferScanOutFlag(l.second.GetValidatedType() !=
1214 HWC2::Composition::Client);
1215 }
1216
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001217 return backend_->ValidateDisplay(this, num_types, num_requests);
1218}
1219
1220std::vector<HwcLayer *> HwcDisplay::GetOrderLayersByZPos() {
1221 std::vector<HwcLayer *> ordered_layers;
1222 ordered_layers.reserve(layers_.size());
1223
1224 for (auto &[handle, layer] : layers_) {
1225 ordered_layers.emplace_back(&layer);
1226 }
1227
1228 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
1229 [](const HwcLayer *lhs, const HwcLayer *rhs) {
1230 return lhs->GetZOrder() < rhs->GetZOrder();
1231 });
1232
1233 return ordered_layers;
1234}
1235
Roman Stratiienko099c3112022-01-20 11:50:54 +02001236HWC2::Error HwcDisplay::GetDisplayVsyncPeriod(
1237 uint32_t *outVsyncPeriod /* ns */) {
1238 return GetDisplayAttribute(configs_.active_config_id,
1239 HWC2_ATTRIBUTE_VSYNC_PERIOD,
1240 (int32_t *)(outVsyncPeriod));
1241}
1242
Roman Stratiienko6b405052022-12-10 19:09:10 +02001243#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001244HWC2::Error HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
Roman Stratiienko456e2d62022-01-29 01:17:39 +02001245 if (IsInHeadlessMode()) {
1246 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1247 return HWC2::Error::None;
1248 }
1249 /* Primary display should be always internal,
1250 * otherwise SF will be unhappy and will crash
1251 */
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001252 if (GetPipe().connector->Get()->IsInternal() || handle_ == kPrimaryDisplay)
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001253 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001254 else if (GetPipe().connector->Get()->IsExternal())
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001255 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1256 else
1257 return HWC2::Error::BadConfig;
1258
1259 return HWC2::Error::None;
1260}
1261
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001262HWC2::Error HwcDisplay::SetActiveConfigWithConstraints(
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001263 hwc2_config_t config,
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001264 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1265 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienkof2c060f2023-09-18 22:46:08 +03001266 if (type_ == HWC2::DisplayType::Virtual) {
1267 return HWC2::Error::None;
1268 }
1269
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001270 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1271 return HWC2::Error::BadParameter;
1272 }
1273
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001274 uint32_t current_vsync_period{};
1275 GetDisplayVsyncPeriod(&current_vsync_period);
1276
1277 if (vsyncPeriodChangeConstraints->seamlessRequired) {
1278 return HWC2::Error::SeamlessNotAllowed;
1279 }
1280
1281 outTimeline->refreshTimeNanos = vsyncPeriodChangeConstraints
1282 ->desiredTimeNanos -
1283 current_vsync_period;
1284 auto ret = SetActiveConfigInternal(config, outTimeline->refreshTimeNanos);
1285 if (ret != HWC2::Error::None) {
1286 return ret;
1287 }
1288
1289 outTimeline->refreshRequired = true;
1290 outTimeline->newVsyncAppliedTimeNanos = vsyncPeriodChangeConstraints
1291 ->desiredTimeNanos;
1292
Drew Davenport33121b72024-12-13 14:59:35 -07001293 vsync_worker_->SetVsyncTimestampTracking(true);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +02001294
1295 return HWC2::Error::None;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001296}
1297
1298HWC2::Error HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1299 return HWC2::Error::Unsupported;
1300}
1301
1302HWC2::Error HwcDisplay::GetSupportedContentTypes(
1303 uint32_t *outNumSupportedContentTypes,
1304 const uint32_t *outSupportedContentTypes) {
1305 if (outSupportedContentTypes == nullptr)
1306 *outNumSupportedContentTypes = 0;
1307
1308 return HWC2::Error::None;
1309}
1310
1311HWC2::Error HwcDisplay::SetContentType(int32_t contentType) {
Sasha McIntosh173247b2024-09-18 18:06:52 -04001312 /* Maps exactly to the content_type DRM connector property:
1313 * https://elixir.bootlin.com/linux/v6.11/source/include/uapi/drm/drm_mode.h#L107
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001314 */
Sasha McIntosh173247b2024-09-18 18:06:52 -04001315 if (contentType < HWC2_CONTENT_TYPE_NONE || contentType > HWC2_CONTENT_TYPE_GAME)
1316 return HWC2::Error::BadParameter;
1317
1318 content_type_ = contentType;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001319
1320 return HWC2::Error::None;
1321}
1322#endif
1323
Roman Stratiienko6b405052022-12-10 19:09:10 +02001324#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001325HWC2::Error HwcDisplay::GetDisplayIdentificationData(uint8_t *outPort,
1326 uint32_t *outDataSize,
1327 uint8_t *outData) {
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001328 if (IsInHeadlessMode()) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001329 return HWC2::Error::Unsupported;
Roman Stratiienko3dacd472022-01-11 19:18:34 +02001330 }
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001331
Roman Stratiienko19c162f2022-02-01 09:35:08 +02001332 auto blob = GetPipe().connector->Get()->GetEdidBlob();
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001333 if (!blob) {
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001334 return HWC2::Error::Unsupported;
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001335 }
1336
Roman Stratiienkof87d8082022-05-06 11:33:56 +03001337 *outPort = handle_; /* TDOD(nobody): What should be here? */
1338
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001339 if (outData) {
1340 *outDataSize = std::min(*outDataSize, blob->length);
1341 memcpy(outData, blob->data, *outDataSize);
1342 } else {
1343 *outDataSize = blob->length;
1344 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001345
1346 return HWC2::Error::None;
1347}
1348
1349HWC2::Error HwcDisplay::GetDisplayCapabilities(uint32_t *outNumCapabilities,
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001350 uint32_t *outCapabilities) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001351 if (outNumCapabilities == nullptr) {
1352 return HWC2::Error::BadParameter;
1353 }
1354
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001355 bool skip_ctm = false;
1356
1357 // Skip client CTM if user requested DRM_OR_IGNORE
Drew Davenport93443182023-12-14 09:25:45 +00001358 if (GetHwc()->GetResMan().GetCtmHandling() == CtmHandling::kDrmOrIgnore)
Roman Stratiienko0da91bf2023-01-17 18:06:04 +02001359 skip_ctm = true;
1360
1361 // Skip client CTM if DRM can handle it
1362 if (!skip_ctm && !IsInHeadlessMode() &&
1363 GetPipe().crtc->Get()->GetCtmProperty())
1364 skip_ctm = true;
1365
1366 if (!skip_ctm) {
1367 *outNumCapabilities = 0;
1368 return HWC2::Error::None;
1369 }
1370
1371 *outNumCapabilities = 1;
1372 if (outCapabilities) {
1373 outCapabilities[0] = HWC2_DISPLAY_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
1374 }
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001375
1376 return HWC2::Error::None;
1377}
1378
1379HWC2::Error HwcDisplay::GetDisplayBrightnessSupport(bool *supported) {
1380 *supported = false;
1381 return HWC2::Error::None;
1382}
1383
1384HWC2::Error HwcDisplay::SetDisplayBrightness(float /* brightness */) {
1385 return HWC2::Error::Unsupported;
1386}
1387
Roman Stratiienko6b405052022-12-10 19:09:10 +02001388#endif /* __ANDROID_API__ > 28 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001389
Roman Stratiienko6b405052022-12-10 19:09:10 +02001390#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001391
1392HWC2::Error HwcDisplay::GetRenderIntents(
1393 int32_t mode, uint32_t *outNumIntents,
1394 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1395 if (mode != HAL_COLOR_MODE_NATIVE) {
1396 return HWC2::Error::BadParameter;
1397 }
1398
1399 if (outIntents == nullptr) {
1400 *outNumIntents = 1;
1401 return HWC2::Error::None;
1402 }
1403 *outNumIntents = 1;
1404 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1405 return HWC2::Error::None;
1406}
1407
1408HWC2::Error HwcDisplay::SetColorModeWithIntent(int32_t mode, int32_t intent) {
1409 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1410 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1411 return HWC2::Error::BadParameter;
1412
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001413 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1414 return HWC2::Error::Unsupported;
1415
Sasha McIntosh5294f092024-09-18 18:14:54 -04001416 auto err = SetColorMode(mode);
1417 if (err != HWC2::Error::None) return err;
1418
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001419 return HWC2::Error::None;
1420}
1421
Roman Stratiienko6b405052022-12-10 19:09:10 +02001422#endif /* __ANDROID_API__ > 27 */
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001423
1424const Backend *HwcDisplay::backend() const {
1425 return backend_.get();
1426}
1427
1428void HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1429 backend_ = std::move(backend);
1430}
1431
Roman Stratiienko3627beb2022-01-04 16:02:55 +02001432} // namespace android