blob: 67734050c7dd0ebaa2990f349d20cc8e900fb39c [file] [log] [blame]
Vishal Bhoj78e90492015-12-07 01:36:32 +05301/*
2 * Copyright (C) 2010 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
19#include <string.h>
20#include <errno.h>
21#include <pthread.h>
22
23#include <cutils/log.h>
24#include <cutils/atomic.h>
25#include <hardware/hardware.h>
26#include <hardware/gralloc.h>
27
28#include <sys/ioctl.h>
29
30#include "alloc_device.h"
31#include "gralloc_priv.h"
32#include "gralloc_helper.h"
33#include "framebuffer_device.h"
34
35#if GRALLOC_ARM_UMP_MODULE
36#include <ump/ump.h>
37#include <ump/ump_ref_drv.h>
38#endif
39
40#if GRALLOC_ARM_DMA_BUF_MODULE
41#include <linux/ion.h>
42#include <ion/ion.h>
43#endif
44
45#define GRALLOC_ALIGN( value, base ) (((value) + ((base) - 1)) & ~((base) - 1))
46
47
48#if GRALLOC_SIMULATE_FAILURES
49#include <cutils/properties.h>
50
51/* system property keys for controlling simulated UMP allocation failures */
52#define PROP_MALI_TEST_GRALLOC_FAIL_FIRST "mali.test.gralloc.fail_first"
53#define PROP_MALI_TEST_GRALLOC_FAIL_INTERVAL "mali.test.gralloc.fail_interval"
54
55static int __ump_alloc_should_fail()
56{
57
58 static unsigned int call_count = 0;
59 unsigned int first_fail = 0;
60 int fail_period = 0;
61 int fail = 0;
62
63 ++call_count;
64
65 /* read the system properties that control failure simulation */
66 {
67 char prop_value[PROPERTY_VALUE_MAX];
68
69 if (property_get(PROP_MALI_TEST_GRALLOC_FAIL_FIRST, prop_value, "0") > 0)
70 {
71 sscanf(prop_value, "%11u", &first_fail);
72 }
73
74 if (property_get(PROP_MALI_TEST_GRALLOC_FAIL_INTERVAL, prop_value, "0") > 0)
75 {
76 sscanf(prop_value, "%11u", &fail_period);
77 }
78 }
79
80 /* failure simulation is enabled by setting the first_fail property to non-zero */
81 if (first_fail > 0)
82 {
83 LOGI("iteration %u (fail=%u, period=%u)\n", call_count, first_fail, fail_period);
84
85 fail = (call_count == first_fail) ||
86 (call_count > first_fail && fail_period > 0 && 0 == (call_count - first_fail) % fail_period);
87
88 if (fail)
89 {
90 AERR("failed ump_ref_drv_allocate on iteration #%d\n", call_count);
91 }
92 }
93
94 return fail;
95}
96#endif
97
98
99static int gralloc_alloc_buffer(alloc_device_t *dev, size_t size, int usage, buffer_handle_t *pHandle)
100{
101#if GRALLOC_ARM_DMA_BUF_MODULE
102 {
103 private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
104 ion_user_handle_t ion_hnd;
105 unsigned char *cpu_ptr;
106 int shared_fd;
107 int ret;
108
109 ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
110
111 if (ret != 0)
112 {
113 AERR("Failed to ion_alloc from ion_client:%d", m->ion_client);
114 return -1;
115 }
116
117 ret = ion_share(m->ion_client, ion_hnd, &shared_fd);
118
119 if (ret != 0)
120 {
121 AERR("ion_share( %d ) failed", m->ion_client);
122
123 if (0 != ion_free(m->ion_client, ion_hnd))
124 {
125 AERR("ion_free( %d ) failed", m->ion_client);
126 }
127
128 return -1;
129 }
130
Chia-I Wu06695a62017-05-08 12:55:28 -0700131 // we do not need ion_hnd once we have shared_fd
132 if (0 != ion_free(m->ion_client, ion_hnd))
133 {
134 AWAR("ion_free( %d ) failed", m->ion_client);
135 }
136 ion_hnd = ION_INVALID_HANDLE;
137
Vishal Bhoj78e90492015-12-07 01:36:32 +0530138 cpu_ptr = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_fd, 0);
139
140 if (MAP_FAILED == cpu_ptr)
141 {
142 AERR("ion_map( %d ) failed", m->ion_client);
143
Vishal Bhoj78e90492015-12-07 01:36:32 +0530144 close(shared_fd);
145 return -1;
146 }
147
148 private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_ION, usage, size, cpu_ptr, private_handle_t::LOCK_STATE_MAPPED);
149
150 if (NULL != hnd)
151 {
152 hnd->share_fd = shared_fd;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530153 *pHandle = hnd;
154 return 0;
155 }
156 else
157 {
158 AERR("Gralloc out of mem for ion_client:%d", m->ion_client);
159 }
160
161 close(shared_fd);
162 ret = munmap(cpu_ptr, size);
163
164 if (0 != ret)
165 {
166 AERR("munmap failed for base:%p size: %lu", cpu_ptr, (unsigned long)size);
167 }
168
Vishal Bhoj78e90492015-12-07 01:36:32 +0530169 return -1;
170 }
171#endif
172
173#if GRALLOC_ARM_UMP_MODULE
174 MALI_IGNORE(dev);
175 {
176 ump_handle ump_mem_handle;
177 void *cpu_ptr;
178 ump_secure_id ump_id;
179 ump_alloc_constraints constraints;
180
181 size = round_up_to_page_size(size);
182
183 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
184 {
185 constraints = UMP_REF_DRV_CONSTRAINT_USE_CACHE;
186 }
187 else
188 {
189 constraints = UMP_REF_DRV_CONSTRAINT_NONE;
190 }
191
192#ifdef GRALLOC_SIMULATE_FAILURES
193
194 /* if the failure condition matches, fail this iteration */
195 if (__ump_alloc_should_fail())
196 {
197 ump_mem_handle = UMP_INVALID_MEMORY_HANDLE;
198 }
199 else
200#endif
201 {
202 ump_mem_handle = ump_ref_drv_allocate(size, constraints);
203
204 if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle)
205 {
206 cpu_ptr = ump_mapped_pointer_get(ump_mem_handle);
207
208 if (NULL != cpu_ptr)
209 {
210 ump_id = ump_secure_id_get(ump_mem_handle);
211
212 if (UMP_INVALID_SECURE_ID != ump_id)
213 {
214 private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_UMP, usage, size, cpu_ptr,
215 private_handle_t::LOCK_STATE_MAPPED, ump_id, ump_mem_handle);
216
217 if (NULL != hnd)
218 {
219 *pHandle = hnd;
220 return 0;
221 }
222 else
223 {
224 AERR("gralloc_alloc_buffer() failed to allocate handle. ump_handle = %p, ump_id = %d", ump_mem_handle, ump_id);
225 }
226 }
227 else
228 {
229 AERR("gralloc_alloc_buffer() failed to retrieve valid secure id. ump_handle = %p", ump_mem_handle);
230 }
231
232 ump_mapped_pointer_release(ump_mem_handle);
233 }
234 else
235 {
236 AERR("gralloc_alloc_buffer() failed to map UMP memory. ump_handle = %p", ump_mem_handle);
237 }
238
239 ump_reference_release(ump_mem_handle);
240 }
241 else
242 {
243 AERR("gralloc_alloc_buffer() failed to allocate UMP memory. size:%d constraints: %d", size, constraints);
244 }
245 }
246
247 return -1;
248 }
249#endif
250
251}
252
253static int gralloc_alloc_framebuffer_locked(alloc_device_t *dev, size_t size, int usage, buffer_handle_t *pHandle)
254{
255 private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
256
257 // allocate the framebuffer
258 if (m->framebuffer == NULL)
259 {
260 // initialize the framebuffer, the framebuffer is mapped once and forever.
261 int err = init_frame_buffer_locked(m);
262
263 if (err < 0)
264 {
265 return err;
266 }
267 }
268
269 const uint32_t bufferMask = m->bufferMask;
270 const uint32_t numBuffers = m->numBuffers;
271 const size_t bufferSize = m->finfo.line_length * m->info.yres;
272
273 if (numBuffers == 1)
274 {
275 // If we have only one buffer, we never use page-flipping. Instead,
276 // we return a regular buffer which will be memcpy'ed to the main
277 // screen when post is called.
278 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
279 AERR("fallback to single buffering. Virtual Y-res too small %d", m->info.yres);
280 return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
281 }
282
283 if (bufferMask >= ((1LU << numBuffers) - 1))
284 {
285 // We ran out of buffers.
286 return -ENOMEM;
287 }
288
289 void *vaddr = m->framebuffer->base;
290
291 // find a free slot
292 for (uint32_t i = 0 ; i < numBuffers ; i++)
293 {
294 if ((bufferMask & (1LU << i)) == 0)
295 {
296 m->bufferMask |= (1LU << i);
297 break;
298 }
299
300 vaddr = (void *)((uintptr_t)vaddr + bufferSize);
301 }
302
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700303 int fbdev_fd = m->framebuffer->shallow_fbdev_fd;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530304 // The entire framebuffer memory is already mapped, now create a buffer object for parts of this memory
305 private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, usage, size, vaddr,
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700306 0, fbdev_fd, (uintptr_t)vaddr - (uintptr_t) m->framebuffer->base);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530307#if GRALLOC_ARM_UMP_MODULE
308 hnd->ump_id = m->framebuffer->ump_id;
309
310 /* create a backing ump memory handle if the framebuffer is exposed as a secure ID */
311 if ((int)UMP_INVALID_SECURE_ID != hnd->ump_id)
312 {
313 hnd->ump_mem_handle = (int)ump_handle_create_from_secure_id(hnd->ump_id);
314
315 if ((int)UMP_INVALID_MEMORY_HANDLE == hnd->ump_mem_handle)
316 {
317 AINF("warning: unable to create UMP handle from secure ID %i\n", hnd->ump_id);
318 }
319 }
320
321#endif
322
323#if GRALLOC_ARM_DMA_BUF_MODULE
324 {
325#ifdef FBIOGET_DMABUF
326 struct fb_dmabuf_export fb_dma_buf;
327
Chia-I Wu57b9e8a2017-05-08 12:50:36 -0700328 if (ioctl(fbdev_fd, FBIOGET_DMABUF, &fb_dma_buf) == 0)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530329 {
330 AINF("framebuffer accessed with dma buf (fd 0x%x)\n", (int)fb_dma_buf.fd);
331 hnd->share_fd = fb_dma_buf.fd;
332 }
333
334#endif
335 }
336#endif
337
338 *pHandle = hnd;
339
340 return 0;
341}
342
343static int gralloc_alloc_framebuffer(alloc_device_t *dev, size_t size, int usage, buffer_handle_t *pHandle)
344{
345 private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
346 pthread_mutex_lock(&m->lock);
347 int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle);
348 pthread_mutex_unlock(&m->lock);
349 return err;
350}
351
352static int alloc_device_alloc(alloc_device_t *dev, int w, int h, int format, int usage, buffer_handle_t *pHandle, int *pStride)
353{
354 if (!pHandle || !pStride)
355 {
356 return -EINVAL;
357 }
358
359 size_t size;
360 size_t stride;
361
362 if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP || format == HAL_PIXEL_FORMAT_YV12
363 /* HAL_PIXEL_FORMAT_YCbCr_420_SP, HAL_PIXEL_FORMAT_YCbCr_420_P, HAL_PIXEL_FORMAT_YCbCr_422_I are not defined in Android.
364 * To enable Mali DDK EGLImage support for those formats, firstly, you have to add them in Android system/core/include/system/graphics.h.
365 * Then, define SUPPORT_LEGACY_FORMAT in the same header file(Mali DDK will also check this definition).
366 */
367#ifdef SUPPORT_LEGACY_FORMAT
368 || format == HAL_PIXEL_FORMAT_YCbCr_420_SP || format == HAL_PIXEL_FORMAT_YCbCr_420_P || format == HAL_PIXEL_FORMAT_YCbCr_422_I
369#endif
370 )
371 {
372 switch (format)
373 {
374 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
375 stride = GRALLOC_ALIGN(w, 16);
376 size = GRALLOC_ALIGN(h, 16) * (stride + GRALLOC_ALIGN(stride / 2, 16));
377 break;
378
379 case HAL_PIXEL_FORMAT_YV12:
380#ifdef SUPPORT_LEGACY_FORMAT
381 case HAL_PIXEL_FORMAT_YCbCr_420_P:
382#endif
383 stride = GRALLOC_ALIGN(w, 16);
384 size = GRALLOC_ALIGN(h, 2) * (stride + GRALLOC_ALIGN(stride / 2, 16));
385
386 break;
387#ifdef SUPPORT_LEGACY_FORMAT
388
389 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
390 stride = GRALLOC_ALIGN(w, 16);
391 size = GRALLOC_ALIGN(h, 16) * (stride + GRALLOC_ALIGN(stride / 2, 16));
392 break;
393
394 case HAL_PIXEL_FORMAT_YCbCr_422_I:
395 stride = GRALLOC_ALIGN(w, 16);
396 size = h * stride * 2;
397
398 break;
399#endif
400
401 default:
402 return -EINVAL;
403 }
404 }
405 else
406 {
407 int bpp = 0;
408
409 switch (format)
410 {
411 case HAL_PIXEL_FORMAT_RGBA_8888:
412 case HAL_PIXEL_FORMAT_RGBX_8888:
413 case HAL_PIXEL_FORMAT_BGRA_8888:
414 bpp = 4;
415 break;
416
417 case HAL_PIXEL_FORMAT_RGB_888:
418 bpp = 3;
419 break;
420
421 case HAL_PIXEL_FORMAT_RGB_565:
422#if PLATFORM_SDK_VERSION < 19
423 case HAL_PIXEL_FORMAT_RGBA_5551:
424 case HAL_PIXEL_FORMAT_RGBA_4444:
425#endif
426 bpp = 2;
427 break;
428
429 default:
430 return -EINVAL;
431 }
432
433 size_t bpr = GRALLOC_ALIGN(w * bpp, 64);
434 size = bpr * h;
435 stride = bpr / bpp;
436 }
437
438 int err;
439
440#ifndef MALI_600
441
442 if (usage & GRALLOC_USAGE_HW_FB)
443 {
444 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
445 }
446 else
447#endif
448
449 {
450 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
451 }
452
453 if (err < 0)
454 {
455 return err;
456 }
457
458 /* match the framebuffer format */
459 if (usage & GRALLOC_USAGE_HW_FB)
460 {
461#ifdef GRALLOC_16_BITS
462 format = HAL_PIXEL_FORMAT_RGB_565;
463#else
464 format = HAL_PIXEL_FORMAT_BGRA_8888;
465#endif
466 }
467
468 private_handle_t *hnd = (private_handle_t *)*pHandle;
469 int private_usage = usage & (GRALLOC_USAGE_PRIVATE_0 |
470 GRALLOC_USAGE_PRIVATE_1);
471
472 switch (private_usage)
473 {
474 case 0:
475 hnd->yuv_info = MALI_YUV_BT601_NARROW;
476 break;
477
478 case GRALLOC_USAGE_PRIVATE_1:
479 hnd->yuv_info = MALI_YUV_BT601_WIDE;
480 break;
481
482 case GRALLOC_USAGE_PRIVATE_0:
483 hnd->yuv_info = MALI_YUV_BT709_NARROW;
484 break;
485
486 case (GRALLOC_USAGE_PRIVATE_0 | GRALLOC_USAGE_PRIVATE_1):
487 hnd->yuv_info = MALI_YUV_BT709_WIDE;
488 break;
489 }
490
491 hnd->width = w;
492 hnd->height = h;
493 hnd->format = format;
494 hnd->stride = stride;
495
496 *pStride = stride;
497 return 0;
498}
499
500static int alloc_device_free(alloc_device_t *dev, buffer_handle_t handle)
501{
502 if (private_handle_t::validate(handle) < 0)
503 {
504 return -EINVAL;
505 }
506
507 private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(handle);
508
509 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
510 {
511 // free this buffer
512 private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
513 const size_t bufferSize = m->finfo.line_length * m->info.yres;
514 int index = ((uintptr_t)hnd->base - (uintptr_t)m->framebuffer->base) / bufferSize;
515 m->bufferMask &= ~(1 << index);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530516
517#if GRALLOC_ARM_UMP_MODULE
518
519 if ((int)UMP_INVALID_MEMORY_HANDLE != hnd->ump_mem_handle)
520 {
521 ump_reference_release((ump_handle)hnd->ump_mem_handle);
522 }
523
524#endif
525 }
526 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
527 {
528#if GRALLOC_ARM_UMP_MODULE
529
530 /* Buffer might be unregistered so we need to check for invalid ump handle*/
531 if ((int)UMP_INVALID_MEMORY_HANDLE != hnd->ump_mem_handle)
532 {
533 ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
534 ump_reference_release((ump_handle)hnd->ump_mem_handle);
535 }
536
537#else
538 AERR("Can't free ump memory for handle:0x%p. Not supported.", hnd);
539#endif
540 }
541 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
542 {
543#if GRALLOC_ARM_DMA_BUF_MODULE
544 private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
545
546 /* Buffer might be unregistered so we need to check for invalid ump handle*/
547 if (0 != hnd->base)
548 {
549 if (0 != munmap((void *)hnd->base, hnd->size))
550 {
551 AERR("Failed to munmap handle 0x%p", hnd);
552 }
553 }
554
555 close(hnd->share_fd);
556
Vishal Bhoj78e90492015-12-07 01:36:32 +0530557 memset((void *)hnd, 0, sizeof(*hnd));
558#else
559 AERR("Can't free dma_buf memory for handle:0x%x. Not supported.", (unsigned int)hnd);
560#endif
561
562 }
563
564 delete hnd;
565
566 return 0;
567}
568
569static int alloc_device_close(struct hw_device_t *device)
570{
571 alloc_device_t *dev = reinterpret_cast<alloc_device_t *>(device);
572
573 if (dev)
574 {
575#if GRALLOC_ARM_DMA_BUF_MODULE
576 private_module_t *m = reinterpret_cast<private_module_t *>(device);
577
578 if (0 != ion_close(m->ion_client))
579 {
580 AERR("Failed to close ion_client: %d", m->ion_client);
581 }
582
583 close(m->ion_client);
584#endif
585 delete dev;
586#if GRALLOC_ARM_UMP_MODULE
587 ump_close(); // Our UMP memory refs will be released automatically here...
588#endif
589 }
590
591 return 0;
592}
593
594int alloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
595{
596 MALI_IGNORE(name);
597 alloc_device_t *dev;
598
599 dev = new alloc_device_t;
600
601 if (NULL == dev)
602 {
603 return -1;
604 }
605
606#if GRALLOC_ARM_UMP_MODULE
607 ump_result ump_res = ump_open();
608
609 if (UMP_OK != ump_res)
610 {
611 AERR("UMP open failed with %d", ump_res);
612 delete dev;
613 return -1;
614 }
615
616#endif
617
618 /* initialize our state here */
619 memset(dev, 0, sizeof(*dev));
620
621 /* initialize the procs */
622 dev->common.tag = HARDWARE_DEVICE_TAG;
623 dev->common.version = 0;
624 dev->common.module = const_cast<hw_module_t *>(module);
625 dev->common.close = alloc_device_close;
626 dev->alloc = alloc_device_alloc;
627 dev->free = alloc_device_free;
628
629#if GRALLOC_ARM_DMA_BUF_MODULE
630 private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
631 m->ion_client = ion_open();
632
633 if (m->ion_client < 0)
634 {
635 AERR("ion_open failed with %s", strerror(errno));
636 delete dev;
637 return -1;
638 }
639
640#endif
641
642 *device = &dev->common;
643
644 return 0;
645}