blob: 712510b86dd74a2da44df0164d4a86acc85cc171 [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 Stratiienko65f2ba82019-12-20 17:04:01 +020025#include <math.h>
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +020026#include <array>
Sean Pauled2ec4b2016-03-10 15:35:40 -050027#include <map>
28
29namespace android {
30
31class DrmHwcTwo : public hwc2_device_t {
32 public:
Sean Paulac874152016-03-10 16:00:26 -050033 static int HookDevOpen(const struct hw_module_t *module, const char *name,
34 struct hw_device_t **dev);
35
Sean Pauled2ec4b2016-03-10 15:35:40 -050036 DrmHwcTwo();
37
Sean Paulac874152016-03-10 16:00:26 -050038 HWC2::Error Init();
39
Sean Pauled2ec4b2016-03-10 15:35:40 -050040 private:
41 class HwcLayer {
42 public:
Sean Paulac874152016-03-10 16:00:26 -050043 HWC2::Composition sf_type() const {
44 return sf_type_;
45 }
46 HWC2::Composition validated_type() const {
47 return validated_type_;
48 }
49 void accept_type_change() {
50 sf_type_ = validated_type_;
51 }
52 void set_validated_type(HWC2::Composition type) {
53 validated_type_ = type;
54 }
55 bool type_changed() const {
56 return sf_type_ != validated_type_;
57 }
58
59 uint32_t z_order() const {
60 return z_order_;
61 }
62
63 buffer_handle_t buffer() {
64 return buffer_;
65 }
66 void set_buffer(buffer_handle_t buffer) {
67 buffer_ = buffer;
68 }
69
70 int take_acquire_fence() {
71 return acquire_fence_.Release();
72 }
73 void set_acquire_fence(int acquire_fence) {
74 acquire_fence_.Set(dup(acquire_fence));
75 }
76
77 int release_fence() {
78 return release_fence_.get();
79 }
80 int take_release_fence() {
81 return release_fence_.Release();
82 }
83 void manage_release_fence() {
84 release_fence_.Set(release_fence_raw_);
85 release_fence_raw_ = -1;
86 }
87 OutputFd release_fence_output() {
88 return OutputFd(&release_fence_raw_);
89 }
90
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020091 hwc_rect_t display_frame() {
92 return display_frame_;
93 }
94
Sean Paulac874152016-03-10 16:00:26 -050095 void PopulateDrmLayer(DrmHwcLayer *layer);
96
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020097 bool RequireScalingOrPhasing() {
98 float src_width = source_crop_.right - source_crop_.left;
99 float src_height = source_crop_.bottom - source_crop_.top;
100
101 float dest_width = display_frame_.right - display_frame_.left;
102 float dest_height = display_frame_.bottom - display_frame_.top;
103
104 bool scaling = src_width != dest_width || src_height != dest_height;
105 bool phasing = (source_crop_.left - floor(source_crop_.left) != 0) ||
106 (source_crop_.top - floor(source_crop_.top) != 0);
107 return scaling || phasing;
108 }
109
Sean Paulac874152016-03-10 16:00:26 -0500110 // Layer hooks
Sean Pauled2ec4b2016-03-10 15:35:40 -0500111 HWC2::Error SetCursorPosition(int32_t x, int32_t y);
112 HWC2::Error SetLayerBlendMode(int32_t mode);
113 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
114 HWC2::Error SetLayerColor(hwc_color_t color);
115 HWC2::Error SetLayerCompositionType(int32_t type);
116 HWC2::Error SetLayerDataspace(int32_t dataspace);
117 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
118 HWC2::Error SetLayerPlaneAlpha(float alpha);
119 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
120 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
121 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
122 HWC2::Error SetLayerTransform(int32_t transform);
123 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
124 HWC2::Error SetLayerZOrder(uint32_t z);
Sean Paulac874152016-03-10 16:00:26 -0500125
126 private:
127 // sf_type_ stores the initial type given to us by surfaceflinger,
128 // validated_type_ stores the type after running ValidateDisplay
129 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
130 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
131
132 HWC2::BlendMode blending_ = HWC2::BlendMode::None;
John Stultz62307032019-02-27 21:35:03 -0800133 buffer_handle_t buffer_ = NULL;
Sean Paulac874152016-03-10 16:00:26 -0500134 UniqueFd acquire_fence_;
135 int release_fence_raw_ = -1;
136 UniqueFd release_fence_;
137 hwc_rect_t display_frame_;
138 float alpha_ = 1.0f;
139 hwc_frect_t source_crop_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800140 int32_t cursor_x_;
141 int32_t cursor_y_;
Roman Kovalivskyibb375692019-12-11 17:48:44 +0200142 hwc_color_t layer_color_;
Sean Paulac874152016-03-10 16:00:26 -0500143 HWC2::Transform transform_ = HWC2::Transform::None;
144 uint32_t z_order_ = 0;
145 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
146 };
147
148 struct HwcCallback {
149 HwcCallback(hwc2_callback_data_t d, hwc2_function_pointer_t f)
150 : data(d), func(f) {
151 }
152 hwc2_callback_data_t data;
153 hwc2_function_pointer_t func;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500154 };
155
156 class HwcDisplay {
157 public:
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100158 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
159 std::shared_ptr<Importer> importer, hwc2_display_t handle,
160 HWC2::DisplayType type);
Sean Paulac874152016-03-10 16:00:26 -0500161 HwcDisplay(const HwcDisplay &) = delete;
162 HWC2::Error Init(std::vector<DrmPlane *> *planes);
163
164 HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
165 hwc2_function_pointer_t func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200166 void RegisterRefreshCallback(hwc2_callback_data_t data,
167 hwc2_function_pointer_t func);
168
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300169 void ClearDisplay();
Sean Paulac874152016-03-10 16:00:26 -0500170
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200171 std::string Dump();
172
Sean Pauled2ec4b2016-03-10 15:35:40 -0500173 // HWC Hooks
174 HWC2::Error AcceptDisplayChanges();
175 HWC2::Error CreateLayer(hwc2_layer_t *layer);
176 HWC2::Error DestroyLayer(hwc2_layer_t layer);
177 HWC2::Error GetActiveConfig(hwc2_config_t *config);
178 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
179 hwc2_layer_t *layers,
180 int32_t *types);
181 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
182 int32_t format, int32_t dataspace);
183 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
184 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
185 int32_t *value);
186 HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
187 hwc2_config_t *configs);
188 HWC2::Error GetDisplayName(uint32_t *size, char *name);
189 HWC2::Error GetDisplayRequests(int32_t *display_requests,
190 uint32_t *num_elements, hwc2_layer_t *layers,
191 int32_t *layer_requests);
192 HWC2::Error GetDisplayType(int32_t *type);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300193#if PLATFORM_SDK_VERSION > 27
194 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
195 int32_t *outIntents);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +0300196 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300197#endif
John Stultz8c7229d2020-02-07 21:31:08 +0000198#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800199 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
200 uint32_t *outDataSize,
201 uint8_t *outData);
202 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
203 uint32_t *outCapabilities);
John Stultz8c7229d2020-02-07 21:31:08 +0000204#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -0500205 HWC2::Error GetDozeSupport(int32_t *support);
Sean Paulac874152016-03-10 16:00:26 -0500206 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
207 float *max_luminance,
208 float *max_average_luminance,
209 float *min_luminance);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500210 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
211 int32_t *fences);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000212 HWC2::Error PresentDisplay(int32_t *present_fence);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500213 HWC2::Error SetActiveConfig(hwc2_config_t config);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300214 HWC2::Error ChosePreferredConfig();
Sean Pauled2ec4b2016-03-10 15:35:40 -0500215 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
216 int32_t dataspace, hwc_region_t damage);
217 HWC2::Error SetColorMode(int32_t mode);
218 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
219 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
220 HWC2::Error SetPowerMode(int32_t mode);
221 HWC2::Error SetVsyncEnabled(int32_t enabled);
222 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100223 HwcLayer *get_layer(hwc2_layer_t layer) {
224 auto it = layers_.find(layer);
225 if (it == layers_.end())
226 return nullptr;
227 return &it->second;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500228 }
229
230 private:
Rob Herring4f6c62e2018-05-17 14:33:02 -0500231 HWC2::Error CreateComposition(bool test);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000232 void AddFenceToPresentFence(int fd);
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200233 bool HardwareSupportsLayerType(HWC2::Composition comp_type);
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200234 uint32_t CalcPixOps(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
235 size_t first_z, size_t size);
236 void MarkValidated(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
237 size_t client_first_z, size_t client_size);
Sean Paulac874152016-03-10 16:00:26 -0500238
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200239 constexpr static size_t MATRIX_SIZE = 16;
240
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100241 ResourceManager *resource_manager_;
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100242 DrmDevice *drm_;
Sean Paulac874152016-03-10 16:00:26 -0500243 DrmDisplayCompositor compositor_;
244 std::shared_ptr<Importer> importer_;
245 std::unique_ptr<Planner> planner_;
Sean Paulac874152016-03-10 16:00:26 -0500246
247 std::vector<DrmPlane *> primary_planes_;
248 std::vector<DrmPlane *> overlay_planes_;
249
250 VSyncWorker vsync_worker_;
251 DrmConnector *connector_ = NULL;
252 DrmCrtc *crtc_ = NULL;
253 hwc2_display_t handle_;
254 HWC2::DisplayType type_;
255 uint32_t layer_idx_ = 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500256 std::map<hwc2_layer_t, HwcLayer> layers_;
Sean Paulac874152016-03-10 16:00:26 -0500257 HwcLayer client_layer_;
Matteo Franchinc56eede2019-12-03 17:10:38 +0000258 UniqueFd present_fence_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800259 int32_t color_mode_;
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200260 std::array<float, MATRIX_SIZE> color_transform_matrix_;
261 android_color_transform_t color_transform_hint_;
Sean Paulac874152016-03-10 16:00:26 -0500262
263 uint32_t frame_no_ = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200264 /* Statistics */
265 struct Stats {
266 Stats minus(Stats b) {
267 return {total_frames_ - b.total_frames_,
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200268 total_pixops_ - b.total_pixops_,
269 gpu_pixops_ - b.gpu_pixops_,
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200270 failed_kms_validate_ - b.failed_kms_validate_,
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200271 failed_kms_present_ - b.failed_kms_present_,
272 frames_flattened_ - b.frames_flattened_};
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200273 }
274
275 uint32_t total_frames_ = 0;
276 uint64_t total_pixops_ = 0;
277 uint64_t gpu_pixops_ = 0;
278 uint32_t failed_kms_validate_ = 0;
279 uint32_t failed_kms_present_ = 0;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200280 uint32_t frames_flattened_ = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200281 } total_stats_, prev_stats_;
282 std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500283 };
284
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300285 class DrmHotplugHandler : public DrmEventHandler {
286 public:
287 DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
288 : hwc2_(hwc2), drm_(drm) {
289 }
290 void HandleEvent(uint64_t timestamp_us);
291
292 private:
293 DrmHwcTwo *hwc2_;
294 DrmDevice *drm_;
295 };
296
Sean Pauled2ec4b2016-03-10 15:35:40 -0500297 static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
298 return static_cast<DrmHwcTwo *>(dev);
299 }
300
301 template <typename PFN, typename T>
302 static hwc2_function_pointer_t ToHook(T function) {
303 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
304 return reinterpret_cast<hwc2_function_pointer_t>(function);
305 }
306
307 template <typename T, typename HookType, HookType func, typename... Args>
308 static T DeviceHook(hwc2_device_t *dev, Args... args) {
309 DrmHwcTwo *hwc = toDrmHwcTwo(dev);
310 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
311 }
312
Vincent Donnefort315444c2019-10-09 14:23:42 +0100313 static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) {
314 auto it = hwc->displays_.find(display_handle);
315 if (it == hwc->displays_.end())
316 return nullptr;
317
318 return &it->second;
319 }
320
Sean Pauled2ec4b2016-03-10 15:35:40 -0500321 template <typename HookType, HookType func, typename... Args>
322 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
323 Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100324 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
325 if (!display)
326 return static_cast<int32_t>(HWC2::Error::BadDisplay);
327
328 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500329 }
330
331 template <typename HookType, HookType func, typename... Args>
332 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
333 hwc2_layer_t layer_handle, Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100334 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
335 if (!display)
336 return static_cast<int32_t>(HWC2::Error::BadDisplay);
337
Vincent Donnefort9abec032019-10-09 15:43:43 +0100338 HwcLayer *layer = display->get_layer(layer_handle);
339 if (!layer)
340 return static_cast<int32_t>(HWC2::Error::BadLayer);
341
342 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500343 }
344
345 // hwc2_device_t hooks
346 static int HookDevClose(hw_device_t *dev);
347 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
348 int32_t *out_capabilities);
349 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
350 int32_t descriptor);
351
352 // Device functions
353 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
Sean Paulf72cccd2018-08-27 13:59:08 -0400354 int32_t *format, hwc2_display_t *display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500355 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200356 void Dump(uint32_t *outSize, char *outBuffer);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500357 uint32_t GetMaxVirtualDisplayCount();
358 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
359 hwc2_function_pointer_t function);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300360 HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type);
361 void HandleDisplayHotplug(hwc2_display_t displayid, int state);
362 void HandleInitialHotplugState(DrmDevice *drmDevice);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500363
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100364 ResourceManager resource_manager_;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500365 std::map<hwc2_display_t, HwcDisplay> displays_;
Sean Paulac874152016-03-10 16:00:26 -0500366 std::map<HWC2::Callback, HwcCallback> callbacks_;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200367
368 std::string mDumpString;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500369};
Sean Paulf72cccd2018-08-27 13:59:08 -0400370} // namespace android