OSDN Git Service

Correct the header file reference
[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
102         int32_t createDescriptor(gralloc1_buffer_descriptor_t *outDescriptor);
103         static int32_t createDescriptorHook(gralloc1_device_t *device,
104                                             gralloc1_buffer_descriptor_t *outDescriptor)
105         {
106                 return getAdapter(device)->createDescriptor(outDescriptor);
107         }
108
109         int32_t destroyDescriptor(gralloc1_buffer_descriptor_t descriptor);
110         static int32_t destroyDescriptorHook(gralloc1_device_t *device,
111                                              gralloc1_buffer_descriptor_t descriptor)
112         {
113                 return getAdapter(device)->destroyDescriptor(descriptor);
114         }
115
116         static int32_t setConsumerUsageHook(gralloc1_device_t *device,
117                                             gralloc1_buffer_descriptor_t descriptorId,
118                                             uint64_t intUsage)
119         {
120                 return getAdapter(device)->setConsumerUsage(descriptorId, intUsage);
121         }
122
123         static int32_t setDimensionsHook(gralloc1_device_t *device,
124                                          gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
125                                          uint32_t height)
126         {
127                 return getAdapter(device)->setDimensions(descriptorId, width, height);
128         }
129
130         static int32_t setFormatHook(gralloc1_device_t *device,
131                                      gralloc1_buffer_descriptor_t descriptorId, int32_t format)
132         {
133                 return getAdapter(device)->setFormat(descriptorId, format);
134         }
135
136         static int32_t setProducerUsageHook(gralloc1_device_t *device,
137                                             gralloc1_buffer_descriptor_t descriptorId,
138                                             uint64_t intUsage)
139         {
140                 return getAdapter(device)->setProducerUsage(descriptorId, intUsage);
141         }
142
143         int32_t getNumFlexPlanes(buffer_handle_t buffer, uint32_t *outNumPlanes);
144         static int32_t getNumFlexPlanesHook(gralloc1_device_t *device, buffer_handle_t buffer,
145                                             uint32_t *outNumPlanes)
146         {
147                 return getAdapter(device)->getNumFlexPlanes(buffer, outNumPlanes);
148         }
149
150         int32_t getBackingStore(buffer_handle_t buffer, gralloc1_backing_store_t *outStore);
151         static int32_t getBackingStoreHook(gralloc1_device_t *device, buffer_handle_t buffer,
152                                            gralloc1_backing_store_t *outStore)
153         {
154                 return getAdapter(device)->getBackingStore(buffer, outStore);
155         }
156
157         int32_t getConsumerUsage(buffer_handle_t buffer,
158                                  uint64_t * /*gralloc1_consumer_usage_t*/ outUsage);
159         static int32_t getConsumerUsageHook(gralloc1_device_t *device, buffer_handle_t buffer,
160                                             uint64_t * /*gralloc1_consumer_usage_t*/ outUsage)
161         {
162                 return getAdapter(device)->getConsumerUsage(buffer, outUsage);
163         }
164
165         int32_t getDimensions(buffer_handle_t buffer, uint32_t *outWidth, uint32_t *outHeight);
166         static int32_t getDimensionsHook(gralloc1_device_t *device, buffer_handle_t buffer,
167                                          uint32_t *outWidth, uint32_t *outHeight)
168         {
169                 return getAdapter(device)->getDimensions(buffer, outWidth, outHeight);
170         }
171
172         int32_t getFormat(buffer_handle_t buffer, int32_t *outFormat);
173         static int32_t getFormatHook(gralloc1_device_t *device, buffer_handle_t buffer,
174                                      int32_t *outFormat)
175         {
176                 return getAdapter(device)->getFormat(buffer, outFormat);
177         }
178
179         int32_t getProducerUsage(buffer_handle_t buffer,
180                                  uint64_t * /*gralloc1_producer_usage_t*/ outUsage);
181         static int32_t getProducerUsageHook(gralloc1_device_t *device, buffer_handle_t buffer,
182                                             uint64_t * /*gralloc1_producer_usage_t*/ outUsage)
183         {
184                 return getAdapter(device)->getProducerUsage(buffer, outUsage);
185         }
186
187         int32_t getStride(buffer_handle_t buffer, uint32_t *outStride);
188         static int32_t getStrideHook(gralloc1_device_t *device, buffer_handle_t buffer,
189                                      uint32_t *outStride)
190         {
191                 return getAdapter(device)->getStride(buffer, outStride);
192         }
193
194         // Buffer Management functions
195         int32_t allocate(struct cros_gralloc_buffer_descriptor *descriptor,
196                          buffer_handle_t *outBufferHandle);
197         static int32_t allocateBuffers(gralloc1_device_t *device, uint32_t numDescriptors,
198                                        const gralloc1_buffer_descriptor_t *descriptors,
199                                        buffer_handle_t *outBuffers);
200
201         int32_t release(buffer_handle_t bufferHandle);
202         int32_t retain(buffer_handle_t bufferHandle);
203
204         // Member function pointer 'member' will either be retain or release
205         template <int32_t (CrosGralloc1::*member)(buffer_handle_t bufferHandle)>
206         static int32_t managementHook(gralloc1_device_t *device, buffer_handle_t bufferHandle)
207         {
208                 auto adapter = getAdapter(device);
209                 return ((*adapter).*member)(bufferHandle);
210         }
211
212         // Buffer access functions
213         int32_t lock(buffer_handle_t bufferHandle, gralloc1_producer_usage_t producerUsage,
214                      gralloc1_consumer_usage_t consumerUsage, const gralloc1_rect_t &accessRegion,
215                      void **outData, int32_t acquireFence);
216         int32_t lockFlex(buffer_handle_t bufferHandle, gralloc1_producer_usage_t producerUsage,
217                          gralloc1_consumer_usage_t consumerUsage,
218                          const gralloc1_rect_t &accessRegion, struct android_flex_layout *outFlex,
219                          int32_t acquireFence);
220         int32_t lockYCbCr(buffer_handle_t bufferHandle, gralloc1_producer_usage_t producerUsage,
221                           gralloc1_consumer_usage_t consumerUsage,
222                           const gralloc1_rect_t &accessRegion, struct android_ycbcr *outFlex,
223                           int32_t acquireFence);
224
225         template <typename OUT,
226                   int32_t (CrosGralloc1::*member)(
227                       buffer_handle_t bufferHandle, gralloc1_producer_usage_t,
228                       gralloc1_consumer_usage_t, const gralloc1_rect_t &, OUT *, int32_t)>
229         static int32_t lockHook(gralloc1_device_t *device, buffer_handle_t bufferHandle,
230                                 uint64_t /*gralloc1_producer_usage_t*/ uintProducerUsage,
231                                 uint64_t /*gralloc1_consumer_usage_t*/ uintConsumerUsage,
232                                 const gralloc1_rect_t *accessRegion, OUT *outData,
233                                 int32_t acquireFenceFd)
234         {
235                 auto adapter = getAdapter(device);
236
237                 // Exactly one of producer and consumer usage must be *_USAGE_NONE,
238                 // but we can't check this until the upper levels of the framework
239                 // correctly distinguish between producer and consumer usage
240                 /*
241                 bool hasProducerUsage =
242                         uintProducerUsage != GRALLOC1_PRODUCER_USAGE_NONE;
243                 bool hasConsumerUsage =
244                         uintConsumerUsage != GRALLOC1_CONSUMER_USAGE_NONE;
245                 if (hasProducerUsage && hasConsumerUsage ||
246                         !hasProducerUsage && !hasConsumerUsage) {
247                     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
248                 }
249                 */
250
251                 auto producerUsage = static_cast<gralloc1_producer_usage_t>(uintProducerUsage);
252                 auto consumerUsage = static_cast<gralloc1_consumer_usage_t>(uintConsumerUsage);
253
254                 if (!outData) {
255                         const auto producerCpuUsage =
256                             GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE;
257                         if (producerUsage & (producerCpuUsage != 0)) {
258                                 return CROS_GRALLOC_ERROR_BAD_VALUE;
259                         }
260                         if (consumerUsage & (GRALLOC1_CONSUMER_USAGE_CPU_READ != 0)) {
261                                 return CROS_GRALLOC_ERROR_BAD_VALUE;
262                         }
263                 }
264
265                 if (!accessRegion) {
266                         ALOGE("accessRegion is null");
267                         return CROS_GRALLOC_ERROR_BAD_VALUE;
268                 }
269
270                 return ((*adapter).*member)(bufferHandle, producerUsage, consumerUsage,
271                                             *accessRegion, outData, acquireFenceFd);
272         }
273
274         int32_t unlock(buffer_handle_t bufferHandle, int32_t *outReleaseFence);
275         static int32_t unlockHook(gralloc1_device_t *device, buffer_handle_t bufferHandle,
276                                   int32_t *outReleaseFenceFd)
277         {
278                 auto adapter = getAdapter(device);
279                 *outReleaseFenceFd = -1;
280                 int32_t releaseFence;
281                 auto error = adapter->unlock(bufferHandle, &releaseFence);
282                 if (error == CROS_GRALLOC_ERROR_NONE && releaseFence > 0) {
283                         *outReleaseFenceFd = dup(releaseFence);
284                 }
285                 return error;
286         }
287
288         // Adapter internals
289         std::unique_ptr<cros_gralloc_driver> driver;
290 };
291
292 } // namespace android
293
294 #endif