OSDN Git Service

Track CursorLayer in hwc2.
[android-x86/external-IA-Hardware-Composer.git] / os / android / iahwc2.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 OS_ANDROID_IAHWC2_H_
18 #define OS_ANDROID_IAHWC2_H_
19
20 #include <hardware/hwcomposer2.h>
21
22 #include <gpudevice.h>
23 #include <hwclayer.h>
24 #include <platformdefines.h>
25
26 #include <map>
27 #include <utility>
28
29 #include "hwcservice.h"
30
31 namespace hwcomposer {
32 class GpuDevice;
33 class NativeDisplay;
34 }
35
36 namespace android {
37 class HwcService;
38 class IAHWC2 : public hwc2_device_t {
39  public:
40   static int HookDevOpen(const struct hw_module_t *module, const char *name,
41                          struct hw_device_t **dev);
42
43   IAHWC2();
44
45   HWC2::Error Init();
46   hwcomposer::NativeDisplay *GetPrimaryDisplay();
47   hwcomposer::NativeDisplay *GetExtendedDisplay(uint32_t);
48
49  private:
50   class Hwc2Layer {
51    public:
52     HWC2::Composition sf_type() const {
53       return sf_type_;
54     }
55     HWC2::Composition validated_type() const {
56       return validated_type_;
57     }
58     void accept_type_change() {
59       sf_type_ = validated_type_;
60     }
61     void set_validated_type(HWC2::Composition type) {
62       validated_type_ = type;
63     }
64     bool type_changed() const {
65       return sf_type_ != validated_type_;
66     }
67
68     uint32_t z_order() const {
69       return hwc_layer_.GetZorder();
70     }
71
72     void set_buffer(buffer_handle_t buffer) {
73       native_handle_.handle_ = buffer;
74       hwc_layer_.SetNativeHandle(&native_handle_);
75     }
76
77     void set_acquire_fence(int acquire_fence) {
78       if (acquire_fence > 0)
79         hwc_layer_.SetAcquireFence(dup(acquire_fence));
80     }
81
82     hwcomposer::HwcLayer *GetLayer() {
83       return &hwc_layer_;
84     }
85
86     bool IsCursorLayer() const {
87       return is_cursor_layer_;
88     }
89
90     // Layer hooks
91     HWC2::Error SetCursorPosition(int32_t x, int32_t y);
92     HWC2::Error SetLayerBlendMode(int32_t mode);
93     HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
94     HWC2::Error SetLayerColor(hwc_color_t color);
95     HWC2::Error SetLayerCompositionType(int32_t type);
96     HWC2::Error SetLayerDataspace(int32_t dataspace);
97     HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
98     HWC2::Error SetLayerPlaneAlpha(float alpha);
99     HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
100     HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
101     HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
102     HWC2::Error SetLayerTransform(int32_t transform);
103     HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
104     HWC2::Error SetLayerZOrder(uint32_t z);
105
106    private:
107     // sf_type_ stores the initial type given to us by surfaceflinger,
108     // validated_type_ stores the type after running ValidateDisplay
109     HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
110     HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
111     bool is_cursor_layer_ = false;
112     android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
113     hwcomposer::HwcLayer hwc_layer_;
114     struct gralloc_handle native_handle_;
115   };
116
117   class HwcDisplay {
118    public:
119     HwcDisplay();
120     HwcDisplay(const HwcDisplay &) = delete;
121     HWC2::Error Init(hwcomposer::NativeDisplay *display, int display_index,
122                      bool disable_explicit_sync);
123     HWC2::Error InitVirtualDisplay(hwcomposer::NativeDisplay *display,
124                                    uint32_t width, uint32_t height,
125                                    bool disable_explicit_sync);
126
127     HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
128                                       hwc2_function_pointer_t func);
129
130     HWC2::Error RegisterRefreshCallback(hwc2_callback_data_t data,
131                                         hwc2_function_pointer_t func);
132
133     HWC2::Error RegisterHotPlugCallback(hwc2_callback_data_t data,
134                                         hwc2_function_pointer_t func);
135
136     // HWC Hooks
137     HWC2::Error AcceptDisplayChanges();
138     HWC2::Error CreateLayer(hwc2_layer_t *layer);
139     HWC2::Error DestroyLayer(hwc2_layer_t layer);
140     HWC2::Error GetActiveConfig(hwc2_config_t *config);
141     HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
142                                            hwc2_layer_t *layers,
143                                            int32_t *types);
144     HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
145                                        int32_t format, int32_t dataspace);
146     HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
147     HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
148                                     int32_t *value);
149     HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
150                                   hwc2_config_t *configs);
151     HWC2::Error GetDisplayName(uint32_t *size, char *name);
152     HWC2::Error GetDisplayRequests(int32_t *display_requests,
153                                    uint32_t *num_elements, hwc2_layer_t *layers,
154                                    int32_t *layer_requests);
155     HWC2::Error GetDisplayType(int32_t *type);
156     HWC2::Error GetDozeSupport(int32_t *support);
157     HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
158                                    float *max_luminance,
159                                    float *max_average_luminance,
160                                    float *min_luminance);
161     HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
162                                  int32_t *fences);
163     HWC2::Error PresentDisplay(int32_t *retire_fence);
164     HWC2::Error SetActiveConfig(hwc2_config_t config);
165     HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
166                                 int32_t dataspace, hwc_region_t damage);
167     HWC2::Error SetColorMode(int32_t mode);
168     HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
169     HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
170     HWC2::Error SetPowerMode(int32_t mode);
171     HWC2::Error SetVsyncEnabled(int32_t enabled);
172     HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
173     Hwc2Layer &get_layer(hwc2_layer_t layer) {
174       return layers_.at(layer);
175     }
176     hwcomposer::NativeDisplay *GetDisplay();
177
178    private:
179     hwcomposer::NativeDisplay *display_ = NULL;
180     hwc2_display_t handle_;
181     HWC2::DisplayType type_;
182     int layer_idx_ = 0;
183     std::map<hwc2_layer_t, Hwc2Layer> layers_;
184     Hwc2Layer client_layer_;
185     int32_t color_mode_;
186
187     uint32_t frame_no_ = 0;
188     // True after validateDisplay
189     bool checkValidateDisplay = false;
190     bool disable_explicit_sync_ = false;
191   };
192
193   static IAHWC2 *toIAHWC2(hwc2_device_t *dev) {
194     return static_cast<IAHWC2 *>(dev);
195   }
196
197   template <typename PFN, typename T>
198   static hwc2_function_pointer_t ToHook(T function) {
199     static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
200     return reinterpret_cast<hwc2_function_pointer_t>(function);
201   }
202
203   template <typename T, typename HookType, HookType func, typename... Args>
204   static T DeviceHook(hwc2_device_t *dev, Args... args) {
205     IAHWC2 *hwc = toIAHWC2(dev);
206     return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
207   }
208
209   template <typename HookType, HookType func, typename... Args>
210   static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
211                              Args... args) {
212     IAHWC2 *hwc = toIAHWC2(dev);
213     if (display_handle == HWC_DISPLAY_PRIMARY) {
214       HwcDisplay &display = hwc->primary_display_;
215       return static_cast<int32_t>((display.*func)(std::forward<Args>(args)...));
216     }
217
218     if (display_handle == HWC_DISPLAY_VIRTUAL) {
219       return static_cast<int32_t>(
220           (hwc->virtual_display_.*func)(std::forward<Args>(args)...));
221     }
222
223     // TODO(kalyank): How do we map extended display id in case of more than
224     // one external display.
225     HwcDisplay *display = hwc->extended_displays_.at(0).get();
226     return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
227   }
228
229   template <typename HookType, HookType func, typename... Args>
230   static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
231                            hwc2_layer_t layer_handle, Args... args) {
232     IAHWC2 *hwc = toIAHWC2(dev);
233     if (display_handle == HWC_DISPLAY_PRIMARY) {
234       HwcDisplay &display = hwc->primary_display_;
235       Hwc2Layer &layer = display.get_layer(layer_handle);
236       return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
237     }
238
239     if (display_handle == HWC_DISPLAY_VIRTUAL) {
240       Hwc2Layer &layer = hwc->virtual_display_.get_layer(layer_handle);
241       return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
242     }
243
244     // TODO(kalyank): How do we map extended display id in case of more than
245     // one external display.
246     HwcDisplay *display = hwc->extended_displays_.at(0).get();
247     Hwc2Layer &layer = display->get_layer(layer_handle);
248     return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
249   }
250
251   // hwc2_device_t hooks
252   static int HookDevClose(hw_device_t *dev);
253   static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
254                                      int32_t *out_capabilities);
255   static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
256                                                     int32_t descriptor);
257
258   // Device functions
259   HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
260                                    int32_t *format, hwc2_display_t *display);
261   HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
262   void Dump(uint32_t *size, char *buffer);
263   uint32_t GetMaxVirtualDisplayCount();
264   HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
265                                hwc2_function_pointer_t function);
266
267   hwcomposer::GpuDevice device_;
268   std::vector<std::unique_ptr<HwcDisplay>> extended_displays_;
269   HwcDisplay primary_display_;
270   HwcDisplay virtual_display_;
271
272   bool disable_explicit_sync_ = false;
273   android::HwcService hwcService_;
274 };
275 }  // namespace android
276
277 #endif  // OS_ANDROID_IAHWC2_H_