OSDN Git Service

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