OSDN Git Service

ff510a45ffdf107193d43e67162618418d5713b8
[android-x86/external-minigbm.git] / cros_gralloc / gralloc1 / cros_gralloc1_module.h
1 /*
2  * Copyright 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 CROS_GRALLOC1_MODULE_H
18 #define CROS_GRALLOC1_MODULE_H
19
20 #include <memory.h>
21
22 #include "../cros_gralloc_driver.h"
23
24 #include <hardware/gralloc1.h>
25 #include <utils/Log.h>
26
27 struct cros_gralloc_module;
28
29 namespace android
30 {
31
32 typedef enum {
33         CROS_GRALLOC_ERROR_NONE = 0,
34         CROS_GRALLOC_ERROR_BAD_DESCRIPTOR = 1,
35         CROS_GRALLOC_ERROR_BAD_HANDLE = 2,
36         CROS_GRALLOC_ERROR_BAD_VALUE = 3,
37         CROS_GRALLOC_ERROR_NOT_SHARED = 4,
38         CROS_GRALLOC_ERROR_NO_RESOURCES = 5,
39         CROS_GRALLOC_ERROR_UNDEFINED = 6,
40         CROS_GRALLOC_ERROR_UNSUPPORTED = 7,
41 } cros_gralloc_error_t;
42
43 class CrosGralloc1 : public gralloc1_device_t
44 {
45       public:
46         CrosGralloc1();
47         ~CrosGralloc1();
48
49         bool Init();
50
51         static int HookDevOpen(const struct hw_module_t *mod, const char *name,
52                                struct hw_device_t **device);
53         static int HookDevClose(hw_device_t *dev);
54
55       private:
56         static inline CrosGralloc1 *getAdapter(gralloc1_device_t *device)
57         {
58                 return static_cast<CrosGralloc1 *>(device);
59         }
60
61         // getCapabilities
62
63         void doGetCapabilities(uint32_t *outCount,
64                                int32_t * /*gralloc1_capability_t*/ outCapabilities);
65         static void getCapabilitiesHook(gralloc1_device_t *device, uint32_t *outCount,
66                                         int32_t * /*gralloc1_capability_t*/ outCapabilities)
67         {
68                 getAdapter(device)->doGetCapabilities(outCount, outCapabilities);
69         };
70
71         // getFunction
72
73         gralloc1_function_pointer_t
74             doGetFunction(int32_t /*gralloc1_function_descriptor_t*/ descriptor);
75         static gralloc1_function_pointer_t
76         getFunctionHook(gralloc1_device_t *device,
77                         int32_t /*gralloc1_function_descriptor_t*/ descriptor)
78         {
79                 return getAdapter(device)->doGetFunction(descriptor);
80         }
81
82         // dump
83
84         void dump(uint32_t *outSize, char *outBuffer);
85         static void dumpHook(gralloc1_device_t *device, uint32_t *outSize, char *outBuffer)
86         {
87                 return getAdapter(device)->dump(outSize, outBuffer);
88         }
89
90         // Buffer descriptor functions
91
92         int32_t setConsumerUsage(gralloc1_buffer_descriptor_t descriptorId, uint64_t intUsage);
93
94         int32_t setProducerUsage(gralloc1_buffer_descriptor_t descriptorId, uint64_t intUsage);
95
96         int32_t setDimensions(gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
97                               uint32_t height);
98
99         int32_t setFormat(gralloc1_buffer_descriptor_t descriptorId, int32_t format);
100
101         int32_t createDescriptor(gralloc1_buffer_descriptor_t *outDescriptor);
102         static int32_t createDescriptorHook(gralloc1_device_t *device,
103                                             gralloc1_buffer_descriptor_t *outDescriptor)
104         {
105                 return getAdapter(device)->createDescriptor(outDescriptor);
106         }
107
108         int32_t destroyDescriptor(gralloc1_buffer_descriptor_t descriptor);
109         static int32_t destroyDescriptorHook(gralloc1_device_t *device,
110                                              gralloc1_buffer_descriptor_t descriptor)
111         {
112                 return getAdapter(device)->destroyDescriptor(descriptor);
113         }
114
115         static int32_t setConsumerUsageHook(gralloc1_device_t *device,
116                                             gralloc1_buffer_descriptor_t descriptorId,
117                                             uint64_t intUsage)
118         {
119                 return getAdapter(device)->setConsumerUsage(descriptorId, intUsage);
120         }
121
122         static int32_t setDimensionsHook(gralloc1_device_t *device,
123                                          gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
124                                          uint32_t height)
125         {
126                 return getAdapter(device)->setDimensions(descriptorId, width, height);
127         }
128
129         static int32_t setFormatHook(gralloc1_device_t *device,
130                                      gralloc1_buffer_descriptor_t descriptorId, int32_t format)
131         {
132                 return getAdapter(device)->setFormat(descriptorId, format);
133         }
134
135         static int32_t setProducerUsageHook(gralloc1_device_t *device,
136                                             gralloc1_buffer_descriptor_t descriptorId,
137                                             uint64_t intUsage)
138         {
139                 return getAdapter(device)->setProducerUsage(descriptorId, intUsage);
140         }
141
142         int32_t getNumFlexPlanes(buffer_handle_t buffer, uint32_t *outNumPlanes);
143         static int32_t getNumFlexPlanesHook(gralloc1_device_t *device, buffer_handle_t buffer,
144                                             uint32_t *outNumPlanes)
145         {
146                 return getAdapter(device)->getNumFlexPlanes(buffer, outNumPlanes);
147         }
148
149         int32_t getBackingStore(buffer_handle_t buffer, gralloc1_backing_store_t *outStore);
150         static int32_t getBackingStoreHook(gralloc1_device_t *device, buffer_handle_t buffer,
151                                            gralloc1_backing_store_t *outStore)
152         {
153                 return getAdapter(device)->getBackingStore(buffer, outStore);
154         }
155
156         int32_t getConsumerUsage(buffer_handle_t buffer,
157                                  uint64_t * /*gralloc1_consumer_usage_t*/ outUsage);
158         static int32_t getConsumerUsageHook(gralloc1_device_t *device, buffer_handle_t buffer,
159                                             uint64_t * /*gralloc1_consumer_usage_t*/ outUsage)
160         {
161                 return getAdapter(device)->getConsumerUsage(buffer, outUsage);
162         }
163
164         int32_t getDimensions(buffer_handle_t buffer, uint32_t *outWidth, uint32_t *outHeight);
165         static int32_t getDimensionsHook(gralloc1_device_t *device, buffer_handle_t buffer,
166                                          uint32_t *outWidth, uint32_t *outHeight)
167         {
168                 return getAdapter(device)->getDimensions(buffer, outWidth, outHeight);
169         }
170
171         int32_t getFormat(buffer_handle_t buffer, int32_t *outFormat);
172         static int32_t getFormatHook(gralloc1_device_t *device, buffer_handle_t buffer,
173                                      int32_t *outFormat)
174         {
175                 return getAdapter(device)->getFormat(buffer, outFormat);
176         }
177
178         int32_t getProducerUsage(buffer_handle_t buffer,
179                                  uint64_t * /*gralloc1_producer_usage_t*/ outUsage);
180         static int32_t getProducerUsageHook(gralloc1_device_t *device, buffer_handle_t buffer,
181                                             uint64_t * /*gralloc1_producer_usage_t*/ outUsage)
182         {
183                 return getAdapter(device)->getProducerUsage(buffer, outUsage);
184         }
185
186         int32_t getStride(buffer_handle_t buffer, uint32_t *outStride);
187         static int32_t getStrideHook(gralloc1_device_t *device, buffer_handle_t buffer,
188                                      uint32_t *outStride)
189         {
190                 return getAdapter(device)->getStride(buffer, outStride);
191         }
192
193         // Buffer Management functions
194         int32_t allocate(struct cros_gralloc_buffer_descriptor *descriptor,
195                          buffer_handle_t *outBufferHandle);
196         static int32_t allocateBuffers(gralloc1_device_t *device, uint32_t numDescriptors,
197                                        const gralloc1_buffer_descriptor_t *descriptors,
198                                        buffer_handle_t *outBuffers);
199
200         int32_t release(buffer_handle_t bufferHandle);
201         int32_t retain(buffer_handle_t bufferHandle);
202
203         // Member function pointer 'member' will either be retain or release
204         template <int32_t (CrosGralloc1::*member)(buffer_handle_t bufferHandle)>
205         static int32_t managementHook(gralloc1_device_t *device, buffer_handle_t bufferHandle)
206         {
207                 auto adapter = getAdapter(device);
208                 return ((*adapter).*member)(bufferHandle);
209         }
210
211         // Buffer access functions
212         int32_t lock(buffer_handle_t bufferHandle, gralloc1_producer_usage_t producerUsage,
213                      gralloc1_consumer_usage_t consumerUsage, const gralloc1_rect_t &accessRegion,
214                      void **outData, int32_t acquireFence);
215         int32_t lockFlex(buffer_handle_t bufferHandle, gralloc1_producer_usage_t producerUsage,
216                          gralloc1_consumer_usage_t consumerUsage,
217                          const gralloc1_rect_t &accessRegion, struct android_flex_layout *outFlex,
218                          int32_t acquireFence);
219         int32_t lockYCbCr(buffer_handle_t bufferHandle, gralloc1_producer_usage_t producerUsage,
220                           gralloc1_consumer_usage_t consumerUsage,
221                           const gralloc1_rect_t &accessRegion, struct android_ycbcr *outFlex,
222                           int32_t acquireFence);
223
224         template <typename OUT,
225                   int32_t (CrosGralloc1::*member)(
226                       buffer_handle_t bufferHandle, gralloc1_producer_usage_t,
227                       gralloc1_consumer_usage_t, const gralloc1_rect_t &, OUT *, int32_t)>
228         static int32_t lockHook(gralloc1_device_t *device, buffer_handle_t bufferHandle,
229                                 uint64_t /*gralloc1_producer_usage_t*/ uintProducerUsage,
230                                 uint64_t /*gralloc1_consumer_usage_t*/ uintConsumerUsage,
231                                 const gralloc1_rect_t *accessRegion, OUT *outData,
232                                 int32_t acquireFenceFd)
233         {
234                 auto adapter = getAdapter(device);
235
236                 // Exactly one of producer and consumer usage must be *_USAGE_NONE,
237                 // but we can't check this until the upper levels of the framework
238                 // correctly distinguish between producer and consumer usage
239                 /*
240                 bool hasProducerUsage =
241                         uintProducerUsage != GRALLOC1_PRODUCER_USAGE_NONE;
242                 bool hasConsumerUsage =
243                         uintConsumerUsage != GRALLOC1_CONSUMER_USAGE_NONE;
244                 if (hasProducerUsage && hasConsumerUsage ||
245                         !hasProducerUsage && !hasConsumerUsage) {
246                     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
247                 }
248                 */
249
250                 auto producerUsage = static_cast<gralloc1_producer_usage_t>(uintProducerUsage);
251                 auto consumerUsage = static_cast<gralloc1_consumer_usage_t>(uintConsumerUsage);
252
253                 if (!outData) {
254                         const auto producerCpuUsage =
255                             GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE;
256                         if (producerUsage & (producerCpuUsage != 0)) {
257                                 return CROS_GRALLOC_ERROR_BAD_VALUE;
258                         }
259                         if (consumerUsage & (GRALLOC1_CONSUMER_USAGE_CPU_READ != 0)) {
260                                 return CROS_GRALLOC_ERROR_BAD_VALUE;
261                         }
262                 }
263
264                 if (!accessRegion) {
265                         ALOGE("accessRegion is null");
266                         return CROS_GRALLOC_ERROR_BAD_VALUE;
267                 }
268
269                 return ((*adapter).*member)(bufferHandle, producerUsage, consumerUsage,
270                                             *accessRegion, outData, acquireFenceFd);
271         }
272
273         int32_t unlock(buffer_handle_t bufferHandle, int32_t *outReleaseFence);
274         static int32_t unlockHook(gralloc1_device_t *device, buffer_handle_t bufferHandle,
275                                   int32_t *outReleaseFenceFd)
276         {
277                 auto adapter = getAdapter(device);
278                 *outReleaseFenceFd = -1;
279                 int32_t releaseFence;
280                 auto error = adapter->unlock(bufferHandle, &releaseFence);
281                 if (error == CROS_GRALLOC_ERROR_NONE && releaseFence > 0) {
282                         *outReleaseFenceFd = dup(releaseFence);
283                 }
284                 return error;
285         }
286
287         // Adapter internals
288         std::unique_ptr<cros_gralloc_driver> driver;
289 };
290
291 } // namespace android
292
293 #endif