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"
30 #include "multidisplaymanager.h"
32 namespace hwcomposer {
39 class IAHWC2 : public hwc2_device_t {
41 static int HookDevOpen(const struct hw_module_t *module, const char *name,
42 struct hw_device_t **dev);
51 HWC2::Composition sf_type() const {
54 HWC2::Composition validated_type() const {
55 return validated_type_;
57 void accept_type_change() {
58 sf_type_ = validated_type_;
60 void set_validated_type(HWC2::Composition type) {
61 validated_type_ = type;
63 bool type_changed() const {
64 return sf_type_ != validated_type_;
67 uint32_t z_order() const {
68 return hwc_layer_.GetZorder();
71 void set_buffer(buffer_handle_t buffer) {
72 native_handle_.handle_ = buffer;
73 hwc_layer_.SetNativeHandle(&native_handle_);
76 void set_acquire_fence(int acquire_fence) {
77 if (acquire_fence > 0)
78 hwc_layer_.SetAcquireFence(dup(acquire_fence));
81 hwcomposer::HwcLayer *GetLayer() {
86 HWC2::Error SetCursorPosition(int32_t x, int32_t y);
87 HWC2::Error SetLayerBlendMode(int32_t mode);
88 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
89 HWC2::Error SetLayerColor(hwc_color_t color);
90 HWC2::Error SetLayerCompositionType(int32_t type);
91 HWC2::Error SetLayerDataspace(int32_t dataspace);
92 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
93 HWC2::Error SetLayerPlaneAlpha(float alpha);
94 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
95 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
96 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
97 HWC2::Error SetLayerTransform(int32_t transform);
98 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
99 HWC2::Error SetLayerZOrder(uint32_t z);
102 // sf_type_ stores the initial type given to us by surfaceflinger,
103 // validated_type_ stores the type after running ValidateDisplay
104 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
105 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
108 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN;
109 hwcomposer::HwcLayer hwc_layer_;
110 struct gralloc_handle native_handle_;
116 HwcDisplay(const HwcDisplay &) = delete;
117 HWC2::Error Init(MultiDisplayManager *display_manager,
118 hwcomposer::NativeDisplay *display, int display_index,
119 bool disable_explicit_sync);
120 HWC2::Error InitVirtualDisplay(MultiDisplayManager *display_manager,
121 hwcomposer::NativeDisplay *display,
122 uint32_t width, uint32_t height,
123 bool disable_explicit_sync);
125 HWC2::Error RegisterVsyncCallback(hwc2_callback_data_t data,
126 hwc2_function_pointer_t func);
128 HWC2::Error RegisterRefreshCallback(hwc2_callback_data_t data,
129 hwc2_function_pointer_t func);
131 HWC2::Error RegisterHotPlugCallback(hwc2_callback_data_t data,
132 hwc2_function_pointer_t func);
135 HWC2::Error AcceptDisplayChanges();
136 HWC2::Error CreateLayer(hwc2_layer_t *layer);
137 HWC2::Error DestroyLayer(hwc2_layer_t layer);
138 HWC2::Error GetActiveConfig(hwc2_config_t *config);
139 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements,
140 hwc2_layer_t *layers,
142 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height,
143 int32_t format, int32_t dataspace);
144 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes);
145 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute,
147 HWC2::Error GetDisplayConfigs(uint32_t *num_configs,
148 hwc2_config_t *configs);
149 HWC2::Error GetDisplayName(uint32_t *size, char *name);
150 HWC2::Error GetDisplayRequests(int32_t *display_requests,
151 uint32_t *num_elements, hwc2_layer_t *layers,
152 int32_t *layer_requests);
153 HWC2::Error GetDisplayType(int32_t *type);
154 HWC2::Error GetDozeSupport(int32_t *support);
155 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,
156 float *max_luminance,
157 float *max_average_luminance,
158 float *min_luminance);
159 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers,
161 HWC2::Error PresentDisplay(int32_t *retire_fence);
162 HWC2::Error SetActiveConfig(hwc2_config_t config);
163 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
164 int32_t dataspace, hwc_region_t damage);
165 HWC2::Error SetColorMode(int32_t mode);
166 HWC2::Error SetColorTransform(const float *matrix, int32_t hint);
167 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence);
168 HWC2::Error SetPowerMode(int32_t mode);
169 HWC2::Error SetVsyncEnabled(int32_t enabled);
170 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests);
171 HwcLayer &get_layer(hwc2_layer_t layer) {
172 return layers_.at(layer);
176 hwcomposer::NativeDisplay *display_ = NULL;
177 hwc2_display_t handle_;
178 HWC2::DisplayType type_;
180 std::map<hwc2_layer_t, HwcLayer> layers_;
181 HwcLayer client_layer_;
184 uint32_t frame_no_ = 0;
185 // True after validateDisplay
186 bool checkValidateDisplay = false;
187 bool disable_explicit_sync_ = false;
188 MultiDisplayManager *display_manager_;
191 static IAHWC2 *toIAHWC2(hwc2_device_t *dev) {
192 return static_cast<IAHWC2 *>(dev);
195 template <typename PFN, typename T>
196 static hwc2_function_pointer_t ToHook(T function) {
197 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
198 return reinterpret_cast<hwc2_function_pointer_t>(function);
201 template <typename T, typename HookType, HookType func, typename... Args>
202 static T DeviceHook(hwc2_device_t *dev, Args... args) {
203 IAHWC2 *hwc = toIAHWC2(dev);
204 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
207 template <typename HookType, HookType func, typename... Args>
208 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle,
210 IAHWC2 *hwc = toIAHWC2(dev);
211 if (display_handle == HWC_DISPLAY_PRIMARY) {
212 HwcDisplay &display = hwc->primary_display_;
213 return static_cast<int32_t>((display.*func)(std::forward<Args>(args)...));
216 if (display_handle == HWC_DISPLAY_VIRTUAL) {
217 return static_cast<int32_t>(
218 (hwc->virtual_display_.*func)(std::forward<Args>(args)...));
221 // TODO(kalyank): How do we map extended display id in case of more than
222 // one external display.
223 HwcDisplay &display = hwc->extended_displays_.at(0);
224 return static_cast<int32_t>((display.*func)(std::forward<Args>(args)...));
227 template <typename HookType, HookType func, typename... Args>
228 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle,
229 hwc2_layer_t layer_handle, Args... args) {
230 IAHWC2 *hwc = toIAHWC2(dev);
231 if (display_handle == HWC_DISPLAY_PRIMARY) {
232 HwcDisplay &display = hwc->primary_display_;
233 HwcLayer &layer = display.get_layer(layer_handle);
234 return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
237 if (display_handle == HWC_DISPLAY_VIRTUAL) {
238 HwcLayer &layer = hwc->virtual_display_.get_layer(layer_handle);
239 return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
242 // TODO(kalyank): How do we map extended display id in case of more than
243 // one external display.
244 HwcDisplay &display = hwc->extended_displays_.at(0);
245 HwcLayer &layer = display.get_layer(layer_handle);
246 return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
249 // hwc2_device_t hooks
250 static int HookDevClose(hw_device_t *dev);
251 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count,
252 int32_t *out_capabilities);
253 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device,
257 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height,
258 int32_t *format, hwc2_display_t *display);
259 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display);
260 void Dump(uint32_t *size, char *buffer);
261 uint32_t GetMaxVirtualDisplayCount();
262 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data,
263 hwc2_function_pointer_t function);
265 hwcomposer::GpuDevice device_;
266 std::map<uint32_t, HwcDisplay> extended_displays_;
267 HwcDisplay primary_display_;
268 HwcDisplay virtual_display_;
270 bool disable_explicit_sync_ = false;
271 android::HwcService hwcService_;
272 MultiDisplayManager display_manager_;
274 } // namespace android
276 #endif // OS_ANDROID_IAHWC2_H_