1 /*
2  * Copyright (C) 2016 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * You may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #include <hardware/hardware.h>
19 #include <hardware/gralloc1.h>
20 
21 #include "mali_gralloc_module.h"
22 
23 #include "mali_gralloc_private_interface.h"
24 #include "mali_gralloc_buffer.h"
25 #include "mali_gralloc_ion.h"
26 #include "mali_gralloc_bufferdescriptor.h"
27 #include "mali_gralloc_bufferallocation.h"
28 #include "mali_gralloc_reference.h"
29 #include "mali_gralloc_bufferaccess.h"
30 #include "framebuffer_device.h"
31 #include "gralloc_buffer_priv.h"
32 #include "mali_gralloc_debug.h"
33 
34 typedef struct mali_gralloc_func
35 {
36 	gralloc1_function_descriptor_t desc;
37 	gralloc1_function_pointer_t func;
38 } mali_gralloc_func;
39 
mali_gralloc_dump(gralloc1_device_t * device,uint32_t * outSize,char * outBuffer)40 static void mali_gralloc_dump(gralloc1_device_t *device, uint32_t *outSize, char *outBuffer)
41 {
42 	if (NULL == outSize)
43 	{
44 		ALOGE("Invalid pointer to outSize and return");
45 		return;
46 	}
47 
48 	mali_gralloc_dump_internal(outSize, outBuffer);
49 	GRALLOC_UNUSED(device);
50 }
51 
mali_gralloc_create_descriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t * outDescriptor)52 static int32_t mali_gralloc_create_descriptor(gralloc1_device_t *device, gralloc1_buffer_descriptor_t *outDescriptor)
53 {
54 	int ret = 0;
55 	ret = mali_gralloc_create_descriptor_internal(outDescriptor);
56 	GRALLOC_UNUSED(device);
57 	return ret;
58 }
59 
mali_gralloc_destroy_descriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor)60 static int32_t mali_gralloc_destroy_descriptor(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor)
61 {
62 	int ret = 0;
63 	ret = mali_gralloc_destroy_descriptor_internal(descriptor);
64 	GRALLOC_UNUSED(device);
65 	return ret;
66 }
67 
mali_gralloc_set_consumer_usage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_consumer_usage_t usage)68 static int32_t mali_gralloc_set_consumer_usage(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
69                                                /*uint64_t */ gralloc1_consumer_usage_t usage)
70 {
71 	int ret = 0;
72 	ret = mali_gralloc_set_consumerusage_internal(descriptor, usage);
73 	GRALLOC_UNUSED(device);
74 	return ret;
75 }
76 
mali_gralloc_set_dimensions(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)77 static int32_t mali_gralloc_set_dimensions(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
78                                            uint32_t width, uint32_t height)
79 {
80 	int ret = 0;
81 	ret = mali_gralloc_set_dimensions_internal(descriptor, width, height);
82 	GRALLOC_UNUSED(device);
83 	return ret;
84 }
85 
mali_gralloc_set_format(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,android_pixel_format_t format)86 static int32_t mali_gralloc_set_format(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
87                                        /*int32_t*/ android_pixel_format_t format)
88 {
89 	int ret = 0;
90 	ret = mali_gralloc_set_format_internal(descriptor, format);
91 	GRALLOC_UNUSED(device);
92 	return ret;
93 }
94 
mali_gralloc_set_producer_usage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_producer_usage_t usage)95 static int32_t mali_gralloc_set_producer_usage(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
96                                                /*uint64_t */ gralloc1_producer_usage_t usage)
97 {
98 	int ret = 0;
99 	ret = mali_gralloc_set_producerusage_internal(descriptor, usage);
100 	GRALLOC_UNUSED(device);
101 	return ret;
102 }
103 
mali_gralloc_get_backing_store(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_backing_store_t * outStore)104 static int32_t mali_gralloc_get_backing_store(gralloc1_device_t *device, buffer_handle_t buffer,
105                                               gralloc1_backing_store_t *outStore)
106 {
107 	int ret = 0;
108 	ret = mali_gralloc_get_backing_store_internal(buffer, outStore);
109 	GRALLOC_UNUSED(device);
110 	return ret;
111 }
112 
mali_gralloc_get_consumer_usage(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t * outUsage)113 static int32_t mali_gralloc_get_consumer_usage(gralloc1_device_t *device, buffer_handle_t buffer,
114                                                uint64_t * /*gralloc1_consumer_usage_t*/ outUsage)
115 {
116 	int ret = 0;
117 	ret = mali_gralloc_get_consumer_usage_internal(buffer, outUsage);
118 	GRALLOC_UNUSED(device);
119 	return ret;
120 }
121 
mali_gralloc_get_dimensions(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)122 static int32_t mali_gralloc_get_dimensions(gralloc1_device_t *device, buffer_handle_t buffer, uint32_t *outWidth,
123                                            uint32_t *outHeight)
124 {
125 	int ret = 0;
126 	ret = mali_gralloc_get_dimensions_internal(buffer, outWidth, outHeight);
127 	GRALLOC_UNUSED(device);
128 	return ret;
129 }
130 
mali_gralloc_get_format(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outFormat)131 static int32_t mali_gralloc_get_format(gralloc1_device_t *device, buffer_handle_t buffer, int32_t *outFormat)
132 {
133 	int ret = 0;
134 	ret = mali_gralloc_get_format_internal(buffer, outFormat);
135 	GRALLOC_UNUSED(device);
136 	return ret;
137 }
138 
mali_gralloc_get_producer_usage(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t * outUsage)139 static int32_t mali_gralloc_get_producer_usage(gralloc1_device_t *device, buffer_handle_t buffer,
140                                                uint64_t * /*gralloc1_producer_usage_t*/ outUsage)
141 {
142 	int ret = 0;
143 	ret = mali_gralloc_get_producer_usage_internal(buffer, outUsage);
144 	GRALLOC_UNUSED(device);
145 	return ret;
146 }
147 
mali_gralloc_get_stride(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outStride)148 static int32_t mali_gralloc_get_stride(gralloc1_device_t *device, buffer_handle_t buffer, uint32_t *outStride)
149 {
150 	GRALLOC_UNUSED(device);
151 
152 	int stride;
153 
154 	if (mali_gralloc_query_getstride(buffer, &stride) < 0)
155 	{
156 		return GRALLOC1_ERROR_UNSUPPORTED;
157 	}
158 
159 	*outStride = (uint32_t)stride;
160 
161 	return GRALLOC1_ERROR_NONE;
162 }
163 
mali_gralloc_allocate(gralloc1_device_t * device,uint32_t numDescriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * outBuffers)164 static int32_t mali_gralloc_allocate(gralloc1_device_t *device, uint32_t numDescriptors,
165                                      const gralloc1_buffer_descriptor_t *descriptors, buffer_handle_t *outBuffers)
166 {
167 	mali_gralloc_module *m;
168 	m = reinterpret_cast<private_module_t *>(device->common.module);
169 	buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(*descriptors);
170 	uint64_t usage;
171 	bool shared = false;
172 
173 	usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage;
174 
175 #if DISABLE_FRAMEBUFFER_HAL != 1
176 
177 	if (usage & GRALLOC_USAGE_HW_FB)
178 	{
179 		int byte_stride;
180 		int pixel_stride;
181 		int width, height;
182 		uint64_t format;
183 
184 		format = bufDescriptor->hal_format;
185 		width = bufDescriptor->width;
186 		height = bufDescriptor->height;
187 
188 #if GRALLOC_FB_SWAP_RED_BLUE == 1
189 #ifdef GRALLOC_16_BITS
190 		format = HAL_PIXEL_FORMAT_RGB_565;
191 #else
192 		format = HAL_PIXEL_FORMAT_BGRA_8888;
193 #endif
194 #endif
195 
196 		if (fb_alloc_framebuffer(m, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, outBuffers,
197 		                         &pixel_stride, &byte_stride) < 0)
198 		{
199 			return GRALLOC1_ERROR_NO_RESOURCES;
200 		}
201 		else
202 		{
203 			private_handle_t *hnd = (private_handle_t *)*outBuffers;
204 
205 			/* Allocate a meta-data buffer for framebuffer too. fbhal
206 			 * ones wont need it but for hwc they will.
207 			 *
208 			 * Explicitly ignore allocation errors since it is not critical to have
209 			 */
210 			(void)gralloc_buffer_attr_allocate(hnd);
211 
212 			hnd->req_format = format;
213 			hnd->format = format;
214 			hnd->yuv_info = MALI_YUV_BT601_NARROW;
215 			hnd->internal_format = format;
216 			hnd->byte_stride = byte_stride;
217 			hnd->width = width;
218 			hnd->height = height;
219 			hnd->stride = pixel_stride;
220 			hnd->internalWidth = width;
221 			hnd->internalHeight = height;
222 		}
223 	}
224 	else
225 #endif
226 	{
227 		if (mali_gralloc_buffer_allocate(m, (gralloc_buffer_descriptor_t *)descriptors, numDescriptors, outBuffers,
228 		                                 &shared) < 0)
229 		{
230 			ALOGE("Failed to allocate buffer.");
231 			return GRALLOC1_ERROR_NO_RESOURCES;
232 		}
233 
234 		if (!shared && 1 != numDescriptors)
235 		{
236 			return GRALLOC1_ERROR_NOT_SHARED;
237 		}
238 	}
239 
240 	return GRALLOC1_ERROR_NONE;
241 }
242 
mali_gralloc_retain(gralloc1_device_t * device,buffer_handle_t buffer)243 static int32_t mali_gralloc_retain(gralloc1_device_t *device, buffer_handle_t buffer)
244 {
245 	mali_gralloc_module *m;
246 	m = reinterpret_cast<private_module_t *>(device->common.module);
247 
248 	if (private_handle_t::validate(buffer) < 0)
249 	{
250 		return GRALLOC1_ERROR_BAD_HANDLE;
251 	}
252 
253 	if (mali_gralloc_reference_retain(m, buffer) < 0)
254 	{
255 		return GRALLOC1_ERROR_NO_RESOURCES;
256 	}
257 
258 	return GRALLOC1_ERROR_NONE;
259 }
260 
mali_gralloc_release(gralloc1_device_t * device,buffer_handle_t buffer)261 static int32_t mali_gralloc_release(gralloc1_device_t *device, buffer_handle_t buffer)
262 {
263 	mali_gralloc_module *m;
264 	m = reinterpret_cast<private_module_t *>(device->common.module);
265 
266 	if (mali_gralloc_reference_release(m, buffer, true) < 0)
267 	{
268 		return GRALLOC1_ERROR_BAD_HANDLE;
269 	}
270 
271 	return GRALLOC1_ERROR_NONE;
272 }
273 
mali_gralloc1_get_num_flex_planes(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outNumPlanes)274 static int32_t mali_gralloc1_get_num_flex_planes(gralloc1_device_t *device, buffer_handle_t buffer,
275                                                  uint32_t *outNumPlanes)
276 {
277 	mali_gralloc_module *m;
278 	m = reinterpret_cast<private_module_t *>(device->common.module);
279 
280 	if (private_handle_t::validate(buffer) < 0)
281 	{
282 		return GRALLOC1_ERROR_BAD_HANDLE;
283 	}
284 
285 	if (mali_gralloc_get_num_flex_planes(m, buffer, outNumPlanes) < 0)
286 	{
287 		return GRALLOC1_ERROR_UNSUPPORTED;
288 	}
289 
290 	return GRALLOC1_ERROR_NONE;
291 }
292 
mali_gralloc1_lock_async(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t producerUsage,uint64_t consumerUsage,const gralloc1_rect_t * accessRegion,void ** outData,int32_t acquireFence)293 static int32_t mali_gralloc1_lock_async(gralloc1_device_t *device, buffer_handle_t buffer,
294                                         uint64_t /*gralloc1_producer_usage_t*/ producerUsage,
295                                         uint64_t /*gralloc1_consumer_usage_t*/ consumerUsage,
296                                         const gralloc1_rect_t *accessRegion, void **outData, int32_t acquireFence)
297 {
298 	mali_gralloc_module *m;
299 	m = reinterpret_cast<private_module_t *>(device->common.module);
300 
301 	if (private_handle_t::validate(buffer) < 0)
302 	{
303 		return GRALLOC1_ERROR_BAD_HANDLE;
304 	}
305 
306 	if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
307 	{
308 		return GRALLOC1_ERROR_BAD_VALUE;
309 	}
310 
311 	if (mali_gralloc_lock_async(m, buffer, producerUsage | consumerUsage, accessRegion->left, accessRegion->top,
312 	                            accessRegion->width, accessRegion->height, outData, acquireFence) < 0)
313 	{
314 		return GRALLOC1_ERROR_UNSUPPORTED;
315 	}
316 
317 	return GRALLOC1_ERROR_NONE;
318 }
319 
mali_gralloc1_lock_flex_async(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t producerUsage,uint64_t consumerUsage,const gralloc1_rect_t * accessRegion,struct android_flex_layout * outFlexLayout,int32_t acquireFence)320 static int32_t mali_gralloc1_lock_flex_async(gralloc1_device_t *device, buffer_handle_t buffer,
321                                              uint64_t /*gralloc1_producer_usage_t*/ producerUsage,
322                                              uint64_t /*gralloc1_consumer_usage_t*/ consumerUsage,
323                                              const gralloc1_rect_t *accessRegion,
324                                              struct android_flex_layout *outFlexLayout, int32_t acquireFence)
325 {
326 	mali_gralloc_module *m;
327 	m = reinterpret_cast<private_module_t *>(device->common.module);
328 
329 	if (private_handle_t::validate(buffer) < 0)
330 	{
331 		return GRALLOC1_ERROR_BAD_HANDLE;
332 	}
333 
334 	if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
335 	{
336 		return GRALLOC1_ERROR_BAD_VALUE;
337 	}
338 
339 	if (mali_gralloc_lock_flex_async(m, buffer, producerUsage | consumerUsage, accessRegion->left, accessRegion->top,
340 	                                 accessRegion->width, accessRegion->height, outFlexLayout, acquireFence) < 0)
341 	{
342 		return GRALLOC1_ERROR_UNSUPPORTED;
343 	}
344 
345 	return GRALLOC1_ERROR_NONE;
346 }
347 
mali_gralloc1_unlock_async(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outReleaseFence)348 static int32_t mali_gralloc1_unlock_async(gralloc1_device_t *device, buffer_handle_t buffer, int32_t *outReleaseFence)
349 {
350 	mali_gralloc_module *m;
351 	m = reinterpret_cast<private_module_t *>(device->common.module);
352 
353 	if (private_handle_t::validate(buffer) < 0)
354 	{
355 		return GRALLOC1_ERROR_BAD_HANDLE;
356 	}
357 
358 	mali_gralloc_unlock_async(m, buffer, outReleaseFence);
359 	return GRALLOC1_ERROR_NONE;
360 }
361 
362 static const mali_gralloc_func mali_gralloc_func_list[] = {
363 	{ GRALLOC1_FUNCTION_DUMP, (gralloc1_function_pointer_t)mali_gralloc_dump },
364 	{ GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_create_descriptor },
365 	{ GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_destroy_descriptor },
366 	{ GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_consumer_usage },
367 	{ GRALLOC1_FUNCTION_SET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_set_dimensions },
368 	{ GRALLOC1_FUNCTION_SET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_set_format },
369 	{ GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_producer_usage },
370 	{ GRALLOC1_FUNCTION_GET_BACKING_STORE, (gralloc1_function_pointer_t)mali_gralloc_get_backing_store },
371 	{ GRALLOC1_FUNCTION_GET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_consumer_usage },
372 	{ GRALLOC1_FUNCTION_GET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_get_dimensions },
373 	{ GRALLOC1_FUNCTION_GET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_get_format },
374 	{ GRALLOC1_FUNCTION_GET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_producer_usage },
375 	{ GRALLOC1_FUNCTION_GET_STRIDE, (gralloc1_function_pointer_t)mali_gralloc_get_stride },
376 	{ GRALLOC1_FUNCTION_ALLOCATE, (gralloc1_function_pointer_t)mali_gralloc_allocate },
377 	{ GRALLOC1_FUNCTION_RETAIN, (gralloc1_function_pointer_t)mali_gralloc_retain },
378 	{ GRALLOC1_FUNCTION_RELEASE, (gralloc1_function_pointer_t)mali_gralloc_release },
379 	{ GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES, (gralloc1_function_pointer_t)mali_gralloc1_get_num_flex_planes },
380 	{ GRALLOC1_FUNCTION_LOCK, (gralloc1_function_pointer_t)mali_gralloc1_lock_async },
381 	{ GRALLOC1_FUNCTION_LOCK_FLEX, (gralloc1_function_pointer_t)mali_gralloc1_lock_flex_async },
382 	{ GRALLOC1_FUNCTION_UNLOCK, (gralloc1_function_pointer_t)mali_gralloc1_unlock_async },
383 
384 	/* GRALLOC1_FUNCTION_INVALID has to be the last descriptor on the list. */
385 	{ GRALLOC1_FUNCTION_INVALID, NULL }
386 };
387 
mali_gralloc_getCapabilities(gralloc1_device_t * dev,uint32_t * outCount,int32_t * outCapabilities)388 static void mali_gralloc_getCapabilities(gralloc1_device_t *dev, uint32_t *outCount, int32_t *outCapabilities)
389 {
390 	GRALLOC_UNUSED(dev);
391 	GRALLOC_UNUSED(outCapabilities);
392 
393 	if (outCount != NULL)
394 	{
395 		*outCount = 0;
396 	}
397 }
398 
mali_gralloc_getFunction(gralloc1_device_t * dev,int32_t descriptor)399 static gralloc1_function_pointer_t mali_gralloc_getFunction(gralloc1_device_t *dev, int32_t descriptor)
400 {
401 	GRALLOC_UNUSED(dev);
402 	gralloc1_function_pointer_t rval = NULL;
403 	uint32_t pos = 0;
404 
405 	while (mali_gralloc_func_list[pos].desc != GRALLOC1_FUNCTION_INVALID)
406 	{
407 		if (mali_gralloc_func_list[pos].desc == descriptor)
408 		{
409 			rval = mali_gralloc_func_list[pos].func;
410 			break;
411 		}
412 
413 		pos++;
414 	}
415 
416 	if (rval == NULL)
417 	{
418 		rval = mali_gralloc_private_interface_getFunction(descriptor);
419 	}
420 
421 	return rval;
422 }
423 
mali_gralloc_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)424 int mali_gralloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
425 {
426 	gralloc1_device_t *dev;
427 
428 	GRALLOC_UNUSED(name);
429 
430 	dev = new gralloc1_device_t;
431 
432 	if (NULL == dev)
433 	{
434 		return -1;
435 	}
436 
437 	/* initialize our state here */
438 	memset(dev, 0, sizeof(*dev));
439 
440 	/* initialize the procs */
441 	dev->common.tag = HARDWARE_DEVICE_TAG;
442 	dev->common.version = 0;
443 	dev->common.module = const_cast<hw_module_t *>(module);
444 	dev->common.close = mali_gralloc_ion_device_close;
445 
446 	dev->getCapabilities = mali_gralloc_getCapabilities;
447 	dev->getFunction = mali_gralloc_getFunction;
448 
449 	*device = &dev->common;
450 
451 	return 0;
452 }
453