| 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> | 
| Marco Nelissen | a4b587c | 2009-07-07 09:29:00 -0700 | [diff] [blame] | 22 | #include <stdlib.h> | 
|  | 23 | #include <string.h> | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 24 |  | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 25 | #include <sys/mman.h> | 
|  | 26 | #include <sys/stat.h> | 
|  | 27 | #include <sys/types.h> | 
|  | 28 | #include <sys/ioctl.h> | 
|  | 29 |  | 
|  | 30 | #include <cutils/ashmem.h> | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 31 | #include <cutils/log.h> | 
|  | 32 | #include <cutils/atomic.h> | 
|  | 33 |  | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 34 | #include <hardware/hardware.h> | 
|  | 35 | #include <hardware/gralloc.h> | 
|  | 36 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 37 | #include "gralloc_priv.h" | 
| Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 38 | #include "gr.h" | 
| Mathias Agopian | 3e1f89b | 2009-07-16 18:04:54 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | /*****************************************************************************/ | 
|  | 41 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 42 | struct gralloc_context_t { | 
|  | 43 | alloc_device_t  device; | 
|  | 44 | /* our private data here */ | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | static int gralloc_alloc_buffer(alloc_device_t* dev, | 
|  | 48 | size_t size, int usage, buffer_handle_t* pHandle); | 
|  | 49 |  | 
|  | 50 | /*****************************************************************************/ | 
|  | 51 |  | 
|  | 52 | int fb_device_open(const hw_module_t* module, const char* name, | 
|  | 53 | hw_device_t** device); | 
|  | 54 |  | 
|  | 55 | static int gralloc_device_open(const hw_module_t* module, const char* name, | 
|  | 56 | hw_device_t** device); | 
|  | 57 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 58 | extern int gralloc_lock(gralloc_module_t const* module, | 
|  | 59 | buffer_handle_t handle, int usage, | 
| Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 60 | int l, int t, int w, int h, | 
|  | 61 | void** vaddr); | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 62 |  | 
|  | 63 | extern int gralloc_unlock(gralloc_module_t const* module, | 
|  | 64 | buffer_handle_t handle); | 
|  | 65 |  | 
| Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 66 | extern int gralloc_register_buffer(gralloc_module_t const* module, | 
|  | 67 | buffer_handle_t handle); | 
|  | 68 |  | 
|  | 69 | extern int gralloc_unregister_buffer(gralloc_module_t const* module, | 
|  | 70 | buffer_handle_t handle); | 
|  | 71 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 72 | /*****************************************************************************/ | 
|  | 73 |  | 
|  | 74 | static struct hw_module_methods_t gralloc_module_methods = { | 
|  | 75 | open: gralloc_device_open | 
|  | 76 | }; | 
|  | 77 |  | 
|  | 78 | struct private_module_t HAL_MODULE_INFO_SYM = { | 
|  | 79 | base: { | 
|  | 80 | common: { | 
|  | 81 | tag: HARDWARE_MODULE_TAG, | 
|  | 82 | version_major: 1, | 
|  | 83 | version_minor: 0, | 
|  | 84 | id: GRALLOC_HARDWARE_MODULE_ID, | 
|  | 85 | name: "Graphics Memory Allocator Module", | 
|  | 86 | author: "The Android Open Source Project", | 
|  | 87 | methods: &gralloc_module_methods | 
|  | 88 | }, | 
| Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 89 | registerBuffer: gralloc_register_buffer, | 
|  | 90 | unregisterBuffer: gralloc_unregister_buffer, | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 91 | lock: gralloc_lock, | 
|  | 92 | unlock: gralloc_unlock, | 
|  | 93 | }, | 
|  | 94 | framebuffer: 0, | 
|  | 95 | flags: 0, | 
|  | 96 | numBuffers: 0, | 
|  | 97 | bufferMask: 0, | 
|  | 98 | lock: PTHREAD_MUTEX_INITIALIZER, | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 99 | currentBuffer: 0, | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 100 | }; | 
|  | 101 |  | 
|  | 102 | /*****************************************************************************/ | 
|  | 103 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 104 | static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, | 
|  | 105 | size_t size, int usage, buffer_handle_t* pHandle) | 
|  | 106 | { | 
|  | 107 | private_module_t* m = reinterpret_cast<private_module_t*>( | 
|  | 108 | dev->common.module); | 
|  | 109 |  | 
|  | 110 | // allocate the framebuffer | 
|  | 111 | if (m->framebuffer == NULL) { | 
| Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 112 | // initialize the framebuffer, the framebuffer is mapped once | 
|  | 113 | // and forever. | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 114 | int err = mapFrameBufferLocked(m); | 
|  | 115 | if (err < 0) { | 
|  | 116 | return err; | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 120 | const uint32_t bufferMask = m->bufferMask; | 
|  | 121 | const uint32_t numBuffers = m->numBuffers; | 
|  | 122 | const size_t bufferSize = m->finfo.line_length * m->info.yres; | 
|  | 123 | if (numBuffers == 1) { | 
|  | 124 | // If we have only one buffer, we never use page-flipping. Instead, | 
|  | 125 | // we return a regular buffer which will be memcpy'ed to the main | 
|  | 126 | // screen when post is called. | 
|  | 127 | int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; | 
|  | 128 | return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | if (bufferMask >= ((1LU<<numBuffers)-1)) { | 
|  | 132 | // We ran out of buffers. | 
|  | 133 | return -ENOMEM; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | // create a "fake" handles for it | 
|  | 137 | intptr_t vaddr = intptr_t(m->framebuffer->base); | 
|  | 138 | private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size, | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 139 | private_handle_t::PRIV_FLAGS_FRAMEBUFFER); | 
|  | 140 |  | 
|  | 141 | // find a free slot | 
|  | 142 | for (uint32_t i=0 ; i<numBuffers ; i++) { | 
|  | 143 | if ((bufferMask & (1LU<<i)) == 0) { | 
|  | 144 | m->bufferMask |= (1LU<<i); | 
|  | 145 | break; | 
|  | 146 | } | 
|  | 147 | vaddr += bufferSize; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | hnd->base = vaddr; | 
| Mathias Agopian | 72c8508 | 2009-06-10 16:06:28 -0700 | [diff] [blame] | 151 | hnd->offset = vaddr - intptr_t(m->framebuffer->base); | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 152 | *pHandle = hnd; | 
|  | 153 |  | 
|  | 154 | return 0; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | static int gralloc_alloc_framebuffer(alloc_device_t* dev, | 
|  | 158 | size_t size, int usage, buffer_handle_t* pHandle) | 
|  | 159 | { | 
|  | 160 | private_module_t* m = reinterpret_cast<private_module_t*>( | 
|  | 161 | dev->common.module); | 
|  | 162 | pthread_mutex_lock(&m->lock); | 
|  | 163 | int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle); | 
|  | 164 | pthread_mutex_unlock(&m->lock); | 
|  | 165 | return err; | 
|  | 166 | } | 
|  | 167 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 168 | static int gralloc_alloc_buffer(alloc_device_t* dev, | 
|  | 169 | size_t size, int usage, buffer_handle_t* pHandle) | 
|  | 170 | { | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 171 | int err = 0; | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 172 | int fd = -1; | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | size = roundUpToPageSize(size); | 
|  | 175 |  | 
| Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 176 | fd = ashmem_create_region("gralloc-buffer", size); | 
| Mathias Agopian | bfc010a | 2009-07-07 12:43:35 -0700 | [diff] [blame] | 177 | if (fd < 0) { | 
| Steve Block | 60d056b | 2012-01-08 10:17:53 +0000 | [diff] [blame] | 178 | ALOGE("couldn't create ashmem (%s)", strerror(-errno)); | 
| Mathias Agopian | bfc010a | 2009-07-07 12:43:35 -0700 | [diff] [blame] | 179 | err = -errno; | 
|  | 180 | } | 
|  | 181 |  | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 182 | if (err == 0) { | 
| Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 183 | private_handle_t* hnd = new private_handle_t(fd, size, 0); | 
|  | 184 | gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>( | 
|  | 185 | dev->common.module); | 
|  | 186 | err = mapBuffer(module, hnd); | 
|  | 187 | if (err == 0) { | 
|  | 188 | *pHandle = hnd; | 
|  | 189 | } | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 190 | } | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 191 |  | 
| Steve Block | 60d056b | 2012-01-08 10:17:53 +0000 | [diff] [blame] | 192 | ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); | 
| Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 193 |  | 
|  | 194 | return err; | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
|  | 197 | /*****************************************************************************/ | 
|  | 198 |  | 
|  | 199 | static int gralloc_alloc(alloc_device_t* dev, | 
|  | 200 | int w, int h, int format, int usage, | 
|  | 201 | buffer_handle_t* pHandle, int* pStride) | 
|  | 202 | { | 
|  | 203 | if (!pHandle || !pStride) | 
|  | 204 | return -EINVAL; | 
|  | 205 |  | 
| Mathias Agopian | 8bf1f75 | 2009-06-25 17:38:50 -0700 | [diff] [blame] | 206 | size_t size, stride; | 
| Mathias Agopian | 9da751b | 2010-02-16 14:04:00 -0800 | [diff] [blame] | 207 |  | 
|  | 208 | int align = 4; | 
|  | 209 | int bpp = 0; | 
|  | 210 | switch (format) { | 
|  | 211 | case HAL_PIXEL_FORMAT_RGBA_8888: | 
|  | 212 | case HAL_PIXEL_FORMAT_RGBX_8888: | 
|  | 213 | case HAL_PIXEL_FORMAT_BGRA_8888: | 
|  | 214 | bpp = 4; | 
|  | 215 | break; | 
|  | 216 | case HAL_PIXEL_FORMAT_RGB_888: | 
|  | 217 | bpp = 3; | 
|  | 218 | break; | 
|  | 219 | case HAL_PIXEL_FORMAT_RGB_565: | 
|  | 220 | case HAL_PIXEL_FORMAT_RGBA_5551: | 
|  | 221 | case HAL_PIXEL_FORMAT_RGBA_4444: | 
| Eino-Ville Talvala | 984272d | 2012-04-13 10:08:47 -0700 | [diff] [blame] | 222 | case HAL_PIXEL_FORMAT_RAW_SENSOR: | 
| Mathias Agopian | 9da751b | 2010-02-16 14:04:00 -0800 | [diff] [blame] | 223 | bpp = 2; | 
|  | 224 | break; | 
|  | 225 | default: | 
|  | 226 | return -EINVAL; | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 227 | } | 
| Mathias Agopian | 9da751b | 2010-02-16 14:04:00 -0800 | [diff] [blame] | 228 | size_t bpr = (w*bpp + (align-1)) & ~(align-1); | 
|  | 229 | size = bpr * h; | 
|  | 230 | stride = bpr / bpp; | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 231 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 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 | } | 
| Mathias Agopian | 8bf1f75 | 2009-06-25 17:38:50 -0700 | [diff] [blame] | 238 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 239 | if (err < 0) { | 
|  | 240 | return err; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | *pStride = stride; | 
|  | 244 | return 0; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | static int gralloc_free(alloc_device_t* dev, | 
|  | 248 | buffer_handle_t handle) | 
|  | 249 | { | 
|  | 250 | if (private_handle_t::validate(handle) < 0) | 
|  | 251 | return -EINVAL; | 
|  | 252 |  | 
|  | 253 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); | 
| Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 254 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 255 | // free this buffer | 
|  | 256 | private_module_t* m = reinterpret_cast<private_module_t*>( | 
|  | 257 | dev->common.module); | 
|  | 258 | const size_t bufferSize = m->finfo.line_length * m->info.yres; | 
|  | 259 | int index = (hnd->base - m->framebuffer->base) / bufferSize; | 
|  | 260 | m->bufferMask &= ~(1<<index); | 
| Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 261 | } else { | 
| Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 262 | gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>( | 
|  | 263 | dev->common.module); | 
|  | 264 | terminateBuffer(module, const_cast<private_handle_t*>(hnd)); | 
|  | 265 | } | 
| Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 266 |  | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 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 | 
| Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 279 | * all here. | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 280 | */ | 
| Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 281 | free(ctx); | 
|  | 282 | } | 
|  | 283 | return 0; | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | int gralloc_device_open(const hw_module_t* module, const char* name, | 
|  | 287 | hw_device_t** device) | 
|  | 288 | { | 
|  | 289 | int status = -EINVAL; | 
|  | 290 | if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { | 
|  | 291 | gralloc_context_t *dev; | 
|  | 292 | dev = (gralloc_context_t*)malloc(sizeof(*dev)); | 
|  | 293 |  | 
|  | 294 | /* initialize our state here */ | 
|  | 295 | memset(dev, 0, sizeof(*dev)); | 
|  | 296 |  | 
|  | 297 | /* initialize the procs */ | 
|  | 298 | dev->device.common.tag = HARDWARE_DEVICE_TAG; | 
|  | 299 | dev->device.common.version = 0; | 
|  | 300 | dev->device.common.module = const_cast<hw_module_t*>(module); | 
|  | 301 | dev->device.common.close = gralloc_close; | 
|  | 302 |  | 
|  | 303 | dev->device.alloc   = gralloc_alloc; | 
|  | 304 | dev->device.free    = gralloc_free; | 
|  | 305 |  | 
|  | 306 | *device = &dev->device.common; | 
|  | 307 | status = 0; | 
|  | 308 | } else { | 
|  | 309 | status = fb_device_open(module, name, device); | 
|  | 310 | } | 
|  | 311 | return status; | 
|  | 312 | } |