1 /*
2  * Copyright (C) 2016, 2018-2019 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 <inttypes.h>
19 #include <hardware/hardware.h>
20 #include <hardware/gralloc1.h>
21 
22 #include "mali_gralloc_module.h"
23 
24 #include "mali_gralloc_private_interface.h"
25 #include "mali_gralloc_buffer.h"
26 #include "mali_gralloc_ion.h"
27 #include "mali_gralloc_bufferdescriptor.h"
28 #include "mali_gralloc_bufferallocation.h"
29 #include "mali_gralloc_reference.h"
30 #include "mali_gralloc_bufferaccess.h"
31 #include "framebuffer_device.h"
32 #include "gralloc_buffer_priv.h"
33 #include "mali_gralloc_debug.h"
34 
35 typedef struct mali_gralloc_func
36 {
37 	gralloc1_function_descriptor_t desc;
38 	gralloc1_function_pointer_t func;
39 } mali_gralloc_func;
40 
mali_gralloc_dump(gralloc1_device_t * device,uint32_t * outSize,char * outBuffer)41 static void mali_gralloc_dump(gralloc1_device_t *device, uint32_t *outSize, char *outBuffer)
42 {
43 	if (NULL == outSize)
44 	{
45 		ALOGE("Invalid pointer to outSize and return");
46 		return;
47 	}
48 
49 	mali_gralloc_dump_internal(outSize, outBuffer);
50 	GRALLOC_UNUSED(device);
51 }
52 
mali_gralloc_create_descriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t * outDescriptor)53 static int32_t mali_gralloc_create_descriptor(gralloc1_device_t *device, gralloc1_buffer_descriptor_t *outDescriptor)
54 {
55 	int ret = 0;
56 	ret = mali_gralloc_create_descriptor_internal(outDescriptor);
57 	GRALLOC_UNUSED(device);
58 	return ret;
59 }
60 
mali_gralloc_destroy_descriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor)61 static int32_t mali_gralloc_destroy_descriptor(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor)
62 {
63 	int ret = 0;
64 	ret = mali_gralloc_destroy_descriptor_internal(descriptor);
65 	GRALLOC_UNUSED(device);
66 	return ret;
67 }
68 
mali_gralloc_set_consumer_usage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_consumer_usage_t usage)69 static int32_t mali_gralloc_set_consumer_usage(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
70                                                /*uint64_t */ gralloc1_consumer_usage_t usage)
71 {
72 	int ret = 0;
73 	ret = mali_gralloc_set_consumerusage_internal(descriptor, usage);
74 	GRALLOC_UNUSED(device);
75 	return ret;
76 }
77 
mali_gralloc_set_dimensions(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)78 static int32_t mali_gralloc_set_dimensions(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
79                                            uint32_t width, uint32_t height)
80 {
81 	int ret = 0;
82 	ret = mali_gralloc_set_dimensions_internal(descriptor, width, height);
83 	GRALLOC_UNUSED(device);
84 	return ret;
85 }
86 
mali_gralloc_set_format(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,android_pixel_format_t format)87 static int32_t mali_gralloc_set_format(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
88                                        /*int32_t*/ android_pixel_format_t format)
89 {
90 	int ret = 0;
91 	ret = mali_gralloc_set_format_internal(descriptor, format);
92 	GRALLOC_UNUSED(device);
93 	return ret;
94 }
95 
mali_gralloc_set_producer_usage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_producer_usage_t usage)96 static int32_t mali_gralloc_set_producer_usage(gralloc1_device_t *device, gralloc1_buffer_descriptor_t descriptor,
97                                                /*uint64_t */ gralloc1_producer_usage_t usage)
98 {
99 	int ret = 0;
100 	ret = mali_gralloc_set_producerusage_internal(descriptor, usage);
101 	GRALLOC_UNUSED(device);
102 	return ret;
103 }
104 
mali_gralloc_get_backing_store(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_backing_store_t * outStore)105 static int32_t mali_gralloc_get_backing_store(gralloc1_device_t *device, buffer_handle_t buffer,
106                                               gralloc1_backing_store_t *outStore)
107 {
108 	int ret = 0;
109 	ret = mali_gralloc_get_backing_store_internal(buffer, outStore);
110 	GRALLOC_UNUSED(device);
111 	return ret;
112 }
113 
mali_gralloc_get_consumer_usage(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t * outUsage)114 static int32_t mali_gralloc_get_consumer_usage(gralloc1_device_t *device, buffer_handle_t buffer,
115                                                uint64_t * /*gralloc1_consumer_usage_t*/ outUsage)
116 {
117 	int ret = 0;
118 	ret = mali_gralloc_get_consumer_usage_internal(buffer, outUsage);
119 	GRALLOC_UNUSED(device);
120 	return ret;
121 }
122 
mali_gralloc_get_dimensions(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)123 static int32_t mali_gralloc_get_dimensions(gralloc1_device_t *device, buffer_handle_t buffer, uint32_t *outWidth,
124                                            uint32_t *outHeight)
125 {
126 	int ret = 0;
127 	ret = mali_gralloc_get_dimensions_internal(buffer, outWidth, outHeight);
128 	GRALLOC_UNUSED(device);
129 	return ret;
130 }
131 
mali_gralloc_get_format(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outFormat)132 static int32_t mali_gralloc_get_format(gralloc1_device_t *device, buffer_handle_t buffer, int32_t *outFormat)
133 {
134 	int ret = 0;
135 	ret = mali_gralloc_get_format_internal(buffer, outFormat);
136 	GRALLOC_UNUSED(device);
137 	return ret;
138 }
139 
mali_gralloc_get_producer_usage(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t * outUsage)140 static int32_t mali_gralloc_get_producer_usage(gralloc1_device_t *device, buffer_handle_t buffer,
141                                                uint64_t * /*gralloc1_producer_usage_t*/ outUsage)
142 {
143 	int ret = 0;
144 	ret = mali_gralloc_get_producer_usage_internal(buffer, outUsage);
145 	GRALLOC_UNUSED(device);
146 	return ret;
147 }
148 
mali_gralloc_get_stride(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outStride)149 static int32_t mali_gralloc_get_stride(gralloc1_device_t *device, buffer_handle_t buffer, uint32_t *outStride)
150 {
151 	GRALLOC_UNUSED(device);
152 	int stride;
153 
154 	const int ret = mali_gralloc_query_getstride(buffer, &stride);
155 	if (ret == GRALLOC1_ERROR_NONE)
156 	{
157 		*outStride = (uint32_t)stride;
158 	}
159 
160 	return ret;
161 }
162 
mali_gralloc_allocate(gralloc1_device_t * device,uint32_t numDescriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * outBuffers)163 int32_t mali_gralloc_allocate(gralloc1_device_t *device, uint32_t numDescriptors,
164                               const gralloc1_buffer_descriptor_t *descriptors, buffer_handle_t *outBuffers)
165 {
166 	mali_gralloc_module *m;
167 	m = reinterpret_cast<private_module_t *>(device->common.module);
168 	buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(*descriptors);
169 	uint64_t usage;
170 	bool shared = false;
171 
172 	/* Initialise output parameters. */
173 	for (uint32_t i = 0; i < numDescriptors; i++)
174 	{
175 		outBuffers[i] = NULL;
176 	}
177 
178 	usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage;
179 
180 #if DISABLE_FRAMEBUFFER_HAL != 1
181 
182 	if (usage & GRALLOC_USAGE_HW_FB)
183 	{
184 		if (mali_gralloc_fb_allocate(m, bufDescriptor, outBuffers) < 0)
185 		{
186 			return GRALLOC1_ERROR_NO_RESOURCES;
187 		}
188 	}
189 	else
190 #endif
191 	{
192 		if (mali_gralloc_buffer_allocate(m, (gralloc_buffer_descriptor_t *)descriptors, numDescriptors, outBuffers,
193 		                                 &shared) < 0)
194 		{
195 			ALOGE("Failed to allocate buffer.");
196 			return GRALLOC1_ERROR_NO_RESOURCES;
197 		}
198 
199 		if (!shared && 1 != numDescriptors)
200 		{
201 			return GRALLOC1_ERROR_NOT_SHARED;
202 		}
203 	}
204 
205 	return GRALLOC1_ERROR_NONE;
206 }
207 
208 
mali_gralloc_retain(gralloc1_device_t * device,buffer_handle_t buffer)209 static int32_t mali_gralloc_retain(gralloc1_device_t *device, buffer_handle_t buffer)
210 {
211 	mali_gralloc_module *m;
212 	m = reinterpret_cast<private_module_t *>(device->common.module);
213 
214 	if (private_handle_t::validate(buffer) < 0)
215 	{
216 		return GRALLOC1_ERROR_BAD_HANDLE;
217 	}
218 
219 	if (mali_gralloc_reference_retain(m, buffer) < 0)
220 	{
221 		return GRALLOC1_ERROR_NO_RESOURCES;
222 	}
223 
224 	return GRALLOC1_ERROR_NONE;
225 }
226 
mali_gralloc_release(gralloc1_device_t * device,buffer_handle_t buffer)227 static int32_t mali_gralloc_release(gralloc1_device_t *device, buffer_handle_t buffer)
228 {
229 	mali_gralloc_module *m;
230 	m = reinterpret_cast<private_module_t *>(device->common.module);
231 
232 	if (mali_gralloc_reference_release(m, buffer, true) < 0)
233 	{
234 		return GRALLOC1_ERROR_BAD_HANDLE;
235 	}
236 
237 	return GRALLOC1_ERROR_NONE;
238 }
239 
240 /*
241  *  Returns the number of flex layout planes which are needed to represent the
242  *  given buffer.
243  *
244  * @param device       [in]   Gralloc device.
245  * @param buffer       [in]   The buffer handle for which the number of planes should be queried
246  * @param outNumPlanes [out]  The number of flex planes required to describe the given buffer
247  *
248  * @return GRALLOC1_ERROR_NONE - The locking is successful;
249  *         GRALLOC1_ERROR_BAD_HANDLE - The buffer handle is invalid
250  *         GRALLOC1_ERROR_BAD_VALUE - The flex plane pointer is invalid
251  *         Appropriate error, otherwise
252  */
mali_gralloc1_get_num_flex_planes(const gralloc1_device_t * const device,const buffer_handle_t buffer,uint32_t * const outNumPlanes)253 static int32_t mali_gralloc1_get_num_flex_planes(const gralloc1_device_t * const device,
254                                                  const buffer_handle_t buffer,
255                                                  uint32_t * const outNumPlanes)
256 {
257 	mali_gralloc_module *m;
258 	m = reinterpret_cast<private_module_t *>(device->common.module);
259 
260 	if (private_handle_t::validate(buffer) < 0)
261 	{
262 		AERR("Invalid buffer handle");
263 		return GRALLOC1_ERROR_BAD_HANDLE;
264 	}
265 
266 	if (outNumPlanes == NULL)
267 	{
268 		AERR("Invalid flex plane pointer");
269 		return GRALLOC1_ERROR_BAD_VALUE;
270 	}
271 
272 	int status = mali_gralloc_get_num_flex_planes(m, buffer, outNumPlanes);
273 	if (status != 0)
274 	{
275 		return status;
276 	}
277 
278 	return GRALLOC1_ERROR_NONE;
279 }
280 
281 /*
282  *  Locks the Gralloc 1.0 buffer for the specified CPU usage.
283  *
284  * @param device        [in]    Gralloc1 device.
285  * @param buffer        [in]    The buffer to lock.
286  * @param producerUsage [in]    The producer usage flags to request (gralloc1_producer_usage_t).
287  * @param consumerUsage [in]    The consumer usage flags to request (gralloc1_consumer_usage_t).
288  * @param accessRegion  [in]    The portion of the buffer that the client
289  *                              intends to access.
290  * @param outData       [out]   To be filled with a CPU-accessible pointer to
291  *                              the buffer data for CPU usage.
292  * @param acquireFence  [in]    Refers to an acquire sync fence object.
293  *
294  * @return GRALLOC1_ERROR_NONE, when locking is successful;
295  *         appropriate error, otherwise
296  *
297  * @Notes: Locking a buffer simultaneously for write or read/write leaves the
298  *         buffer's content into an indeterminate state.
299  */
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)300 static int32_t mali_gralloc1_lock_async(gralloc1_device_t *device,
301                                         buffer_handle_t buffer,
302                                         uint64_t producerUsage,
303                                         uint64_t consumerUsage,
304                                         const gralloc1_rect_t *accessRegion,
305                                         void **outData,
306                                         int32_t acquireFence)
307 {
308 	int status = 0;
309 	mali_gralloc_module *m;
310 	m = reinterpret_cast<private_module_t *>(device->common.module);
311 
312 	if (private_handle_t::validate(buffer) < 0)
313 	{
314 		return GRALLOC1_ERROR_BAD_HANDLE;
315 	}
316 
317 	if (accessRegion == NULL)
318 	{
319 		return GRALLOC1_ERROR_BAD_VALUE;
320 	}
321 
322 	/* The lock usage constraints
323 	 * 1. Exactly one of producerUsage and consumerUsage must be *_USAGE_NONE.
324 	 * 2. The usage which is not *_USAGE_NONE must be one of the *_USAGE_CPU_*
325 	 *    as applicable.
326 	 * are relaxed to accommodate an issue in shims with regard to lock usage mapping.
327 	 */
328 	//if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
329 	//{
330 	//	return GRALLOC1_ERROR_BAD_VALUE;
331 	//}
332 
333 	status = mali_gralloc_lock_async(m, buffer, producerUsage | consumerUsage,
334 	                                 accessRegion->left, accessRegion->top,
335 	                                 accessRegion->width, accessRegion->height,
336 	                                 outData, acquireFence);
337 	if (status != 0)
338 	{
339 		if (status == -EINVAL)
340 		{
341 			return GRALLOC1_ERROR_BAD_VALUE;
342 		}
343 
344 		return GRALLOC1_ERROR_UNSUPPORTED;
345 	}
346 
347 	return GRALLOC1_ERROR_NONE;
348 }
349 
350 /*
351  *  Locks the Gralloc 1.0 buffer, for the specified CPU usage, asynchronously.
352  *
353  * @param device        [in]    Gralloc1 device.
354  * @param buffer        [in]    The buffer to lock.
355  * @param producerUsage [in]    The producer usage flags requested (gralloc1_producer_usage_t).
356  * @param consumerUsage [in]    The consumer usage flags requested (gralloc1_consumer_usage_t).
357  * @param accessRegion  [in]    The portion of the buffer that the client
358  *                              intends to access.
359  * @param outFlexLayout [out]   To be filled with the description of the planes
360  *                              in the buffer
361  * @param acquireFence  [in]    Refers to an acquire sync fence object.
362  *
363  * @return GRALLOC1_ERROR_NONE - Locking is successful;
364  *         GRALLOC1_ERROR_BAD_HANDLE - Invalid buffer handle
365  *         GRALLOC1_ERROR_BAD_VALUE - Invalid usage parameters
366  *         GRALLOC1_ERROR_UNSUPPORTED - Any other error, like wrong access
367  *                                      region parameters, invalid buffer ownership, etc.
368  *
369  * @Notes: Locking a buffer simultaneously for write or read/write leaves the
370  *         buffer's content into an indeterminate state.
371  */
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)372 static int32_t mali_gralloc1_lock_flex_async(gralloc1_device_t *device, buffer_handle_t buffer,
373                                              uint64_t producerUsage,
374                                              uint64_t consumerUsage,
375                                              const gralloc1_rect_t *accessRegion,
376                                              struct android_flex_layout *outFlexLayout,
377                                              int32_t acquireFence)
378 {
379 	int status = 0;
380 	mali_gralloc_module *m;
381 	m = reinterpret_cast<private_module_t *>(device->common.module);
382 
383 	if (private_handle_t::validate(buffer) < 0)
384 	{
385 		return GRALLOC1_ERROR_BAD_HANDLE;
386 	}
387 
388 	if (outFlexLayout == NULL)
389 	{
390 		return GRALLOC1_ERROR_BAD_VALUE;
391 	}
392 
393 	/* The lock usage constraints
394 	 * 1. Exactly one of producerUsage and consumerUsage must be *_USAGE_NONE.
395 	 * 2. The usage which is not *_USAGE_NONE must be one of the *_USAGE_CPU_*
396 	 *    as applicable.
397 	 * are relaxed to accommodate an issue in shims with regard to lock usage mapping.
398 	 */
399 	//if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
400 	//{
401 	//	return GRALLOC1_ERROR_BAD_VALUE;
402 	//}
403 
404 	status = mali_gralloc_lock_flex_async(m, buffer, producerUsage | consumerUsage,
405 	                                      accessRegion->left, accessRegion->top,
406 	                                      accessRegion->width, accessRegion->height,
407 	                                      outFlexLayout, acquireFence);
408 	if (status != 0)
409 	{
410 		if (status == -EINVAL)
411 		{
412 			return GRALLOC1_ERROR_BAD_VALUE;
413 		}
414 
415 		return GRALLOC1_ERROR_UNSUPPORTED;
416 	}
417 
418 	return GRALLOC1_ERROR_NONE;
419 }
420 
421 /*
422  *  Unlocks the Gralloc 1.0 buffer asynchronously.
423  *
424  * @param device          [in]   Gralloc1 device.
425  * @param buffer          [in]   The buffer to unlock.
426  * @param outReleaseFence [out]  Refers to an acquire sync fence object.
427  *
428  * @return 0, when the locking is successful;
429  *         Appropriate error, otherwise
430  *
431  * Note: unlocking a buffer which is not locked results in an unexpected behaviour.
432  *       Though it is possible to create a state machine to track the buffer state to
433  *       recognize erroneous conditions, it is expected of client to adhere to API
434  *       call sequence
435  */
mali_gralloc1_unlock_async(const gralloc1_device_t * const device,buffer_handle_t buffer,int32_t * const outReleaseFence)436 static int32_t mali_gralloc1_unlock_async(const gralloc1_device_t * const device,
437                                           buffer_handle_t buffer,
438                                           int32_t *const outReleaseFence)
439 {
440 	mali_gralloc_module *m;
441 	m = reinterpret_cast<private_module_t *>(device->common.module);
442 
443 	if (private_handle_t::validate(buffer) < 0)
444 	{
445 		return GRALLOC1_ERROR_BAD_HANDLE;
446 	}
447 
448 	const int status = mali_gralloc_unlock_async(m, buffer, outReleaseFence);
449 	if (status != 0)
450 	{
451 		if (status == -EINVAL)
452 		{
453 			return GRALLOC1_ERROR_BAD_VALUE;
454 		}
455 
456 		return GRALLOC1_ERROR_UNSUPPORTED;
457 	}
458 
459 	return GRALLOC1_ERROR_NONE;
460 }
461 
462 #if PLATFORM_SDK_VERSION >= 26
mali_gralloc1_set_layer_count(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t layerCount)463 static int32_t mali_gralloc1_set_layer_count(gralloc1_device_t* device,
464                                              gralloc1_buffer_descriptor_t descriptor,
465                                              uint32_t layerCount)
466 {
467 	int ret = 0;
468 	ret = mali_gralloc_set_layer_count_internal(descriptor, layerCount);
469 	GRALLOC_UNUSED(device);
470 	return ret;
471 }
472 
mali_gralloc1_get_layer_count(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outLayerCount)473 static int32_t mali_gralloc1_get_layer_count(gralloc1_device_t* device, buffer_handle_t buffer, uint32_t* outLayerCount)
474 {
475 	int ret = 0;
476 	ret = mali_gralloc_get_layer_count_internal(buffer, outLayerCount);
477 	GRALLOC_UNUSED(device);
478 	return ret;
479 }
480 #endif
481 
mali_gralloc1_validate_buffer_size(gralloc1_device_t * device,buffer_handle_t buffer,const gralloc1_buffer_descriptor_info_t * descriptorInfo,uint32_t stride)482 static int32_t mali_gralloc1_validate_buffer_size(gralloc1_device_t* device, buffer_handle_t buffer,
483                                                   const gralloc1_buffer_descriptor_info_t* descriptorInfo,
484                                                   uint32_t stride)
485 {
486 	mali_gralloc_module *m;
487 	m = reinterpret_cast<private_module_t *>(device->common.module);
488 
489 	if (buffer == 0)
490 	{
491 		AERR("Bad input buffer handle %p to validate buffer size", buffer);
492 		return GRALLOC1_ERROR_BAD_HANDLE;
493 	}
494 
495 	if (private_handle_t::validate(buffer) < 0)
496 	{
497 		AERR("Buffer: %p is corrupted, for validating its size", buffer);
498 		return GRALLOC1_ERROR_BAD_HANDLE;
499 	}
500 
501 	if (!descriptorInfo->width || !descriptorInfo->height ||!descriptorInfo->layerCount)
502 	{
503 		AERR("Invalid buffer descriptor attributes, width = %d height = %d  layerCount = %d",
504 		      descriptorInfo->width, descriptorInfo->height, descriptorInfo->layerCount);
505 		return GRALLOC1_ERROR_BAD_VALUE;
506 	}
507 
508 	if (descriptorInfo->format == 0)
509 	{
510 		AERR("Invalid descriptor format to validate the buffer size");
511 		return GRALLOC1_ERROR_BAD_VALUE;
512 	}
513 
514 	buffer_descriptor_t grallocDescriptor;
515 	grallocDescriptor.width = descriptorInfo->width;
516 	grallocDescriptor.height = descriptorInfo->height;
517 	grallocDescriptor.layer_count = descriptorInfo->layerCount;
518 	grallocDescriptor.hal_format = descriptorInfo->format;
519 	grallocDescriptor.producer_usage = descriptorInfo->producerUsage;
520 	grallocDescriptor.consumer_usage = descriptorInfo->consumerUsage;
521 	grallocDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
522 
523 	/* Derive the buffer size for the given descriptor */
524 	const int result = mali_gralloc_derive_format_and_size(m, &grallocDescriptor);
525 	if (result)
526 	{
527 		AERR("Failed to derive format and size for the given descriptor information. error: %d", result);
528 		return GRALLOC1_ERROR_UNSUPPORTED;
529 	}
530 
531 	/* Validate the buffer parameters against descriptor info */
532 	private_handle_t *gralloc_buffer = (private_handle_t *)buffer;
533 
534 	/* The buffer size must be greater than (or equal to) what would have been
535 	 * allocated with descriptor
536 	 */
537 	if ((size_t)gralloc_buffer->size < grallocDescriptor.size)
538 	{
539 		ALOGW("Buf size mismatch. Buffer size = %u, Descriptor (derived) size = %zu",
540 		       gralloc_buffer->size, grallocDescriptor.size);
541 		return GRALLOC1_ERROR_BAD_VALUE;
542 	}
543 
544 	if ((uint32_t)gralloc_buffer->stride != stride)
545 	{
546 		ALOGW("Stride mismatch. Expected stride = %d, Buffer stride = %d",
547 		                       stride, gralloc_buffer->stride);
548 		return GRALLOC1_ERROR_BAD_VALUE;
549 	}
550 
551 	return GRALLOC1_ERROR_NONE;
552 }
553 
mali_gralloc1_get_transport_size(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outNumFds,uint32_t * outNumInts)554 static int32_t mali_gralloc1_get_transport_size(gralloc1_device_t* device, buffer_handle_t buffer,
555                                                 uint32_t *outNumFds, uint32_t *outNumInts)
556 {
557 	GRALLOC_UNUSED(device);
558 
559 	if (buffer == 0)
560 	{
561 		AERR("Bad input buffer handle %p to query transport size", buffer);
562 		return GRALLOC1_ERROR_BAD_HANDLE;
563 	}
564 
565 	if (outNumFds == 0 || outNumInts == 0)
566 	{
567 		AERR("Bad output pointers outNumFds=%p outNumInts=%p to populate", outNumFds, outNumInts);
568 		return GRALLOC1_ERROR_BAD_HANDLE;
569 	}
570 
571 	if (private_handle_t::validate(buffer) < 0)
572 	{
573 		AERR("Buffer %p, for querying transport size, is corrupted", buffer);
574 		return GRALLOC1_ERROR_BAD_HANDLE;
575 	}
576 
577 	*outNumFds = buffer->numFds;
578 	*outNumInts = buffer->numInts;
579 
580 	return GRALLOC1_ERROR_NONE;
581 }
582 
mali_gralloc1_import_buffer(gralloc1_device_t * device,const buffer_handle_t rawHandle,buffer_handle_t * outBuffer)583 static int32_t mali_gralloc1_import_buffer(gralloc1_device_t* device, const buffer_handle_t rawHandle,
584                                            buffer_handle_t* outBuffer)
585 {
586 	mali_gralloc_module *m;
587 	m = reinterpret_cast<private_module_t *>(device->common.module);
588 
589 	if (rawHandle == 0)
590 	{
591 		AERR("Bad input raw handle %p to import", rawHandle);
592 		return GRALLOC1_ERROR_BAD_HANDLE;
593 	}
594 
595 	if (outBuffer == 0)
596 	{
597 		AERR("Bad output buffer pointer %p to populate", outBuffer);
598 		return GRALLOC1_ERROR_BAD_HANDLE;
599 	}
600 
601 	/* The rawHandle could have been cloned locally or received from another
602 	 * HAL server/client or another process. Hence clone it locally before
603 	 * importing the buffer
604 	 */
605 	native_handle_t* bufferHandle = native_handle_clone(rawHandle);
606 	if (!bufferHandle)
607 	{
608 		AERR("Failed to clone %p for importing it", rawHandle);
609 		return GRALLOC1_ERROR_BAD_HANDLE;
610 	}
611 
612 	if (private_handle_t::validate(bufferHandle) < 0)
613 	{
614 		AERR("Failed to import corrupted buffer: %p", bufferHandle);
615 		return GRALLOC1_ERROR_BAD_HANDLE;
616 	}
617 
618 	if (mali_gralloc_reference_retain(m, bufferHandle) < 0)
619 	{
620 		AERR("Buffer: %p cannot be imported", bufferHandle);
621 		return GRALLOC1_ERROR_NO_RESOURCES;
622 	}
623 
624 	*outBuffer = bufferHandle;
625 
626 	return GRALLOC1_ERROR_NONE;
627 }
628 
629 static const mali_gralloc_func mali_gralloc_func_list[] = {
630 	{ GRALLOC1_FUNCTION_DUMP, (gralloc1_function_pointer_t)mali_gralloc_dump },
631 	{ GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_create_descriptor },
632 	{ GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_destroy_descriptor },
633 	{ GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_consumer_usage },
634 	{ GRALLOC1_FUNCTION_SET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_set_dimensions },
635 	{ GRALLOC1_FUNCTION_SET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_set_format },
636 	{ GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_producer_usage },
637 	{ GRALLOC1_FUNCTION_GET_BACKING_STORE, (gralloc1_function_pointer_t)mali_gralloc_get_backing_store },
638 	{ GRALLOC1_FUNCTION_GET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_consumer_usage },
639 	{ GRALLOC1_FUNCTION_GET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_get_dimensions },
640 	{ GRALLOC1_FUNCTION_GET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_get_format },
641 	{ GRALLOC1_FUNCTION_GET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_producer_usage },
642 	{ GRALLOC1_FUNCTION_GET_STRIDE, (gralloc1_function_pointer_t)mali_gralloc_get_stride },
643 	{ GRALLOC1_FUNCTION_ALLOCATE, (gralloc1_function_pointer_t)mali_gralloc_allocate },
644 	{ GRALLOC1_FUNCTION_RETAIN, (gralloc1_function_pointer_t)mali_gralloc_retain },
645 	{ GRALLOC1_FUNCTION_RELEASE, (gralloc1_function_pointer_t)mali_gralloc_release },
646 	{ GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES, (gralloc1_function_pointer_t)mali_gralloc1_get_num_flex_planes },
647 	{ GRALLOC1_FUNCTION_LOCK, (gralloc1_function_pointer_t)mali_gralloc1_lock_async },
648 	{ GRALLOC1_FUNCTION_LOCK_FLEX, (gralloc1_function_pointer_t)mali_gralloc1_lock_flex_async },
649 	{ GRALLOC1_FUNCTION_UNLOCK, (gralloc1_function_pointer_t)mali_gralloc1_unlock_async },
650 #if PLATFORM_SDK_VERSION >= 26
651 	{ GRALLOC1_FUNCTION_SET_LAYER_COUNT, (gralloc1_function_pointer_t)mali_gralloc1_set_layer_count },
652 	{ GRALLOC1_FUNCTION_GET_LAYER_COUNT, (gralloc1_function_pointer_t)mali_gralloc1_get_layer_count },
653 #endif
654 	{ GRALLOC1_FUNCTION_VALIDATE_BUFFER_SIZE, (gralloc1_function_pointer_t)mali_gralloc1_validate_buffer_size },
655 	{ GRALLOC1_FUNCTION_GET_TRANSPORT_SIZE, (gralloc1_function_pointer_t)mali_gralloc1_get_transport_size },
656 	{ GRALLOC1_FUNCTION_IMPORT_BUFFER, (gralloc1_function_pointer_t)mali_gralloc1_import_buffer },
657 
658 	/* GRALLOC1_FUNCTION_INVALID has to be the last descriptor on the list. */
659 	{ GRALLOC1_FUNCTION_INVALID, NULL }
660 };
661 
mali_gralloc_getCapabilities(gralloc1_device_t * dev,uint32_t * outCount,int32_t * outCapabilities)662 static void mali_gralloc_getCapabilities(gralloc1_device_t *dev, uint32_t *outCount, int32_t *outCapabilities)
663 {
664 	GRALLOC_UNUSED(dev);
665 #if PLATFORM_SDK_VERSION >= 26
666 	if (outCount != NULL)
667 	{
668 		*outCount = 1;
669 	}
670 
671 	if (outCapabilities != NULL)
672 	{
673 		*(outCapabilities++) = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
674 	}
675 #else
676 	GRALLOC_UNUSED(outCapabilities);
677 	if (outCount != NULL)
678 	{
679 		*outCount = 0;
680 	}
681 #endif
682 }
683 
mali_gralloc_getFunction(gralloc1_device_t * dev,int32_t descriptor)684 static gralloc1_function_pointer_t mali_gralloc_getFunction(gralloc1_device_t *dev, int32_t descriptor)
685 {
686 	GRALLOC_UNUSED(dev);
687 	gralloc1_function_pointer_t rval = NULL;
688 	uint32_t pos = 0;
689 
690 	while (mali_gralloc_func_list[pos].desc != GRALLOC1_FUNCTION_INVALID)
691 	{
692 		if (mali_gralloc_func_list[pos].desc == descriptor)
693 		{
694 			rval = mali_gralloc_func_list[pos].func;
695 			break;
696 		}
697 
698 		pos++;
699 	}
700 
701 	if (rval == NULL)
702 	{
703 		rval = mali_gralloc_private_interface_getFunction(descriptor);
704 	}
705 
706 	return rval;
707 }
708 
mali_gralloc_device_close(struct hw_device_t * device)709 static int mali_gralloc_device_close(struct hw_device_t *device)
710 {
711 	gralloc1_device_t *dev = reinterpret_cast<gralloc1_device_t *>(device);
712 	if (dev)
713 	{
714 		delete dev;
715 	}
716 
717 	mali_gralloc_ion_close();
718 
719 	return 0;
720 }
721 
mali_gralloc_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)722 int mali_gralloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
723 {
724 	gralloc1_device_t *dev;
725 
726 	GRALLOC_UNUSED(name);
727 
728 	dev = new gralloc1_device_t;
729 
730 	if (NULL == dev)
731 	{
732 		return -1;
733 	}
734 
735 	/* initialize our state here */
736 	memset(dev, 0, sizeof(*dev));
737 
738 	/* initialize the procs */
739 	dev->common.tag = HARDWARE_DEVICE_TAG;
740 	dev->common.version = 0;
741 	dev->common.module = const_cast<hw_module_t *>(module);
742 	dev->common.close = mali_gralloc_device_close;
743 
744 	dev->getCapabilities = mali_gralloc_getCapabilities;
745 	dev->getFunction = mali_gralloc_getFunction;
746 
747 	private_module_t *p = reinterpret_cast<private_module_t*>(dev->common.module);
748 	if (p->ionfd == -1)
749 		p->ionfd = mali_gralloc_ion_open();
750 
751 	*device = &dev->common;
752 
753 	return 0;
754 }
755