blob: 9928a75e604804eebf5c2a613a4f19fc7c48088b [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;
182 int master_fd = open("/dev/pmem", O_RDWR, 0);
183 if (master_fd >= 0) {
Mathias Agopian3e1f89b2009-07-16 18:04:54 -0700184
185 size_t size;
186 pmem_region region;
187 if (ioctl(master_fd, PMEM_GET_TOTAL_SIZE, &region) < 0) {
188 LOGE("PMEM_GET_TOTAL_SIZE failed, limp mode");
189 size = 8<<20; // 8 MiB
190 } else {
191 size = region.len;
192 }
193 sAllocator.setSize(size);
194
195 void* base = mmap(0, size,
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700196 PROT_READ|PROT_WRITE, MAP_SHARED, master_fd, 0);
197 if (base == MAP_FAILED) {
198 err = -errno;
199 base = 0;
200 close(master_fd);
201 master_fd = -1;
202 }
203 m->pmem_master = master_fd;
204 m->pmem_master_base = base;
205 } else {
206 err = -errno;
207 }
208 return err;
209}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700210
Mathias Agopian14784232009-07-02 17:32:15 -0700211static int init_pmem_area(private_module_t* m)
212{
Mathias Agopian14784232009-07-02 17:32:15 -0700213 pthread_mutex_lock(&m->lock);
Mathias Agopian3d6ddfe2009-07-06 20:19:16 -0700214 int err = m->pmem_master;
215 if (err == -1) {
Mathias Agopian85ce19a2009-07-07 16:30:42 -0700216 // first time, try to initialize pmem
Mathias Agopian14784232009-07-02 17:32:15 -0700217 err = init_pmem_area_locked(m);
218 if (err) {
219 m->pmem_master = err;
220 }
Mathias Agopian85ce19a2009-07-07 16:30:42 -0700221 } else if (err < 0) {
222 // pmem couldn't be initialized, never use it
223 } else {
224 // pmem OK
225 err = 0;
Mathias Agopian14784232009-07-02 17:32:15 -0700226 }
227 pthread_mutex_unlock(&m->lock);
228 return err;
229}
230
Mathias Agopiana8a75162009-04-10 14:24:31 -0700231static int gralloc_alloc_buffer(alloc_device_t* dev,
232 size_t size, int usage, buffer_handle_t* pHandle)
233{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700234 int err = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700235 int flags = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700236
237 int fd = -1;
238 void* base = 0;
239 int offset = 0;
240 int lockState = 0;
241
242 size = roundUpToPageSize(size);
243
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700244#if HAVE_ANDROID_OS // should probably define HAVE_PMEM somewhere
245
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700246 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
247 // enable pmem in that case, so our software GL can fallback to
248 // the copybit module.
249 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
250 }
251
Mathias Agopiana8a75162009-04-10 14:24:31 -0700252 if (usage & GRALLOC_USAGE_HW_2D) {
253 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
254 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700255
Mathias Agopiana8a75162009-04-10 14:24:31 -0700256 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700257try_ashmem:
Mathias Agopian440d4e42009-07-07 16:36:31 -0700258 fd = ashmem_create_region("gralloc-buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700259 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700260 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700261 err = -errno;
262 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700263 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700264 private_module_t* m = reinterpret_cast<private_module_t*>(
265 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700266
Mathias Agopian14784232009-07-02 17:32:15 -0700267 err = init_pmem_area(m);
268 if (err == 0) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700269 // PMEM buffers are always mmapped
270 base = m->pmem_master_base;
271 lockState |= private_handle_t::LOCK_STATE_MAPPED;
272
273 offset = sAllocator.allocate(size);
274 if (offset < 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700275 // no more pmem memory
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700276 err = -ENOMEM;
277 } else {
Mathias Agopian14784232009-07-02 17:32:15 -0700278 struct pmem_region sub = { offset, size };
279
280 // now create the "sub-heap"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700281 fd = open("/dev/pmem", O_RDWR, 0);
Mathias Agopian14784232009-07-02 17:32:15 -0700282 err = fd < 0 ? fd : 0;
283
284 // and connect to it
285 if (err == 0)
286 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
287
288 // and make it available to the client process
289 if (err == 0)
290 err = ioctl(fd, PMEM_MAP, &sub);
291
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700292 if (err < 0) {
293 err = -errno;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700294 close(fd);
295 sAllocator.deallocate(offset);
296 fd = -1;
297 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700298 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700299 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700300 } else {
301 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
302 // the caller didn't request PMEM, so we can try something else
303 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
304 err = 0;
305 goto try_ashmem;
306 } else {
307 LOGE("couldn't open pmem (%s)", strerror(-errno));
308 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700309 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700310 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700311
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700312#else // HAVE_ANDROID_OS
313
314 fd = ashmem_create_region("Buffer", size);
315 if (fd < 0) {
316 LOGE("couldn't create ashmem (%s)", strerror(-errno));
317 err = -errno;
318 }
319
320#endif // HAVE_ANDROID_OS
321
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700322 if (err == 0) {
323 private_handle_t* hnd = new private_handle_t(fd, size, flags);
324 hnd->offset = offset;
325 hnd->base = int(base)+offset;
326 hnd->lockState = lockState;
327 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700328 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700329
330 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
331
332 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700333}
334
335/*****************************************************************************/
336
337static int gralloc_alloc(alloc_device_t* dev,
338 int w, int h, int format, int usage,
339 buffer_handle_t* pHandle, int* pStride)
340{
341 if (!pHandle || !pStride)
342 return -EINVAL;
343
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700344 size_t size, stride;
345 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
346 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
347 {
348 // FIXME: there is no way to return the vstride
349 int vstride;
350 stride = (w + 1) & ~1;
351 switch (format) {
352 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
353 size = stride * h * 2;
354 break;
355 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
356 vstride = (h+1) & ~1;
357 size = (stride * vstride) + (w/2 * h/2) * 2;
358 break;
359 default:
360 return -EINVAL;
361 }
362 } else {
363 int align = 4;
364 int bpp = 0;
365 switch (format) {
366 case HAL_PIXEL_FORMAT_RGBA_8888:
367 case HAL_PIXEL_FORMAT_BGRA_8888:
368 bpp = 4;
369 break;
370 case HAL_PIXEL_FORMAT_RGB_565:
371 case HAL_PIXEL_FORMAT_RGBA_5551:
372 case HAL_PIXEL_FORMAT_RGBA_4444:
373 bpp = 2;
374 break;
375 default:
376 return -EINVAL;
377 }
378 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
379 size = bpr * h;
380 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700381 }
382
Mathias Agopiana8a75162009-04-10 14:24:31 -0700383 int err;
384 if (usage & GRALLOC_USAGE_HW_FB) {
385 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
386 } else {
387 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
388 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700389
Mathias Agopiana8a75162009-04-10 14:24:31 -0700390 if (err < 0) {
391 return err;
392 }
393
394 *pStride = stride;
395 return 0;
396}
397
398static int gralloc_free(alloc_device_t* dev,
399 buffer_handle_t handle)
400{
401 if (private_handle_t::validate(handle) < 0)
402 return -EINVAL;
403
404 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700405 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700406 // free this buffer
407 private_module_t* m = reinterpret_cast<private_module_t*>(
408 dev->common.module);
409 const size_t bufferSize = m->finfo.line_length * m->info.yres;
410 int index = (hnd->base - m->framebuffer->base) / bufferSize;
411 m->bufferMask &= ~(1<<index);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700412 } else {
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700413#if HAVE_ANDROID_OS
Mathias Agopianbd80b382009-07-07 17:53:43 -0700414 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) {
415 if (hnd->fd >= 0) {
416 struct pmem_region sub = { hnd->offset, hnd->size };
417 int err = ioctl(hnd->fd, PMEM_UNMAP, &sub);
418 LOGE_IF(err<0, "PMEM_UNMAP failed (%s), "
419 "fd=%d, sub.offset=%lu, sub.size=%lu",
420 strerror(errno), hnd->fd, hnd->offset, hnd->size);
421 if (err == 0) {
422 // we can't deallocate the memory in case of UNMAP failure
423 // because it would give that process access to someone else's
424 // surfaces, which would be a security breach.
425 sAllocator.deallocate(hnd->offset);
426 }
Mathias Agopian14784232009-07-02 17:32:15 -0700427 }
428 }
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700429#endif // HAVE_ANDROID_OS
Mathias Agopianbd80b382009-07-07 17:53:43 -0700430 gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
431 dev->common.module);
432 terminateBuffer(module, const_cast<private_handle_t*>(hnd));
433 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700434
Mathias Agopiana8a75162009-04-10 14:24:31 -0700435 close(hnd->fd);
436 delete hnd;
437 return 0;
438}
439
440/*****************************************************************************/
441
442static int gralloc_close(struct hw_device_t *dev)
443{
444 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
445 if (ctx) {
446 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700447 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700448 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700449 free(ctx);
450 }
451 return 0;
452}
453
454int gralloc_device_open(const hw_module_t* module, const char* name,
455 hw_device_t** device)
456{
457 int status = -EINVAL;
458 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
459 gralloc_context_t *dev;
460 dev = (gralloc_context_t*)malloc(sizeof(*dev));
461
462 /* initialize our state here */
463 memset(dev, 0, sizeof(*dev));
464
465 /* initialize the procs */
466 dev->device.common.tag = HARDWARE_DEVICE_TAG;
467 dev->device.common.version = 0;
468 dev->device.common.module = const_cast<hw_module_t*>(module);
469 dev->device.common.close = gralloc_close;
470
471 dev->device.alloc = gralloc_alloc;
472 dev->device.free = gralloc_free;
473
474 *device = &dev->device.common;
475 status = 0;
476 } else {
477 status = fb_device_open(module, name, device);
478 }
479 return status;
480}