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> |
| 18 | |
| 19 | #include <sys/mman.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <cutils/ashmem.h> |
| 24 | #include <cutils/log.h> |
| 25 | |
| 26 | #include <hardware/hardware.h> |
| 27 | #include <hardware/gralloc.h> |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | #include <errno.h> |
| 31 | #include <pthread.h> |
| 32 | |
| 33 | #include <cutils/log.h> |
| 34 | #include <cutils/atomic.h> |
| 35 | |
| 36 | #include "gralloc_priv.h" |
| 37 | |
| 38 | /*****************************************************************************/ |
| 39 | |
| 40 | struct gralloc_context_t { |
| 41 | alloc_device_t device; |
| 42 | /* our private data here */ |
| 43 | }; |
| 44 | |
| 45 | static int gralloc_alloc_buffer(alloc_device_t* dev, |
| 46 | size_t size, int usage, buffer_handle_t* pHandle); |
| 47 | |
| 48 | /*****************************************************************************/ |
| 49 | |
| 50 | int fb_device_open(const hw_module_t* module, const char* name, |
| 51 | hw_device_t** device); |
| 52 | |
| 53 | static int gralloc_device_open(const hw_module_t* module, const char* name, |
| 54 | hw_device_t** device); |
| 55 | |
| 56 | extern int gralloc_map(gralloc_module_t const* module, |
| 57 | buffer_handle_t handle, void** vaddr); |
| 58 | |
| 59 | extern int gralloc_unmap(gralloc_module_t const* module, |
| 60 | buffer_handle_t handle); |
| 61 | |
| 62 | extern int gralloc_lock(gralloc_module_t const* module, |
| 63 | buffer_handle_t handle, int usage, |
| 64 | int l, int t, int w, int h); |
| 65 | |
| 66 | extern int gralloc_unlock(gralloc_module_t const* module, |
| 67 | buffer_handle_t handle); |
| 68 | |
| 69 | /*****************************************************************************/ |
| 70 | |
| 71 | static struct hw_module_methods_t gralloc_module_methods = { |
| 72 | open: gralloc_device_open |
| 73 | }; |
| 74 | |
| 75 | struct private_module_t HAL_MODULE_INFO_SYM = { |
| 76 | base: { |
| 77 | common: { |
| 78 | tag: HARDWARE_MODULE_TAG, |
| 79 | version_major: 1, |
| 80 | version_minor: 0, |
| 81 | id: GRALLOC_HARDWARE_MODULE_ID, |
| 82 | name: "Graphics Memory Allocator Module", |
| 83 | author: "The Android Open Source Project", |
| 84 | methods: &gralloc_module_methods |
| 85 | }, |
| 86 | map: gralloc_map, |
| 87 | unmap: gralloc_unmap, |
| 88 | lock: gralloc_lock, |
| 89 | unlock: gralloc_unlock, |
| 90 | }, |
| 91 | framebuffer: 0, |
| 92 | flags: 0, |
| 93 | numBuffers: 0, |
| 94 | bufferMask: 0, |
| 95 | lock: PTHREAD_MUTEX_INITIALIZER, |
| 96 | currentBuffer: 0 |
| 97 | }; |
| 98 | |
| 99 | /*****************************************************************************/ |
| 100 | |
| 101 | /* |
| 102 | * This function creates a buffer_handle_t initialized with the given fd. |
| 103 | * the offset passed in parameter is used to mmap() this fd later at this |
| 104 | * offset. |
| 105 | */ |
| 106 | |
| 107 | static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, |
| 108 | size_t size, int usage, buffer_handle_t* pHandle) |
| 109 | { |
| 110 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 111 | dev->common.module); |
| 112 | |
| 113 | // allocate the framebuffer |
| 114 | if (m->framebuffer == NULL) { |
| 115 | // initialize the framebuffer |
| 116 | int err = mapFrameBufferLocked(m); |
| 117 | if (err < 0) { |
| 118 | return err; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (m->framebuffer->base == 0) { |
| 123 | void *vaddr; |
| 124 | m->base.map(&m->base, m->framebuffer, &vaddr); |
| 125 | } |
| 126 | |
| 127 | const uint32_t bufferMask = m->bufferMask; |
| 128 | const uint32_t numBuffers = m->numBuffers; |
| 129 | const size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 130 | if (numBuffers == 1) { |
| 131 | // If we have only one buffer, we never use page-flipping. Instead, |
| 132 | // we return a regular buffer which will be memcpy'ed to the main |
| 133 | // screen when post is called. |
| 134 | int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; |
| 135 | return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle); |
| 136 | } |
| 137 | |
| 138 | if (bufferMask >= ((1LU<<numBuffers)-1)) { |
| 139 | // We ran out of buffers. |
| 140 | return -ENOMEM; |
| 141 | } |
| 142 | |
| 143 | // create a "fake" handles for it |
| 144 | intptr_t vaddr = intptr_t(m->framebuffer->base); |
| 145 | private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size, |
| 146 | private_handle_t::PRIV_FLAGS_USES_PMEM | |
| 147 | private_handle_t::PRIV_FLAGS_FRAMEBUFFER); |
| 148 | |
| 149 | // find a free slot |
| 150 | for (uint32_t i=0 ; i<numBuffers ; i++) { |
| 151 | if ((bufferMask & (1LU<<i)) == 0) { |
| 152 | m->bufferMask |= (1LU<<i); |
| 153 | break; |
| 154 | } |
| 155 | vaddr += bufferSize; |
| 156 | } |
| 157 | |
| 158 | hnd->base = vaddr; |
| 159 | *pHandle = hnd; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int gralloc_alloc_framebuffer(alloc_device_t* dev, |
| 165 | size_t size, int usage, buffer_handle_t* pHandle) |
| 166 | { |
| 167 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 168 | dev->common.module); |
| 169 | pthread_mutex_lock(&m->lock); |
| 170 | int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle); |
| 171 | pthread_mutex_unlock(&m->lock); |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static int gralloc_alloc_buffer(alloc_device_t* dev, |
| 177 | size_t size, int usage, buffer_handle_t* pHandle) |
| 178 | { |
| 179 | size = roundUpToPageSize(size); |
| 180 | int flags = 0; |
| 181 | if (usage & GRALLOC_USAGE_HW_2D) { |
| 182 | flags |= private_handle_t::PRIV_FLAGS_USES_PMEM; |
| 183 | } |
| 184 | int fd; |
| 185 | if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) { |
| 186 | fd = ashmem_create_region("Buffer", size); |
| 187 | } else { |
| 188 | fd = open("/dev/pmem", O_RDWR, 0); |
| 189 | // Note: Currently pmem get sized when it is mmaped. |
| 190 | // This means that just doing the open doesn't actually allocate |
| 191 | // anything. We basically need to do an implicit "mmap" |
| 192 | // (under the hood) for pmem regions. However, the current |
| 193 | // code will work okay for now thanks to the reference-counting. |
| 194 | } |
| 195 | if (fd < 0) { |
| 196 | return -errno; |
| 197 | } |
| 198 | private_handle_t* hnd = new private_handle_t(fd, size, flags); |
| 199 | *pHandle = hnd; |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /*****************************************************************************/ |
| 204 | |
| 205 | static int gralloc_alloc(alloc_device_t* dev, |
| 206 | int w, int h, int format, int usage, |
| 207 | buffer_handle_t* pHandle, int* pStride) |
| 208 | { |
| 209 | if (!pHandle || !pStride) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | int align = 4; |
| 213 | int bpp = 0; |
| 214 | switch (format) { |
| 215 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 216 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 217 | bpp = 4; |
| 218 | break; |
| 219 | case HAL_PIXEL_FORMAT_RGB_565: |
| 220 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 221 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 222 | bpp = 2; |
| 223 | break; |
| 224 | default: |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | size_t bpr = (w*bpp + (align-1)) & ~(align-1); |
| 229 | size_t size = bpr * h; |
| 230 | size_t stride = bpr / bpp; |
| 231 | |
| 232 | int err; |
| 233 | if (usage & GRALLOC_USAGE_HW_FB) { |
| 234 | err = gralloc_alloc_framebuffer(dev, size, usage, pHandle); |
| 235 | } else { |
| 236 | err = gralloc_alloc_buffer(dev, size, usage, pHandle); |
| 237 | } |
| 238 | if (err < 0) { |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | *pStride = stride; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int gralloc_free(alloc_device_t* dev, |
| 247 | buffer_handle_t handle) |
| 248 | { |
| 249 | if (private_handle_t::validate(handle) < 0) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 253 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
| 254 | // free this buffer |
| 255 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 256 | dev->common.module); |
| 257 | const size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 258 | int index = (hnd->base - m->framebuffer->base) / bufferSize; |
| 259 | m->bufferMask &= ~(1<<index); |
| 260 | } |
| 261 | |
| 262 | if (hnd->base) { |
Mathias Agopian | c3ce41d | 2009-04-14 18:24:09 -0700 | [diff] [blame] | 263 | LOGW("handle %p still mapped at %p", hnd, hnd->base); |
| 264 | //gralloc_unmap((gralloc_module_t*) dev->common.module, handle); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | close(hnd->fd); |
| 268 | delete hnd; |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /*****************************************************************************/ |
| 273 | |
| 274 | static int gralloc_close(struct hw_device_t *dev) |
| 275 | { |
| 276 | gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev); |
| 277 | if (ctx) { |
| 278 | /* TODO: keep a list of all buffer_handle_t created, and free them |
| 279 | * all here |
| 280 | */ |
| 281 | |
| 282 | private_module_t* m = reinterpret_cast<private_module_t*>(dev->module); |
| 283 | pthread_mutex_lock(&m->lock); |
| 284 | if (m->framebuffer) { |
| 285 | m->base.unmap(&m->base, m->framebuffer); |
| 286 | } |
| 287 | pthread_mutex_unlock(&m->lock); |
| 288 | |
| 289 | free(ctx); |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | int gralloc_device_open(const hw_module_t* module, const char* name, |
| 295 | hw_device_t** device) |
| 296 | { |
| 297 | int status = -EINVAL; |
| 298 | if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
| 299 | gralloc_context_t *dev; |
| 300 | dev = (gralloc_context_t*)malloc(sizeof(*dev)); |
| 301 | |
| 302 | /* initialize our state here */ |
| 303 | memset(dev, 0, sizeof(*dev)); |
| 304 | |
| 305 | /* initialize the procs */ |
| 306 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 307 | dev->device.common.version = 0; |
| 308 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 309 | dev->device.common.close = gralloc_close; |
| 310 | |
| 311 | dev->device.alloc = gralloc_alloc; |
| 312 | dev->device.free = gralloc_free; |
| 313 | |
| 314 | *device = &dev->device.common; |
| 315 | status = 0; |
| 316 | } else { |
| 317 | status = fb_device_open(module, name, device); |
| 318 | } |
| 319 | return status; |
| 320 | } |