blob: bf95c3e452b55b062b115f2b35f96fa299186fc1 [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 Stratiienko22fe9612023-01-17 21:22:29 +020026#include "compositor/FlatteningController.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020027#include "compositor/LayerData.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020028#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020029#include "drm/ResourceManager.h"
30#include "drm/VSyncWorker.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020031#include "hwc2_device/HwcLayer.h"
32
33namespace android {
34
35class Backend;
36class DrmHwcTwo;
37
Roman Stratiienko456e2d62022-01-29 01:17:39 +020038inline constexpr uint32_t kPrimaryDisplay = 0;
39
Roman Stratiienko3627beb2022-01-04 16:02:55 +020040class HwcDisplay {
41 public:
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020042 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020043 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020044 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020045
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020046 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
47 void SetPipeline(DrmDisplayPipeline *pipeline);
48
Roman Stratiienko3627beb2022-01-04 16:02:55 +020049 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
50 std::vector<HwcLayer *> GetOrderLayersByZPos();
51
52 void ClearDisplay();
53
54 std::string Dump();
55
56 // HWC Hooks
57 HWC2::Error AcceptDisplayChanges();
58 HWC2::Error CreateLayer(hwc2_layer_t *layer);
59 HWC2::Error DestroyLayer(hwc2_layer_t layer);
60 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
61 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
62 hwc2_layer_t *layers, int32_t *types);
63 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
64 int32_t format, int32_t dataspace);
65 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
66 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
67 int32_t *value);
68 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs);
69 HWC2::Error GetDisplayName(uint32_t *size, char *name);
70 HWC2::Error GetDisplayRequests(int32_t *display_requests,
71 uint32_t *num_elements, hwc2_layer_t *layers,
72 int32_t *layer_requests);
73 HWC2::Error GetDisplayType(int32_t *type);
Roman Stratiienko6b405052022-12-10 19:09:10 +020074#if __ANDROID_API__ > 27
Roman Stratiienko3627beb2022-01-04 16:02:55 +020075 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
76 int32_t *outIntents);
77 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
78#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020079#if __ANDROID_API__ > 28
Roman Stratiienko3627beb2022-01-04 16:02:55 +020080 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
81 uint32_t *outDataSize,
82 uint8_t *outData);
83 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
84 uint32_t *outCapabilities);
85 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
86 HWC2::Error SetDisplayBrightness(float);
87#endif
Roman Stratiienko6b405052022-12-10 19:09:10 +020088#if __ANDROID_API__ > 29
Roman Stratiienko3627beb2022-01-04 16:02:55 +020089 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020090
91 HWC2::Error SetActiveConfigWithConstraints(
92 hwc2_config_t config,
93 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
94 hwc_vsync_period_change_timeline_t *outTimeline);
95 HWC2::Error SetAutoLowLatencyMode(bool on);
96 HWC2::Error GetSupportedContentTypes(
97 uint32_t *outNumSupportedContentTypes,
98 const uint32_t *outSupportedContentTypes);
99
100 HWC2::Error SetContentType(int32_t contentType);
101#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +0200102 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200103
104 HWC2::Error GetDozeSupport(int32_t *support);
105 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
106 float *max_luminance,
107 float *max_average_luminance,
108 float *min_luminance);
109 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
110 int32_t *fences);
Roman Stratiienkodd214942022-05-03 18:24:49 +0300111 HWC2::Error PresentDisplay(int32_t *out_present_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200112 HWC2::Error SetActiveConfig(hwc2_config_t config);
113 HWC2::Error ChosePreferredConfig();
114 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
115 int32_t dataspace, hwc_region_t damage);
116 HWC2::Error SetColorMode(int32_t mode);
117 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
118 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
119 HWC2::Error SetPowerMode(int32_t mode);
120 HWC2::Error SetVsyncEnabled(int32_t enabled);
121 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
122 HwcLayer *get_layer(hwc2_layer_t layer) {
123 auto it = layers_.find(layer);
124 if (it == layers_.end())
125 return nullptr;
126 return &it->second;
127 }
128
129 /* Statistics */
130 struct Stats {
131 Stats minus(Stats b) const {
132 return {total_frames_ - b.total_frames_,
133 total_pixops_ - b.total_pixops_,
134 gpu_pixops_ - b.gpu_pixops_,
135 failed_kms_validate_ - b.failed_kms_validate_,
136 failed_kms_present_ - b.failed_kms_present_,
137 frames_flattened_ - b.frames_flattened_};
138 }
139
140 uint32_t total_frames_ = 0;
141 uint64_t total_pixops_ = 0;
142 uint64_t gpu_pixops_ = 0;
143 uint32_t failed_kms_validate_ = 0;
144 uint32_t failed_kms_present_ = 0;
145 uint32_t frames_flattened_ = 0;
146 };
147
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200148 const Backend *backend() const;
149 void set_backend(std::unique_ptr<Backend> backend);
150
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200151 auto GetHwc2() {
152 return hwc2_;
153 }
154
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200155 std::map<hwc2_layer_t, HwcLayer> &layers() {
156 return layers_;
157 }
158
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200159 auto &GetPipe() {
160 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200161 }
162
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200163 bool CtmByGpu();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200164
165 Stats &total_stats() {
166 return total_stats_;
167 }
168
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200169 /* Headless mode required to keep SurfaceFlinger alive when all display are
170 * disconnected, Without headless mode Android will continuously crash.
171 * Only single internal (primary) display is required to be in HEADLESS mode
172 * to prevent the crash. See:
173 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
174 */
175 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200176 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200177 }
178
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200179 void Deinit();
180
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200181 auto GetFlatCon() {
182 return flatcon_;
183 }
184
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200185 private:
Roman Stratiienko0137f862022-01-04 18:27:40 +0200186 HwcDisplayConfigs configs_;
187
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200188 DrmHwcTwo *const hwc2_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200189
Roman Stratiienko76892782023-01-16 17:15:53 +0200190 SharedFd present_fence_;
Roman Stratiienkodd214942022-05-03 18:24:49 +0300191
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200192 std::optional<DrmMode> staged_mode_;
193 int64_t staged_mode_change_time_{};
194 uint32_t staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200195
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200196 DrmDisplayPipeline *pipeline_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200197
198 std::unique_ptr<Backend> backend_;
Roman Stratiienko22fe9612023-01-17 21:22:29 +0200199 std::shared_ptr<FlatteningController> flatcon_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200200
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200201 std::shared_ptr<VSyncWorker> vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200202 bool vsync_event_en_{};
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200203 bool vsync_tracking_en_{};
204 int64_t last_vsync_ts_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200205
206 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200208
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200209 uint32_t layer_idx_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200210
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200211 std::map<hwc2_layer_t, HwcLayer> layers_;
212 HwcLayer client_layer_;
213 int32_t color_mode_{};
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200214 static constexpr int kCtmRows = 3;
215 static constexpr int kCtmCols = 3;
216 std::shared_ptr<drm_color_ctm> color_matrix_;
217 android_color_transform_t color_transform_hint_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200218
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200219 std::shared_ptr<DrmKmsPlan> current_plan_;
220
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200221 uint32_t frame_no_ = 0;
222 Stats total_stats_;
223 Stats prev_stats_;
224 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200225
Roman Stratiienko0da91bf2023-01-17 18:06:04 +0200226 void SetColorMarixToIdentity();
227
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200228 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200229
230 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200231};
232
233} // namespace android