blob: 033447046aa2a9e45bdb471f06d605eb71c27173 [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
21#include <optional>
22
Roman Stratiienko0137f862022-01-04 18:27:40 +020023#include "HwcDisplayConfigs.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020024#include "compositor/LayerData.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020025#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020026#include "drm/ResourceManager.h"
27#include "drm/VSyncWorker.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020028#include "hwc2_device/HwcLayer.h"
29
30namespace android {
31
32class Backend;
33class DrmHwcTwo;
34
Roman Stratiienko456e2d62022-01-29 01:17:39 +020035inline constexpr uint32_t kPrimaryDisplay = 0;
36
Roman Stratiienko3627beb2022-01-04 16:02:55 +020037class HwcDisplay {
38 public:
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020039 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020040 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020041 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020042
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020043 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
44 void SetPipeline(DrmDisplayPipeline *pipeline);
45
Roman Stratiienko3627beb2022-01-04 16:02:55 +020046 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
47 std::vector<HwcLayer *> GetOrderLayersByZPos();
48
49 void ClearDisplay();
50
51 std::string Dump();
52
53 // HWC Hooks
54 HWC2::Error AcceptDisplayChanges();
55 HWC2::Error CreateLayer(hwc2_layer_t *layer);
56 HWC2::Error DestroyLayer(hwc2_layer_t layer);
57 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
58 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
59 hwc2_layer_t *layers, int32_t *types);
60 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
61 int32_t format, int32_t dataspace);
62 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
63 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
64 int32_t *value);
65 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs);
66 HWC2::Error GetDisplayName(uint32_t *size, char *name);
67 HWC2::Error GetDisplayRequests(int32_t *display_requests,
68 uint32_t *num_elements, hwc2_layer_t *layers,
69 int32_t *layer_requests);
70 HWC2::Error GetDisplayType(int32_t *type);
Roman Stratiienko6b405052022-12-10 19:09:10 +020071#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +020072 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
73 int32_t *outIntents);
74 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
75#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020076#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +020077 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
78 uint32_t *outDataSize,
79 uint8_t *outData);
80 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
81 uint32_t *outCapabilities);
82 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
83 HWC2::Error SetDisplayBrightness(float);
84#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020085#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +020086 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020087
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
Roman Stratiienko099c3112022-01-20 11:50:54 +020099 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200100
101 HWC2::Error GetDozeSupport(int32_t *support);
102 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
103 float *max_luminance,
104 float *max_average_luminance,
105 float *min_luminance);
106 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
107 int32_t *fences);
Roman Stratiienkodd214942022-05-03 18:24:49 +0300108 HWC2::Error PresentDisplay(int32_t *out_present_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200109 HWC2::Error SetActiveConfig(hwc2_config_t config);
110 HWC2::Error ChosePreferredConfig();
111 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
112 int32_t dataspace, hwc_region_t damage);
113 HWC2::Error SetColorMode(int32_t mode);
114 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
115 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
116 HWC2::Error SetPowerMode(int32_t mode);
117 HWC2::Error SetVsyncEnabled(int32_t enabled);
118 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
119 HwcLayer *get_layer(hwc2_layer_t layer) {
120 auto it = layers_.find(layer);
121 if (it == layers_.end())
122 return nullptr;
123 return &it->second;
124 }
125
126 /* Statistics */
127 struct Stats {
128 Stats minus(Stats b) const {
129 return {total_frames_ - b.total_frames_,
130 total_pixops_ - b.total_pixops_,
131 gpu_pixops_ - b.gpu_pixops_,
132 failed_kms_validate_ - b.failed_kms_validate_,
133 failed_kms_present_ - b.failed_kms_present_,
134 frames_flattened_ - b.frames_flattened_};
135 }
136
137 uint32_t total_frames_ = 0;
138 uint64_t total_pixops_ = 0;
139 uint64_t gpu_pixops_ = 0;
140 uint32_t failed_kms_validate_ = 0;
141 uint32_t failed_kms_present_ = 0;
142 uint32_t frames_flattened_ = 0;
143 };
144
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200145 const Backend *backend() const;
146 void set_backend(std::unique_ptr<Backend> backend);
147
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200148 auto GetHwc2() {
149 return hwc2_;
150 }
151
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200152 std::map<hwc2_layer_t, HwcLayer> &layers() {
153 return layers_;
154 }
155
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200156 auto &GetPipe() {
157 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200158 }
159
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200160 android_color_transform_t &color_transform_hint() {
161 return color_transform_hint_;
162 }
163
164 Stats &total_stats() {
165 return total_stats_;
166 }
167
168 /* returns true if composition should be sent to client */
Roman Stratiienko099c3112022-01-20 11:50:54 +0200169 bool ProcessClientFlatteningState(bool skip);
170 void ProcessFlatenningVsyncInternal();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200171
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200172 /* Headless mode required to keep SurfaceFlinger alive when all display are
173 * disconnected, Without headless mode Android will continuously crash.
174 * Only single internal (primary) display is required to be in HEADLESS mode
175 * to prevent the crash. See:
176 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
177 */
178 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200179 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200180 }
181
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200182 void Deinit();
183
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200184 private:
185 enum ClientFlattenningState : int32_t {
186 Disabled = -3,
187 NotRequired = -2,
188 Flattened = -1,
189 ClientRefreshRequested = 0,
190 VsyncCountdownMax = 60, /* 1 sec @ 60FPS */
191 };
192
193 std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200194
195 constexpr static size_t MATRIX_SIZE = 16;
196
Roman Stratiienko0137f862022-01-04 18:27:40 +0200197 HwcDisplayConfigs configs_;
198
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200199 DrmHwcTwo *const hwc2_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200200
Roman Stratiienkodd214942022-05-03 18:24:49 +0300201 UniqueFd present_fence_;
202
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200203 std::optional<DrmMode> staged_mode_;
204 int64_t staged_mode_change_time_{};
205 uint32_t staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200206
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200207 DrmDisplayPipeline *pipeline_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200208
209 std::unique_ptr<Backend> backend_;
210
211 VSyncWorker vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200212 bool vsync_event_en_{};
213 bool vsync_flattening_en_{};
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200214 bool vsync_tracking_en_{};
215 int64_t last_vsync_ts_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200216
217 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200219
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200220 uint32_t layer_idx_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200221
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200222 std::map<hwc2_layer_t, HwcLayer> layers_;
223 HwcLayer client_layer_;
224 int32_t color_mode_{};
225 std::array<float, MATRIX_SIZE> color_transform_matrix_{};
226 android_color_transform_t color_transform_hint_;
227
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200228 std::shared_ptr<DrmKmsPlan> current_plan_;
229
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200230 uint32_t frame_no_ = 0;
231 Stats total_stats_;
232 Stats prev_stats_;
233 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200234
235 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200236
237 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200238};
239
240} // namespace android