blob: d493597de459fc94d1777d2896d96056591934a5 [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
Roman Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Roman Stratiienko3627beb2022-01-04 16:02:55 +020018
19#include <hardware/hwcomposer2.h>
20
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020021#include <atomic>
Roman Stratiienko3627beb2022-01-04 16:02:55 +020022#include <optional>
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020023#include <sstream>
Roman Stratiienko3627beb2022-01-04 16:02:55 +020024
Sasha McIntosh851ea4d2024-12-04 17:14:55 -050025#include <ui/GraphicTypes.h>
26
Roman Stratiienko0137f862022-01-04 18:27:40 +020027#include "HwcDisplayConfigs.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060028#include "compositor/DisplayInfo.h"
Roman Stratiienko22fe9612023-01-17 21:22:29 +020029#include "compositor/FlatteningController.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020030#include "compositor/LayerData.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020031#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020032#include "drm/ResourceManager.h"
33#include "drm/VSyncWorker.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020034#include "hwc2_device/HwcLayer.h"
35
36namespace android {
37
38class Backend;
Drew Davenport93443182023-12-14 09:25:45 +000039class DrmHwc;
Roman Stratiienko3627beb2022-01-04 16:02:55 +020040
Roman Stratiienkoe501a972025-01-26 14:10:31 +020041class FrontendDisplayBase {
42 public:
43 virtual ~FrontendDisplayBase() = default;
44};
45
Roman Stratiienko456e2d62022-01-29 01:17:39 +020046inline constexpr uint32_t kPrimaryDisplay = 0;
47
Roman Stratiienko2a93e4c2022-12-19 18:24:47 +020048// NOLINTNEXTLINE
Roman Stratiienko3627beb2022-01-04 16:02:55 +020049class HwcDisplay {
50 public:
Drew Davenport8998f8b2024-10-24 10:15:12 -060051 enum ConfigError {
52 kNone,
53 kBadConfig,
54 kSeamlessNotAllowed,
Manasi Navare23bcfb72025-01-29 22:57:19 +000055 kSeamlessNotPossible,
56 kConfigFailed,
Drew Davenport8998f8b2024-10-24 10:15:12 -060057 };
58
Drew Davenport93443182023-12-14 09:25:45 +000059 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwc *hwc);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020060 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020061 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020062
Drew Davenport76c17a82025-01-15 15:02:45 -070063 void SetColorTransformMatrix(
64 const std::array<float, 16> &color_transform_matrix);
65
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020066 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
Roman Stratiienko63762a92023-09-18 22:33:45 +030067 void SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020068
Roman Stratiienko3627beb2022-01-04 16:02:55 +020069 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
70 std::vector<HwcLayer *> GetOrderLayersByZPos();
71
72 void ClearDisplay();
73
74 std::string Dump();
75
Drew Davenportf7e88332024-09-06 12:54:38 -060076 const HwcDisplayConfigs &GetDisplayConfigs() const {
77 return configs_;
78 }
79
Drew Davenport9799ab82024-10-23 10:15:45 -060080 // Get the config representing the mode that has been committed to KMS.
Drew Davenport8998f8b2024-10-24 10:15:12 -060081 auto GetCurrentConfig() const -> const HwcDisplayConfig *;
Drew Davenport9799ab82024-10-23 10:15:45 -060082
Drew Davenport85be25d2024-10-23 10:26:34 -060083 // Get the config that was last requested through SetActiveConfig and similar
84 // functions. This may differ from the GetCurrentConfig if the config change
85 // is queued up to take effect in the future.
Drew Davenport8998f8b2024-10-24 10:15:12 -060086 auto GetLastRequestedConfig() const -> const HwcDisplayConfig *;
87
Drew Davenport97b5abc2024-11-07 10:43:54 -070088 // Set a config synchronously. If the requested config fails to be committed,
89 // this will return with an error. Otherwise, the config will have been
90 // committed to the kernel on successful return.
91 ConfigError SetConfig(hwc2_config_t config);
92
Drew Davenport8998f8b2024-10-24 10:15:12 -060093 // Queue a configuration change to take effect in the future.
94 auto QueueConfig(hwc2_config_t config, int64_t desired_time, bool seamless,
95 QueuedConfigTiming *out_timing) -> ConfigError;
Drew Davenport85be25d2024-10-23 10:26:34 -060096
Drew Davenportfe70c802024-11-07 13:02:21 -070097 // Get the HwcDisplayConfig, or nullptor if none.
98 auto GetConfig(hwc2_config_t config_id) const -> const HwcDisplayConfig *;
99
Lucas Berthou30808a22025-02-05 17:52:33 +0000100 auto GetDisplayBoundsMm() -> std::pair<int32_t, int32_t>;
Drew Davenportf6383482025-02-21 10:05:39 -0700101
Drew Davenport7f356c82025-01-17 16:32:06 -0700102 // To be called after SetDisplayProperties. Returns an empty vector if the
103 // requested layers have been validated, otherwise the vector describes
104 // the requested composition type changes.
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200105 using ChangedLayer = std::pair<ILayerId, HWC2::Composition>;
Drew Davenport7f356c82025-01-17 16:32:06 -0700106 auto ValidateStagedComposition() -> std::vector<ChangedLayer>;
107
Drew Davenportb864ccf2025-01-22 14:57:36 -0700108 // Mark previously validated properties as ready to present.
109 auto AcceptValidatedComposition() -> void;
110
Drew Davenportf6383482025-02-21 10:05:39 -0700111 using ReleaseFence = std::pair<ILayerId, SharedFd>;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700112 // Present previously staged properties, and return fences to indicate when
113 // the new content has been presented, and when the previous buffers have
Drew Davenportf6383482025-02-21 10:05:39 -0700114 // been released. If |desired_present_time| is set, ensure that the
115 // composition is presented at the closest vsync to that requested time.
116 // Otherwise, present immediately.
117 auto PresentStagedComposition(std::optional<int64_t> desired_present_time,
118 SharedFd &out_present_fence,
Roman Stratiienko16d3a2d2025-01-27 17:09:22 +0200119 std::vector<ReleaseFence> &out_release_fences)
120 -> bool;
Drew Davenportb864ccf2025-01-22 14:57:36 -0700121
Roman Stratiienkoe501a972025-01-26 14:10:31 +0200122 auto GetFrontendPrivateData() -> std::shared_ptr<FrontendDisplayBase> {
123 return frontend_private_data_;
124 }
125
126 auto SetFrontendPrivateData(std::shared_ptr<FrontendDisplayBase> data) {
127 frontend_private_data_ = std::move(data);
128 }
129
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200130 auto CreateLayer(ILayerId new_layer_id) -> bool;
131 auto DestroyLayer(ILayerId layer_id) -> bool;
132
Drew Davenporta241a772024-09-24 11:26:30 -0600133 // HWC2 Hooks - these should not be used outside of the hwc2 device.
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200134 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200135 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
136 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
137 int32_t *value);
Drew Davenportf7e88332024-09-06 12:54:38 -0600138 HWC2::Error LegacyGetDisplayConfigs(uint32_t *num_configs,
139 hwc2_config_t *configs);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200140 HWC2::Error GetDisplayName(uint32_t *size, char *name);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200141 HWC2::Error GetDisplayType(int32_t *type);
Roman Stratiienko6b405052022-12-10 19:09:10 +0200142#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200143 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
144 int32_t *outIntents);
145 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
146#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200147#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200148 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
149 uint32_t *outDataSize,
150 uint8_t *outData);
151 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
152 uint32_t *outCapabilities);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200153#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200154#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200155 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200156
157 HWC2::Error SetActiveConfigWithConstraints(
158 hwc2_config_t config,
159 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
160 hwc_vsync_period_change_timeline_t *outTimeline);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200161
162 HWC2::Error SetContentType(int32_t contentType);
163#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +0200164 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200165
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200166 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
167 float *max_luminance,
168 float *max_average_luminance,
169 float *min_luminance);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200170 HWC2::Error SetActiveConfig(hwc2_config_t config);
171 HWC2::Error ChosePreferredConfig();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200172 HWC2::Error SetColorMode(int32_t mode);
173 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200174 HWC2::Error SetPowerMode(int32_t mode);
175 HWC2::Error SetVsyncEnabled(int32_t enabled);
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200176 HwcLayer *get_layer(ILayerId layer) {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200177 auto it = layers_.find(layer);
178 if (it == layers_.end())
179 return nullptr;
180 return &it->second;
181 }
182
183 /* Statistics */
184 struct Stats {
185 Stats minus(Stats b) const {
186 return {total_frames_ - b.total_frames_,
187 total_pixops_ - b.total_pixops_,
188 gpu_pixops_ - b.gpu_pixops_,
189 failed_kms_validate_ - b.failed_kms_validate_,
190 failed_kms_present_ - b.failed_kms_present_,
191 frames_flattened_ - b.frames_flattened_};
192 }
193
194 uint32_t total_frames_ = 0;
195 uint64_t total_pixops_ = 0;
196 uint64_t gpu_pixops_ = 0;
197 uint32_t failed_kms_validate_ = 0;
198 uint32_t failed_kms_present_ = 0;
199 uint32_t frames_flattened_ = 0;
200 };
201
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200202 const Backend *backend() const;
203 void set_backend(std::unique_ptr<Backend> backend);
204
Drew Davenport93443182023-12-14 09:25:45 +0000205 auto GetHwc() {
206 return hwc_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200207 }
208
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200209 auto layers() -> std::map<ILayerId, HwcLayer> & {
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200210 return layers_;
211 }
212
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200213 auto &GetPipe() {
214 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200215 }
216
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200217 bool CtmByGpu();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218
219 Stats &total_stats() {
220 return total_stats_;
221 }
222
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200223 /* Headless mode required to keep SurfaceFlinger alive when all display are
224 * disconnected, Without headless mode Android will continuously crash.
225 * Only single internal (primary) display is required to be in HEADLESS mode
226 * to prevent the crash. See:
227 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
228 */
229 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200230 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200231 }
232
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200233 void Deinit();
234
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200235 auto GetFlatCon() {
236 return flatcon_;
237 }
238
Roman Stratiienko70ab9392025-01-23 12:11:48 +0200239 auto GetClientLayer() -> HwcLayer & {
240 return client_layer_;
241 }
242
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300243 auto &GetWritebackLayer() {
244 return writeback_layer_;
245 }
246
247 void SetVirtualDisplayResolution(uint16_t width, uint16_t height) {
248 virtual_disp_width_ = width;
249 virtual_disp_height_ = height;
250 }
251
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600252 auto getDisplayPhysicalOrientation() -> std::optional<PanelOrientation>;
253
Roman Stratiienko45cdacc2025-02-12 04:26:10 +0200254 bool NeedsClientLayerUpdate() const;
255
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200256 private:
Drew Davenport97b5abc2024-11-07 10:43:54 -0700257 AtomicCommitArgs CreateModesetCommit(
258 const HwcDisplayConfig *config,
259 const std::optional<LayerData> &modeset_layer);
260
Drew Davenportf6383482025-02-21 10:05:39 -0700261 // Sleep the current thread until |present_time| is closest to the next
262 // expected vsync time.
263 void WaitForPresentTime(int64_t present_time, uint32_t vsync_period_ns);
264
Roman Stratiienko0137f862022-01-04 18:27:40 +0200265 HwcDisplayConfigs configs_;
266
Drew Davenport93443182023-12-14 09:25:45 +0000267 DrmHwc *const hwc_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200268
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200269 int64_t staged_mode_change_time_{};
Drew Davenportfe70c802024-11-07 13:02:21 -0700270 std::optional<uint32_t> staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200271
Roman Stratiienko63762a92023-09-18 22:33:45 +0300272 std::shared_ptr<DrmDisplayPipeline> pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200273
274 std::unique_ptr<Backend> backend_;
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200275 std::shared_ptr<FlatteningController> flatcon_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200276
Drew Davenport30f4e9c2024-12-16 16:12:11 -0700277 std::unique_ptr<VSyncWorker> vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200278 bool vsync_event_en_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200279
280 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200281 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200282
Roman Stratiienkoee6d8432025-02-14 06:25:30 +0200283 std::map<ILayerId, HwcLayer> layers_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200284 HwcLayer client_layer_;
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300285 std::unique_ptr<HwcLayer> writeback_layer_;
286 uint16_t virtual_disp_width_{};
287 uint16_t virtual_disp_height_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200288 int32_t color_mode_{};
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200289 std::shared_ptr<drm_color_ctm> color_matrix_;
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500290 std::shared_ptr<drm_color_ctm> identity_color_matrix_;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200291 android_color_transform_t color_transform_hint_{};
Sasha McIntosh12d302c2025-01-30 15:29:06 -0500292 bool ctm_has_offset_ = false;
Sasha McIntosh173247b2024-09-18 18:06:52 -0400293 int32_t content_type_{};
Sasha McIntosh5294f092024-09-18 18:14:54 -0400294 Colorspace colorspace_{};
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500295 int32_t min_bpc_{};
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500296 std::shared_ptr<hdr_output_metadata> hdr_metadata_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200297
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200298 std::shared_ptr<DrmKmsPlan> current_plan_;
299
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200300 uint32_t frame_no_ = 0;
301 Stats total_stats_;
302 Stats prev_stats_;
303 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200304
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400305 void SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200306
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200307 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200308
309 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Sasha McIntoshf9062b62024-11-12 10:55:06 -0500310 HWC2::Error SetHdrOutputMetadata(ui::Hdr hdrType);
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -0500311 HWC2::Error SetOutputType(uint32_t hdr_output_type);
312
Sasha McIntoshf8c14112024-12-11 12:03:32 -0500313 auto GetEdid() -> EdidWrapperUnique & {
314 return GetPipe().connector->Get()->GetParsedEdid();
315 }
Roman Stratiienkoe501a972025-01-26 14:10:31 +0200316
317 std::shared_ptr<FrontendDisplayBase> frontend_private_data_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200318};
319
320} // namespace android