OSDN Git Service

drm_hwcomposer: CI: Upgrade clang-* to v12
[android-x86/external-drm_hwcomposer.git] / DrmHwcTwo.h
1 /*
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
17 #ifndef ANDROID_DRM_HWC_TWO_H_
18 #define ANDROID_DRM_HWC_TWO_H_
19
20 #include <hardware/hwcomposer2.h>
21 #include <math.h>
22
23 #include <array>
24 #include <map>
25
26 #include "compositor/DrmDisplayCompositor.h"
27 #include "compositor/Planner.h"
28 #include "drm/ResourceManager.h"
29 #include "drm/VSyncWorker.h"
30 #include "drmhwcomposer.h"
31
32 namespace android {
33
34 class Backend;
35
36 class DrmHwcTwo : public hwc2_device_t {
37  public:
38   static int HookDevOpen(const struct hw_module_t *module, const char *name,
39                          struct hw_device_t **dev);
40
41   DrmHwcTwo();
42
43   HWC2::Error Init();
44
45   hwc2_callback_data_t hotplug_callback_data_ = NULL;
46   HWC2_PFN_HOTPLUG hotplug_callback_hook_ = NULL;
47   std::mutex hotplug_callback_lock;
48
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);
54   }
55
56   class HwcLayer {
57    public:
58     HWC2::Composition sf_type() const {
59       return sf_type_;
60     }
61     HWC2::Composition validated_type() const {
62       return validated_type_;
63     }
64     void accept_type_change() {
65       sf_type_ = validated_type_;
66     }
67     void set_validated_type(HWC2::Composition type) {
68       validated_type_ = type;
69     }
70     bool type_changed() const {
71       return sf_type_ != validated_type_;
72     }
73
74     uint32_t z_order() const {
75       return z_order_;
76     }
77
78     buffer_handle_t buffer() {
79       return buffer_;
80     }
81     void set_buffer(buffer_handle_t buffer) {
82       buffer_ = buffer;
83     }
84
85     hwc_rect_t display_frame() {
86       return display_frame_;
87     }
88
89     void PopulateDrmLayer(DrmHwcLayer *layer);
90
91     bool RequireScalingOrPhasing() {
92       float src_width = source_crop_.right - source_crop_.left;
93       float src_height = source_crop_.bottom - source_crop_.top;
94
95       float dest_width = display_frame_.right - display_frame_.left;
96       float dest_height = display_frame_.bottom - display_frame_.top;
97
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;
102     }
103
104     // Layer hooks
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);
119
120     UniqueFd acquire_fence_;
121
122     /*
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.
127      */
128     UniqueFd release_fence_;
129
130    private:
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;
135
136     HWC2::BlendMode blending_ = HWC2::BlendMode::None;
137     buffer_handle_t buffer_ = NULL;
138     hwc_rect_t display_frame_;
139     float alpha_ = 1.0f;
140     hwc_frect_t source_crop_;
141     int32_t cursor_x_;
142     int32_t cursor_y_;
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;
147   };
148
149   class HwcDisplay {
150    public:
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);
155
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();
162
163     void ClearDisplay();
164
165     std::string Dump();
166
167     // HWC Hooks
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,
174                                            int32_t *types);
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,
179                                     int32_t *value);
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);
191 #endif
192 #if PLATFORM_SDK_VERSION > 28
193     HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
194                                              uint32_t *outDataSize,
195                                              uint8_t *outData);
196     HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
197                                        uint32_t *outCapabilities);
198     HWC2::Error GetDisplayBrightnessSupport(bool *supported);
199     HWC2::Error SetDisplayBrightness(float);
200 #endif
201 #if PLATFORM_SDK_VERSION > 29
202     HWC2::Error GetDisplayConnectionType(uint32_t *outType);
203     HWC2::Error GetDisplayVsyncPeriod(hwc2_vsync_period_t *outVsyncPeriod);
204
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);
213
214     HWC2::Error SetContentType(int32_t contentType);
215 #endif
216
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,
223                                  int32_t *fences);
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())
238         return nullptr;
239       return &it->second;
240     }
241
242     /* Statistics */
243     struct Stats {
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_};
251       }
252
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;
259     };
260
261     const Backend *backend() const {
262       return backend_.get();
263     }
264     void set_backend(std::unique_ptr<Backend> backend) {
265       backend_ = std::move(backend);
266     }
267
268     const std::vector<DrmPlane *> &primary_planes() const {
269       return primary_planes_;
270     }
271
272     const std::vector<DrmPlane *> &overlay_planes() const {
273       return overlay_planes_;
274     }
275
276     std::map<hwc2_layer_t, HwcLayer> &layers() {
277       return layers_;
278     }
279
280     const DrmDisplayCompositor &compositor() const {
281       return compositor_;
282     }
283
284     const DrmDevice *drm() const {
285       return drm_;
286     }
287
288     const DrmConnector *connector() const {
289       return connector_;
290     }
291
292     ResourceManager *resource_manager() const {
293       return resource_manager_;
294     }
295
296     android_color_transform_t &color_transform_hint() {
297       return color_transform_hint_;
298     }
299
300     Stats &total_stats() {
301       return total_stats_;
302     }
303
304    private:
305     void AddFenceToPresentFence(UniqueFd fd);
306
307     constexpr static size_t MATRIX_SIZE = 16;
308
309     ResourceManager *resource_manager_;
310     DrmDevice *drm_;
311     DrmDisplayCompositor compositor_;
312     std::unique_ptr<Planner> planner_;
313
314     std::vector<DrmPlane *> primary_planes_;
315     std::vector<DrmPlane *> overlay_planes_;
316
317     std::unique_ptr<Backend> backend_;
318
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_;
331
332     uint32_t frame_no_ = 0;
333     Stats total_stats_;
334     Stats prev_stats_;
335     std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
336   };
337
338   class DrmHotplugHandler : public DrmEventHandler {
339    public:
340     DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
341         : hwc2_(hwc2), drm_(drm) {
342     }
343     void HandleEvent(uint64_t timestamp_us);
344
345    private:
346     DrmHwcTwo *hwc2_;
347     DrmDevice *drm_;
348   };
349
350  private:
351   static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
352     return static_cast<DrmHwcTwo *>(dev);
353   }
354
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);
359   }
360
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)...));
365   }
366
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())
370       return nullptr;
371
372     return &it->second;
373   }
374
375   template <typename HookType, HookType func, typename... Args>
376   static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
377                              Args... args) {
378     HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
379     if (!display)
380       return static_cast<int32_t>(HWC2::Error::BadDisplay);
381
382     return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
383   }
384
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);
389     if (!display)
390       return static_cast<int32_t>(HWC2::Error::BadDisplay);
391
392     HwcLayer *layer = display->get_layer(layer_handle);
393     if (!layer)
394       return static_cast<int32_t>(HWC2::Error::BadLayer);
395
396     return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
397   }
398
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,
404                                                     int32_t descriptor);
405
406   // Device functions
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);
417
418   ResourceManager resource_manager_;
419   std::map<hwc2_display_t, HwcDisplay> displays_;
420
421   std::string mDumpString;
422 };
423 }  // namespace android
424
425 #endif