blob: 76456b72f1c7004ad59806b5b1a61f6086908725 [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
17#ifndef ANDROID_HWC2_DEVICE_HWC_DISPLAY_H
18#define ANDROID_HWC2_DEVICE_HWC_DISPLAY_H
19
20#include <hardware/hwcomposer2.h>
21
22#include <optional>
23
Roman Stratiienko0137f862022-01-04 18:27:40 +020024#include "HwcDisplayConfigs.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020025#include "compositor/DrmDisplayCompositor.h"
26#include "drm/ResourceManager.h"
27#include "drm/VSyncWorker.h"
28#include "drmhwcomposer.h"
29#include "hwc2_device/HwcLayer.h"
30
31namespace android {
32
33class Backend;
34class DrmHwcTwo;
35
Roman Stratiienko456e2d62022-01-29 01:17:39 +020036inline constexpr uint32_t kPrimaryDisplay = 0;
37
Roman Stratiienko3627beb2022-01-04 16:02:55 +020038class HwcDisplay {
39 public:
40 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
41 hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2);
42 HwcDisplay(const HwcDisplay &) = delete;
43 HWC2::Error Init(std::vector<DrmPlane *> *planes);
44
45 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
46 std::vector<HwcLayer *> GetOrderLayersByZPos();
47
48 void ClearDisplay();
49
50 std::string Dump();
51
52 // HWC Hooks
53 HWC2::Error AcceptDisplayChanges();
54 HWC2::Error CreateLayer(hwc2_layer_t *layer);
55 HWC2::Error DestroyLayer(hwc2_layer_t layer);
56 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
57 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
58 hwc2_layer_t *layers, int32_t *types);
59 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
60 int32_t format, int32_t dataspace);
61 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
62 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
63 int32_t *value);
64 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs);
65 HWC2::Error GetDisplayName(uint32_t *size, char *name);
66 HWC2::Error GetDisplayRequests(int32_t *display_requests,
67 uint32_t *num_elements, hwc2_layer_t *layers,
68 int32_t *layer_requests);
69 HWC2::Error GetDisplayType(int32_t *type);
70#if PLATFORM_SDK_VERSION > 27
71 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
72 int32_t *outIntents);
73 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
74#endif
75#if PLATFORM_SDK_VERSION > 28
76 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
77 uint32_t *outDataSize,
78 uint8_t *outData);
79 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
80 uint32_t *outCapabilities);
81 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
82 HWC2::Error SetDisplayBrightness(float);
83#endif
84#if PLATFORM_SDK_VERSION > 29
85 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
86 HWC2::Error GetDisplayVsyncPeriod(hwc2_vsync_period_t *outVsyncPeriod);
87
88 HWC2::Error SetActiveConfigWithConstraints(
89 hwc2_config_t config,
90 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
91 hwc_vsync_period_change_timeline_t *outTimeline);
92 HWC2::Error SetAutoLowLatencyMode(bool on);
93 HWC2::Error GetSupportedContentTypes(
94 uint32_t *outNumSupportedContentTypes,
95 const uint32_t *outSupportedContentTypes);
96
97 HWC2::Error SetContentType(int32_t contentType);
98#endif
99
100 HWC2::Error GetDozeSupport(int32_t *support);
101 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
102 float *max_luminance,
103 float *max_average_luminance,
104 float *min_luminance);
105 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
106 int32_t *fences);
107 HWC2::Error PresentDisplay(int32_t *present_fence);
108 HWC2::Error SetActiveConfig(hwc2_config_t config);
109 HWC2::Error ChosePreferredConfig();
110 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
111 int32_t dataspace, hwc_region_t damage);
112 HWC2::Error SetColorMode(int32_t mode);
113 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
114 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
115 HWC2::Error SetPowerMode(int32_t mode);
116 HWC2::Error SetVsyncEnabled(int32_t enabled);
117 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
118 HwcLayer *get_layer(hwc2_layer_t layer) {
119 auto it = layers_.find(layer);
120 if (it == layers_.end())
121 return nullptr;
122 return &it->second;
123 }
124
125 /* Statistics */
126 struct Stats {
127 Stats minus(Stats b) const {
128 return {total_frames_ - b.total_frames_,
129 total_pixops_ - b.total_pixops_,
130 gpu_pixops_ - b.gpu_pixops_,
131 failed_kms_validate_ - b.failed_kms_validate_,
132 failed_kms_present_ - b.failed_kms_present_,
133 frames_flattened_ - b.frames_flattened_};
134 }
135
136 uint32_t total_frames_ = 0;
137 uint64_t total_pixops_ = 0;
138 uint64_t gpu_pixops_ = 0;
139 uint32_t failed_kms_validate_ = 0;
140 uint32_t failed_kms_present_ = 0;
141 uint32_t frames_flattened_ = 0;
142 };
143
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200144 const Backend *backend() const;
145 void set_backend(std::unique_ptr<Backend> backend);
146
147 const std::vector<DrmPlane *> &primary_planes() const {
148 return primary_planes_;
149 }
150
151 const std::vector<DrmPlane *> &overlay_planes() const {
152 return overlay_planes_;
153 }
154
155 std::map<hwc2_layer_t, HwcLayer> &layers() {
156 return layers_;
157 }
158
159 const DrmDisplayCompositor &compositor() const {
160 return compositor_;
161 }
162
163 const DrmDevice *drm() const {
164 return drm_;
165 }
166
167 const DrmConnector *connector() const {
168 return connector_;
169 }
170
171 ResourceManager *resource_manager() const {
172 return resource_manager_;
173 }
174
175 android_color_transform_t &color_transform_hint() {
176 return color_transform_hint_;
177 }
178
179 Stats &total_stats() {
180 return total_stats_;
181 }
182
183 /* returns true if composition should be sent to client */
184 bool ProcessClientFlatteningState(bool skip) {
185 int flattenning_state = flattenning_state_;
186 if (flattenning_state == ClientFlattenningState::Disabled) {
187 return false;
188 }
189
190 if (skip) {
191 flattenning_state_ = ClientFlattenningState::NotRequired;
192 return false;
193 }
194
195 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
196 flattenning_state_ = ClientFlattenningState::Flattened;
197 return true;
198 }
199
200 flattening_vsync_worker_.VSyncControl(true);
201 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
202 return false;
203 }
204
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200205 /* Headless mode required to keep SurfaceFlinger alive when all display are
206 * disconnected, Without headless mode Android will continuously crash.
207 * Only single internal (primary) display is required to be in HEADLESS mode
208 * to prevent the crash. See:
209 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
210 */
211 bool IsInHeadlessMode() {
Roman Stratiienko456e2d62022-01-29 01:17:39 +0200212 return handle_ == kPrimaryDisplay &&
213 connector_->state() != DRM_MODE_CONNECTED;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200214 }
215
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216 private:
217 enum ClientFlattenningState : int32_t {
218 Disabled = -3,
219 NotRequired = -2,
220 Flattened = -1,
221 ClientRefreshRequested = 0,
222 VsyncCountdownMax = 60, /* 1 sec @ 60FPS */
223 };
224
225 std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired};
226 VSyncWorker flattening_vsync_worker_;
227
228 constexpr static size_t MATRIX_SIZE = 16;
229
Roman Stratiienko0137f862022-01-04 18:27:40 +0200230 HwcDisplayConfigs configs_;
231
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232 DrmHwcTwo *hwc2_;
233
234 std::optional<DrmMode> staged_mode;
235
236 ResourceManager *resource_manager_;
237 DrmDevice *drm_;
238 DrmDisplayCompositor compositor_;
239
240 std::vector<DrmPlane *> primary_planes_;
241 std::vector<DrmPlane *> overlay_planes_;
242
243 std::unique_ptr<Backend> backend_;
244
245 VSyncWorker vsync_worker_;
246 DrmConnector *connector_ = nullptr;
247 DrmCrtc *crtc_ = nullptr;
248 hwc2_display_t handle_;
249 HWC2::DisplayType type_;
250 uint32_t layer_idx_ = 0;
251 std::map<hwc2_layer_t, HwcLayer> layers_;
252 HwcLayer client_layer_;
253 int32_t color_mode_{};
254 std::array<float, MATRIX_SIZE> color_transform_matrix_{};
255 android_color_transform_t color_transform_hint_;
256
257 uint32_t frame_no_ = 0;
258 Stats total_stats_;
259 Stats prev_stats_;
260 std::string DumpDelta(HwcDisplay::Stats delta);
261};
262
263} // namespace android
264
265#endif