blob: a315540bdf52ea523a968bf7e21d1b35aee5e173 [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 Davenport93443182023-12-14 09:25:45 +000044 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwc *hwc);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020045 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020046 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020047
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020048 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
Roman Stratiienko63762a92023-09-18 22:33:45 +030049 void SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline);
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020050
Roman Stratiienko3627beb2022-01-04 16:02:55 +020051 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
52 std::vector<HwcLayer *> GetOrderLayersByZPos();
53
54 void ClearDisplay();
55
56 std::string Dump();
57
Drew Davenportf7e88332024-09-06 12:54:38 -060058 const HwcDisplayConfigs &GetDisplayConfigs() const {
59 return configs_;
60 }
61
Drew Davenport9799ab82024-10-23 10:15:45 -060062 // Get the config representing the mode that has been committed to KMS.
63 const HwcDisplayConfig *GetCurrentConfig() const;
64
Drew Davenport85be25d2024-10-23 10:26:34 -060065 // Get the config that was last requested through SetActiveConfig and similar
66 // functions. This may differ from the GetCurrentConfig if the config change
67 // is queued up to take effect in the future.
68 const HwcDisplayConfig *GetLastRequestedConfig() const;
69
Drew Davenporta241a772024-09-24 11:26:30 -060070 // HWC2 Hooks - these should not be used outside of the hwc2 device.
Roman Stratiienko3627beb2022-01-04 16:02:55 +020071 HWC2::Error AcceptDisplayChanges();
72 HWC2::Error CreateLayer(hwc2_layer_t *layer);
73 HWC2::Error DestroyLayer(hwc2_layer_t layer);
74 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
75 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
76 hwc2_layer_t *layers, int32_t *types);
77 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
78 int32_t format, int32_t dataspace);
79 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
80 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
81 int32_t *value);
Drew Davenportf7e88332024-09-06 12:54:38 -060082 HWC2::Error LegacyGetDisplayConfigs(uint32_t *num_configs,
83 hwc2_config_t *configs);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020084 HWC2::Error GetDisplayName(uint32_t *size, char *name);
85 HWC2::Error GetDisplayRequests(int32_t *display_requests,
86 uint32_t *num_elements, hwc2_layer_t *layers,
87 int32_t *layer_requests);
88 HWC2::Error GetDisplayType(int32_t *type);
Roman Stratiienko6b405052022-12-10 19:09:10 +020089#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +020090 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
91 int32_t *outIntents);
92 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
93#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020094#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +020095 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
96 uint32_t *outDataSize,
97 uint8_t *outData);
98 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
99 uint32_t *outCapabilities);
100 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
101 HWC2::Error SetDisplayBrightness(float);
102#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +0200103#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200104 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200105
106 HWC2::Error SetActiveConfigWithConstraints(
107 hwc2_config_t config,
108 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
109 hwc_vsync_period_change_timeline_t *outTimeline);
110 HWC2::Error SetAutoLowLatencyMode(bool on);
111 HWC2::Error GetSupportedContentTypes(
112 uint32_t *outNumSupportedContentTypes,
113 const uint32_t *outSupportedContentTypes);
114
115 HWC2::Error SetContentType(int32_t contentType);
116#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +0200117 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200118
119 HWC2::Error GetDozeSupport(int32_t *support);
120 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
121 float *max_luminance,
122 float *max_average_luminance,
123 float *min_luminance);
124 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
125 int32_t *fences);
Roman Stratiienkodd214942022-05-03 18:24:49 +0300126 HWC2::Error PresentDisplay(int32_t *out_present_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200127 HWC2::Error SetActiveConfig(hwc2_config_t config);
128 HWC2::Error ChosePreferredConfig();
129 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
130 int32_t dataspace, hwc_region_t damage);
131 HWC2::Error SetColorMode(int32_t mode);
132 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
133 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
134 HWC2::Error SetPowerMode(int32_t mode);
135 HWC2::Error SetVsyncEnabled(int32_t enabled);
136 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
137 HwcLayer *get_layer(hwc2_layer_t layer) {
138 auto it = layers_.find(layer);
139 if (it == layers_.end())
140 return nullptr;
141 return &it->second;
142 }
143
144 /* Statistics */
145 struct Stats {
146 Stats minus(Stats b) const {
147 return {total_frames_ - b.total_frames_,
148 total_pixops_ - b.total_pixops_,
149 gpu_pixops_ - b.gpu_pixops_,
150 failed_kms_validate_ - b.failed_kms_validate_,
151 failed_kms_present_ - b.failed_kms_present_,
152 frames_flattened_ - b.frames_flattened_};
153 }
154
155 uint32_t total_frames_ = 0;
156 uint64_t total_pixops_ = 0;
157 uint64_t gpu_pixops_ = 0;
158 uint32_t failed_kms_validate_ = 0;
159 uint32_t failed_kms_present_ = 0;
160 uint32_t frames_flattened_ = 0;
161 };
162
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200163 const Backend *backend() const;
164 void set_backend(std::unique_ptr<Backend> backend);
165
Drew Davenport93443182023-12-14 09:25:45 +0000166 auto GetHwc() {
167 return hwc_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200168 }
169
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200170 std::map<hwc2_layer_t, HwcLayer> &layers() {
171 return layers_;
172 }
173
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200174 auto &GetPipe() {
175 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200176 }
177
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200178 bool CtmByGpu();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200179
180 Stats &total_stats() {
181 return total_stats_;
182 }
183
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200184 /* Headless mode required to keep SurfaceFlinger alive when all display are
185 * disconnected, Without headless mode Android will continuously crash.
186 * Only single internal (primary) display is required to be in HEADLESS mode
187 * to prevent the crash. See:
188 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
189 */
190 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200191 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200192 }
193
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200194 void Deinit();
195
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200196 auto GetFlatCon() {
197 return flatcon_;
198 }
199
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300200 auto &GetWritebackLayer() {
201 return writeback_layer_;
202 }
203
204 void SetVirtualDisplayResolution(uint16_t width, uint16_t height) {
205 virtual_disp_width_ = width;
206 virtual_disp_height_ = height;
207 }
208
Tim Van Pattena2f3efa2024-10-15 17:44:54 -0600209 auto getDisplayPhysicalOrientation() -> std::optional<PanelOrientation>;
210
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 private:
Roman Stratiienko0137f862022-01-04 18:27:40 +0200212 HwcDisplayConfigs configs_;
213
Drew Davenport93443182023-12-14 09:25:45 +0000214 DrmHwc *const hwc_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200215
Roman Stratiienko76892782023-01-16 17:15:53 +0200216 SharedFd present_fence_;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300217
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200218 std::optional<DrmMode> staged_mode_;
219 int64_t staged_mode_change_time_{};
220 uint32_t staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200221
Roman Stratiienko63762a92023-09-18 22:33:45 +0300222 std::shared_ptr<DrmDisplayPipeline> pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200223
224 std::unique_ptr<Backend> backend_;
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200225 std::shared_ptr<FlatteningController> flatcon_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200226
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200227 std::shared_ptr<VSyncWorker> vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200228 bool vsync_event_en_{};
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200229 bool vsync_tracking_en_{};
230 int64_t last_vsync_ts_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200231
232 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200233 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200234
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200235 uint32_t layer_idx_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200236
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200237 std::map<hwc2_layer_t, HwcLayer> layers_;
238 HwcLayer client_layer_;
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300239 std::unique_ptr<HwcLayer> writeback_layer_;
240 uint16_t virtual_disp_width_{};
241 uint16_t virtual_disp_height_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200242 int32_t color_mode_{};
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200243 static constexpr int kCtmRows = 3;
244 static constexpr int kCtmCols = 3;
245 std::shared_ptr<drm_color_ctm> color_matrix_;
246 android_color_transform_t color_transform_hint_{};
Sasha McIntosh173247b2024-09-18 18:06:52 -0400247 int32_t content_type_{};
Sasha McIntosh5294f092024-09-18 18:14:54 -0400248 Colorspace colorspace_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200249
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200250 std::shared_ptr<DrmKmsPlan> current_plan_;
251
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200252 uint32_t frame_no_ = 0;
253 Stats total_stats_;
254 Stats prev_stats_;
255 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200256
Sasha McIntosha37df7c2024-09-20 12:31:08 -0400257 void SetColorMatrixToIdentity();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200258
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200259 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200260
261 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200262};
263
264} // namespace android