blob: 89ae2f61a8dbb12a03981ab4d5c2d97bf1efc879 [file] [log] [blame]
Sean Pauled2ec4b2016-03-10 15:35:40 -05001/*
2 * Copyright (C) 2016 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
Sean Paulac874152016-03-10 16:00:26 -050017#include "drmdisplaycompositor.h"
18#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050019#include "platform.h"
Alexandru Gheorghec5463582018-03-27 15:52:02 +010020#include "resourcemanager.h"
Sean Paulac874152016-03-10 16:00:26 -050021#include "vsyncworker.h"
22
Sean Pauled2ec4b2016-03-10 15:35:40 -050023#include <hardware/hwcomposer2.h>
24
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +020025#include <array>
Sean Pauled2ec4b2016-03-10 15:35:40 -050026#include <map>
27
28namespace android {
29
30class DrmHwcTwo : public hwc2_device_t {
31 public:
Sean Paulac874152016-03-10 16:00:26 -050032 static int HookDevOpen(const struct hw_module_t *module, const char *name,
33 struct hw_device_t **dev);
34
Sean Pauled2ec4b2016-03-10 15:35:40 -050035 DrmHwcTwo();
36
Sean Paulac874152016-03-10 16:00:26 -050037 HWC2::Error Init();
38
Sean Pauled2ec4b2016-03-10 15:35:40 -050039 private:
40 class HwcLayer {
41 public:
Sean Paulac874152016-03-10 16:00:26 -050042 HWC2::Composition sf_type() const {
43 return sf_type_;
44 }
45 HWC2::Composition validated_type() const {
46 return validated_type_;
47 }
48 void accept_type_change() {
49 sf_type_ = validated_type_;
50 }
51 void set_validated_type(HWC2::Composition type) {
52 validated_type_ = type;
53 }
54 bool type_changed() const {
55 return sf_type_ != validated_type_;
56 }
57
58 uint32_t z_order() const {
59 return z_order_;
60 }
61
62 buffer_handle_t buffer() {
63 return buffer_;
64 }
65 void set_buffer(buffer_handle_t buffer) {
66 buffer_ = buffer;
67 }
68
69 int take_acquire_fence() {
70 return acquire_fence_.Release();
71 }
72 void set_acquire_fence(int acquire_fence) {
73 acquire_fence_.Set(dup(acquire_fence));
74 }
75
76 int release_fence() {
77 return release_fence_.get();
78 }
79 int take_release_fence() {
80 return release_fence_.Release();
81 }
82 void manage_release_fence() {
83 release_fence_.Set(release_fence_raw_);
84 release_fence_raw_ = -1;
85 }
86 OutputFd release_fence_output() {
87 return OutputFd(&release_fence_raw_);
88 }
89
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020090 hwc_rect_t display_frame() {
91 return display_frame_;
92 }
93
Sean Paulac874152016-03-10 16:00:26 -050094 void PopulateDrmLayer(DrmHwcLayer *layer);
95
96 // Layer hooks
Sean Pauled2ec4b2016-03-10 15:35:40 -050097 HWC2::Error SetCursorPosition(int32_t x, int32_t y);
98 HWC2::Error SetLayerBlendMode(int32_t mode);
99 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
100 HWC2::Error SetLayerColor(hwc_color_t color);
101 HWC2::Error SetLayerCompositionType(int32_t type);
102 HWC2::Error SetLayerDataspace(int32_t dataspace);
103 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
104 HWC2::Error SetLayerPlaneAlpha(float alpha);
105 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
106 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
107 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
108 HWC2::Error SetLayerTransform(int32_t transform);
109 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
110 HWC2::Error SetLayerZOrder(uint32_t z);
Sean Paulac874152016-03-10 16:00:26 -0500111
112 private:
113 // sf_type_ stores the initial type given to us by surfaceflinger,
114 // validated_type_ stores the type after running ValidateDisplay
115 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
116 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
117
118 HWC2::BlendMode blending_ = HWC2::BlendMode::None;
John Stultz62307032019-02-27 21:35:03 -0800119 buffer_handle_t buffer_ = NULL;
Sean Paulac874152016-03-10 16:00:26 -0500120 UniqueFd acquire_fence_;
121 int release_fence_raw_ = -1;
122 UniqueFd release_fence_;
123 hwc_rect_t display_frame_;
124 float alpha_ = 1.0f;
125 hwc_frect_t source_crop_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800126 int32_t cursor_x_;
127 int32_t cursor_y_;
Roman Kovalivskyibb375692019-12-11 17:48:44 +0200128 hwc_color_t layer_color_;
Sean Paulac874152016-03-10 16:00:26 -0500129 HWC2::Transform transform_ = HWC2::Transform::None;
130 uint32_t z_order_ = 0;
131 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
132 };
133
134 struct HwcCallback {
135 HwcCallback(hwc2_callback_data_t d, hwc2_function_pointer_t f)
136 : data(d), func(f) {
137 }
138 hwc2_callback_data_t data;
139 hwc2_function_pointer_t func;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500140 };
141
142 class HwcDisplay {
143 public:
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100144 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
145 std::shared_ptr<Importer> importer, hwc2_display_t handle,
146 HWC2::DisplayType type);
Sean Paulac874152016-03-10 16:00:26 -0500147 HwcDisplay(const HwcDisplay &) = delete;
148 HWC2::Error Init(std::vector<DrmPlane *> *planes);
149
150 HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
151 hwc2_function_pointer_t func);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300152 void ClearDisplay();
Sean Paulac874152016-03-10 16:00:26 -0500153
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200154 std::string Dump();
155
Sean Pauled2ec4b2016-03-10 15:35:40 -0500156 // HWC Hooks
157 HWC2::Error AcceptDisplayChanges();
158 HWC2::Error CreateLayer(hwc2_layer_t *layer);
159 HWC2::Error DestroyLayer(hwc2_layer_t layer);
160 HWC2::Error GetActiveConfig(hwc2_config_t *config);
161 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
162 hwc2_layer_t *layers,
163 int32_t *types);
164 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
165 int32_t format, int32_t dataspace);
166 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
167 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
168 int32_t *value);
169 HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
170 hwc2_config_t *configs);
171 HWC2::Error GetDisplayName(uint32_t *size, char *name);
172 HWC2::Error GetDisplayRequests(int32_t *display_requests,
173 uint32_t *num_elements, hwc2_layer_t *layers,
174 int32_t *layer_requests);
175 HWC2::Error GetDisplayType(int32_t *type);
John Stultz8c7229d2020-02-07 21:31:08 +0000176#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800177 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
178 uint32_t *outDataSize,
179 uint8_t *outData);
180 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
181 uint32_t *outCapabilities);
John Stultz8c7229d2020-02-07 21:31:08 +0000182#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -0500183 HWC2::Error GetDozeSupport(int32_t *support);
Sean Paulac874152016-03-10 16:00:26 -0500184 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
185 float *max_luminance,
186 float *max_average_luminance,
187 float *min_luminance);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500188 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
189 int32_t *fences);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000190 HWC2::Error PresentDisplay(int32_t *present_fence);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500191 HWC2::Error SetActiveConfig(hwc2_config_t config);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300192 HWC2::Error ChosePreferredConfig();
Sean Pauled2ec4b2016-03-10 15:35:40 -0500193 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
194 int32_t dataspace, hwc_region_t damage);
195 HWC2::Error SetColorMode(int32_t mode);
196 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
197 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
198 HWC2::Error SetPowerMode(int32_t mode);
199 HWC2::Error SetVsyncEnabled(int32_t enabled);
200 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100201 HwcLayer *get_layer(hwc2_layer_t layer) {
202 auto it = layers_.find(layer);
203 if (it == layers_.end())
204 return nullptr;
205 return &it->second;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500206 }
207
208 private:
Rob Herring4f6c62e2018-05-17 14:33:02 -0500209 HWC2::Error CreateComposition(bool test);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000210 void AddFenceToPresentFence(int fd);
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200211 bool HardwareSupportsLayerType(HWC2::Composition comp_type);
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200212 uint32_t CalcPixOps(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
213 size_t first_z, size_t size);
214 void MarkValidated(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
215 size_t client_first_z, size_t client_size);
Sean Paulac874152016-03-10 16:00:26 -0500216
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200217 constexpr static size_t MATRIX_SIZE = 16;
218
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100219 ResourceManager *resource_manager_;
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100220 DrmDevice *drm_;
Sean Paulac874152016-03-10 16:00:26 -0500221 DrmDisplayCompositor compositor_;
222 std::shared_ptr<Importer> importer_;
223 std::unique_ptr<Planner> planner_;
Sean Paulac874152016-03-10 16:00:26 -0500224
225 std::vector<DrmPlane *> primary_planes_;
226 std::vector<DrmPlane *> overlay_planes_;
227
228 VSyncWorker vsync_worker_;
229 DrmConnector *connector_ = NULL;
230 DrmCrtc *crtc_ = NULL;
231 hwc2_display_t handle_;
232 HWC2::DisplayType type_;
233 uint32_t layer_idx_ = 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500234 std::map<hwc2_layer_t, HwcLayer> layers_;
Sean Paulac874152016-03-10 16:00:26 -0500235 HwcLayer client_layer_;
Matteo Franchinc56eede2019-12-03 17:10:38 +0000236 UniqueFd present_fence_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800237 int32_t color_mode_;
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200238 std::array<float, MATRIX_SIZE> color_transform_matrix_;
239 android_color_transform_t color_transform_hint_;
Sean Paulac874152016-03-10 16:00:26 -0500240
241 uint32_t frame_no_ = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200242 /* Statistics */
243 struct Stats {
244 Stats minus(Stats b) {
245 return {total_frames_ - b.total_frames_,
246 total_pixops_ - b.total_pixops_, gpu_pixops_ - b.gpu_pixops_,
247 failed_kms_validate_ - b.failed_kms_validate_,
248 failed_kms_present_ - b.failed_kms_present_};
249 }
250
251 uint32_t total_frames_ = 0;
252 uint64_t total_pixops_ = 0;
253 uint64_t gpu_pixops_ = 0;
254 uint32_t failed_kms_validate_ = 0;
255 uint32_t failed_kms_present_ = 0;
256 } total_stats_, prev_stats_;
257 std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500258 };
259
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300260 class DrmHotplugHandler : public DrmEventHandler {
261 public:
262 DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
263 : hwc2_(hwc2), drm_(drm) {
264 }
265 void HandleEvent(uint64_t timestamp_us);
266
267 private:
268 DrmHwcTwo *hwc2_;
269 DrmDevice *drm_;
270 };
271
Sean Pauled2ec4b2016-03-10 15:35:40 -0500272 static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
273 return static_cast<DrmHwcTwo *>(dev);
274 }
275
276 template <typename PFN, typename T>
277 static hwc2_function_pointer_t ToHook(T function) {
278 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
279 return reinterpret_cast<hwc2_function_pointer_t>(function);
280 }
281
282 template <typename T, typename HookType, HookType func, typename... Args>
283 static T DeviceHook(hwc2_device_t *dev, Args... args) {
284 DrmHwcTwo *hwc = toDrmHwcTwo(dev);
285 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
286 }
287
Vincent Donnefort315444c2019-10-09 14:23:42 +0100288 static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) {
289 auto it = hwc->displays_.find(display_handle);
290 if (it == hwc->displays_.end())
291 return nullptr;
292
293 return &it->second;
294 }
295
Sean Pauled2ec4b2016-03-10 15:35:40 -0500296 template <typename HookType, HookType func, typename... Args>
297 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
298 Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100299 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
300 if (!display)
301 return static_cast<int32_t>(HWC2::Error::BadDisplay);
302
303 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500304 }
305
306 template <typename HookType, HookType func, typename... Args>
307 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
308 hwc2_layer_t layer_handle, Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100309 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
310 if (!display)
311 return static_cast<int32_t>(HWC2::Error::BadDisplay);
312
Vincent Donnefort9abec032019-10-09 15:43:43 +0100313 HwcLayer *layer = display->get_layer(layer_handle);
314 if (!layer)
315 return static_cast<int32_t>(HWC2::Error::BadLayer);
316
317 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500318 }
319
320 // hwc2_device_t hooks
321 static int HookDevClose(hw_device_t *dev);
322 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
323 int32_t *out_capabilities);
324 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
325 int32_t descriptor);
326
327 // Device functions
328 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
Sean Paulf72cccd2018-08-27 13:59:08 -0400329 int32_t *format, hwc2_display_t *display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500330 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200331 void Dump(uint32_t *outSize, char *outBuffer);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500332 uint32_t GetMaxVirtualDisplayCount();
333 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
334 hwc2_function_pointer_t function);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300335 HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type);
336 void HandleDisplayHotplug(hwc2_display_t displayid, int state);
337 void HandleInitialHotplugState(DrmDevice *drmDevice);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500338
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100339 ResourceManager resource_manager_;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500340 std::map<hwc2_display_t, HwcDisplay> displays_;
Sean Paulac874152016-03-10 16:00:26 -0500341 std::map<HWC2::Callback, HwcCallback> callbacks_;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200342
343 std::string mDumpString;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500344};
Sean Paulf72cccd2018-08-27 13:59:08 -0400345} // namespace android