OSDN Git Service

Set scaling mode through environment variable.
[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  public:
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 XTranslateCoordinates(uint32_t x_translation) {
78       x_translation_ = x_translation;
79     }
80
81     void set_acquire_fence(int acquire_fence) {
82       if (acquire_fence > 0)
83         hwc_layer_.SetAcquireFence(acquire_fence);
84     }
85
86     hwcomposer::HwcLayer *GetLayer() {
87       return &hwc_layer_;
88     }
89
90     bool IsCursorLayer() const {
91       return hwc_layer_.IsCursorLayer();
92     }
93
94     // Layer hooks
95     HWC2::Error SetCursorPosition(int32_t x, int32_t y);
96     HWC2::Error SetLayerBlendMode(int32_t mode);
97     HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
98     HWC2::Error SetLayerColor(hwc_color_t color);
99     HWC2::Error SetLayerCompositionType(int32_t type);
100     HWC2::Error SetLayerDataspace(int32_t dataspace);
101     HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
102     HWC2::Error SetLayerPlaneAlpha(float alpha);
103     HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
104     HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
105     HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
106     HWC2::Error SetLayerTransform(int32_t transform);
107     HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
108     HWC2::Error SetLayerZOrder(uint32_t z);
109
110    private:
111     // sf_type_ stores the initial type given to us by surfaceflinger,
112     // validated_type_ stores the type after running ValidateDisplay
113     HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
114     HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
115     android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
116     hwcomposer::HwcLayer hwc_layer_;
117     struct gralloc_handle native_handle_;
118     uint32_t x_translation_ = 0;
119   };
120
121   class HwcDisplay {
122    public:
123     HwcDisplay();
124     HwcDisplay(const HwcDisplay &) = delete;
125     HWC2::Error Init(hwcomposer::NativeDisplay *display, int display_index,
126                      bool disable_explicit_sync, uint32_t scaling_mode);
127     HWC2::Error InitVirtualDisplay(hwcomposer::NativeDisplay *display,
128                                    uint32_t width, uint32_t height,
129                                    bool disable_explicit_sync);
130     HWC2::Error InitNestedDisplay(hwcomposer::NativeDisplay *display,
131                                   bool disable_explicit_sync);
132
133     HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
134                                       hwc2_function_pointer_t func);
135
136     HWC2::Error RegisterRefreshCallback(hwc2_callback_data_t data,
137                                         hwc2_function_pointer_t func);
138
139     HWC2::Error RegisterHotPlugCallback(hwc2_callback_data_t data,
140                                         hwc2_function_pointer_t func);
141
142     // HWC Hooks
143     void FreeAllLayers();
144     HWC2::Error AcceptDisplayChanges();
145     HWC2::Error CreateLayer(hwc2_layer_t *layer);
146     HWC2::Error DestroyLayer(hwc2_layer_t layer);
147     HWC2::Error GetActiveConfig(hwc2_config_t *config);
148     HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
149                                            hwc2_layer_t *layers,
150                                            int32_t *types);
151     HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
152                                        int32_t format, int32_t dataspace);
153     HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
154     HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
155                                     int32_t *value);
156     HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
157                                   hwc2_config_t *configs);
158     HWC2::Error GetDisplayName(uint32_t *size, char *name);
159     HWC2::Error GetDisplayRequests(int32_t *display_requests,
160                                    uint32_t *num_elements, hwc2_layer_t *layers,
161                                    int32_t *layer_requests);
162     HWC2::Error GetDisplayType(int32_t *type);
163     HWC2::Error GetDozeSupport(int32_t *support);
164     HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
165                                    float *max_luminance,
166                                    float *max_average_luminance,
167                                    float *min_luminance);
168     HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
169                                  int32_t *fences);
170     HWC2::Error PresentDisplay(int32_t *retire_fence);
171     HWC2::Error SetActiveConfig(hwc2_config_t config);
172     HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
173                                 int32_t dataspace, hwc_region_t damage);
174     HWC2::Error SetColorMode(int32_t mode);
175     HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
176     HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
177     HWC2::Error SetPowerMode(int32_t mode);
178     HWC2::Error SetVsyncEnabled(int32_t enabled);
179     HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
180     Hwc2Layer &get_layer(hwc2_layer_t layer) {
181       return layers_.at(layer);
182     }
183     hwcomposer::NativeDisplay *GetDisplay();
184
185    private:
186     hwcomposer::NativeDisplay *display_ = NULL;
187     hwc2_display_t handle_;
188     HWC2::DisplayType type_;
189     int layer_idx_ = 0;
190     std::map<hwc2_layer_t, Hwc2Layer> layers_;
191     Hwc2Layer client_layer_;
192     int32_t color_mode_;
193
194     uint32_t frame_no_ = 0;
195     // True after validateDisplay
196     bool checkValidateDisplay = false;
197     bool disable_explicit_sync_ = false;
198     uint32_t scaling_mode_ = 0;
199   };
200
201   static IAHWC2 *toIAHWC2(hwc2_device_t *dev) {
202     return static_cast<IAHWC2 *>(dev);
203   }
204
205   template <typename PFN, typename T>
206   static hwc2_function_pointer_t ToHook(T function) {
207     static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
208     return reinterpret_cast<hwc2_function_pointer_t>(function);
209   }
210
211   template <typename T, typename HookType, HookType func, typename... Args>
212   static T DeviceHook(hwc2_device_t *dev, Args... args) {
213     IAHWC2 *hwc = toIAHWC2(dev);
214     return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
215   }
216
217   template <typename HookType, HookType func, typename... Args>
218   static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
219                              Args... args) {
220     IAHWC2 *hwc = toIAHWC2(dev);
221     if (display_handle == HWC_DISPLAY_PRIMARY) {
222       HwcDisplay &display = hwc->primary_display_;
223       return static_cast<int32_t>((display.*func)(std::forward<Args>(args)...));
224     }
225
226     if (display_handle == HWC_DISPLAY_VIRTUAL) {
227       return static_cast<int32_t>(
228           (hwc->virtual_display_.*func)(std::forward<Args>(args)...));
229     }
230 #ifdef NESTED_DISPLAY_SUPPORT
231     if (display_handle == HWC_DISPLAY_NESTED) {
232       return static_cast<int32_t>(
233           (hwc->nested_display_.*func)(std::forward<Args>(args)...));
234     }
235 #endif
236     if (display_handle == HWC_DISPLAY_EXTERNAL) {
237       HwcDisplay *display = hwc->extended_displays_.at(0).get();
238       return static_cast<int32_t>(
239           (display->*func)(std::forward<Args>(args)...));
240     }
241
242     HwcDisplay *display = hwc->extended_displays_.at(1).get();
243     return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
244   }
245
246   template <typename HookType, HookType func, typename... Args>
247   static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
248                            hwc2_layer_t layer_handle, Args... args) {
249     IAHWC2 *hwc = toIAHWC2(dev);
250     if (display_handle == HWC_DISPLAY_PRIMARY) {
251       HwcDisplay &display = hwc->primary_display_;
252       Hwc2Layer &layer = display.get_layer(layer_handle);
253       return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
254     }
255
256     if (display_handle == HWC_DISPLAY_VIRTUAL) {
257       Hwc2Layer &layer = hwc->virtual_display_.get_layer(layer_handle);
258       return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
259     }
260 #ifdef NESTED_DISPLAY_SUPPORT
261     if (display_handle == HWC_DISPLAY_NESTED) {
262       Hwc2Layer &layer = hwc->nested_display_.get_layer(layer_handle);
263       return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
264     }
265 #endif
266     if (display_handle == HWC_DISPLAY_EXTERNAL) {
267       HwcDisplay *display = hwc->extended_displays_.at(0).get();
268       Hwc2Layer &layer = display->get_layer(layer_handle);
269       return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
270     }
271
272     HwcDisplay *display = hwc->extended_displays_.at(1).get();
273     Hwc2Layer &layer = display->get_layer(layer_handle);
274     return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
275   }
276
277   // hwc2_device_t hooks
278   static int HookDevClose(hw_device_t *dev);
279   static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
280                                      int32_t *out_capabilities);
281   static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
282                                                     int32_t descriptor);
283
284   // Device functions
285   HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
286                                    int32_t *format, hwc2_display_t *display);
287   HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
288   void Dump(uint32_t *size, char *buffer);
289   uint32_t GetMaxVirtualDisplayCount();
290   HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
291                                hwc2_function_pointer_t function);
292
293   hwcomposer::GpuDevice device_;
294   std::vector<std::unique_ptr<HwcDisplay>> extended_displays_;
295   HwcDisplay primary_display_;
296   HwcDisplay virtual_display_;
297   HwcDisplay nested_display_;
298
299   bool disable_explicit_sync_ = false;
300   android::HwcService hwcService_;
301   uint32_t scaling_mode_ = 0;
302 };
303 }  // namespace android
304
305 #endif  // OS_ANDROID_IAHWC2_H_