blob: 9c113fdba5d8df8c01e1e27989f0da14788db966 [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
Roman Stratiienko0137f862022-01-04 18:27:40 +020025#include "HwcDisplayConfigs.h"
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060026#include "compositor/DisplayInfo.h"
Roman Stratiienko22fe9612023-01-17 21:22:29 +020027#include "compositor/FlatteningController.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020028#include "compositor/LayerData.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020029#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020030#include "drm/ResourceManager.h"
31#include "drm/VSyncWorker.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020032#include "hwc2_device/HwcLayer.h"
33
34namespace android {
35
36class Backend;
Drew Davenport93443182023-12-14 09:25:45 +000037class DrmHwc;
Roman Stratiienko3627beb2022-01-04 16:02:55 +020038
Roman Stratiienko456e2d62022-01-29 01:17:39 +020039inline constexpr uint32_t kPrimaryDisplay = 0;
40
Roman Stratiienko2a93e4c2022-12-19 18:24:47 +020041// NOLINTNEXTLINE
Roman Stratiienko3627beb2022-01-04 16:02:55 +020042class HwcDisplay {
43 public:
Drew Davenport8998f8b2024-10-24 10:15:12 -060044 enum ConfigError {
45 kNone,
46 kBadConfig,
47 kSeamlessNotAllowed,
48 kSeamlessNotPossible
49 };
50
Drew Davenport93443182023-12-14 09:25:45 +000051 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwc *hwc);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020052 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020053 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020054
Drew Davenport76c17a82025-01-15 15:02:45 -070055 void SetColorTransformMatrix(
56 const std::array<float, 16> &color_transform_matrix);
57
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020058 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
Roman Stratiienko63762a92023-09-18 22:33:45 +030059 void SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020060
Roman Stratiienko3627beb2022-01-04 16:02:55 +020061 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
62 std::vector<HwcLayer *> GetOrderLayersByZPos();
63
64 void ClearDisplay();
65
66 std::string Dump();
67
Drew Davenportf7e88332024-09-06 12:54:38 -060068 const HwcDisplayConfigs &GetDisplayConfigs() const {
69 return configs_;
70 }
71
Drew Davenport9799ab82024-10-23 10:15:45 -060072 // Get the config representing the mode that has been committed to KMS.
Drew Davenport8998f8b2024-10-24 10:15:12 -060073 auto GetCurrentConfig() const -> const HwcDisplayConfig *;
Drew Davenport9799ab82024-10-23 10:15:45 -060074
Drew Davenport85be25d2024-10-23 10:26:34 -060075 // Get the config that was last requested through SetActiveConfig and similar
76 // functions. This may differ from the GetCurrentConfig if the config change
77 // is queued up to take effect in the future.
Drew Davenport8998f8b2024-10-24 10:15:12 -060078 auto GetLastRequestedConfig() const -> const HwcDisplayConfig *;
79
Drew Davenport97b5abc2024-11-07 10:43:54 -070080 // Set a config synchronously. If the requested config fails to be committed,
81 // this will return with an error. Otherwise, the config will have been
82 // committed to the kernel on successful return.
83 ConfigError SetConfig(hwc2_config_t config);
84
Drew Davenport8998f8b2024-10-24 10:15:12 -060085 // Queue a configuration change to take effect in the future.
86 auto QueueConfig(hwc2_config_t config, int64_t desired_time, bool seamless,
87 QueuedConfigTiming *out_timing) -> ConfigError;
Drew Davenport85be25d2024-10-23 10:26:34 -060088
Drew Davenportfe70c802024-11-07 13:02:21 -070089 // Get the HwcDisplayConfig, or nullptor if none.
90 auto GetConfig(hwc2_config_t config_id) const -> const HwcDisplayConfig *;
91
Drew Davenport7f356c82025-01-17 16:32:06 -070092 // To be called after SetDisplayProperties. Returns an empty vector if the
93 // requested layers have been validated, otherwise the vector describes
94 // the requested composition type changes.
95 using ChangedLayer = std::pair<hwc2_layer_t, HWC2::Composition>;
96 auto ValidateStagedComposition() -> std::vector<ChangedLayer>;
97
Drew Davenporta241a772024-09-24 11:26:30 -060098 // HWC2 Hooks - these should not be used outside of the hwc2 device.
Roman Stratiienko3627beb2022-01-04 16:02:55 +020099 HWC2::Error AcceptDisplayChanges();
100 HWC2::Error CreateLayer(hwc2_layer_t *layer);
101 HWC2::Error DestroyLayer(hwc2_layer_t layer);
102 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
103 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
104 hwc2_layer_t *layers, int32_t *types);
105 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
106 int32_t format, int32_t dataspace);
107 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
108 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
109 int32_t *value);
Drew Davenportf7e88332024-09-06 12:54:38 -0600110 HWC2::Error LegacyGetDisplayConfigs(uint32_t *num_configs,
111 hwc2_config_t *configs);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200112 HWC2::Error GetDisplayName(uint32_t *size, char *name);
113 HWC2::Error GetDisplayRequests(int32_t *display_requests,
114 uint32_t *num_elements, hwc2_layer_t *layers,
115 int32_t *layer_requests);
116 HWC2::Error GetDisplayType(int32_t *type);
Roman Stratiienko6b405052022-12-10 19:09:10 +0200117#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200118 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
119 int32_t *outIntents);
120 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
121#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200122#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200123 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
124 uint32_t *outDataSize,
125 uint8_t *outData);
126 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
127 uint32_t *outCapabilities);
128 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
129 HWC2::Error SetDisplayBrightness(float);
130#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200131#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200132 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200133
134 HWC2::Error SetActiveConfigWithConstraints(
135 hwc2_config_t config,
136 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
137 hwc_vsync_period_change_timeline_t *outTimeline);
138 HWC2::Error SetAutoLowLatencyMode(bool on);
139 HWC2::Error GetSupportedContentTypes(
140 uint32_t *outNumSupportedContentTypes,
141 const uint32_t *outSupportedContentTypes);
142
143 HWC2::Error SetContentType(int32_t contentType);
144#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +0200145 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200146
147 HWC2::Error GetDozeSupport(int32_t *support);
148 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
149 float *max_luminance,
150 float *max_average_luminance,
151 float *min_luminance);
152 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
153 int32_t *fences);
Roman Stratiienkodd214942022-05-03 18:24:49 +0300154 HWC2::Error PresentDisplay(int32_t *out_present_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200155 HWC2::Error SetActiveConfig(hwc2_config_t config);
156 HWC2::Error ChosePreferredConfig();
157 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
158 int32_t dataspace, hwc_region_t damage);
159 HWC2::Error SetColorMode(int32_t mode);
160 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
161 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
162 HWC2::Error SetPowerMode(int32_t mode);
163 HWC2::Error SetVsyncEnabled(int32_t enabled);
164 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
165 HwcLayer *get_layer(hwc2_layer_t layer) {
166 auto it = layers_.find(layer);
167 if (it == layers_.end())
168 return nullptr;
169 return &it->second;
170 }
171
172 /* Statistics */
173 struct Stats {
174 Stats minus(Stats b) const {
175 return {total_frames_ - b.total_frames_,
176 total_pixops_ - b.total_pixops_,
177 gpu_pixops_ - b.gpu_pixops_,
178 failed_kms_validate_ - b.failed_kms_validate_,
179 failed_kms_present_ - b.failed_kms_present_,
180 frames_flattened_ - b.frames_flattened_};
181 }
182
183 uint32_t total_frames_ = 0;
184 uint64_t total_pixops_ = 0;
185 uint64_t gpu_pixops_ = 0;
186 uint32_t failed_kms_validate_ = 0;
187 uint32_t failed_kms_present_ = 0;
188 uint32_t frames_flattened_ = 0;
189 };
190
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200191 const Backend *backend() const;
192 void set_backend(std::unique_ptr<Backend> backend);
193
Drew Davenport93443182023-12-14 09:25:45 +0000194 auto GetHwc() {
195 return hwc_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200196 }
197
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200198 std::map<hwc2_layer_t, HwcLayer> &layers() {
199 return layers_;
200 }
201
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200202 auto &GetPipe() {
203 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200204 }
205
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200206 bool CtmByGpu();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207
208 Stats &total_stats() {
209 return total_stats_;
210 }
211
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200212 /* Headless mode required to keep SurfaceFlinger alive when all display are
213 * disconnected, Without headless mode Android will continuously crash.
214 * Only single internal (primary) display is required to be in HEADLESS mode
215 * to prevent the crash. See:
216 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
217 */
218 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200219 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200220 }
221
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200222 void Deinit();
223
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200224 auto GetFlatCon() {
225 return flatcon_;
226 }
227
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300228 auto &GetWritebackLayer() {
229 return writeback_layer_;
230 }
231
232 void SetVirtualDisplayResolution(uint16_t width, uint16_t height) {
233 virtual_disp_width_ = width;
234 virtual_disp_height_ = height;
235 }
236
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600237 auto getDisplayPhysicalOrientation() -> std::optional<PanelOrientation>;
238
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200239 private:
Drew Davenport97b5abc2024-11-07 10:43:54 -0700240 AtomicCommitArgs CreateModesetCommit(
241 const HwcDisplayConfig *config,
242 const std::optional<LayerData> &modeset_layer);
243
Roman Stratiienko0137f862022-01-04 18:27:40 +0200244 HwcDisplayConfigs configs_;
245
Drew Davenport93443182023-12-14 09:25:45 +0000246 DrmHwc *const hwc_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200247
Roman Stratiienko76892782023-01-16 17:15:53 +0200248 SharedFd present_fence_;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300249
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200250 int64_t staged_mode_change_time_{};
Drew Davenportfe70c802024-11-07 13:02:21 -0700251 std::optional<uint32_t> staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200252
Roman Stratiienko63762a92023-09-18 22:33:45 +0300253 std::shared_ptr<DrmDisplayPipeline> pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200254
255 std::unique_ptr<Backend> backend_;
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200256 std::shared_ptr<FlatteningController> flatcon_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200257
Drew Davenport30f4e9c2024-12-16 16:12:11 -0700258 std::unique_ptr<VSyncWorker> vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200259 bool vsync_event_en_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200260
261 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200262 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200263
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200264 uint32_t layer_idx_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200265
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200266 std::map<hwc2_layer_t, HwcLayer> layers_;
267 HwcLayer client_layer_;
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300268 std::unique_ptr<HwcLayer> writeback_layer_;
269 uint16_t virtual_disp_width_{};
270 uint16_t virtual_disp_height_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200271 int32_t color_mode_{};
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200272 std::shared_ptr<drm_color_ctm> color_matrix_;
273 android_color_transform_t color_transform_hint_{};
Sasha McIntosh173247b2024-09-18 18:06:52 -0400274 int32_t content_type_{};
Sasha McIntosh5294f092024-09-18 18:14:54 -0400275 Colorspace colorspace_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200276
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200277 std::shared_ptr<DrmKmsPlan> current_plan_;
278
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200279 uint32_t frame_no_ = 0;
280 Stats total_stats_;
281 Stats prev_stats_;
282 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200283
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400284 void SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200285
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200286 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200287
288 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200289};
290
291} // namespace android