OSDN Git Service

drm_hwcomposer: use CamelCase in source/header files related to class
[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 "drm/ResourceManager.h"
28 #include "drm/VSyncWorker.h"
29 #include "drmhwcomposer.h"
30 #include "platform/platform.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   class HwcLayer {
46    public:
47     HWC2::Composition sf_type() const {
48       return sf_type_;
49     }
50     HWC2::Composition validated_type() const {
51       return validated_type_;
52     }
53     void accept_type_change() {
54       sf_type_ = validated_type_;
55     }
56     void set_validated_type(HWC2::Composition type) {
57       validated_type_ = type;
58     }
59     bool type_changed() const {
60       return sf_type_ != validated_type_;
61     }
62
63     uint32_t z_order() const {
64       return z_order_;
65     }
66
67     buffer_handle_t buffer() {
68       return buffer_;
69     }
70     void set_buffer(buffer_handle_t buffer) {
71       buffer_ = buffer;
72     }
73
74     int take_acquire_fence() {
75       return acquire_fence_.Release();
76     }
77     void set_acquire_fence(int acquire_fence) {
78       acquire_fence_.Set(dup(acquire_fence));
79     }
80
81     int release_fence() {
82       return release_fence_.get();
83     }
84     int take_release_fence() {
85       return release_fence_.Release();
86     }
87     void manage_release_fence() {
88       release_fence_.Set(release_fence_raw_);
89       release_fence_raw_ = -1;
90     }
91     OutputFd release_fence_output() {
92       return OutputFd(&release_fence_raw_);
93     }
94
95     hwc_rect_t display_frame() {
96       return display_frame_;
97     }
98
99     void PopulateDrmLayer(DrmHwcLayer *layer);
100
101     bool RequireScalingOrPhasing() {
102       float src_width = source_crop_.right - source_crop_.left;
103       float src_height = source_crop_.bottom - source_crop_.top;
104
105       float dest_width = display_frame_.right - display_frame_.left;
106       float dest_height = display_frame_.bottom - display_frame_.top;
107
108       bool scaling = src_width != dest_width || src_height != dest_height;
109       bool phasing = (source_crop_.left - floor(source_crop_.left) != 0) ||
110                      (source_crop_.top - floor(source_crop_.top) != 0);
111       return scaling || phasing;
112     }
113
114     // Layer hooks
115     HWC2::Error SetCursorPosition(int32_t x, int32_t y);
116     HWC2::Error SetLayerBlendMode(int32_t mode);
117     HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
118     HWC2::Error SetLayerColor(hwc_color_t color);
119     HWC2::Error SetLayerCompositionType(int32_t type);
120     HWC2::Error SetLayerDataspace(int32_t dataspace);
121     HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
122     HWC2::Error SetLayerPlaneAlpha(float alpha);
123     HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
124     HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
125     HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
126     HWC2::Error SetLayerTransform(int32_t transform);
127     HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
128     HWC2::Error SetLayerZOrder(uint32_t z);
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     UniqueFd acquire_fence_;
139     int release_fence_raw_ = -1;
140     UniqueFd release_fence_;
141     hwc_rect_t display_frame_;
142     float alpha_ = 1.0f;
143     hwc_frect_t source_crop_;
144     int32_t cursor_x_;
145     int32_t cursor_y_;
146     hwc_color_t layer_color_;
147     HWC2::Transform transform_ = HWC2::Transform::None;
148     uint32_t z_order_ = 0;
149     android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
150   };
151
152   struct HwcCallback {
153     HwcCallback(hwc2_callback_data_t d, hwc2_function_pointer_t f)
154         : data(d), func(f) {
155     }
156     hwc2_callback_data_t data;
157     hwc2_function_pointer_t func;
158   };
159
160   class HwcDisplay {
161    public:
162     HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
163                std::shared_ptr<Importer> importer, hwc2_display_t handle,
164                HWC2::DisplayType type);
165     HwcDisplay(const HwcDisplay &) = delete;
166     HWC2::Error Init(std::vector<DrmPlane *> *planes);
167
168     HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
169                                       hwc2_function_pointer_t func);
170     void RegisterRefreshCallback(hwc2_callback_data_t data,
171                                  hwc2_function_pointer_t func);
172     HWC2::Error CreateComposition(bool test);
173     bool HardwareSupportsLayerType(HWC2::Composition comp_type);
174     uint32_t CalcPixOps(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
175                         size_t first_z, size_t size);
176     void MarkValidated(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map,
177                        size_t client_first_z, size_t client_size);
178
179     void ClearDisplay();
180
181     std::string Dump();
182
183     // HWC Hooks
184     HWC2::Error AcceptDisplayChanges();
185     HWC2::Error CreateLayer(hwc2_layer_t *layer);
186     HWC2::Error DestroyLayer(hwc2_layer_t layer);
187     HWC2::Error GetActiveConfig(hwc2_config_t *config);
188     HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
189                                            hwc2_layer_t *layers,
190                                            int32_t *types);
191     HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
192                                        int32_t format, int32_t dataspace);
193     HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
194     HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
195                                     int32_t *value);
196     HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
197                                   hwc2_config_t *configs);
198     HWC2::Error GetDisplayName(uint32_t *size, char *name);
199     HWC2::Error GetDisplayRequests(int32_t *display_requests,
200                                    uint32_t *num_elements, hwc2_layer_t *layers,
201                                    int32_t *layer_requests);
202     HWC2::Error GetDisplayType(int32_t *type);
203 #if PLATFORM_SDK_VERSION > 27
204     HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
205                                  int32_t *outIntents);
206     HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
207 #endif
208 #if PLATFORM_SDK_VERSION > 28
209     HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
210                                              uint32_t *outDataSize,
211                                              uint8_t *outData);
212     HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
213                                        uint32_t *outCapabilities);
214     HWC2::Error GetDisplayBrightnessSupport(bool *supported);
215     HWC2::Error SetDisplayBrightness(float);
216 #endif
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     const std::shared_ptr<Importer> &importer() const {
293       return importer_;
294     }
295
296     ResourceManager *resource_manager() const {
297       return resource_manager_;
298     }
299
300     android_color_transform_t &color_transform_hint() {
301       return color_transform_hint_;
302     }
303
304     Stats &total_stats() {
305       return total_stats_;
306     }
307
308    private:
309     void AddFenceToPresentFence(int fd);
310
311     constexpr static size_t MATRIX_SIZE = 16;
312
313     ResourceManager *resource_manager_;
314     DrmDevice *drm_;
315     DrmDisplayCompositor compositor_;
316     std::shared_ptr<Importer> importer_;
317     std::unique_ptr<Planner> planner_;
318
319     std::vector<DrmPlane *> primary_planes_;
320     std::vector<DrmPlane *> overlay_planes_;
321
322     std::unique_ptr<Backend> backend_;
323
324     VSyncWorker vsync_worker_;
325     DrmConnector *connector_ = NULL;
326     DrmCrtc *crtc_ = NULL;
327     hwc2_display_t handle_;
328     HWC2::DisplayType type_;
329     uint32_t layer_idx_ = 0;
330     std::map<hwc2_layer_t, HwcLayer> layers_;
331     HwcLayer client_layer_;
332     UniqueFd present_fence_;
333     int32_t color_mode_;
334     std::array<float, MATRIX_SIZE> color_transform_matrix_;
335     android_color_transform_t color_transform_hint_;
336
337     uint32_t frame_no_ = 0;
338     Stats total_stats_;
339     Stats prev_stats_;
340     std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta);
341   };
342
343   class DrmHotplugHandler : public DrmEventHandler {
344    public:
345     DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm)
346         : hwc2_(hwc2), drm_(drm) {
347     }
348     void HandleEvent(uint64_t timestamp_us);
349
350    private:
351     DrmHwcTwo *hwc2_;
352     DrmDevice *drm_;
353   };
354
355  private:
356   static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) {
357     return static_cast<DrmHwcTwo *>(dev);
358   }
359
360   template <typename PFN, typename T>
361   static hwc2_function_pointer_t ToHook(T function) {
362     static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
363     return reinterpret_cast<hwc2_function_pointer_t>(function);
364   }
365
366   template <typename T, typename HookType, HookType func, typename... Args>
367   static T DeviceHook(hwc2_device_t *dev, Args... args) {
368     DrmHwcTwo *hwc = toDrmHwcTwo(dev);
369     return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
370   }
371
372   static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) {
373     auto it = hwc->displays_.find(display_handle);
374     if (it == hwc->displays_.end())
375       return nullptr;
376
377     return &it->second;
378   }
379
380   template <typename HookType, HookType func, typename... Args>
381   static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
382                              Args... args) {
383     HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
384     if (!display)
385       return static_cast<int32_t>(HWC2::Error::BadDisplay);
386
387     return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
388   }
389
390   template <typename HookType, HookType func, typename... Args>
391   static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
392                            hwc2_layer_t layer_handle, Args... args) {
393     HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle);
394     if (!display)
395       return static_cast<int32_t>(HWC2::Error::BadDisplay);
396
397     HwcLayer *layer = display->get_layer(layer_handle);
398     if (!layer)
399       return static_cast<int32_t>(HWC2::Error::BadLayer);
400
401     return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
402   }
403
404   // hwc2_device_t hooks
405   static int HookDevClose(hw_device_t *dev);
406   static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
407                                      int32_t *out_capabilities);
408   static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
409                                                     int32_t descriptor);
410
411   // Device functions
412   HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
413                                    int32_t *format, hwc2_display_t *display);
414   HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
415   void Dump(uint32_t *outSize, char *outBuffer);
416   uint32_t GetMaxVirtualDisplayCount();
417   HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
418                                hwc2_function_pointer_t function);
419   HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type);
420   void HandleDisplayHotplug(hwc2_display_t displayid, int state);
421   void HandleInitialHotplugState(DrmDevice *drmDevice);
422
423   ResourceManager resource_manager_;
424   std::map<hwc2_display_t, HwcDisplay> displays_;
425   std::map<HWC2::Callback, HwcCallback> callbacks_;
426
427   std::string mDumpString;
428 };
429 }  // namespace android
430
431 #endif