blob: 54aa64c2406b2237273397ec0b19aa2f9dac9055 [file] [log] [blame]
Mathias Agopiana8a75162009-04-10 14:24:31 -07001/*
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 Agopian8c4ab1f2009-06-11 16:32:05 -070018#include <unistd.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070019#include <fcntl.h>
20#include <errno.h>
21#include <pthread.h>
Marco Nelissena4b587c2009-07-07 09:29:00 -070022#include <stdlib.h>
23#include <string.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070024
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070025#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 Agopiana8a75162009-04-10 14:24:31 -070031#include <cutils/log.h>
32#include <cutils/atomic.h>
33
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070034#include <hardware/hardware.h>
35#include <hardware/gralloc.h>
36
Mathias Agopiana8a75162009-04-10 14:24:31 -070037#include "gralloc_priv.h"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070038#include "allocator.h"
39
40#if HAVE_ANDROID_OS
41#include <linux/android_pmem.h>
42#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -070043
44/*****************************************************************************/
45
Mathias Agopian3e1f89b2009-07-16 18:04:54 -070046static SimpleBestFitAllocator sAllocator;
47
48/*****************************************************************************/
49
Mathias Agopiana8a75162009-04-10 14:24:31 -070050struct gralloc_context_t {
51 alloc_device_t device;
52 /* our private data here */
53};
54
55static int gralloc_alloc_buffer(alloc_device_t* dev,
56 size_t size, int usage, buffer_handle_t* pHandle);
57
58/*****************************************************************************/
59
60int fb_device_open(const hw_module_t* module, const char* name,
61 hw_device_t** device);
62
63static int gralloc_device_open(const hw_module_t* module, const char* name,
64 hw_device_t** device);
65
Mathias Agopiana8a75162009-04-10 14:24:31 -070066extern int gralloc_lock(gralloc_module_t const* module,
67 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070068 int l, int t, int w, int h,
69 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -070070
71extern int gralloc_unlock(gralloc_module_t const* module,
72 buffer_handle_t handle);
73
Mathias Agopian988b8bd2009-05-04 14:26:56 -070074extern int gralloc_register_buffer(gralloc_module_t const* module,
75 buffer_handle_t handle);
76
77extern int gralloc_unregister_buffer(gralloc_module_t const* module,
78 buffer_handle_t handle);
79
Mathias Agopiana8a75162009-04-10 14:24:31 -070080/*****************************************************************************/
81
82static struct hw_module_methods_t gralloc_module_methods = {
83 open: gralloc_device_open
84};
85
86struct private_module_t HAL_MODULE_INFO_SYM = {
87 base: {
88 common: {
89 tag: HARDWARE_MODULE_TAG,
90 version_major: 1,
91 version_minor: 0,
92 id: GRALLOC_HARDWARE_MODULE_ID,
93 name: "Graphics Memory Allocator Module",
94 author: "The Android Open Source Project",
95 methods: &gralloc_module_methods
96 },
Mathias Agopian988b8bd2009-05-04 14:26:56 -070097 registerBuffer: gralloc_register_buffer,
98 unregisterBuffer: gralloc_unregister_buffer,
Mathias Agopiana8a75162009-04-10 14:24:31 -070099 lock: gralloc_lock,
100 unlock: gralloc_unlock,
101 },
102 framebuffer: 0,
103 flags: 0,
104 numBuffers: 0,
105 bufferMask: 0,
106 lock: PTHREAD_MUTEX_INITIALIZER,
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700107 currentBuffer: 0,
108 pmem_master: -1,
109 pmem_master_base: 0
Mathias Agopiana8a75162009-04-10 14:24:31 -0700110};
111
112/*****************************************************************************/
113
Mathias Agopiana8a75162009-04-10 14:24:31 -0700114static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
115 size_t size, int usage, buffer_handle_t* pHandle)
116{
117 private_module_t* m = reinterpret_cast<private_module_t*>(
118 dev->common.module);
119
120 // allocate the framebuffer
121 if (m->framebuffer == NULL) {
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700122 // initialize the framebuffer, the framebuffer is mapped once
123 // and forever.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700124 int err = mapFrameBufferLocked(m);
125 if (err < 0) {
126 return err;
127 }
128 }
129
Mathias Agopiana8a75162009-04-10 14:24:31 -0700130 const uint32_t bufferMask = m->bufferMask;
131 const uint32_t numBuffers = m->numBuffers;
132 const size_t bufferSize = m->finfo.line_length * m->info.yres;
133 if (numBuffers == 1) {
134 // If we have only one buffer, we never use page-flipping. Instead,
135 // we return a regular buffer which will be memcpy'ed to the main
136 // screen when post is called.
137 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
138 return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
139 }
140
141 if (bufferMask >= ((1LU<<numBuffers)-1)) {
142 // We ran out of buffers.
143 return -ENOMEM;
144 }
145
146 // create a "fake" handles for it
147 intptr_t vaddr = intptr_t(m->framebuffer->base);
148 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size,
149 private_handle_t::PRIV_FLAGS_USES_PMEM |
150 private_handle_t::PRIV_FLAGS_FRAMEBUFFER);
151
152 // find a free slot
153 for (uint32_t i=0 ; i<numBuffers ; i++) {
154 if ((bufferMask & (1LU<<i)) == 0) {
155 m->bufferMask |= (1LU<<i);
156 break;
157 }
158 vaddr += bufferSize;
159 }
160
161 hnd->base = vaddr;
Mathias Agopian72c85082009-06-10 16:06:28 -0700162 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700163 *pHandle = hnd;
164
165 return 0;
166}
167
168static int gralloc_alloc_framebuffer(alloc_device_t* dev,
169 size_t size, int usage, buffer_handle_t* pHandle)
170{
171 private_module_t* m = reinterpret_cast<private_module_t*>(
172 dev->common.module);
173 pthread_mutex_lock(&m->lock);
174 int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle);
175 pthread_mutex_unlock(&m->lock);
176 return err;
177}
178
Mathias Agopian14784232009-07-02 17:32:15 -0700179static int init_pmem_area_locked(private_module_t* m)
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700180{
181 int err = 0;
Marco Nelissen15c9d3a2009-07-17 11:14:53 -0700182#if HAVE_ANDROID_OS // should probably define HAVE_PMEM somewhere
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700183 int master_fd = open("/dev/pmem", O_RDWR, 0);
184 if (master_fd >= 0) {
Mathias Agopian3e1f89b2009-07-16 18:04:54 -0700185
186 size_t size;
187 pmem_region region;
188 if (ioctl(master_fd, PMEM_GET_TOTAL_SIZE, &region) < 0) {
189 LOGE("PMEM_GET_TOTAL_SIZE failed, limp mode");
190 size = 8<<20; // 8 MiB
191 } else {
192 size = region.len;
193 }
194 sAllocator.setSize(size);
195
196 void* base = mmap(0, size,
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700197 PROT_READ|PROT_WRITE, MAP_SHARED, master_fd, 0);
198 if (base == MAP_FAILED) {
199 err = -errno;
200 base = 0;
201 close(master_fd);
202 master_fd = -1;
203 }
204 m->pmem_master = master_fd;
205 m->pmem_master_base = base;
206 } else {
207 err = -errno;
208 }
209 return err;
Marco Nelissen15c9d3a2009-07-17 11:14:53 -0700210#else
211 return -1;
212#endif
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700213}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700214
Mathias Agopian14784232009-07-02 17:32:15 -0700215static int init_pmem_area(private_module_t* m)
216{
Mathias Agopian14784232009-07-02 17:32:15 -0700217 pthread_mutex_lock(&m->lock);
Mathias Agopian3d6ddfe2009-07-06 20:19:16 -0700218 int err = m->pmem_master;
219 if (err == -1) {
Mathias Agopian85ce19a2009-07-07 16:30:42 -0700220 // first time, try to initialize pmem
Mathias Agopian14784232009-07-02 17:32:15 -0700221 err = init_pmem_area_locked(m);
222 if (err) {
223 m->pmem_master = err;
224 }
Mathias Agopian85ce19a2009-07-07 16:30:42 -0700225 } else if (err < 0) {
226 // pmem couldn't be initialized, never use it
227 } else {
228 // pmem OK
229 err = 0;
Mathias Agopian14784232009-07-02 17:32:15 -0700230 }
231 pthread_mutex_unlock(&m->lock);
232 return err;
233}
234
Mathias Agopiana8a75162009-04-10 14:24:31 -0700235static int gralloc_alloc_buffer(alloc_device_t* dev,
236 size_t size, int usage, buffer_handle_t* pHandle)
237{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700238 int err = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700239 int flags = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700240
241 int fd = -1;
242 void* base = 0;
243 int offset = 0;
244 int lockState = 0;
245
246 size = roundUpToPageSize(size);
247
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700248#if HAVE_ANDROID_OS // should probably define HAVE_PMEM somewhere
249
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700250 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
251 // enable pmem in that case, so our software GL can fallback to
252 // the copybit module.
253 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
254 }
255
Mathias Agopiana8a75162009-04-10 14:24:31 -0700256 if (usage & GRALLOC_USAGE_HW_2D) {
257 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
258 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700259
Mathias Agopiana8a75162009-04-10 14:24:31 -0700260 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700261try_ashmem:
Mathias Agopian440d4e42009-07-07 16:36:31 -0700262 fd = ashmem_create_region("gralloc-buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700263 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700264 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700265 err = -errno;
266 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700267 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700268 private_module_t* m = reinterpret_cast<private_module_t*>(
269 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700270
Mathias Agopian14784232009-07-02 17:32:15 -0700271 err = init_pmem_area(m);
272 if (err == 0) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700273 // PMEM buffers are always mmapped
274 base = m->pmem_master_base;
275 lockState |= private_handle_t::LOCK_STATE_MAPPED;
276
277 offset = sAllocator.allocate(size);
278 if (offset < 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700279 // no more pmem memory
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700280 err = -ENOMEM;
281 } else {
Mathias Agopian14784232009-07-02 17:32:15 -0700282 struct pmem_region sub = { offset, size };
283
284 // now create the "sub-heap"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700285 fd = open("/dev/pmem", O_RDWR, 0);
Mathias Agopian14784232009-07-02 17:32:15 -0700286 err = fd < 0 ? fd : 0;
287
288 // and connect to it
289 if (err == 0)
290 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
291
292 // and make it available to the client process
293 if (err == 0)
294 err = ioctl(fd, PMEM_MAP, &sub);
295
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700296 if (err < 0) {
297 err = -errno;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700298 close(fd);
299 sAllocator.deallocate(offset);
300 fd = -1;
301 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700302 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700303 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700304 } else {
305 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
306 // the caller didn't request PMEM, so we can try something else
307 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
308 err = 0;
309 goto try_ashmem;
310 } else {
311 LOGE("couldn't open pmem (%s)", strerror(-errno));
312 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700313 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700314 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700315
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700316#else // HAVE_ANDROID_OS
317
318 fd = ashmem_create_region("Buffer", size);
319 if (fd < 0) {
320 LOGE("couldn't create ashmem (%s)", strerror(-errno));
321 err = -errno;
322 }
323
324#endif // HAVE_ANDROID_OS
325
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700326 if (err == 0) {
327 private_handle_t* hnd = new private_handle_t(fd, size, flags);
328 hnd->offset = offset;
329 hnd->base = int(base)+offset;
330 hnd->lockState = lockState;
331 *pHandle = hnd;
Mathias Agopiane83629a2009-08-07 18:13:47 -0700332 memset((void*)hnd->base, 0, size);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700333 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700334
335 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
336
337 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700338}
339
340/*****************************************************************************/
341
342static int gralloc_alloc(alloc_device_t* dev,
343 int w, int h, int format, int usage,
344 buffer_handle_t* pHandle, int* pStride)
345{
346 if (!pHandle || !pStride)
347 return -EINVAL;
348
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700349 size_t size, stride;
350 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
351 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
352 {
353 // FIXME: there is no way to return the vstride
354 int vstride;
355 stride = (w + 1) & ~1;
356 switch (format) {
357 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
358 size = stride * h * 2;
359 break;
360 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
361 vstride = (h+1) & ~1;
362 size = (stride * vstride) + (w/2 * h/2) * 2;
363 break;
364 default:
365 return -EINVAL;
366 }
367 } else {
368 int align = 4;
369 int bpp = 0;
370 switch (format) {
371 case HAL_PIXEL_FORMAT_RGBA_8888:
372 case HAL_PIXEL_FORMAT_BGRA_8888:
373 bpp = 4;
374 break;
375 case HAL_PIXEL_FORMAT_RGB_565:
376 case HAL_PIXEL_FORMAT_RGBA_5551:
377 case HAL_PIXEL_FORMAT_RGBA_4444:
378 bpp = 2;
379 break;
380 default:
381 return -EINVAL;
382 }
383 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
384 size = bpr * h;
385 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700386 }
387
Mathias Agopiana8a75162009-04-10 14:24:31 -0700388 int err;
389 if (usage & GRALLOC_USAGE_HW_FB) {
390 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
391 } else {
392 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
393 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700394
Mathias Agopiana8a75162009-04-10 14:24:31 -0700395 if (err < 0) {
396 return err;
397 }
398
399 *pStride = stride;
400 return 0;
401}
402
403static int gralloc_free(alloc_device_t* dev,
404 buffer_handle_t handle)
405{
406 if (private_handle_t::validate(handle) < 0)
407 return -EINVAL;
408
409 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700410 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700411 // free this buffer
412 private_module_t* m = reinterpret_cast<private_module_t*>(
413 dev->common.module);
414 const size_t bufferSize = m->finfo.line_length * m->info.yres;
415 int index = (hnd->base - m->framebuffer->base) / bufferSize;
416 m->bufferMask &= ~(1<<index);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700417 } else {
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700418#if HAVE_ANDROID_OS
Mathias Agopianbd80b382009-07-07 17:53:43 -0700419 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) {
420 if (hnd->fd >= 0) {
421 struct pmem_region sub = { hnd->offset, hnd->size };
422 int err = ioctl(hnd->fd, PMEM_UNMAP, &sub);
423 LOGE_IF(err<0, "PMEM_UNMAP failed (%s), "
424 "fd=%d, sub.offset=%lu, sub.size=%lu",
425 strerror(errno), hnd->fd, hnd->offset, hnd->size);
426 if (err == 0) {
427 // we can't deallocate the memory in case of UNMAP failure
428 // because it would give that process access to someone else's
429 // surfaces, which would be a security breach.
430 sAllocator.deallocate(hnd->offset);
431 }
Mathias Agopian14784232009-07-02 17:32:15 -0700432 }
433 }
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700434#endif // HAVE_ANDROID_OS
Mathias Agopianbd80b382009-07-07 17:53:43 -0700435 gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
436 dev->common.module);
437 terminateBuffer(module, const_cast<private_handle_t*>(hnd));
438 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700439
Mathias Agopiana8a75162009-04-10 14:24:31 -0700440 close(hnd->fd);
441 delete hnd;
442 return 0;
443}
444
445/*****************************************************************************/
446
447static int gralloc_close(struct hw_device_t *dev)
448{
449 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
450 if (ctx) {
451 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700452 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700453 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700454 free(ctx);
455 }
456 return 0;
457}
458
459int gralloc_device_open(const hw_module_t* module, const char* name,
460 hw_device_t** device)
461{
462 int status = -EINVAL;
463 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
464 gralloc_context_t *dev;
465 dev = (gralloc_context_t*)malloc(sizeof(*dev));
466
467 /* initialize our state here */
468 memset(dev, 0, sizeof(*dev));
469
470 /* initialize the procs */
471 dev->device.common.tag = HARDWARE_DEVICE_TAG;
472 dev->device.common.version = 0;
473 dev->device.common.module = const_cast<hw_module_t*>(module);
474 dev->device.common.close = gralloc_close;
475
476 dev->device.alloc = gralloc_alloc;
477 dev->device.free = gralloc_free;
478
479 *device = &dev->device.common;
480 status = 0;
481 } else {
482 status = fb_device_open(module, name, device);
483 }
484 return status;
485}