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>
27 struct cros_gralloc_module;
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;
43 class CrosGralloc1 : public gralloc1_device_t
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);
56 static inline CrosGralloc1 *getAdapter(gralloc1_device_t *device)
58 return static_cast<CrosGralloc1 *>(device);
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)
68 getAdapter(device)->doGetCapabilities(outCount, outCapabilities);
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)
79 return getAdapter(device)->doGetFunction(descriptor);
84 void dump(uint32_t *outSize, char *outBuffer);
85 static void dumpHook(gralloc1_device_t *device, uint32_t *outSize, char *outBuffer)
87 return getAdapter(device)->dump(outSize, outBuffer);
90 // Buffer descriptor functions
92 int32_t setConsumerUsage(gralloc1_buffer_descriptor_t descriptorId, uint64_t intUsage);
94 int32_t setProducerUsage(gralloc1_buffer_descriptor_t descriptorId, uint64_t intUsage);
96 int32_t setDimensions(gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
99 int32_t setFormat(gralloc1_buffer_descriptor_t descriptorId, int32_t format);
101 int32_t createDescriptor(gralloc1_buffer_descriptor_t *outDescriptor);
102 static int32_t createDescriptorHook(gralloc1_device_t *device,
103 gralloc1_buffer_descriptor_t *outDescriptor)
105 return getAdapter(device)->createDescriptor(outDescriptor);
108 int32_t destroyDescriptor(gralloc1_buffer_descriptor_t descriptor);
109 static int32_t destroyDescriptorHook(gralloc1_device_t *device,
110 gralloc1_buffer_descriptor_t descriptor)
112 return getAdapter(device)->destroyDescriptor(descriptor);
115 static int32_t setConsumerUsageHook(gralloc1_device_t *device,
116 gralloc1_buffer_descriptor_t descriptorId,
119 return getAdapter(device)->setConsumerUsage(descriptorId, intUsage);
122 static int32_t setDimensionsHook(gralloc1_device_t *device,
123 gralloc1_buffer_descriptor_t descriptorId, uint32_t width,
126 return getAdapter(device)->setDimensions(descriptorId, width, height);
129 static int32_t setFormatHook(gralloc1_device_t *device,
130 gralloc1_buffer_descriptor_t descriptorId, int32_t format)
132 return getAdapter(device)->setFormat(descriptorId, format);
135 static int32_t setProducerUsageHook(gralloc1_device_t *device,
136 gralloc1_buffer_descriptor_t descriptorId,
139 return getAdapter(device)->setProducerUsage(descriptorId, intUsage);
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)
146 return getAdapter(device)->getNumFlexPlanes(buffer, outNumPlanes);
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)
153 return getAdapter(device)->getBackingStore(buffer, outStore);
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)
161 return getAdapter(device)->getConsumerUsage(buffer, outUsage);
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)
168 return getAdapter(device)->getDimensions(buffer, outWidth, outHeight);
171 int32_t getFormat(buffer_handle_t buffer, int32_t *outFormat);
172 static int32_t getFormatHook(gralloc1_device_t *device, buffer_handle_t buffer,
175 return getAdapter(device)->getFormat(buffer, outFormat);
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)
183 return getAdapter(device)->getProducerUsage(buffer, outUsage);
186 int32_t getStride(buffer_handle_t buffer, uint32_t *outStride);
187 static int32_t getStrideHook(gralloc1_device_t *device, buffer_handle_t buffer,
190 return getAdapter(device)->getStride(buffer, outStride);
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);
200 int32_t release(buffer_handle_t bufferHandle);
201 int32_t retain(buffer_handle_t bufferHandle);
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)
207 auto adapter = getAdapter(device);
208 return ((*adapter).*member)(bufferHandle);
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);
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)
234 auto adapter = getAdapter(device);
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
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);
250 auto producerUsage = static_cast<gralloc1_producer_usage_t>(uintProducerUsage);
251 auto consumerUsage = static_cast<gralloc1_consumer_usage_t>(uintConsumerUsage);
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;
259 if (consumerUsage & (GRALLOC1_CONSUMER_USAGE_CPU_READ != 0)) {
260 return CROS_GRALLOC_ERROR_BAD_VALUE;
265 ALOGE("accessRegion is null");
266 return CROS_GRALLOC_ERROR_BAD_VALUE;
269 return ((*adapter).*member)(bufferHandle, producerUsage, consumerUsage,
270 *accessRegion, outData, acquireFenceFd);
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)
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);
288 std::unique_ptr<cros_gralloc_driver> driver;
291 } // namespace android