2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_DRM_HWC_TWO_H_
18 #define ANDROID_DRM_HWC_TWO_H_
20 #include <hardware/hwcomposer2.h>
26 #include "compositor/DrmDisplayCompositor.h"
27 #include "compositor/Planner.h"
28 #include "drm/ResourceManager.h"
29 #include "drm/VSyncWorker.h"
30 #include "drmhwcomposer.h"
36 class DrmHwcTwo : public hwc2_device_t {
38 static int HookDevOpen(const struct hw_module_t *module, const char *name,
39 struct hw_device_t **dev);
45 hwc2_callback_data_t hotplug_callback_data_ = NULL;
46 HWC2_PFN_HOTPLUG hotplug_callback_hook_ = NULL;
47 std::mutex hotplug_callback_lock;
49 void SetHotplugCallback(hwc2_callback_data_t data,
50 hwc2_function_pointer_t hook) {
51 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
52 hotplug_callback_data_ = data;
53 hotplug_callback_hook_ = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook);
58 HWC2::Composition sf_type() const {
61 HWC2::Composition validated_type() const {
62 return validated_type_;
64 void accept_type_change() {
65 sf_type_ = validated_type_;
67 void set_validated_type(HWC2::Composition type) {
68 validated_type_ = type;
70 bool type_changed() const {
71 return sf_type_ != validated_type_;
74 uint32_t z_order() const {
78 buffer_handle_t buffer() {
81 void set_buffer(buffer_handle_t buffer) {
85 hwc_rect_t display_frame() {
86 return display_frame_;
89 void PopulateDrmLayer(DrmHwcLayer *layer);
91 bool RequireScalingOrPhasing() {
92 float src_width = source_crop_.right - source_crop_.left;
93 float src_height = source_crop_.bottom - source_crop_.top;
95 float dest_width = display_frame_.right - display_frame_.left;
96 float dest_height = display_frame_.bottom - display_frame_.top;
98 bool scaling = src_width != dest_width || src_height != dest_height;
99 bool phasing = (source_crop_.left - floor(source_crop_.left) != 0) ||
100 (source_crop_.top - floor(source_crop_.top) != 0);
101 return scaling || phasing;
105 HWC2::Error SetCursorPosition(int32_t x, int32_t y);
106 HWC2::Error SetLayerBlendMode(int32_t mode);
107 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
108 HWC2::Error SetLayerColor(hwc_color_t color);
109 HWC2::Error SetLayerCompositionType(int32_t type);
110 HWC2::Error SetLayerDataspace(int32_t dataspace);
111 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
112 HWC2::Error SetLayerPlaneAlpha(float alpha);
113 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
114 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
115 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
116 HWC2::Error SetLayerTransform(int32_t transform);
117 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
118 HWC2::Error SetLayerZOrder(uint32_t order);
120 UniqueFd acquire_fence_;
123 * Release fence is not used.
124 * There is no release fence support available in the DRM/KMS. In case no
125 * release fence provided application will use this buffer for writing when
126 * the next frame present fence is signaled.
128 UniqueFd release_fence_;
131 // sf_type_ stores the initial type given to us by surfaceflinger,
132 // validated_type_ stores the type after running ValidateDisplay
133 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
134 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
136 HWC2::BlendMode blending_ = HWC2::BlendMode::None;
137 buffer_handle_t buffer_ = NULL;
138 hwc_rect_t display_frame_;
140 hwc_frect_t source_crop_;
143 hwc_color_t layer_color_;
144 HWC2::Transform transform_ = HWC2::Transform::None;
145 uint32_t z_order_ = 0;
146 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
151 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
152 hwc2_display_t handle, HWC2::DisplayType type);
153 HwcDisplay(const HwcDisplay &) = delete;
154 HWC2::Error Init(std::vector<DrmPlane *> *planes);
156 void RegisterVsyncCallback(hwc2_callback_data_t data,
157 hwc2_function_pointer_t func);
158 void RegisterRefreshCallback(hwc2_callback_data_t data,
159 hwc2_function_pointer_t func);
160 HWC2::Error CreateComposition(bool test);
161 std::vector<DrmHwcTwo::HwcLayer *> GetOrderLayersByZPos();
168 HWC2::Error AcceptDisplayChanges();
169 HWC2::Error CreateLayer(hwc2_layer_t *layer);
170 HWC2::Error DestroyLayer(hwc2_layer_t layer);
171 HWC2::Error GetActiveConfig(hwc2_config_t *config);
172 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
173 hwc2_layer_t *layers,
175 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
176 int32_t format, int32_t dataspace);
177 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
178 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
180 HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
181 hwc2_config_t *configs);
182 HWC2::Error GetDisplayName(uint32_t *size, char *name);
183 HWC2::Error GetDisplayRequests(int32_t *display_requests,
184 uint32_t *num_elements, hwc2_layer_t *layers,
185 int32_t *layer_requests);
186 HWC2::Error GetDisplayType(int32_t *type);
187 #if PLATFORM_SDK_VERSION > 27
188 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
189 int32_t *outIntents);
190 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
192 #if PLATFORM_SDK_VERSION > 28
193 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
194 uint32_t *outDataSize,
196 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
197 uint32_t *outCapabilities);
198 HWC2::Error GetDisplayBrightnessSupport(bool *supported);
199 HWC2::Error SetDisplayBrightness(float);
201 #if PLATFORM_SDK_VERSION > 29
202 HWC2::Error GetDisplayConnectionType(uint32_t *outType);
203 HWC2::Error GetDisplayVsyncPeriod(hwc2_vsync_period_t *outVsyncPeriod);
205 HWC2::Error SetActiveConfigWithConstraints(
206 hwc2_config_t config,
207 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
208 hwc_vsync_period_change_timeline_t *outTimeline);
209 HWC2::Error SetAutoLowLatencyMode(bool on);
210 HWC2::Error GetSupportedContentTypes(
211 uint32_t *outNumSupportedContentTypes,
212 const uint32_t *outSupportedContentTypes);
214 HWC2::Error SetContentType(int32_t contentType);
217 HWC2::Error GetDozeSupport(int32_t *support);
218 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
219 float *max_luminance,
220 float *max_average_luminance,
221 float *min_luminance);
222 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
224 HWC2::Error PresentDisplay(int32_t *present_fence);
225 HWC2::Error SetActiveConfig(hwc2_config_t config);
226 HWC2::Error ChosePreferredConfig();
227 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
228 int32_t dataspace, hwc_region_t damage);
229 HWC2::Error SetColorMode(int32_t mode);
230 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
231 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
232 HWC2::Error SetPowerMode(int32_t mode);
233 HWC2::Error SetVsyncEnabled(int32_t enabled);
234 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
235 HwcLayer *get_layer(hwc2_layer_t layer) {
236 auto it = layers_.find(layer);
237 if (it == layers_.end())
244 Stats minus(Stats b) {
245 return {total_frames_ - b.total_frames_,
246 total_pixops_ - b.total_pixops_,
247 gpu_pixops_ - b.gpu_pixops_,
248 failed_kms_validate_ - b.failed_kms_validate_,
249 failed_kms_present_ - b.failed_kms_present_,
250 frames_flattened_ - b.frames_flattened_};
253 uint32_t total_frames_ = 0;
254 uint64_t total_pixops_ = 0;
255 uint64_t gpu_pixops_ = 0;
256 uint32_t failed_kms_validate_ = 0;
257 uint32_t failed_kms_present_ = 0;
258 uint32_t frames_flattened_ = 0;
261 const Backend *backend() const {
262 return backend_.get();
264 void set_backend(std::unique_ptr<Backend> backend) {
265 backend_ = std::move(backend);
268 const std::vector<DrmPlane *> &primary_planes() const {
269 return primary_planes_;
272 const std::vector<DrmPlane *> &overlay_planes() const {
273 return overlay_planes_;
276 std::map<hwc2_layer_t, HwcLayer> &layers() {
280 const DrmDisplayCompositor &compositor() const {
284 const DrmDevice *drm() const {
288 const DrmConnector *connector() const {
292 ResourceManager *resource_manager() const {
293 return resource_manager_;
296 android_color_transform_t &color_transform_hint() {
297 return color_transform_hint_;
300 Stats &total_stats() {
305 void AddFenceToPresentFence(UniqueFd fd);
307 constexpr static size_t MATRIX_SIZE = 16;
309 ResourceManager *resource_manager_;
311 DrmDisplayCompositor compositor_;
312 std::unique_ptr<Planner> planner_;
314 std::vector<DrmPlane *> primary_planes_;
315 std::vector<DrmPlane *> overlay_planes_;
317 std::unique_ptr<Backend> backend_;
319 VSyncWorker vsync_worker_;
320 DrmConnector *connector_ = NULL;
321 DrmCrtc *crtc_ = NULL;
322 hwc2_display_t handle_;
323 HWC2::DisplayType type_;
324 uint32_t layer_idx_ = 0;
325 std::map<hwc2_layer_t, HwcLayer> layers_;
326 HwcLayer client_layer_;
327 UniqueFd present_fence_;
328 int32_t color_mode_{};
329 std::array<float, MATRIX_SIZE> color_transform_matrix_{};
330 android_color_transform_t color_transform_hint_;
332 uint32_t frame_no_ = 0;
335 std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
338 class DrmHotplugHandler : public DrmEventHandler {
340 DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
341 : hwc2_(hwc2), drm_(drm) {
343 void HandleEvent(uint64_t timestamp_us);
351 static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
352 return static_cast<DrmHwcTwo *>(dev);
355 template <typename PFN, typename T>
356 static hwc2_function_pointer_t ToHook(T function) {
357 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
358 return reinterpret_cast<hwc2_function_pointer_t>(function);
361 template <typename T, typename HookType, HookType func, typename... Args>
362 static T DeviceHook(hwc2_device_t *dev, Args... args) {
363 DrmHwcTwo *hwc = toDrmHwcTwo(dev);
364 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
367 static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) {
368 auto it = hwc->displays_.find(display_handle);
369 if (it == hwc->displays_.end())
375 template <typename HookType, HookType func, typename... Args>
376 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
378 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
380 return static_cast<int32_t>(HWC2::Error::BadDisplay);
382 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
385 template <typename HookType, HookType func, typename... Args>
386 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
387 hwc2_layer_t layer_handle, Args... args) {
388 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
390 return static_cast<int32_t>(HWC2::Error::BadDisplay);
392 HwcLayer *layer = display->get_layer(layer_handle);
394 return static_cast<int32_t>(HWC2::Error::BadLayer);
396 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
399 // hwc2_device_t hooks
400 static int HookDevClose(hw_device_t *dev);
401 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
402 int32_t *out_capabilities);
403 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
407 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
408 int32_t *format, hwc2_display_t *display);
409 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
410 void Dump(uint32_t *outSize, char *outBuffer);
411 uint32_t GetMaxVirtualDisplayCount();
412 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
413 hwc2_function_pointer_t function);
414 HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type);
415 void HandleDisplayHotplug(hwc2_display_t displayid, int state);
416 void HandleInitialHotplugState(DrmDevice *drmDevice);
418 ResourceManager resource_manager_;
419 std::map<hwc2_display_t, HwcDisplay> displays_;
421 std::string mDumpString;
423 } // namespace android