blob: e7ce7ef00b95f0112373d576dd25c50dcd2bb419 [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);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020086
87 HWC2::Error SetActiveConfigWithConstraints(
88 hwc2_config_t config,
89 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
90 hwc_vsync_period_change_timeline_t *outTimeline);
91 HWC2::Error SetAutoLowLatencyMode(bool on);
92 HWC2::Error GetSupportedContentTypes(
93 uint32_t *outNumSupportedContentTypes,
94 const uint32_t *outSupportedContentTypes);
95
96 HWC2::Error SetContentType(int32_t contentType);
97#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +020098 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020099
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 */
Roman Stratiienko099c3112022-01-20 11:50:54 +0200168 bool ProcessClientFlatteningState(bool skip);
169 void ProcessFlatenningVsyncInternal();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200170
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200171 /* Headless mode required to keep SurfaceFlinger alive when all display are
172 * disconnected, Without headless mode Android will continuously crash.
173 * Only single internal (primary) display is required to be in HEADLESS mode
174 * to prevent the crash. See:
175 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
176 */
177 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200178 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200179 }
180
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200181 private:
182 enum ClientFlattenningState : int32_t {
183 Disabled = -3,
184 NotRequired = -2,
185 Flattened = -1,
186 ClientRefreshRequested = 0,
187 VsyncCountdownMax = 60, /* 1 sec @ 60FPS */
188 };
189
190 std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200191
192 constexpr static size_t MATRIX_SIZE = 16;
193
Roman Stratiienko0137f862022-01-04 18:27:40 +0200194 HwcDisplayConfigs configs_;
195
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200196 DrmHwcTwo *const hwc2_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200198 std::optional<DrmMode> staged_mode_;
199 int64_t staged_mode_change_time_{};
200 uint32_t staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200201
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200202 DrmDisplayPipeline *const pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200203
204 std::unique_ptr<Backend> backend_;
205
206 VSyncWorker vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200207 bool vsync_event_en_{};
208 bool vsync_flattening_en_{};
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200209 bool vsync_tracking_en_{};
210 int64_t last_vsync_ts_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200211
212 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200213 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200214
215 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
216 static uint32_t layer_idx_;
217
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218 std::map<hwc2_layer_t, HwcLayer> layers_;
219 HwcLayer client_layer_;
220 int32_t color_mode_{};
221 std::array<float, MATRIX_SIZE> color_transform_matrix_{};
222 android_color_transform_t color_transform_hint_;
223
224 uint32_t frame_no_ = 0;
225 Stats total_stats_;
226 Stats prev_stats_;
227 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200228
229 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200230
231 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200232};
233
234} // namespace android
235
236#endif