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