OSDN Git Service

Weston: Upload only damaged region.
[android-x86/external-IA-Hardware-Composer.git] / os / linux / linux_frontend.h
1 /*
2  * Copyright (c) 2017 Intel Corporation
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 LINUX_FRONTEND_H_
18 #define LINUX_FRONTEND_H_
19
20 #include <gpudevice.h>
21 #include <hwcdefs.h>
22 #include <hwclayer.h>
23 #include <map>
24 #include <type_traits>
25 #include <vector>
26 #include "iahwc.h"
27 #include "pixeluploader.h"
28 #include "spinlock.h"
29
30 namespace hwcomposer {
31
32 class NativeBufferHandler;
33 class PixelUploader;
34
35 class IAHWC : public iahwc_device {
36  public:
37   IAHWC();
38   int32_t Init();
39
40   class IAHWCLayer : public PixelUploaderLayerCallback {
41    public:
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() {
49       return layer_usage_;
50     }
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() {
58       return layer_index_;
59     }
60     hwcomposer::HwcLayer* GetLayer();
61
62     void UploadDone() override;
63
64    private:
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;
73     int32_t layer_usage_;
74     uint32_t layer_index_;
75     bool upload_in_progress_ = false;
76   };
77
78   class IAHWCDisplay : public PixelUploaderCallback {
79    public:
80     IAHWCDisplay();
81     ~IAHWCDisplay();
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);
89     int ClearAllLayers();
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);
97     bool IsConnected();
98     IAHWCLayer& get_layer(iahwc_layer_t layer) {
99       return layers_.at(layer);
100     }
101
102     int DisableOverlayUsage();
103
104     int EnableOverlayUsage();
105
106     void Synchronize() override;
107
108     int RegisterHotPlugCallback(iahwc_callback_data_t data,
109                                 iahwc_function_ptr_t func);
110     int RunPixelUploader(bool enable);
111
112    private:
113     PixelUploader* raw_data_uploader_ = NULL;
114     hwcomposer::NativeDisplay* native_display_;
115     std::map<iahwc_layer_t, IAHWCLayer> layers_;
116   };
117
118   static IAHWC* toIAHWC(iahwc_device_t* dev) {
119     return static_cast<IAHWC*>(dev);
120   }
121
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);
125
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);
130   }
131
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)...));
136   }
137
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)...));
144   }
145
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);
152
153     return static_cast<int32_t>((layer.*func)(std::forward<Args>(args)...));
154   }
155
156  private:
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_;
162 };
163
164 } // namespace hwcomposer
165 #endif