blob: 42b000a7620fadf93652622d4fd5cbddf47463ee [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:
Roman Stratiienko3dacd472022-01-11 19:18:34 +020040 HwcDisplay(DrmDisplayPipeline *pipeline, hwc2_display_t handle,
41 HWC2::DisplayType type, DrmHwcTwo *hwc2);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020042 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020043 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020044
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
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200147 auto GetHwc2() {
148 return hwc2_;
149 }
150
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200151 std::map<hwc2_layer_t, HwcLayer> &layers() {
152 return layers_;
153 }
154
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200155 auto &GetPipe() {
156 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200157 }
158
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200159 android_color_transform_t &color_transform_hint() {
160 return color_transform_hint_;
161 }
162
163 Stats &total_stats() {
164 return total_stats_;
165 }
166
167 /* returns true if composition should be sent to client */
168 bool ProcessClientFlatteningState(bool skip) {
169 int flattenning_state = flattenning_state_;
170 if (flattenning_state == ClientFlattenningState::Disabled) {
171 return false;
172 }
173
174 if (skip) {
175 flattenning_state_ = ClientFlattenningState::NotRequired;
176 return false;
177 }
178
179 if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) {
180 flattenning_state_ = ClientFlattenningState::Flattened;
181 return true;
182 }
183
184 flattening_vsync_worker_.VSyncControl(true);
185 flattenning_state_ = ClientFlattenningState::VsyncCountdownMax;
186 return false;
187 }
188
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200189 /* Headless mode required to keep SurfaceFlinger alive when all display are
190 * disconnected, Without headless mode Android will continuously crash.
191 * Only single internal (primary) display is required to be in HEADLESS mode
192 * to prevent the crash. See:
193 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
194 */
195 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200196 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200197 }
198
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200199 private:
200 enum ClientFlattenningState : int32_t {
201 Disabled = -3,
202 NotRequired = -2,
203 Flattened = -1,
204 ClientRefreshRequested = 0,
205 VsyncCountdownMax = 60, /* 1 sec @ 60FPS */
206 };
207
208 std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired};
209 VSyncWorker flattening_vsync_worker_;
210
211 constexpr static size_t MATRIX_SIZE = 16;
212
Roman Stratiienko0137f862022-01-04 18:27:40 +0200213 HwcDisplayConfigs configs_;
214
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200215 DrmHwcTwo *const hwc2_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216
217 std::optional<DrmMode> staged_mode;
218
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200219 DrmDisplayPipeline *const pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200220
221 std::unique_ptr<Backend> backend_;
222
223 VSyncWorker vsync_worker_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200224
225 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200226 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200227
228 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
229 static uint32_t layer_idx_;
230
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200231 std::map<hwc2_layer_t, HwcLayer> layers_;
232 HwcLayer client_layer_;
233 int32_t color_mode_{};
234 std::array<float, MATRIX_SIZE> color_transform_matrix_{};
235 android_color_transform_t color_transform_hint_;
236
237 uint32_t frame_no_ = 0;
238 Stats total_stats_;
239 Stats prev_stats_;
240 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200241
242 HWC2::Error Init();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200243};
244
245} // namespace android
246
247#endif