2 * Copyright 2016 The Android Open Source Project
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 CROS_GRALLOC1_MODULE_H
18 #define CROS_GRALLOC1_MODULE_H
22 #include "../cros_gralloc_driver.h"
24 #include <hardware/gralloc1.h>
25 #include <utils/Log.h>
28 struct cros_gralloc_module;
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;
44 class CrosGralloc1 : public gralloc1_device_t
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);
57 static inline CrosGralloc1 *getAdapter(gralloc1_device_t *device)
59 return static_cast<CrosGralloc1 *>(device);
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)
69 getAdapter(device)->doGetCapabilities(outCount, outCapabilities);
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)
80 return getAdapter(device)->doGetFunction(descriptor);
85 void dump(uint32_t *outSize, char *outBuffer);
86 static void dumpHook(gralloc1_device_t *device, uint32_t *outSize, char *outBuffer)
88 return getAdapter(device)->dump(outSize, outBuffer);
91 // Buffer descriptor functions
93 int32_t setConsumerUsage(gralloc1_buffer_descriptor_t descriptorId, uint64_t intUsage);
95 int32_t setProducerUsage(gralloc1_buffer_descriptor_t descriptorId, uint64_t intUsage);
97 int32_t setDimensions(gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
100 int32_t setFormat(gralloc1_buffer_descriptor_t descriptorId, int32_t format);
102 int32_t createDescriptor(gralloc1_buffer_descriptor_t *outDescriptor);
103 static int32_t createDescriptorHook(gralloc1_device_t *device,
104 gralloc1_buffer_descriptor_t *outDescriptor)
106 return getAdapter(device)->createDescriptor(outDescriptor);
109 int32_t destroyDescriptor(gralloc1_buffer_descriptor_t descriptor);
110 static int32_t destroyDescriptorHook(gralloc1_device_t *device,
111 gralloc1_buffer_descriptor_t descriptor)
113 return getAdapter(device)->destroyDescriptor(descriptor);
116 static int32_t setConsumerUsageHook(gralloc1_device_t *device,
117 gralloc1_buffer_descriptor_t descriptorId,
120 return getAdapter(device)->setConsumerUsage(descriptorId, intUsage);
123 static int32_t setDimensionsHook(gralloc1_device_t *device,
124 gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
127 return getAdapter(device)->setDimensions(descriptorId, width, height);
130 static int32_t setFormatHook(gralloc1_device_t *device,
131 gralloc1_buffer_descriptor_t descriptorId, int32_t format)
133 return getAdapter(device)->setFormat(descriptorId, format);
136 static int32_t setProducerUsageHook(gralloc1_device_t *device,
137 gralloc1_buffer_descriptor_t descriptorId,
140 return getAdapter(device)->setProducerUsage(descriptorId, intUsage);
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)
147 return getAdapter(device)->getNumFlexPlanes(buffer, outNumPlanes);
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)
154 return getAdapter(device)->getBackingStore(buffer, outStore);
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)
162 return getAdapter(device)->getConsumerUsage(buffer, outUsage);
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)
169 return getAdapter(device)->getDimensions(buffer, outWidth, outHeight);
172 int32_t getFormat(buffer_handle_t buffer, int32_t *outFormat);
173 static int32_t getFormatHook(gralloc1_device_t *device, buffer_handle_t buffer,
176 return getAdapter(device)->getFormat(buffer, outFormat);
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)
184 return getAdapter(device)->getProducerUsage(buffer, outUsage);
187 int32_t getStride(buffer_handle_t buffer, uint32_t *outStride);
188 static int32_t getStrideHook(gralloc1_device_t *device, buffer_handle_t buffer,
191 return getAdapter(device)->getStride(buffer, outStride);
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);
201 int32_t release(buffer_handle_t bufferHandle);
202 int32_t retain(buffer_handle_t bufferHandle);
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)
208 auto adapter = getAdapter(device);
209 return ((*adapter).*member)(bufferHandle);
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);
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)
235 auto adapter = getAdapter(device);
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
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);
251 auto producerUsage = static_cast<gralloc1_producer_usage_t>(uintProducerUsage);
252 auto consumerUsage = static_cast<gralloc1_consumer_usage_t>(uintConsumerUsage);
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;
260 if (consumerUsage & (GRALLOC1_CONSUMER_USAGE_CPU_READ != 0)) {
261 return CROS_GRALLOC_ERROR_BAD_VALUE;
266 ALOGE("accessRegion is null");
267 return CROS_GRALLOC_ERROR_BAD_VALUE;
270 return ((*adapter).*member)(bufferHandle, producerUsage, consumerUsage,
271 *accessRegion, outData, acquireFenceFd);
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)
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);
289 std::unique_ptr<cros_gralloc_driver> driver;
292 } // namespace android