blob: 653aa4a44e7831d013ec2ac32acbee9e28302ae7 [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
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020021#include <atomic>
Roman Stratiienko3627beb2022-01-04 16:02:55 +020022#include <optional>
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020023#include <sstream>
Roman Stratiienko3627beb2022-01-04 16:02:55 +020024
Roman Stratiienko0137f862022-01-04 18:27:40 +020025#include "HwcDisplayConfigs.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020026#include "compositor/LayerData.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020027#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020028#include "drm/ResourceManager.h"
29#include "drm/VSyncWorker.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020030#include "hwc2_device/HwcLayer.h"
31
32namespace android {
33
34class Backend;
35class DrmHwcTwo;
36
Roman Stratiienko456e2d62022-01-29 01:17:39 +020037inline constexpr uint32_t kPrimaryDisplay = 0;
38
Roman Stratiienko3627beb2022-01-04 16:02:55 +020039class HwcDisplay {
40 public:
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020041 HwcDisplay(hwc2_display_t handle, 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
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020045 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
46 void SetPipeline(DrmDisplayPipeline *pipeline);
47
Roman Stratiienko3627beb2022-01-04 16:02:55 +020048 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
49 std::vector<HwcLayer *> GetOrderLayersByZPos();
50
51 void ClearDisplay();
52
53 std::string Dump();
54
55 // HWC Hooks
56 HWC2::Error AcceptDisplayChanges();
57 HWC2::Error CreateLayer(hwc2_layer_t *layer);
58 HWC2::Error DestroyLayer(hwc2_layer_t layer);
59 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
60 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
61 hwc2_layer_t *layers, int32_t *types);
62 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
63 int32_t format, int32_t dataspace);
64 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
65 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
66 int32_t *value);
67 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs);
68 HWC2::Error GetDisplayName(uint32_t *size, char *name);
69 HWC2::Error GetDisplayRequests(int32_t *display_requests,
70 uint32_t *num_elements, hwc2_layer_t *layers,
71 int32_t *layer_requests);
72 HWC2::Error GetDisplayType(int32_t *type);
Roman Stratiienko6b405052022-12-10 19:09:10 +020073#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +020074 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
75 int32_t *outIntents);
76 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
77#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020078#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +020079 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
80 uint32_t *outDataSize,
81 uint8_t *outData);
82 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
83 uint32_t *outCapabilities);
84 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
85 HWC2::Error SetDisplayBrightness(float);
86#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020087#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +020088 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020089
90 HWC2::Error SetActiveConfigWithConstraints(
91 hwc2_config_t config,
92 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
93 hwc_vsync_period_change_timeline_t *outTimeline);
94 HWC2::Error SetAutoLowLatencyMode(bool on);
95 HWC2::Error GetSupportedContentTypes(
96 uint32_t *outNumSupportedContentTypes,
97 const uint32_t *outSupportedContentTypes);
98
99 HWC2::Error SetContentType(int32_t contentType);
100#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +0200101 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200102
103 HWC2::Error GetDozeSupport(int32_t *support);
104 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
105 float *max_luminance,
106 float *max_average_luminance,
107 float *min_luminance);
108 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
109 int32_t *fences);
Roman Stratiienkodd214942022-05-03 18:24:49 +0300110 HWC2::Error PresentDisplay(int32_t *out_present_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200111 HWC2::Error SetActiveConfig(hwc2_config_t config);
112 HWC2::Error ChosePreferredConfig();
113 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
114 int32_t dataspace, hwc_region_t damage);
115 HWC2::Error SetColorMode(int32_t mode);
116 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
117 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
118 HWC2::Error SetPowerMode(int32_t mode);
119 HWC2::Error SetVsyncEnabled(int32_t enabled);
120 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
121 HwcLayer *get_layer(hwc2_layer_t layer) {
122 auto it = layers_.find(layer);
123 if (it == layers_.end())
124 return nullptr;
125 return &it->second;
126 }
127
128 /* Statistics */
129 struct Stats {
130 Stats minus(Stats b) const {
131 return {total_frames_ - b.total_frames_,
132 total_pixops_ - b.total_pixops_,
133 gpu_pixops_ - b.gpu_pixops_,
134 failed_kms_validate_ - b.failed_kms_validate_,
135 failed_kms_present_ - b.failed_kms_present_,
136 frames_flattened_ - b.frames_flattened_};
137 }
138
139 uint32_t total_frames_ = 0;
140 uint64_t total_pixops_ = 0;
141 uint64_t gpu_pixops_ = 0;
142 uint32_t failed_kms_validate_ = 0;
143 uint32_t failed_kms_present_ = 0;
144 uint32_t frames_flattened_ = 0;
145 };
146
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200147 const Backend *backend() const;
148 void set_backend(std::unique_ptr<Backend> backend);
149
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200150 auto GetHwc2() {
151 return hwc2_;
152 }
153
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200154 std::map<hwc2_layer_t, HwcLayer> &layers() {
155 return layers_;
156 }
157
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200158 auto &GetPipe() {
159 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200160 }
161
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200162 bool CtmByGpu();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200163
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
Roman Stratiienko0137f862022-01-04 18:27:40 +0200195 HwcDisplayConfigs configs_;
196
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200197 DrmHwcTwo *const hwc2_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200198
Roman Stratiienko76892782023-01-16 17:15:53 +0200199 SharedFd present_fence_;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300200
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200201 std::optional<DrmMode> staged_mode_;
202 int64_t staged_mode_change_time_{};
203 uint32_t staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200204
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200205 DrmDisplayPipeline *pipeline_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200206
207 std::unique_ptr<Backend> backend_;
208
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200209 std::shared_ptr<VSyncWorker> vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200210 bool vsync_event_en_{};
211 bool vsync_flattening_en_{};
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200212 bool vsync_tracking_en_{};
213 int64_t last_vsync_ts_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200214
215 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200216 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200217
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200218 uint32_t layer_idx_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200219
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200220 std::map<hwc2_layer_t, HwcLayer> layers_;
221 HwcLayer client_layer_;
222 int32_t color_mode_{};
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200223 static constexpr int kCtmRows = 3;
224 static constexpr int kCtmCols = 3;
225 std::shared_ptr<drm_color_ctm> color_matrix_;
226 android_color_transform_t color_transform_hint_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200227
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
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200235 void SetColorMarixToIdentity();
236
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200237 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200238
239 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200240};
241
242} // namespace android