blob: 5f9f6c5cff855aa9c56ddf25246eba2b608d23b6 [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
Matvii Zorin0ddc3292020-07-27 18:29:15 +030017#ifndef ANDROID_DRM_HWC_TWO_H_
18#define ANDROID_DRM_HWC_TWO_H_
19
Sean Paulac874152016-03-10 16:00:26 -050020#include "drmdisplaycompositor.h"
21#include "drmhwcomposer.h"
Sean Paulac874152016-03-10 16:00:26 -050022#include "platform.h"
Alexandru Gheorghec5463582018-03-27 15:52:02 +010023#include "resourcemanager.h"
Sean Paulac874152016-03-10 16:00:26 -050024#include "vsyncworker.h"
25
Sean Pauled2ec4b2016-03-10 15:35:40 -050026#include <hardware/hwcomposer2.h>
27
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020028#include <math.h>
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +020029#include <array>
Sean Pauled2ec4b2016-03-10 15:35:40 -050030#include <map>
31
32namespace android {
33
34class DrmHwcTwo : public hwc2_device_t {
35 public:
Sean Paulac874152016-03-10 16:00:26 -050036 static int HookDevOpen(const struct hw_module_t *module, const char *name,
37 struct hw_device_t **dev);
38
Sean Pauled2ec4b2016-03-10 15:35:40 -050039 DrmHwcTwo();
40
Sean Paulac874152016-03-10 16:00:26 -050041 HWC2::Error Init();
42
Sean Pauled2ec4b2016-03-10 15:35:40 -050043 class HwcLayer {
44 public:
Sean Paulac874152016-03-10 16:00:26 -050045 HWC2::Composition sf_type() const {
46 return sf_type_;
47 }
48 HWC2::Composition validated_type() const {
49 return validated_type_;
50 }
51 void accept_type_change() {
52 sf_type_ = validated_type_;
53 }
54 void set_validated_type(HWC2::Composition type) {
55 validated_type_ = type;
56 }
57 bool type_changed() const {
58 return sf_type_ != validated_type_;
59 }
60
61 uint32_t z_order() const {
62 return z_order_;
63 }
64
65 buffer_handle_t buffer() {
66 return buffer_;
67 }
68 void set_buffer(buffer_handle_t buffer) {
69 buffer_ = buffer;
70 }
71
72 int take_acquire_fence() {
73 return acquire_fence_.Release();
74 }
75 void set_acquire_fence(int acquire_fence) {
76 acquire_fence_.Set(dup(acquire_fence));
77 }
78
79 int release_fence() {
80 return release_fence_.get();
81 }
82 int take_release_fence() {
83 return release_fence_.Release();
84 }
85 void manage_release_fence() {
86 release_fence_.Set(release_fence_raw_);
87 release_fence_raw_ = -1;
88 }
89 OutputFd release_fence_output() {
90 return OutputFd(&release_fence_raw_);
91 }
92
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020093 hwc_rect_t display_frame() {
94 return display_frame_;
95 }
96
Sean Paulac874152016-03-10 16:00:26 -050097 void PopulateDrmLayer(DrmHwcLayer *layer);
98
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020099 bool RequireScalingOrPhasing() {
100 float src_width = source_crop_.right - source_crop_.left;
101 float src_height = source_crop_.bottom - source_crop_.top;
102
103 float dest_width = display_frame_.right - display_frame_.left;
104 float dest_height = display_frame_.bottom - display_frame_.top;
105
106 bool scaling = src_width != dest_width || src_height != dest_height;
107 bool phasing = (source_crop_.left - floor(source_crop_.left) != 0) ||
108 (source_crop_.top - floor(source_crop_.top) != 0);
109 return scaling || phasing;
110 }
111
Sean Paulac874152016-03-10 16:00:26 -0500112 // Layer hooks
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113 HWC2::Error SetCursorPosition(int32_t x, int32_t y);
114 HWC2::Error SetLayerBlendMode(int32_t mode);
115 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
116 HWC2::Error SetLayerColor(hwc_color_t color);
117 HWC2::Error SetLayerCompositionType(int32_t type);
118 HWC2::Error SetLayerDataspace(int32_t dataspace);
119 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
120 HWC2::Error SetLayerPlaneAlpha(float alpha);
121 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
122 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
123 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
124 HWC2::Error SetLayerTransform(int32_t transform);
125 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
126 HWC2::Error SetLayerZOrder(uint32_t z);
Sean Paulac874152016-03-10 16:00:26 -0500127
128 private:
129 // sf_type_ stores the initial type given to us by surfaceflinger,
130 // validated_type_ stores the type after running ValidateDisplay
131 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
132 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
133
134 HWC2::BlendMode blending_ = HWC2::BlendMode::None;
John Stultz62307032019-02-27 21:35:03 -0800135 buffer_handle_t buffer_ = NULL;
Sean Paulac874152016-03-10 16:00:26 -0500136 UniqueFd acquire_fence_;
137 int release_fence_raw_ = -1;
138 UniqueFd release_fence_;
139 hwc_rect_t display_frame_;
140 float alpha_ = 1.0f;
141 hwc_frect_t source_crop_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800142 int32_t cursor_x_;
143 int32_t cursor_y_;
Roman Kovalivskyibb375692019-12-11 17:48:44 +0200144 hwc_color_t layer_color_;
Sean Paulac874152016-03-10 16:00:26 -0500145 HWC2::Transform transform_ = HWC2::Transform::None;
146 uint32_t z_order_ = 0;
147 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
148 };
149
150 struct HwcCallback {
151 HwcCallback(hwc2_callback_data_t d, hwc2_function_pointer_t f)
152 : data(d), func(f) {
153 }
154 hwc2_callback_data_t data;
155 hwc2_function_pointer_t func;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500156 };
157
158 class HwcDisplay {
159 public:
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100160 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
161 std::shared_ptr<Importer> importer, hwc2_display_t handle,
162 HWC2::DisplayType type);
Sean Paulac874152016-03-10 16:00:26 -0500163 HwcDisplay(const HwcDisplay &) = delete;
164 HWC2::Error Init(std::vector<DrmPlane *> *planes);
165
166 HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
167 hwc2_function_pointer_t func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200168 void RegisterRefreshCallback(hwc2_callback_data_t data,
169 hwc2_function_pointer_t func);
170
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300171 void ClearDisplay();
Sean Paulac874152016-03-10 16:00:26 -0500172
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200173 std::string Dump();
174
Sean Pauled2ec4b2016-03-10 15:35:40 -0500175 // HWC Hooks
176 HWC2::Error AcceptDisplayChanges();
177 HWC2::Error CreateLayer(hwc2_layer_t *layer);
178 HWC2::Error DestroyLayer(hwc2_layer_t layer);
179 HWC2::Error GetActiveConfig(hwc2_config_t *config);
180 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
181 hwc2_layer_t *layers,
182 int32_t *types);
183 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
184 int32_t format, int32_t dataspace);
185 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
186 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
187 int32_t *value);
188 HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
189 hwc2_config_t *configs);
190 HWC2::Error GetDisplayName(uint32_t *size, char *name);
191 HWC2::Error GetDisplayRequests(int32_t *display_requests,
192 uint32_t *num_elements, hwc2_layer_t *layers,
193 int32_t *layer_requests);
194 HWC2::Error GetDisplayType(int32_t *type);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300195#if PLATFORM_SDK_VERSION > 27
196 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
197 int32_t *outIntents);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300198 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300199#endif
John Stultz8c7229d2020-02-07 21:31:08 +0000200#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800201 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
202 uint32_t *outDataSize,
203 uint8_t *outData);
204 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
205 uint32_t *outCapabilities);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300206 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
207 HWC2::Error SetDisplayBrightness(float);
John Stultz8c7229d2020-02-07 21:31:08 +0000208#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -0500209 HWC2::Error GetDozeSupport(int32_t *support);
Sean Paulac874152016-03-10 16:00:26 -0500210 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
211 float *max_luminance,
212 float *max_average_luminance,
213 float *min_luminance);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500214 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
215 int32_t *fences);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000216 HWC2::Error PresentDisplay(int32_t *present_fence);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500217 HWC2::Error SetActiveConfig(hwc2_config_t config);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300218 HWC2::Error ChosePreferredConfig();
Sean Pauled2ec4b2016-03-10 15:35:40 -0500219 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
220 int32_t dataspace, hwc_region_t damage);
221 HWC2::Error SetColorMode(int32_t mode);
222 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
223 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
224 HWC2::Error SetPowerMode(int32_t mode);
225 HWC2::Error SetVsyncEnabled(int32_t enabled);
226 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100227 HwcLayer *get_layer(hwc2_layer_t layer) {
228 auto it = layers_.find(layer);
229 if (it == layers_.end())
230 return nullptr;
231 return &it->second;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500232 }
233
234 private:
Rob Herring4f6c62e2018-05-17 14:33:02 -0500235 HWC2::Error CreateComposition(bool test);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000236 void AddFenceToPresentFence(int fd);
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200237 bool HardwareSupportsLayerType(HWC2::Composition comp_type);
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200238 uint32_t CalcPixOps(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
239 size_t first_z, size_t size);
240 void MarkValidated(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
241 size_t client_first_z, size_t client_size);
Sean Paulac874152016-03-10 16:00:26 -0500242
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200243 constexpr static size_t MATRIX_SIZE = 16;
244
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100245 ResourceManager *resource_manager_;
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100246 DrmDevice *drm_;
Sean Paulac874152016-03-10 16:00:26 -0500247 DrmDisplayCompositor compositor_;
248 std::shared_ptr<Importer> importer_;
249 std::unique_ptr<Planner> planner_;
Sean Paulac874152016-03-10 16:00:26 -0500250
251 std::vector<DrmPlane *> primary_planes_;
252 std::vector<DrmPlane *> overlay_planes_;
253
254 VSyncWorker vsync_worker_;
255 DrmConnector *connector_ = NULL;
256 DrmCrtc *crtc_ = NULL;
257 hwc2_display_t handle_;
258 HWC2::DisplayType type_;
259 uint32_t layer_idx_ = 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500260 std::map<hwc2_layer_t, HwcLayer> layers_;
Sean Paulac874152016-03-10 16:00:26 -0500261 HwcLayer client_layer_;
Matteo Franchinc56eede2019-12-03 17:10:38 +0000262 UniqueFd present_fence_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800263 int32_t color_mode_;
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200264 std::array<float, MATRIX_SIZE> color_transform_matrix_;
265 android_color_transform_t color_transform_hint_;
Sean Paulac874152016-03-10 16:00:26 -0500266
267 uint32_t frame_no_ = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200268 /* Statistics */
269 struct Stats {
270 Stats minus(Stats b) {
271 return {total_frames_ - b.total_frames_,
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200272 total_pixops_ - b.total_pixops_,
273 gpu_pixops_ - b.gpu_pixops_,
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200274 failed_kms_validate_ - b.failed_kms_validate_,
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200275 failed_kms_present_ - b.failed_kms_present_,
276 frames_flattened_ - b.frames_flattened_};
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200277 }
278
279 uint32_t total_frames_ = 0;
280 uint64_t total_pixops_ = 0;
281 uint64_t gpu_pixops_ = 0;
282 uint32_t failed_kms_validate_ = 0;
283 uint32_t failed_kms_present_ = 0;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200284 uint32_t frames_flattened_ = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200285 } total_stats_, prev_stats_;
286 std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500287 };
288
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300289 class DrmHotplugHandler : public DrmEventHandler {
290 public:
291 DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
292 : hwc2_(hwc2), drm_(drm) {
293 }
294 void HandleEvent(uint64_t timestamp_us);
295
296 private:
297 DrmHwcTwo *hwc2_;
298 DrmDevice *drm_;
299 };
300
Matvii Zorine0e7c5c2020-07-27 18:35:39 +0300301 private:
Sean Pauled2ec4b2016-03-10 15:35:40 -0500302 static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
303 return static_cast<DrmHwcTwo *>(dev);
304 }
305
306 template <typename PFN, typename T>
307 static hwc2_function_pointer_t ToHook(T function) {
308 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
309 return reinterpret_cast<hwc2_function_pointer_t>(function);
310 }
311
312 template <typename T, typename HookType, HookType func, typename... Args>
313 static T DeviceHook(hwc2_device_t *dev, Args... args) {
314 DrmHwcTwo *hwc = toDrmHwcTwo(dev);
315 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
316 }
317
Vincent Donnefort315444c2019-10-09 14:23:42 +0100318 static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) {
319 auto it = hwc->displays_.find(display_handle);
320 if (it == hwc->displays_.end())
321 return nullptr;
322
323 return &it->second;
324 }
325
Sean Pauled2ec4b2016-03-10 15:35:40 -0500326 template <typename HookType, HookType func, typename... Args>
327 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
328 Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100329 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
330 if (!display)
331 return static_cast<int32_t>(HWC2::Error::BadDisplay);
332
333 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500334 }
335
336 template <typename HookType, HookType func, typename... Args>
337 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
338 hwc2_layer_t layer_handle, Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100339 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
340 if (!display)
341 return static_cast<int32_t>(HWC2::Error::BadDisplay);
342
Vincent Donnefort9abec032019-10-09 15:43:43 +0100343 HwcLayer *layer = display->get_layer(layer_handle);
344 if (!layer)
345 return static_cast<int32_t>(HWC2::Error::BadLayer);
346
347 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500348 }
349
350 // hwc2_device_t hooks
351 static int HookDevClose(hw_device_t *dev);
352 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
353 int32_t *out_capabilities);
354 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
355 int32_t descriptor);
356
357 // Device functions
358 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
Sean Paulf72cccd2018-08-27 13:59:08 -0400359 int32_t *format, hwc2_display_t *display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500360 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200361 void Dump(uint32_t *outSize, char *outBuffer);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500362 uint32_t GetMaxVirtualDisplayCount();
363 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
364 hwc2_function_pointer_t function);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300365 HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type);
366 void HandleDisplayHotplug(hwc2_display_t displayid, int state);
367 void HandleInitialHotplugState(DrmDevice *drmDevice);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500368
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100369 ResourceManager resource_manager_;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500370 std::map<hwc2_display_t, HwcDisplay> displays_;
Sean Paulac874152016-03-10 16:00:26 -0500371 std::map<HWC2::Callback, HwcCallback> callbacks_;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200372
373 std::string mDumpString;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500374};
Sean Paulf72cccd2018-08-27 13:59:08 -0400375} // namespace android
Matvii Zorin0ddc3292020-07-27 18:29:15 +0300376
377#endif