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);
101 int32_t setInterlace(buffer_handle_t buffer, uint32_t interlace);
102 int32_t setProtectionInfo(buffer_handle_t buffer, uint32_t protection_info);
104 int32_t createDescriptor(gralloc1_buffer_descriptor_t *outDescriptor);
105 static int32_t createDescriptorHook(gralloc1_device_t *device,
106 gralloc1_buffer_descriptor_t *outDescriptor)
108 return getAdapter(device)->createDescriptor(outDescriptor);
111 int32_t destroyDescriptor(gralloc1_buffer_descriptor_t descriptor);
112 static int32_t destroyDescriptorHook(gralloc1_device_t *device,
113 gralloc1_buffer_descriptor_t descriptor)
115 return getAdapter(device)->destroyDescriptor(descriptor);
118 static int32_t setConsumerUsageHook(gralloc1_device_t *device,
119 gralloc1_buffer_descriptor_t descriptorId,
122 return getAdapter(device)->setConsumerUsage(descriptorId, intUsage);
125 static int32_t setDimensionsHook(gralloc1_device_t *device,
126 gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
129 return getAdapter(device)->setDimensions(descriptorId, width, height);
132 static int32_t setFormatHook(gralloc1_device_t *device,
133 gralloc1_buffer_descriptor_t descriptorId, int32_t format)
135 return getAdapter(device)->setFormat(descriptorId, format);
138 static int32_t setInterlaceHook(gralloc1_device_t *device,
139 buffer_handle_t buffer, uint32_t interlace)
141 return getAdapter(device)->setInterlace(buffer, interlace);
144 static int32_t setProtectionInfoHook(gralloc1_device_t *device,
145 buffer_handle_t buffer, uint32_t protection_info)
147 return getAdapter(device)->setProtectionInfo(buffer, protection_info);
150 static int32_t setProducerUsageHook(gralloc1_device_t *device,
151 gralloc1_buffer_descriptor_t descriptorId,
154 return getAdapter(device)->setProducerUsage(descriptorId, intUsage);
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)
161 return getAdapter(device)->getNumFlexPlanes(buffer, outNumPlanes);
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)
168 return getAdapter(device)->getBackingStore(buffer, outStore);
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)
176 return getAdapter(device)->getConsumerUsage(buffer, outUsage);
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)
183 return getAdapter(device)->getDimensions(buffer, outWidth, outHeight);
186 int32_t getFormat(buffer_handle_t buffer, int32_t *outFormat);
187 static int32_t getFormatHook(gralloc1_device_t *device, buffer_handle_t buffer,
190 return getAdapter(device)->getFormat(buffer, outFormat);
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)
198 return getAdapter(device)->getProducerUsage(buffer, outUsage);
201 int32_t getStride(buffer_handle_t buffer, uint32_t *outStride);
202 static int32_t getStrideHook(gralloc1_device_t *device, buffer_handle_t buffer,
205 return getAdapter(device)->getStride(buffer, outStride);
208 int32_t getPrime(buffer_handle_t buffer, uint32_t *prime);
209 static int32_t getPrimeHook(gralloc1_device_t *device, buffer_handle_t buffer,
212 return getAdapter(device)->getPrime(buffer, prime);
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)
219 return getAdapter(device)->getByteStride(buffer, outStride, size);
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);
229 int32_t release(buffer_handle_t bufferHandle);
230 int32_t retain(buffer_handle_t bufferHandle);
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)
236 auto adapter = getAdapter(device);
237 return ((*adapter).*member)(bufferHandle);
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);
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)
263 auto adapter = getAdapter(device);
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
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);
279 auto producerUsage = static_cast<gralloc1_producer_usage_t>(uintProducerUsage);
280 auto consumerUsage = static_cast<gralloc1_consumer_usage_t>(uintConsumerUsage);
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;
288 if (consumerUsage & (GRALLOC1_CONSUMER_USAGE_CPU_READ != 0)) {
289 return CROS_GRALLOC_ERROR_BAD_VALUE;
294 ALOGE("accessRegion is null");
295 return CROS_GRALLOC_ERROR_BAD_VALUE;
298 return ((*adapter).*member)(bufferHandle, producerUsage, consumerUsage,
299 *accessRegion, outData, acquireFenceFd);
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)
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);
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)
320 return getAdapter(device)->setModifier(descriptor, modifier);
324 std::unique_ptr<cros_gralloc_driver> driver;
327 } // namespace android