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 <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
153 int stride;
154
155 if (mali_gralloc_query_getstride(buffer, &stride) < 0)
156 {
157 return GRALLOC1_ERROR_UNSUPPORTED;
158 }
159
160 *outStride = (uint32_t)stride;
161
162 return GRALLOC1_ERROR_NONE;
163 }
164
mali_gralloc_allocate(gralloc1_device_t * device,uint32_t numDescriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * outBuffers)165 static int32_t mali_gralloc_allocate(gralloc1_device_t *device, uint32_t numDescriptors,
166 const gralloc1_buffer_descriptor_t *descriptors, buffer_handle_t *outBuffers)
167 {
168 mali_gralloc_module *m;
169 m = reinterpret_cast<private_module_t *>(device->common.module);
170 buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(*descriptors);
171 uint64_t usage;
172 bool shared = false;
173
174 usage = bufDescriptor->producer_usage | bufDescriptor->consumer_usage;
175
176 #if DISABLE_FRAMEBUFFER_HAL != 1
177
178 if (usage & GRALLOC_USAGE_HW_FB)
179 {
180 int byte_stride;
181 int pixel_stride;
182 int width, height;
183 uint64_t format;
184
185 format = bufDescriptor->hal_format;
186 width = bufDescriptor->width;
187 height = bufDescriptor->height;
188
189 #if GRALLOC_FB_SWAP_RED_BLUE == 1
190 #ifdef GRALLOC_16_BITS
191 format = HAL_PIXEL_FORMAT_RGB_565;
192 #else
193 format = HAL_PIXEL_FORMAT_BGRA_8888;
194 #endif
195 #endif
196
197 if (fb_alloc_framebuffer(m, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, outBuffers,
198 &pixel_stride, &byte_stride) < 0)
199 {
200 return GRALLOC1_ERROR_NO_RESOURCES;
201 }
202 else
203 {
204 private_handle_t *hnd = (private_handle_t *)*outBuffers;
205
206 /* Allocate a meta-data buffer for framebuffer too. fbhal
207 * ones wont need it but for hwc they will.
208 *
209 * Explicitly ignore allocation errors since it is not critical to have
210 */
211 (void)gralloc_buffer_attr_allocate(hnd);
212
213 hnd->req_format = format;
214 hnd->format = format;
215 hnd->yuv_info = MALI_YUV_BT601_NARROW;
216 hnd->internal_format = format;
217 hnd->byte_stride = byte_stride;
218 hnd->width = width;
219 hnd->height = height;
220 hnd->stride = pixel_stride;
221 hnd->internalWidth = width;
222 hnd->internalHeight = height;
223 }
224 }
225 else
226 #endif
227 {
228 if (mali_gralloc_buffer_allocate(m, (gralloc_buffer_descriptor_t *)descriptors, numDescriptors, outBuffers,
229 &shared) < 0)
230 {
231 ALOGE("Failed to allocate buffer.");
232 return GRALLOC1_ERROR_NO_RESOURCES;
233 }
234
235 if (!shared && 1 != numDescriptors)
236 {
237 return GRALLOC1_ERROR_NOT_SHARED;
238 }
239 }
240
241 return GRALLOC1_ERROR_NONE;
242 }
243
mali_gralloc_retain(gralloc1_device_t * device,buffer_handle_t buffer)244 static int32_t mali_gralloc_retain(gralloc1_device_t *device, buffer_handle_t buffer)
245 {
246 mali_gralloc_module *m;
247 m = reinterpret_cast<private_module_t *>(device->common.module);
248
249 if (private_handle_t::validate(buffer) < 0)
250 {
251 return GRALLOC1_ERROR_BAD_HANDLE;
252 }
253
254 if (mali_gralloc_reference_retain(m, buffer) < 0)
255 {
256 return GRALLOC1_ERROR_NO_RESOURCES;
257 }
258
259 return GRALLOC1_ERROR_NONE;
260 }
261
mali_gralloc_release(gralloc1_device_t * device,buffer_handle_t buffer)262 static int32_t mali_gralloc_release(gralloc1_device_t *device, buffer_handle_t buffer)
263 {
264 mali_gralloc_module *m;
265 m = reinterpret_cast<private_module_t *>(device->common.module);
266
267 if (mali_gralloc_reference_release(m, buffer, true) < 0)
268 {
269 return GRALLOC1_ERROR_BAD_HANDLE;
270 }
271
272 return GRALLOC1_ERROR_NONE;
273 }
274
mali_gralloc1_get_num_flex_planes(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outNumPlanes)275 static int32_t mali_gralloc1_get_num_flex_planes(gralloc1_device_t *device, buffer_handle_t buffer,
276 uint32_t *outNumPlanes)
277 {
278 mali_gralloc_module *m;
279 m = reinterpret_cast<private_module_t *>(device->common.module);
280
281 if (private_handle_t::validate(buffer) < 0)
282 {
283 return GRALLOC1_ERROR_BAD_HANDLE;
284 }
285
286 if (mali_gralloc_get_num_flex_planes(m, buffer, outNumPlanes) < 0)
287 {
288 return GRALLOC1_ERROR_UNSUPPORTED;
289 }
290
291 return GRALLOC1_ERROR_NONE;
292 }
293
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)294 static int32_t mali_gralloc1_lock_async(gralloc1_device_t *device, buffer_handle_t buffer,
295 uint64_t /*gralloc1_producer_usage_t*/ producerUsage,
296 uint64_t /*gralloc1_consumer_usage_t*/ consumerUsage,
297 const gralloc1_rect_t *accessRegion, void **outData, int32_t acquireFence)
298 {
299 mali_gralloc_module *m;
300 m = reinterpret_cast<private_module_t *>(device->common.module);
301
302 if (private_handle_t::validate(buffer) < 0)
303 {
304 return GRALLOC1_ERROR_BAD_HANDLE;
305 }
306
307 if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
308 {
309 return GRALLOC1_ERROR_BAD_VALUE;
310 }
311
312 if (mali_gralloc_lock_async(m, buffer, producerUsage | consumerUsage, accessRegion->left, accessRegion->top,
313 accessRegion->width, accessRegion->height, outData, acquireFence) < 0)
314 {
315 return GRALLOC1_ERROR_UNSUPPORTED;
316 }
317
318 return GRALLOC1_ERROR_NONE;
319 }
320
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)321 static int32_t mali_gralloc1_lock_flex_async(gralloc1_device_t *device, buffer_handle_t buffer,
322 uint64_t /*gralloc1_producer_usage_t*/ producerUsage,
323 uint64_t /*gralloc1_consumer_usage_t*/ consumerUsage,
324 const gralloc1_rect_t *accessRegion,
325 struct android_flex_layout *outFlexLayout, int32_t acquireFence)
326 {
327 mali_gralloc_module *m;
328 m = reinterpret_cast<private_module_t *>(device->common.module);
329
330 if (private_handle_t::validate(buffer) < 0)
331 {
332 return GRALLOC1_ERROR_BAD_HANDLE;
333 }
334
335 if (!((producerUsage | consumerUsage) & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)))
336 {
337 return GRALLOC1_ERROR_BAD_VALUE;
338 }
339
340 if (mali_gralloc_lock_flex_async(m, buffer, producerUsage | consumerUsage, accessRegion->left, accessRegion->top,
341 accessRegion->width, accessRegion->height, outFlexLayout, acquireFence) < 0)
342 {
343 return GRALLOC1_ERROR_UNSUPPORTED;
344 }
345
346 return GRALLOC1_ERROR_NONE;
347 }
348
mali_gralloc1_unlock_async(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outReleaseFence)349 static int32_t mali_gralloc1_unlock_async(gralloc1_device_t *device, buffer_handle_t buffer, int32_t *outReleaseFence)
350 {
351 mali_gralloc_module *m;
352 m = reinterpret_cast<private_module_t *>(device->common.module);
353
354 if (private_handle_t::validate(buffer) < 0)
355 {
356 return GRALLOC1_ERROR_BAD_HANDLE;
357 }
358
359 mali_gralloc_unlock_async(m, buffer, outReleaseFence);
360 return GRALLOC1_ERROR_NONE;
361 }
362
363 #if PLATFORM_SDK_VERSION >= 26
mali_gralloc1_set_layer_count(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t layerCount)364 static int32_t mali_gralloc1_set_layer_count(gralloc1_device_t* device,
365 gralloc1_buffer_descriptor_t descriptor,
366 uint32_t layerCount)
367 {
368 int ret = 0;
369 ret = mali_gralloc_set_layer_count_internal(descriptor, layerCount);
370 GRALLOC_UNUSED(device);
371 return ret;
372 }
373
mali_gralloc1_get_layer_count(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outLayerCount)374 static int32_t mali_gralloc1_get_layer_count(gralloc1_device_t* device,
375 buffer_handle_t buffer,
376 uint32_t* outLayerCount)
377 {
378 int ret = 0;
379 ret = mali_gralloc_get_layer_count_internal(buffer, outLayerCount);
380 GRALLOC_UNUSED(device);
381 return ret;
382 }
383 #endif
384
385 #if PLATFORM_SDK_VERSION >= 28
mali_gralloc1_validate_buffer_size(gralloc1_device_t * device,buffer_handle_t buffer,const gralloc1_buffer_descriptor_info_t * descriptorInfo,uint32_t stride)386 static int32_t mali_gralloc1_validate_buffer_size(gralloc1_device_t* device, buffer_handle_t buffer,
387 const gralloc1_buffer_descriptor_info_t* descriptorInfo,
388 uint32_t stride)
389 {
390 mali_gralloc_module *m;
391 m = reinterpret_cast<private_module_t *>(device->common.module);
392
393 if (buffer == 0)
394 {
395 AERR("Bad input buffer handle %p to validate buffer size", buffer);
396 return GRALLOC1_ERROR_BAD_HANDLE;
397 }
398
399 if (private_handle_t::validate(buffer) < 0)
400 {
401 AERR("Buffer: %p is corrupted, for validating its size", buffer);
402 return GRALLOC1_ERROR_BAD_HANDLE;
403 }
404
405 /* if (descriptorInfo->producerUsage & ~VALID_USAGE)
406 {
407 ALOGW("Buffer descriptor with invalid producer usage bits 0x%" PRIx64,
408 descriptorInfo->producerUsage & ~VALID_USAGE);
409 }
410
411 if (descriptorInfo->consumerUsage & ~VALID_USAGE)
412 {
413 ALOGW("Buffer descriptor with invalid consumer usage bits 0x%" PRIx64,
414 descriptorInfo->consumerUsage & ~VALID_USAGE);
415 } */
416
417 if (!descriptorInfo->width || !descriptorInfo->height ||!descriptorInfo->layerCount)
418 {
419 AERR("Invalid buffer descriptor attributes, width = %d height = %d layerCount = %d",
420 descriptorInfo->width, descriptorInfo->height, descriptorInfo->layerCount);
421 return GRALLOC1_ERROR_BAD_VALUE;
422 }
423
424 if (descriptorInfo->format == 0)
425 {
426 AERR("Invalid descriptor format to validate the buffer size");
427 return GRALLOC1_ERROR_BAD_VALUE;
428 }
429
430 buffer_descriptor_t grallocDescriptor;
431 grallocDescriptor.width = descriptorInfo->width;
432 grallocDescriptor.height = descriptorInfo->height;
433 grallocDescriptor.layer_count = descriptorInfo->layerCount;
434 grallocDescriptor.hal_format = descriptorInfo->format;
435 grallocDescriptor.producer_usage = descriptorInfo->producerUsage;
436 grallocDescriptor.consumer_usage = descriptorInfo->consumerUsage;
437 grallocDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
438
439 /* Derive the buffer size for the given descriptor */
440 /* const int result = mali_gralloc_derive_format_and_size(m, &grallocDescriptor);
441 if (result)
442 {
443 AERR("Failed to derive format and size for the given descriptor information. error: %d", result);
444 return GRALLOC1_ERROR_UNSUPPORTED;
445 } */
446
447 /* Validate the buffer parameters against descriptor info */
448 private_handle_t *gralloc_buffer = (private_handle_t *)buffer;
449
450 /* The buffer size must be greater than (or equal to) what would have been
451 * allocated with descriptor
452 */
453 if ((size_t)gralloc_buffer->size < grallocDescriptor.size)
454 {
455 ALOGW("Buf size mismatch. Buffer size = %u, Descriptor (derived) size = %zu",
456 gralloc_buffer->size, grallocDescriptor.size);
457 return GRALLOC1_ERROR_BAD_VALUE;
458 }
459
460 if ((uint32_t)gralloc_buffer->stride != stride)
461 {
462 AERR("Stride mismatch. Expected stride = %d, Buffer stride = %d",
463 stride, gralloc_buffer->stride);
464 return GRALLOC1_ERROR_BAD_VALUE;
465 }
466
467 /* if (gralloc_buffer->internal_format != grallocDescriptor.internal_format)
468 {
469 AERR("Buffer internal format :0x%" PRIx64" does not match descriptor (derived) internal format :0x%"
470 PRIx64, gralloc_buffer->internal_format, grallocDescriptor.internal_format);
471 return GRALLOC1_ERROR_BAD_VALUE;
472 }
473
474 if (gralloc_buffer->alloc_format != grallocDescriptor.alloc_format)
475 {
476 AERR("Buffer alloc format :0x%" PRIx64" does not match descriptor (derived) alloc format :0x%"
477 PRIx64, gralloc_buffer->alloc_format, grallocDescriptor.alloc_format);
478 return GRALLOC1_ERROR_BAD_VALUE;
479 }
480
481 const int format_idx = get_format_index(gralloc_buffer->alloc_format & MALI_GRALLOC_INTFMT_FMT_MASK);
482 if (format_idx == -1)
483 {
484 AERR("Invalid format to validate buffer descriptor");
485 return GRALLOC1_ERROR_BAD_VALUE;
486 }
487 else
488 {
489 for (int i = 0; i < formats[format_idx].npln; i++)
490 {
491 if (gralloc_buffer->plane_info[i].offset != grallocDescriptor.plane_info[i].offset)
492 {
493 AERR("Buffer offset 0x%x mismatch with desc offset 0x%x in plane %d ",
494 gralloc_buffer->plane_info[i].offset, grallocDescriptor.plane_info[i].offset, i);
495 return GRALLOC1_ERROR_BAD_VALUE;
496 }
497
498 if (gralloc_buffer->plane_info[i].byte_stride != grallocDescriptor.plane_info[i].byte_stride)
499 {
500 AERR("Buffer byte stride 0x%x mismatch with desc byte stride 0x%x in plane %d ",
501 gralloc_buffer->plane_info[i].byte_stride, grallocDescriptor.plane_info[i].byte_stride, i);
502 return GRALLOC1_ERROR_BAD_VALUE;
503 }
504
505 if (gralloc_buffer->plane_info[i].alloc_width != grallocDescriptor.plane_info[i].alloc_width)
506 {
507 AERR("Buffer alloc width 0x%x mismatch with desc alloc width 0x%x in plane %d ",
508 gralloc_buffer->plane_info[i].alloc_width, grallocDescriptor.plane_info[i].alloc_width, i);
509 return GRALLOC1_ERROR_BAD_VALUE;
510 }
511
512 if (gralloc_buffer->plane_info[i].alloc_height != grallocDescriptor.plane_info[i].alloc_height)
513 {
514 AERR("Buffer alloc height 0x%x mismatch with desc alloc height 0x%x in plane %d ",
515 gralloc_buffer->plane_info[i].alloc_height, grallocDescriptor.plane_info[i].alloc_height, i);
516 return GRALLOC1_ERROR_BAD_VALUE;
517 }
518 }
519 } */
520
521 if ((uint32_t)gralloc_buffer->width != grallocDescriptor.width)
522 {
523 AERR("Width mismatch. Buffer width = %u, Descriptor width = %u",
524 gralloc_buffer->width, grallocDescriptor.width);
525 return GRALLOC1_ERROR_BAD_VALUE;
526 }
527
528 if ((uint32_t)gralloc_buffer->height != grallocDescriptor.height)
529 {
530 AERR("Height mismatch. Buffer height = %u, Descriptor height = %u",
531 gralloc_buffer->height, grallocDescriptor.height);
532 return GRALLOC1_ERROR_BAD_VALUE;
533 }
534
535 if (gralloc_buffer->layer_count != grallocDescriptor.layer_count)
536 {
537 AERR("Layer Count mismatch. Buffer layer_count = %u, Descriptor layer_count width = %u",
538 gralloc_buffer->layer_count, grallocDescriptor.layer_count);
539 return GRALLOC1_ERROR_BAD_VALUE;
540 }
541
542 return GRALLOC1_ERROR_NONE;
543 }
544
mali_gralloc1_get_transport_size(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outNumFds,uint32_t * outNumInts)545 static int32_t mali_gralloc1_get_transport_size(gralloc1_device_t* device, buffer_handle_t buffer,
546 uint32_t *outNumFds, uint32_t *outNumInts)
547 {
548 GRALLOC_UNUSED(device);
549
550 if (buffer == 0)
551 {
552 AERR("Bad input buffer handle %p to query transport size", buffer);
553 return GRALLOC1_ERROR_BAD_HANDLE;
554 }
555
556 if (outNumFds == 0 || outNumInts == 0)
557 {
558 AERR("Bad output pointers outNumFds=%p outNumInts=%p to populate", outNumFds, outNumInts);
559 return GRALLOC1_ERROR_BAD_HANDLE;
560 }
561
562 if (private_handle_t::validate(buffer) < 0)
563 {
564 AERR("Buffer %p, for querying transport size, is corrupted", buffer);
565 return GRALLOC1_ERROR_BAD_HANDLE;
566 }
567
568 *outNumFds = GRALLOC_ARM_NUM_FDS;
569 *outNumInts = NUM_INTS_IN_PRIVATE_HANDLE;
570
571 return GRALLOC1_ERROR_NONE;
572 }
573
mali_gralloc1_import_buffer(gralloc1_device_t * device,const buffer_handle_t rawHandle,buffer_handle_t * outBuffer)574 static int32_t mali_gralloc1_import_buffer(gralloc1_device_t* device, const buffer_handle_t rawHandle,
575 buffer_handle_t* outBuffer)
576 {
577 mali_gralloc_module *m;
578 m = reinterpret_cast<private_module_t *>(device->common.module);
579
580 if (rawHandle == 0)
581 {
582 AERR("Bad input raw handle %p to import", rawHandle);
583 return GRALLOC1_ERROR_BAD_HANDLE;
584 }
585
586 if (outBuffer == 0)
587 {
588 AERR("Bad output buffer pointer %p to populate", outBuffer);
589 return GRALLOC1_ERROR_BAD_HANDLE;
590 }
591
592 /* The rawHandle could have been cloned locally or received from another
593 * HAL server/client or another process. Hence clone it locally before
594 * importing the buffer
595 */
596 native_handle_t* bufferHandle = native_handle_clone(rawHandle);
597 if (!bufferHandle)
598 {
599 AERR("Failed to clone %p for importing it", rawHandle);
600 return GRALLOC1_ERROR_BAD_HANDLE;
601 }
602
603 if (private_handle_t::validate(bufferHandle) < 0)
604 {
605 AERR("Failed to import corrupted buffer: %p", bufferHandle);
606 return GRALLOC1_ERROR_BAD_HANDLE;
607 }
608
609 if (mali_gralloc_reference_retain(m, bufferHandle) < 0)
610 {
611 AERR("Buffer: %p cannot be imported", bufferHandle);
612 return GRALLOC1_ERROR_NO_RESOURCES;
613 }
614
615 *outBuffer = bufferHandle;
616
617 return GRALLOC1_ERROR_NONE;
618 }
619 #endif
620
621 static const mali_gralloc_func mali_gralloc_func_list[] = {
622 { GRALLOC1_FUNCTION_DUMP, (gralloc1_function_pointer_t)mali_gralloc_dump },
623 { GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_create_descriptor },
624 { GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, (gralloc1_function_pointer_t)mali_gralloc_destroy_descriptor },
625 { GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_consumer_usage },
626 { GRALLOC1_FUNCTION_SET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_set_dimensions },
627 { GRALLOC1_FUNCTION_SET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_set_format },
628 { GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_set_producer_usage },
629 { GRALLOC1_FUNCTION_GET_BACKING_STORE, (gralloc1_function_pointer_t)mali_gralloc_get_backing_store },
630 { GRALLOC1_FUNCTION_GET_CONSUMER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_consumer_usage },
631 { GRALLOC1_FUNCTION_GET_DIMENSIONS, (gralloc1_function_pointer_t)mali_gralloc_get_dimensions },
632 { GRALLOC1_FUNCTION_GET_FORMAT, (gralloc1_function_pointer_t)mali_gralloc_get_format },
633 { GRALLOC1_FUNCTION_GET_PRODUCER_USAGE, (gralloc1_function_pointer_t)mali_gralloc_get_producer_usage },
634 { GRALLOC1_FUNCTION_GET_STRIDE, (gralloc1_function_pointer_t)mali_gralloc_get_stride },
635 { GRALLOC1_FUNCTION_ALLOCATE, (gralloc1_function_pointer_t)mali_gralloc_allocate },
636 { GRALLOC1_FUNCTION_RETAIN, (gralloc1_function_pointer_t)mali_gralloc_retain },
637 { GRALLOC1_FUNCTION_RELEASE, (gralloc1_function_pointer_t)mali_gralloc_release },
638 { GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES, (gralloc1_function_pointer_t)mali_gralloc1_get_num_flex_planes },
639 { GRALLOC1_FUNCTION_LOCK, (gralloc1_function_pointer_t)mali_gralloc1_lock_async },
640 { GRALLOC1_FUNCTION_LOCK_FLEX, (gralloc1_function_pointer_t)mali_gralloc1_lock_flex_async },
641 { GRALLOC1_FUNCTION_UNLOCK, (gralloc1_function_pointer_t)mali_gralloc1_unlock_async },
642
643 #if PLATFORM_SDK_VERSION >= 26
644 { GRALLOC1_FUNCTION_SET_LAYER_COUNT, (gralloc1_function_pointer_t)mali_gralloc1_set_layer_count },
645 { GRALLOC1_FUNCTION_GET_LAYER_COUNT, (gralloc1_function_pointer_t)mali_gralloc1_get_layer_count },
646 #endif
647
648 #if PLATFORM_SDK_VERSION >= 28
649 { GRALLOC1_FUNCTION_VALIDATE_BUFFER_SIZE, (gralloc1_function_pointer_t)mali_gralloc1_validate_buffer_size },
650 { GRALLOC1_FUNCTION_GET_TRANSPORT_SIZE, (gralloc1_function_pointer_t)mali_gralloc1_get_transport_size },
651 { GRALLOC1_FUNCTION_IMPORT_BUFFER, (gralloc1_function_pointer_t)mali_gralloc1_import_buffer },
652 #endif
653
654 /* GRALLOC1_FUNCTION_INVALID has to be the last descriptor on the list. */
655 { GRALLOC1_FUNCTION_INVALID, NULL }
656 };
657
mali_gralloc_getCapabilities(gralloc1_device_t * dev,uint32_t * outCount,int32_t * outCapabilities)658 static void mali_gralloc_getCapabilities(gralloc1_device_t *dev, uint32_t *outCount, int32_t *outCapabilities)
659 {
660 GRALLOC_UNUSED(dev);
661 GRALLOC_UNUSED(outCapabilities);
662
663 if (outCount != NULL)
664 {
665 *outCount = 0;
666 }
667 }
668
mali_gralloc_getFunction(gralloc1_device_t * dev,int32_t descriptor)669 static gralloc1_function_pointer_t mali_gralloc_getFunction(gralloc1_device_t *dev, int32_t descriptor)
670 {
671 GRALLOC_UNUSED(dev);
672 gralloc1_function_pointer_t rval = NULL;
673 uint32_t pos = 0;
674
675 while (mali_gralloc_func_list[pos].desc != GRALLOC1_FUNCTION_INVALID)
676 {
677 if (mali_gralloc_func_list[pos].desc == descriptor)
678 {
679 rval = mali_gralloc_func_list[pos].func;
680 break;
681 }
682
683 pos++;
684 }
685
686 if (rval == NULL)
687 {
688 rval = mali_gralloc_private_interface_getFunction(descriptor);
689 }
690
691 return rval;
692 }
693
mali_gralloc_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)694 int mali_gralloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
695 {
696 gralloc1_device_t *dev;
697
698 GRALLOC_UNUSED(name);
699
700 dev = new gralloc1_device_t;
701
702 if (NULL == dev)
703 {
704 return -1;
705 }
706
707 /* initialize our state here */
708 memset(dev, 0, sizeof(*dev));
709
710 /* initialize the procs */
711 dev->common.tag = HARDWARE_DEVICE_TAG;
712 dev->common.version = 0;
713 dev->common.module = const_cast<hw_module_t *>(module);
714 dev->common.close = mali_gralloc_ion_device_close;
715
716 dev->getCapabilities = mali_gralloc_getCapabilities;
717 dev->getFunction = mali_gralloc_getFunction;
718
719 *device = &dev->common;
720
721 return 0;
722 }
723