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 OS_ANDROID_IAHWC2_H_
18 #define OS_ANDROID_IAHWC2_H_
20 #include <hardware/hwcomposer2.h>
22 #include <gpudevice.h>
24 #include <platformdefines.h>
29 #include "hwcservice.h"
31 namespace hwcomposer {
38 class IAHWC2 : public hwc2_device_t {
40 static int HookDevOpen(const struct hw_module_t *module, const char *name,
41 struct hw_device_t **dev);
46 hwcomposer::NativeDisplay *GetPrimaryDisplay();
47 hwcomposer::NativeDisplay *GetExtendedDisplay(uint32_t);
52 HWC2::Composition sf_type() const {
55 HWC2::Composition validated_type() const {
56 return validated_type_;
58 void accept_type_change() {
59 sf_type_ = validated_type_;
61 void set_validated_type(HWC2::Composition type) {
62 validated_type_ = type;
64 bool type_changed() const {
65 return sf_type_ != validated_type_;
68 uint32_t z_order() const {
69 return hwc_layer_.GetZorder();
72 void set_buffer(buffer_handle_t buffer) {
73 native_handle_.handle_ = buffer;
74 hwc_layer_.SetNativeHandle(&native_handle_);
77 void set_acquire_fence(int acquire_fence) {
78 if (acquire_fence > 0)
79 hwc_layer_.SetAcquireFence(dup(acquire_fence));
82 hwcomposer::HwcLayer *GetLayer() {
86 bool IsCursorLayer() const {
87 return is_cursor_layer_;
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);
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_;
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);
127 HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
128 hwc2_function_pointer_t func);
130 HWC2::Error RegisterRefreshCallback(hwc2_callback_data_t data,
131 hwc2_function_pointer_t func);
133 HWC2::Error RegisterHotPlugCallback(hwc2_callback_data_t data,
134 hwc2_function_pointer_t func);
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,
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,
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,
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);
176 hwcomposer::NativeDisplay *GetDisplay();
179 hwcomposer::NativeDisplay *display_ = NULL;
180 hwc2_display_t handle_;
181 HWC2::DisplayType type_;
183 std::map<hwc2_layer_t, Hwc2Layer> layers_;
184 Hwc2Layer client_layer_;
187 uint32_t frame_no_ = 0;
188 // True after validateDisplay
189 bool checkValidateDisplay = false;
190 bool disable_explicit_sync_ = false;
193 static IAHWC2 *toIAHWC2(hwc2_device_t *dev) {
194 return static_cast<IAHWC2 *>(dev);
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);
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)...));
209 template <typename HookType, HookType func, typename... Args>
210 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
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)...));
218 if (display_handle == HWC_DISPLAY_VIRTUAL) {
219 return static_cast<int32_t>(
220 (hwc->virtual_display_.*func)(std::forward<Args>(args)...));
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)...));
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)...));
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)...));
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)...));
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,
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);
267 hwcomposer::GpuDevice device_;
268 std::vector<std::unique_ptr<HwcDisplay>> extended_displays_;
269 HwcDisplay primary_display_;
270 HwcDisplay virtual_display_;
272 bool disable_explicit_sync_ = false;
273 android::HwcService hwcService_;
275 } // namespace android
277 #endif // OS_ANDROID_IAHWC2_H_