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