blob: 90cffa4a185ffc1be9b2906adc3b71d815d90a2d [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
25#include <map>
26
27namespace android {
28
29class DrmHwcTwo : public hwc2_device_t {
30 public:
Sean Paulac874152016-03-10 16:00:26 -050031 static int HookDevOpen(const struct hw_module_t *module, const char *name,
32 struct hw_device_t **dev);
33
Sean Pauled2ec4b2016-03-10 15:35:40 -050034 DrmHwcTwo();
35
Sean Paulac874152016-03-10 16:00:26 -050036 HWC2::Error Init();
37
Sean Pauled2ec4b2016-03-10 15:35:40 -050038 private:
39 class HwcLayer {
40 public:
Sean Paulac874152016-03-10 16:00:26 -050041 HWC2::Composition sf_type() const {
42 return sf_type_;
43 }
44 HWC2::Composition validated_type() const {
45 return validated_type_;
46 }
47 void accept_type_change() {
48 sf_type_ = validated_type_;
49 }
50 void set_validated_type(HWC2::Composition type) {
51 validated_type_ = type;
52 }
53 bool type_changed() const {
54 return sf_type_ != validated_type_;
55 }
56
57 uint32_t z_order() const {
58 return z_order_;
59 }
60
61 buffer_handle_t buffer() {
62 return buffer_;
63 }
64 void set_buffer(buffer_handle_t buffer) {
65 buffer_ = buffer;
66 }
67
68 int take_acquire_fence() {
69 return acquire_fence_.Release();
70 }
71 void set_acquire_fence(int acquire_fence) {
72 acquire_fence_.Set(dup(acquire_fence));
73 }
74
75 int release_fence() {
76 return release_fence_.get();
77 }
78 int take_release_fence() {
79 return release_fence_.Release();
80 }
81 void manage_release_fence() {
82 release_fence_.Set(release_fence_raw_);
83 release_fence_raw_ = -1;
84 }
85 OutputFd release_fence_output() {
86 return OutputFd(&release_fence_raw_);
87 }
88
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020089 hwc_rect_t display_frame() {
90 return display_frame_;
91 }
92
Sean Paulac874152016-03-10 16:00:26 -050093 void PopulateDrmLayer(DrmHwcLayer *layer);
94
95 // Layer hooks
Sean Pauled2ec4b2016-03-10 15:35:40 -050096 HWC2::Error SetCursorPosition(int32_t x, int32_t y);
97 HWC2::Error SetLayerBlendMode(int32_t mode);
98 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
99 HWC2::Error SetLayerColor(hwc_color_t color);
100 HWC2::Error SetLayerCompositionType(int32_t type);
101 HWC2::Error SetLayerDataspace(int32_t dataspace);
102 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
103 HWC2::Error SetLayerPlaneAlpha(float alpha);
104 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
105 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
106 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
107 HWC2::Error SetLayerTransform(int32_t transform);
108 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
109 HWC2::Error SetLayerZOrder(uint32_t z);
Sean Paulac874152016-03-10 16:00:26 -0500110
111 private:
112 // sf_type_ stores the initial type given to us by surfaceflinger,
113 // validated_type_ stores the type after running ValidateDisplay
114 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
115 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
116
117 HWC2::BlendMode blending_ = HWC2::BlendMode::None;
John Stultz62307032019-02-27 21:35:03 -0800118 buffer_handle_t buffer_ = NULL;
Sean Paulac874152016-03-10 16:00:26 -0500119 UniqueFd acquire_fence_;
120 int release_fence_raw_ = -1;
121 UniqueFd release_fence_;
122 hwc_rect_t display_frame_;
123 float alpha_ = 1.0f;
124 hwc_frect_t source_crop_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800125 int32_t cursor_x_;
126 int32_t cursor_y_;
Sean Paulac874152016-03-10 16:00:26 -0500127 HWC2::Transform transform_ = HWC2::Transform::None;
128 uint32_t z_order_ = 0;
129 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
130 };
131
132 struct HwcCallback {
133 HwcCallback(hwc2_callback_data_t d, hwc2_function_pointer_t f)
134 : data(d), func(f) {
135 }
136 hwc2_callback_data_t data;
137 hwc2_function_pointer_t func;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500138 };
139
140 class HwcDisplay {
141 public:
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100142 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
143 std::shared_ptr<Importer> importer, hwc2_display_t handle,
144 HWC2::DisplayType type);
Sean Paulac874152016-03-10 16:00:26 -0500145 HwcDisplay(const HwcDisplay &) = delete;
146 HWC2::Error Init(std::vector<DrmPlane *> *planes);
147
148 HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
149 hwc2_function_pointer_t func);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300150 void ClearDisplay();
Sean Paulac874152016-03-10 16:00:26 -0500151
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200152 std::string Dump();
153
Sean Pauled2ec4b2016-03-10 15:35:40 -0500154 // HWC Hooks
155 HWC2::Error AcceptDisplayChanges();
156 HWC2::Error CreateLayer(hwc2_layer_t *layer);
157 HWC2::Error DestroyLayer(hwc2_layer_t layer);
158 HWC2::Error GetActiveConfig(hwc2_config_t *config);
159 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
160 hwc2_layer_t *layers,
161 int32_t *types);
162 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
163 int32_t format, int32_t dataspace);
164 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
165 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
166 int32_t *value);
167 HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
168 hwc2_config_t *configs);
169 HWC2::Error GetDisplayName(uint32_t *size, char *name);
170 HWC2::Error GetDisplayRequests(int32_t *display_requests,
171 uint32_t *num_elements, hwc2_layer_t *layers,
172 int32_t *layer_requests);
173 HWC2::Error GetDisplayType(int32_t *type);
174 HWC2::Error GetDozeSupport(int32_t *support);
Sean Paulac874152016-03-10 16:00:26 -0500175 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
176 float *max_luminance,
177 float *max_average_luminance,
178 float *min_luminance);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500179 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
180 int32_t *fences);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000181 HWC2::Error PresentDisplay(int32_t *present_fence);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500182 HWC2::Error SetActiveConfig(hwc2_config_t config);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300183 HWC2::Error ChosePreferredConfig();
Sean Pauled2ec4b2016-03-10 15:35:40 -0500184 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
185 int32_t dataspace, hwc_region_t damage);
186 HWC2::Error SetColorMode(int32_t mode);
187 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
188 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
189 HWC2::Error SetPowerMode(int32_t mode);
190 HWC2::Error SetVsyncEnabled(int32_t enabled);
191 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100192 HwcLayer *get_layer(hwc2_layer_t layer) {
193 auto it = layers_.find(layer);
194 if (it == layers_.end())
195 return nullptr;
196 return &it->second;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500197 }
198
199 private:
Rob Herring4f6c62e2018-05-17 14:33:02 -0500200 HWC2::Error CreateComposition(bool test);
Matteo Franchinc56eede2019-12-03 17:10:38 +0000201 void AddFenceToPresentFence(int fd);
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200202 bool HardwareSupportsLayerType(HWC2::Composition comp_type);
Sean Paulac874152016-03-10 16:00:26 -0500203
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100204 ResourceManager *resource_manager_;
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100205 DrmDevice *drm_;
Sean Paulac874152016-03-10 16:00:26 -0500206 DrmDisplayCompositor compositor_;
207 std::shared_ptr<Importer> importer_;
208 std::unique_ptr<Planner> planner_;
Sean Paulac874152016-03-10 16:00:26 -0500209
210 std::vector<DrmPlane *> primary_planes_;
211 std::vector<DrmPlane *> overlay_planes_;
212
213 VSyncWorker vsync_worker_;
214 DrmConnector *connector_ = NULL;
215 DrmCrtc *crtc_ = NULL;
216 hwc2_display_t handle_;
217 HWC2::DisplayType type_;
218 uint32_t layer_idx_ = 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500219 std::map<hwc2_layer_t, HwcLayer> layers_;
Sean Paulac874152016-03-10 16:00:26 -0500220 HwcLayer client_layer_;
Matteo Franchinc56eede2019-12-03 17:10:38 +0000221 UniqueFd present_fence_;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800222 int32_t color_mode_;
Sean Paulac874152016-03-10 16:00:26 -0500223
224 uint32_t frame_no_ = 0;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200225 /* Statistics */
226 struct Stats {
227 Stats minus(Stats b) {
228 return {total_frames_ - b.total_frames_,
229 total_pixops_ - b.total_pixops_, gpu_pixops_ - b.gpu_pixops_,
230 failed_kms_validate_ - b.failed_kms_validate_,
231 failed_kms_present_ - b.failed_kms_present_};
232 }
233
234 uint32_t total_frames_ = 0;
235 uint64_t total_pixops_ = 0;
236 uint64_t gpu_pixops_ = 0;
237 uint32_t failed_kms_validate_ = 0;
238 uint32_t failed_kms_present_ = 0;
239 } total_stats_, prev_stats_;
240 std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500241 };
242
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300243 class DrmHotplugHandler : public DrmEventHandler {
244 public:
245 DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
246 : hwc2_(hwc2), drm_(drm) {
247 }
248 void HandleEvent(uint64_t timestamp_us);
249
250 private:
251 DrmHwcTwo *hwc2_;
252 DrmDevice *drm_;
253 };
254
Sean Pauled2ec4b2016-03-10 15:35:40 -0500255 static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
256 return static_cast<DrmHwcTwo *>(dev);
257 }
258
259 template <typename PFN, typename T>
260 static hwc2_function_pointer_t ToHook(T function) {
261 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
262 return reinterpret_cast<hwc2_function_pointer_t>(function);
263 }
264
265 template <typename T, typename HookType, HookType func, typename... Args>
266 static T DeviceHook(hwc2_device_t *dev, Args... args) {
267 DrmHwcTwo *hwc = toDrmHwcTwo(dev);
268 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
269 }
270
Vincent Donnefort315444c2019-10-09 14:23:42 +0100271 static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) {
272 auto it = hwc->displays_.find(display_handle);
273 if (it == hwc->displays_.end())
274 return nullptr;
275
276 return &it->second;
277 }
278
Sean Pauled2ec4b2016-03-10 15:35:40 -0500279 template <typename HookType, HookType func, typename... Args>
280 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
281 Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100282 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
283 if (!display)
284 return static_cast<int32_t>(HWC2::Error::BadDisplay);
285
286 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500287 }
288
289 template <typename HookType, HookType func, typename... Args>
290 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
291 hwc2_layer_t layer_handle, Args... args) {
Vincent Donnefort315444c2019-10-09 14:23:42 +0100292 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
293 if (!display)
294 return static_cast<int32_t>(HWC2::Error::BadDisplay);
295
Vincent Donnefort9abec032019-10-09 15:43:43 +0100296 HwcLayer *layer = display->get_layer(layer_handle);
297 if (!layer)
298 return static_cast<int32_t>(HWC2::Error::BadLayer);
299
300 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
Sean Pauled2ec4b2016-03-10 15:35:40 -0500301 }
302
303 // hwc2_device_t hooks
304 static int HookDevClose(hw_device_t *dev);
305 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
306 int32_t *out_capabilities);
307 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
308 int32_t descriptor);
309
310 // Device functions
311 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
Sean Paulf72cccd2018-08-27 13:59:08 -0400312 int32_t *format, hwc2_display_t *display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500313 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200314 void Dump(uint32_t *outSize, char *outBuffer);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500315 uint32_t GetMaxVirtualDisplayCount();
316 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
317 hwc2_function_pointer_t function);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300318 HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type);
319 void HandleDisplayHotplug(hwc2_display_t displayid, int state);
320 void HandleInitialHotplugState(DrmDevice *drmDevice);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500321
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100322 ResourceManager resource_manager_;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500323 std::map<hwc2_display_t, HwcDisplay> displays_;
Sean Paulac874152016-03-10 16:00:26 -0500324 std::map<HWC2::Callback, HwcCallback> callbacks_;
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200325
326 std::string mDumpString;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500327};
Sean Paulf72cccd2018-08-27 13:59:08 -0400328} // namespace android