2 * Copyright (c) 2017 Intel Corporation
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 LINUX_FRONTEND_H_
18 #define LINUX_FRONTEND_H_
20 #include <gpudevice.h>
24 #include <type_traits>
27 #include "pixeluploader.h"
30 namespace hwcomposer {
32 class NativeBufferHandler;
35 class IAHWC : public iahwc_device {
40 class IAHWCLayer : public PixelUploaderLayerCallback {
42 IAHWCLayer(PixelUploader* uploader);
43 ~IAHWCLayer() override;
44 int SetBo(gbm_bo* bo);
45 int SetRawPixelData(iahwc_raw_pixel_data bo);
46 int SetAcquireFence(int32_t acquire_fence);
47 int SetLayerUsage(int32_t layer_usage);
48 int32_t GetLayerUsage() {
51 int SetLayerTransform(int32_t layer_transform);
52 int SetLayerSourceCrop(iahwc_rect_t rect);
53 int SetLayerDisplayFrame(iahwc_rect_t rect);
54 int SetLayerSurfaceDamage(iahwc_region_t region);
55 int SetLayerPlaneAlpha(float alpha);
56 int SetLayerIndex(uint32_t layer_index);
57 uint32_t GetLayerIndex() {
60 hwcomposer::HwcLayer* GetLayer();
62 void UploadDone() override;
65 void ClosePrimeHandles();
66 hwcomposer::HwcLayer iahwc_layer_;
67 struct gbm_handle hwc_handle_;
68 HWCNativeHandle pixel_buffer_ = NULL;
69 uint32_t orig_width_ = 0;
70 uint32_t orig_height_ = 0;
71 uint32_t orig_stride_ = 0;
72 PixelUploader* raw_data_uploader_ = NULL;
74 uint32_t layer_index_;
75 bool upload_in_progress_ = false;
78 class IAHWCDisplay : public PixelUploaderCallback {
82 int Init(hwcomposer::NativeDisplay* display, uint32_t gpu_fd);
83 int GetDisplayInfo(uint32_t config, int attribute, int32_t* value);
84 int GetDisplayName(uint32_t* size, char* name);
85 int GetDisplayConfigs(uint32_t* num_configs, uint32_t* configs);
86 int SetDisplayGamma(float r, float b, float g);
87 int SetDisplayConfig(uint32_t config);
88 int GetDisplayConfig(uint32_t* config);
90 int PresentDisplay(int32_t* release_fd);
91 int RegisterVsyncCallback(iahwc_callback_data_t data,
92 iahwc_function_ptr_t hook);
93 void RegisterPixelUploaderCallback(iahwc_callback_data_t data,
94 iahwc_function_ptr_t hook);
95 int CreateLayer(uint32_t* layer_handle);
96 int DestroyLayer(uint32_t layer_handle);
98 IAHWCLayer& get_layer(iahwc_layer_t layer) {
99 return layers_.at(layer);
102 int DisableOverlayUsage();
104 int EnableOverlayUsage();
106 void Synchronize() override;
108 int RegisterHotPlugCallback(iahwc_callback_data_t data,
109 iahwc_function_ptr_t func);
110 int RunPixelUploader(bool enable);
113 PixelUploader* raw_data_uploader_ = NULL;
114 hwcomposer::NativeDisplay* native_display_;
115 std::map<iahwc_layer_t, IAHWCLayer> layers_;
118 static IAHWC* toIAHWC(iahwc_device_t* dev) {
119 return static_cast<IAHWC*>(dev);
122 static int HookOpen(const iahwc_module_t*, iahwc_device_t**);
123 static int HookClose(iahwc_device_t*);
124 static iahwc_function_ptr_t HookGetFunctionPtr(iahwc_device_t*, int);
126 template <typename PFN, typename T>
127 static iahwc_function_ptr_t ToHook(T function) {
128 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer");
129 return reinterpret_cast<iahwc_function_ptr_t>(function);
132 template <typename T, typename HookType, HookType func, typename... Args>
133 static T DeviceHook(iahwc_device_t* dev, Args... args) {
134 IAHWC* hwc = toIAHWC(dev);
135 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...));
138 template <typename HookType, HookType func, typename... Args>
139 static int32_t DisplayHook(iahwc_device_t* dev,
140 iahwc_display_t display_handle, Args... args) {
141 IAHWC* hwc = toIAHWC(dev);
142 IAHWCDisplay* display = hwc->displays_.at(display_handle);
143 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
146 template <typename HookType, HookType func, typename... Args>
147 static int32_t LayerHook(iahwc_device_t* dev, iahwc_display_t display_handle,
148 iahwc_layer_t layer_handle, Args... args) {
149 IAHWC* hwc = toIAHWC(dev);
150 IAHWCDisplay* display = hwc->displays_.at(display_handle);
151 IAHWCLayer& layer = display->get_layer(layer_handle);
153 return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
157 int GetNumDisplays(int* num_displays);
158 int RegisterCallback(int32_t description, uint32_t display_handle,
159 iahwc_callback_data_t data, iahwc_function_ptr_t hook);
160 hwcomposer::GpuDevice device_;
161 std::vector<IAHWCDisplay*> displays_;
164 } // namespace hwcomposer