Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <limits.h> |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | |
| 28 | #include <cutils/ashmem.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 29 | #include <cutils/log.h> |
| 30 | #include <cutils/atomic.h> |
| 31 | |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 32 | #include <hardware/hardware.h> |
| 33 | #include <hardware/gralloc.h> |
| 34 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 35 | #include "gralloc_priv.h" |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 36 | #include "allocator.h" |
| 37 | |
| 38 | #if HAVE_ANDROID_OS |
| 39 | #include <linux/android_pmem.h> |
| 40 | #endif |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 41 | |
| 42 | /*****************************************************************************/ |
| 43 | |
| 44 | struct gralloc_context_t { |
| 45 | alloc_device_t device; |
| 46 | /* our private data here */ |
| 47 | }; |
| 48 | |
| 49 | static int gralloc_alloc_buffer(alloc_device_t* dev, |
| 50 | size_t size, int usage, buffer_handle_t* pHandle); |
| 51 | |
| 52 | /*****************************************************************************/ |
| 53 | |
| 54 | int fb_device_open(const hw_module_t* module, const char* name, |
| 55 | hw_device_t** device); |
| 56 | |
| 57 | static int gralloc_device_open(const hw_module_t* module, const char* name, |
| 58 | hw_device_t** device); |
| 59 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 60 | extern int gralloc_lock(gralloc_module_t const* module, |
| 61 | buffer_handle_t handle, int usage, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 62 | int l, int t, int w, int h, |
| 63 | void** vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 64 | |
| 65 | extern int gralloc_unlock(gralloc_module_t const* module, |
| 66 | buffer_handle_t handle); |
| 67 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 68 | extern int gralloc_register_buffer(gralloc_module_t const* module, |
| 69 | buffer_handle_t handle); |
| 70 | |
| 71 | extern int gralloc_unregister_buffer(gralloc_module_t const* module, |
| 72 | buffer_handle_t handle); |
| 73 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 74 | /*****************************************************************************/ |
| 75 | |
| 76 | static struct hw_module_methods_t gralloc_module_methods = { |
| 77 | open: gralloc_device_open |
| 78 | }; |
| 79 | |
| 80 | struct private_module_t HAL_MODULE_INFO_SYM = { |
| 81 | base: { |
| 82 | common: { |
| 83 | tag: HARDWARE_MODULE_TAG, |
| 84 | version_major: 1, |
| 85 | version_minor: 0, |
| 86 | id: GRALLOC_HARDWARE_MODULE_ID, |
| 87 | name: "Graphics Memory Allocator Module", |
| 88 | author: "The Android Open Source Project", |
| 89 | methods: &gralloc_module_methods |
| 90 | }, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 91 | registerBuffer: gralloc_register_buffer, |
| 92 | unregisterBuffer: gralloc_unregister_buffer, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 93 | lock: gralloc_lock, |
| 94 | unlock: gralloc_unlock, |
| 95 | }, |
| 96 | framebuffer: 0, |
| 97 | flags: 0, |
| 98 | numBuffers: 0, |
| 99 | bufferMask: 0, |
| 100 | lock: PTHREAD_MUTEX_INITIALIZER, |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 101 | currentBuffer: 0, |
| 102 | pmem_master: -1, |
| 103 | pmem_master_base: 0 |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | /*****************************************************************************/ |
| 107 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 108 | static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, |
| 109 | size_t size, int usage, buffer_handle_t* pHandle) |
| 110 | { |
| 111 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 112 | dev->common.module); |
| 113 | |
| 114 | // allocate the framebuffer |
| 115 | if (m->framebuffer == NULL) { |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 116 | // initialize the framebuffer, the framebuffer is mapped once |
| 117 | // and forever. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 118 | int err = mapFrameBufferLocked(m); |
| 119 | if (err < 0) { |
| 120 | return err; |
| 121 | } |
| 122 | } |
| 123 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 124 | const uint32_t bufferMask = m->bufferMask; |
| 125 | const uint32_t numBuffers = m->numBuffers; |
| 126 | const size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 127 | if (numBuffers == 1) { |
| 128 | // If we have only one buffer, we never use page-flipping. Instead, |
| 129 | // we return a regular buffer which will be memcpy'ed to the main |
| 130 | // screen when post is called. |
| 131 | int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; |
| 132 | return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle); |
| 133 | } |
| 134 | |
| 135 | if (bufferMask >= ((1LU<<numBuffers)-1)) { |
| 136 | // We ran out of buffers. |
| 137 | return -ENOMEM; |
| 138 | } |
| 139 | |
| 140 | // create a "fake" handles for it |
| 141 | intptr_t vaddr = intptr_t(m->framebuffer->base); |
| 142 | private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size, |
| 143 | private_handle_t::PRIV_FLAGS_USES_PMEM | |
| 144 | private_handle_t::PRIV_FLAGS_FRAMEBUFFER); |
| 145 | |
| 146 | // find a free slot |
| 147 | for (uint32_t i=0 ; i<numBuffers ; i++) { |
| 148 | if ((bufferMask & (1LU<<i)) == 0) { |
| 149 | m->bufferMask |= (1LU<<i); |
| 150 | break; |
| 151 | } |
| 152 | vaddr += bufferSize; |
| 153 | } |
| 154 | |
| 155 | hnd->base = vaddr; |
Mathias Agopian | 72c8508 | 2009-06-10 16:06:28 -0700 | [diff] [blame] | 156 | hnd->offset = vaddr - intptr_t(m->framebuffer->base); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 157 | *pHandle = hnd; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int gralloc_alloc_framebuffer(alloc_device_t* dev, |
| 163 | size_t size, int usage, buffer_handle_t* pHandle) |
| 164 | { |
| 165 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 166 | dev->common.module); |
| 167 | pthread_mutex_lock(&m->lock); |
| 168 | int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle); |
| 169 | pthread_mutex_unlock(&m->lock); |
| 170 | return err; |
| 171 | } |
| 172 | |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 173 | static SimpleBestFitAllocator sAllocator(8*1024*1024); |
| 174 | |
| 175 | static int init_pmem_area(private_module_t* m) |
| 176 | { |
| 177 | int err = 0; |
| 178 | int master_fd = open("/dev/pmem", O_RDWR, 0); |
| 179 | if (master_fd >= 0) { |
| 180 | void* base = mmap(0, sAllocator.size(), |
| 181 | PROT_READ|PROT_WRITE, MAP_SHARED, master_fd, 0); |
| 182 | if (base == MAP_FAILED) { |
| 183 | err = -errno; |
| 184 | base = 0; |
| 185 | close(master_fd); |
| 186 | master_fd = -1; |
| 187 | } |
| 188 | m->pmem_master = master_fd; |
| 189 | m->pmem_master_base = base; |
| 190 | } else { |
| 191 | err = -errno; |
| 192 | } |
| 193 | return err; |
| 194 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 195 | |
| 196 | static int gralloc_alloc_buffer(alloc_device_t* dev, |
| 197 | size_t size, int usage, buffer_handle_t* pHandle) |
| 198 | { |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 199 | int err = 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 200 | int flags = 0; |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 201 | |
| 202 | int fd = -1; |
| 203 | void* base = 0; |
| 204 | int offset = 0; |
| 205 | int lockState = 0; |
| 206 | |
| 207 | size = roundUpToPageSize(size); |
| 208 | |
| 209 | if (usage & GRALLOC_USAGE_HW_TEXTURE) { |
| 210 | // enable pmem in that case, so our software GL can fallback to |
| 211 | // the copybit module. |
| 212 | flags |= private_handle_t::PRIV_FLAGS_USES_PMEM; |
| 213 | } |
| 214 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 215 | if (usage & GRALLOC_USAGE_HW_2D) { |
| 216 | flags |= private_handle_t::PRIV_FLAGS_USES_PMEM; |
| 217 | } |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 218 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 219 | if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) { |
| 220 | fd = ashmem_create_region("Buffer", size); |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 221 | if (fd < 0) { |
| 222 | err = -errno; |
| 223 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 224 | } else { |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 225 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 226 | dev->common.module); |
| 227 | |
| 228 | pthread_mutex_lock(&m->lock); |
| 229 | if (m->pmem_master == -1) |
| 230 | err = init_pmem_area(m); |
| 231 | pthread_mutex_unlock(&m->lock); |
| 232 | |
| 233 | if (m->pmem_master >= 0) { |
| 234 | // PMEM buffers are always mmapped |
| 235 | base = m->pmem_master_base; |
| 236 | lockState |= private_handle_t::LOCK_STATE_MAPPED; |
| 237 | |
| 238 | offset = sAllocator.allocate(size); |
| 239 | if (offset < 0) { |
| 240 | err = -ENOMEM; |
| 241 | } else { |
| 242 | fd = open("/dev/pmem", O_RDWR, 0); |
| 243 | err = ioctl(fd, PMEM_CONNECT, m->pmem_master); |
| 244 | if (err < 0) { |
| 245 | err = -errno; |
| 246 | } else { |
| 247 | struct pmem_region sub = { offset, size }; |
| 248 | err = ioctl(fd, PMEM_MAP, &sub); |
| 249 | } |
| 250 | if (err < 0) { |
| 251 | close(fd); |
| 252 | sAllocator.deallocate(offset); |
| 253 | fd = -1; |
| 254 | } |
Mathias Agopian | ed93e8b | 2009-06-16 18:22:45 -0700 | [diff] [blame^] | 255 | //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset); |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 258 | } |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 259 | |
| 260 | if (err == 0) { |
| 261 | private_handle_t* hnd = new private_handle_t(fd, size, flags); |
| 262 | hnd->offset = offset; |
| 263 | hnd->base = int(base)+offset; |
| 264 | hnd->lockState = lockState; |
| 265 | *pHandle = hnd; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 266 | } |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 267 | |
| 268 | LOGE_IF(err, "gralloc failed err=%s", strerror(-err)); |
| 269 | |
| 270 | return err; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /*****************************************************************************/ |
| 274 | |
| 275 | static int gralloc_alloc(alloc_device_t* dev, |
| 276 | int w, int h, int format, int usage, |
| 277 | buffer_handle_t* pHandle, int* pStride) |
| 278 | { |
| 279 | if (!pHandle || !pStride) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | int align = 4; |
| 283 | int bpp = 0; |
| 284 | switch (format) { |
| 285 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 286 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 287 | bpp = 4; |
| 288 | break; |
| 289 | case HAL_PIXEL_FORMAT_RGB_565: |
| 290 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 291 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 292 | bpp = 2; |
| 293 | break; |
| 294 | default: |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | size_t bpr = (w*bpp + (align-1)) & ~(align-1); |
| 299 | size_t size = bpr * h; |
| 300 | size_t stride = bpr / bpp; |
| 301 | |
| 302 | int err; |
| 303 | if (usage & GRALLOC_USAGE_HW_FB) { |
| 304 | err = gralloc_alloc_framebuffer(dev, size, usage, pHandle); |
| 305 | } else { |
| 306 | err = gralloc_alloc_buffer(dev, size, usage, pHandle); |
| 307 | } |
| 308 | if (err < 0) { |
| 309 | return err; |
| 310 | } |
| 311 | |
| 312 | *pStride = stride; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int gralloc_free(alloc_device_t* dev, |
| 317 | buffer_handle_t handle) |
| 318 | { |
| 319 | if (private_handle_t::validate(handle) < 0) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 323 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
| 324 | // free this buffer |
| 325 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 326 | dev->common.module); |
| 327 | const size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 328 | int index = (hnd->base - m->framebuffer->base) / bufferSize; |
| 329 | m->bufferMask &= ~(1<<index); |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 330 | } else if (true || hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) { |
| 331 | if (hnd->fd >= 0) { |
| 332 | sAllocator.deallocate(hnd->offset); |
| 333 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 334 | } |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 335 | |
| 336 | gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>( |
| 337 | dev->common.module); |
| 338 | gralloc_unregister_buffer(m, handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 339 | |
| 340 | close(hnd->fd); |
| 341 | delete hnd; |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /*****************************************************************************/ |
| 346 | |
| 347 | static int gralloc_close(struct hw_device_t *dev) |
| 348 | { |
| 349 | gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev); |
| 350 | if (ctx) { |
| 351 | /* TODO: keep a list of all buffer_handle_t created, and free them |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 352 | * all here. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 353 | */ |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 354 | free(ctx); |
| 355 | } |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | int gralloc_device_open(const hw_module_t* module, const char* name, |
| 360 | hw_device_t** device) |
| 361 | { |
| 362 | int status = -EINVAL; |
| 363 | if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
| 364 | gralloc_context_t *dev; |
| 365 | dev = (gralloc_context_t*)malloc(sizeof(*dev)); |
| 366 | |
| 367 | /* initialize our state here */ |
| 368 | memset(dev, 0, sizeof(*dev)); |
| 369 | |
| 370 | /* initialize the procs */ |
| 371 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 372 | dev->device.common.version = 0; |
| 373 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 374 | dev->device.common.close = gralloc_close; |
| 375 | |
| 376 | dev->device.alloc = gralloc_alloc; |
| 377 | dev->device.free = gralloc_free; |
| 378 | |
| 379 | *device = &dev->device.common; |
| 380 | status = 0; |
| 381 | } else { |
| 382 | status = fb_device_open(module, name, device); |
| 383 | } |
| 384 | return status; |
| 385 | } |