blob: d79efb03040d17975a1109a1c742e9cf81b624d3 [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 Stratiienko4b2cc482022-02-21 14:53:58 +020025#include "compositor/LayerData.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020026#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020027#include "drm/ResourceManager.h"
28#include "drm/VSyncWorker.h"
Roman Stratiienko3627beb2022-01-04 16:02:55 +020029#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 Stratiienkobb594ba2022-02-18 16:52:03 +020040 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020041 HwcDisplay(const HwcDisplay &) = delete;
Roman Stratiienko3dacd472022-01-11 19:18:34 +020042 ~HwcDisplay();
Roman Stratiienko3627beb2022-01-04 16:02:55 +020043
Roman Stratiienkobb594ba2022-02-18 16:52:03 +020044 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */
45 void SetPipeline(DrmDisplayPipeline *pipeline);
46
Roman Stratiienko3627beb2022-01-04 16:02:55 +020047 HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
48 std::vector<HwcLayer *> GetOrderLayersByZPos();
49
50 void ClearDisplay();
51
52 std::string Dump();
53
54 // HWC Hooks
55 HWC2::Error AcceptDisplayChanges();
56 HWC2::Error CreateLayer(hwc2_layer_t *layer);
57 HWC2::Error DestroyLayer(hwc2_layer_t layer);
58 HWC2::Error GetActiveConfig(hwc2_config_t *config) const;
59 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
60 hwc2_layer_t *layers, int32_t *types);
61 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
62 int32_t format, int32_t dataspace);
63 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
64 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
65 int32_t *value);
66 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs);
67 HWC2::Error GetDisplayName(uint32_t *size, char *name);
68 HWC2::Error GetDisplayRequests(int32_t *display_requests,
69 uint32_t *num_elements, hwc2_layer_t *layers,
70 int32_t *layer_requests);
71 HWC2::Error GetDisplayType(int32_t *type);
72#if PLATFORM_SDK_VERSION > 27
73 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
74 int32_t *outIntents);
75 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
76#endif
77#if PLATFORM_SDK_VERSION > 28
78 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
79 uint32_t *outDataSize,
80 uint8_t *outData);
81 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
82 uint32_t *outCapabilities);
83 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
84 HWC2::Error SetDisplayBrightness(float);
85#endif
86#if PLATFORM_SDK_VERSION > 29
87 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
Roman Stratiienko3627beb2022-01-04 16:02:55 +020088
89 HWC2::Error SetActiveConfigWithConstraints(
90 hwc2_config_t config,
91 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
92 hwc_vsync_period_change_timeline_t *outTimeline);
93 HWC2::Error SetAutoLowLatencyMode(bool on);
94 HWC2::Error GetSupportedContentTypes(
95 uint32_t *outNumSupportedContentTypes,
96 const uint32_t *outSupportedContentTypes);
97
98 HWC2::Error SetContentType(int32_t contentType);
99#endif
Roman Stratiienko099c3112022-01-20 11:50:54 +0200100 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200101
102 HWC2::Error GetDozeSupport(int32_t *support);
103 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
104 float *max_luminance,
105 float *max_average_luminance,
106 float *min_luminance);
107 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
108 int32_t *fences);
Roman Stratiienkodd214942022-05-03 18:24:49 +0300109 HWC2::Error PresentDisplay(int32_t *out_present_fence);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200110 HWC2::Error SetActiveConfig(hwc2_config_t config);
111 HWC2::Error ChosePreferredConfig();
112 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
113 int32_t dataspace, hwc_region_t damage);
114 HWC2::Error SetColorMode(int32_t mode);
115 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
116 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
117 HWC2::Error SetPowerMode(int32_t mode);
118 HWC2::Error SetVsyncEnabled(int32_t enabled);
119 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
120 HwcLayer *get_layer(hwc2_layer_t layer) {
121 auto it = layers_.find(layer);
122 if (it == layers_.end())
123 return nullptr;
124 return &it->second;
125 }
126
127 /* Statistics */
128 struct Stats {
129 Stats minus(Stats b) const {
130 return {total_frames_ - b.total_frames_,
131 total_pixops_ - b.total_pixops_,
132 gpu_pixops_ - b.gpu_pixops_,
133 failed_kms_validate_ - b.failed_kms_validate_,
134 failed_kms_present_ - b.failed_kms_present_,
135 frames_flattened_ - b.frames_flattened_};
136 }
137
138 uint32_t total_frames_ = 0;
139 uint64_t total_pixops_ = 0;
140 uint64_t gpu_pixops_ = 0;
141 uint32_t failed_kms_validate_ = 0;
142 uint32_t failed_kms_present_ = 0;
143 uint32_t frames_flattened_ = 0;
144 };
145
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200146 const Backend *backend() const;
147 void set_backend(std::unique_ptr<Backend> backend);
148
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200149 auto GetHwc2() {
150 return hwc2_;
151 }
152
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200153 std::map<hwc2_layer_t, HwcLayer> &layers() {
154 return layers_;
155 }
156
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200157 auto &GetPipe() {
158 return *pipeline_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200159 }
160
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200161 android_color_transform_t &color_transform_hint() {
162 return color_transform_hint_;
163 }
164
165 Stats &total_stats() {
166 return total_stats_;
167 }
168
169 /* returns true if composition should be sent to client */
Roman Stratiienko099c3112022-01-20 11:50:54 +0200170 bool ProcessClientFlatteningState(bool skip);
171 void ProcessFlatenningVsyncInternal();
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200172
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200173 /* Headless mode required to keep SurfaceFlinger alive when all display are
174 * disconnected, Without headless mode Android will continuously crash.
175 * Only single internal (primary) display is required to be in HEADLESS mode
176 * to prevent the crash. See:
177 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios
178 */
179 bool IsInHeadlessMode() {
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200180 return !pipeline_;
Roman Stratiienkof0c507f2022-01-17 18:29:24 +0200181 }
182
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200183 void Deinit();
184
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200185 private:
186 enum ClientFlattenningState : int32_t {
187 Disabled = -3,
188 NotRequired = -2,
189 Flattened = -1,
190 ClientRefreshRequested = 0,
191 VsyncCountdownMax = 60, /* 1 sec @ 60FPS */
192 };
193
194 std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200195
196 constexpr static size_t MATRIX_SIZE = 16;
197
Roman Stratiienko0137f862022-01-04 18:27:40 +0200198 HwcDisplayConfigs configs_;
199
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200200 DrmHwcTwo *const hwc2_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200201
Roman Stratiienkodd214942022-05-03 18:24:49 +0300202 UniqueFd present_fence_;
203
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200204 std::optional<DrmMode> staged_mode_;
205 int64_t staged_mode_change_time_{};
206 uint32_t staged_mode_config_id_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200207
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200208 DrmDisplayPipeline *pipeline_{};
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200209
210 std::unique_ptr<Backend> backend_;
211
212 VSyncWorker vsync_worker_;
Roman Stratiienko099c3112022-01-20 11:50:54 +0200213 bool vsync_event_en_{};
214 bool vsync_flattening_en_{};
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200215 bool vsync_tracking_en_{};
216 int64_t last_vsync_ts_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200217
218 const hwc2_display_t handle_;
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200219 HWC2::DisplayType type_;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200220
Roman Stratiienkobb594ba2022-02-18 16:52:03 +0200221 uint32_t layer_idx_{};
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200222
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200223 std::map<hwc2_layer_t, HwcLayer> layers_;
224 HwcLayer client_layer_;
225 int32_t color_mode_{};
226 std::array<float, MATRIX_SIZE> color_transform_matrix_{};
227 android_color_transform_t color_transform_hint_;
228
Roman Stratiienko9362cef2022-02-02 09:53:50 +0200229 std::shared_ptr<DrmKmsPlan> current_plan_;
230
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200231 uint32_t frame_no_ = 0;
232 Stats total_stats_;
233 Stats prev_stats_;
234 std::string DumpDelta(HwcDisplay::Stats delta);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200235
236 HWC2::Error Init();
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200237
238 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time);
Roman Stratiienko3627beb2022-01-04 16:02:55 +0200239};
240
241} // namespace android
242
243#endif