blob: 8c496dcb34089bdccdb7c14247030187007541bc [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
46struct gralloc_context_t {
47 alloc_device_t device;
48 /* our private data here */
49};
50
51static int gralloc_alloc_buffer(alloc_device_t* dev,
52 size_t size, int usage, buffer_handle_t* pHandle);
53
54/*****************************************************************************/
55
56int fb_device_open(const hw_module_t* module, const char* name,
57 hw_device_t** device);
58
59static int gralloc_device_open(const hw_module_t* module, const char* name,
60 hw_device_t** device);
61
Mathias Agopiana8a75162009-04-10 14:24:31 -070062extern int gralloc_lock(gralloc_module_t const* module,
63 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070064 int l, int t, int w, int h,
65 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -070066
67extern int gralloc_unlock(gralloc_module_t const* module,
68 buffer_handle_t handle);
69
Mathias Agopian988b8bd2009-05-04 14:26:56 -070070extern int gralloc_register_buffer(gralloc_module_t const* module,
71 buffer_handle_t handle);
72
73extern int gralloc_unregister_buffer(gralloc_module_t const* module,
74 buffer_handle_t handle);
75
Mathias Agopiana8a75162009-04-10 14:24:31 -070076/*****************************************************************************/
77
78static struct hw_module_methods_t gralloc_module_methods = {
79 open: gralloc_device_open
80};
81
82struct private_module_t HAL_MODULE_INFO_SYM = {
83 base: {
84 common: {
85 tag: HARDWARE_MODULE_TAG,
86 version_major: 1,
87 version_minor: 0,
88 id: GRALLOC_HARDWARE_MODULE_ID,
89 name: "Graphics Memory Allocator Module",
90 author: "The Android Open Source Project",
91 methods: &gralloc_module_methods
92 },
Mathias Agopian988b8bd2009-05-04 14:26:56 -070093 registerBuffer: gralloc_register_buffer,
94 unregisterBuffer: gralloc_unregister_buffer,
Mathias Agopiana8a75162009-04-10 14:24:31 -070095 lock: gralloc_lock,
96 unlock: gralloc_unlock,
97 },
98 framebuffer: 0,
99 flags: 0,
100 numBuffers: 0,
101 bufferMask: 0,
102 lock: PTHREAD_MUTEX_INITIALIZER,
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700103 currentBuffer: 0,
104 pmem_master: -1,
105 pmem_master_base: 0
Mathias Agopiana8a75162009-04-10 14:24:31 -0700106};
107
108/*****************************************************************************/
109
Mathias Agopiana8a75162009-04-10 14:24:31 -0700110static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
111 size_t size, int usage, buffer_handle_t* pHandle)
112{
113 private_module_t* m = reinterpret_cast<private_module_t*>(
114 dev->common.module);
115
116 // allocate the framebuffer
117 if (m->framebuffer == NULL) {
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700118 // initialize the framebuffer, the framebuffer is mapped once
119 // and forever.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700120 int err = mapFrameBufferLocked(m);
121 if (err < 0) {
122 return err;
123 }
124 }
125
Mathias Agopiana8a75162009-04-10 14:24:31 -0700126 const uint32_t bufferMask = m->bufferMask;
127 const uint32_t numBuffers = m->numBuffers;
128 const size_t bufferSize = m->finfo.line_length * m->info.yres;
129 if (numBuffers == 1) {
130 // If we have only one buffer, we never use page-flipping. Instead,
131 // we return a regular buffer which will be memcpy'ed to the main
132 // screen when post is called.
133 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
134 return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
135 }
136
137 if (bufferMask >= ((1LU<<numBuffers)-1)) {
138 // We ran out of buffers.
139 return -ENOMEM;
140 }
141
142 // create a "fake" handles for it
143 intptr_t vaddr = intptr_t(m->framebuffer->base);
144 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size,
145 private_handle_t::PRIV_FLAGS_USES_PMEM |
146 private_handle_t::PRIV_FLAGS_FRAMEBUFFER);
147
148 // find a free slot
149 for (uint32_t i=0 ; i<numBuffers ; i++) {
150 if ((bufferMask & (1LU<<i)) == 0) {
151 m->bufferMask |= (1LU<<i);
152 break;
153 }
154 vaddr += bufferSize;
155 }
156
157 hnd->base = vaddr;
Mathias Agopian72c85082009-06-10 16:06:28 -0700158 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700159 *pHandle = hnd;
160
161 return 0;
162}
163
164static 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
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700175static SimpleBestFitAllocator sAllocator(8*1024*1024);
176
Mathias Agopian14784232009-07-02 17:32:15 -0700177static int init_pmem_area_locked(private_module_t* m)
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700178{
179 int err = 0;
180 int master_fd = open("/dev/pmem", O_RDWR, 0);
181 if (master_fd >= 0) {
182 void* base = mmap(0, sAllocator.size(),
183 PROT_READ|PROT_WRITE, MAP_SHARED, master_fd, 0);
184 if (base == MAP_FAILED) {
185 err = -errno;
186 base = 0;
187 close(master_fd);
188 master_fd = -1;
189 }
190 m->pmem_master = master_fd;
191 m->pmem_master_base = base;
192 } else {
193 err = -errno;
194 }
195 return err;
196}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700197
Mathias Agopian14784232009-07-02 17:32:15 -0700198static int init_pmem_area(private_module_t* m)
199{
Mathias Agopian14784232009-07-02 17:32:15 -0700200 pthread_mutex_lock(&m->lock);
Mathias Agopian3d6ddfe2009-07-06 20:19:16 -0700201 int err = m->pmem_master;
202 if (err == -1) {
Mathias Agopian85ce19a2009-07-07 16:30:42 -0700203 // first time, try to initialize pmem
Mathias Agopian14784232009-07-02 17:32:15 -0700204 err = init_pmem_area_locked(m);
205 if (err) {
206 m->pmem_master = err;
207 }
Mathias Agopian85ce19a2009-07-07 16:30:42 -0700208 } else if (err < 0) {
209 // pmem couldn't be initialized, never use it
210 } else {
211 // pmem OK
212 err = 0;
Mathias Agopian14784232009-07-02 17:32:15 -0700213 }
214 pthread_mutex_unlock(&m->lock);
215 return err;
216}
217
Mathias Agopiana8a75162009-04-10 14:24:31 -0700218static int gralloc_alloc_buffer(alloc_device_t* dev,
219 size_t size, int usage, buffer_handle_t* pHandle)
220{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700221 int err = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700222 int flags = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700223
224 int fd = -1;
225 void* base = 0;
226 int offset = 0;
227 int lockState = 0;
228
229 size = roundUpToPageSize(size);
230
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700231#if HAVE_ANDROID_OS // should probably define HAVE_PMEM somewhere
232
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700233 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
234 // enable pmem in that case, so our software GL can fallback to
235 // the copybit module.
236 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
237 }
238
Mathias Agopiana8a75162009-04-10 14:24:31 -0700239 if (usage & GRALLOC_USAGE_HW_2D) {
240 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
241 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700242
Mathias Agopiana8a75162009-04-10 14:24:31 -0700243 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700244try_ashmem:
Mathias Agopian440d4e42009-07-07 16:36:31 -0700245 fd = ashmem_create_region("gralloc-buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700246 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700247 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700248 err = -errno;
249 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700250 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700251 private_module_t* m = reinterpret_cast<private_module_t*>(
252 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700253
Mathias Agopian14784232009-07-02 17:32:15 -0700254 err = init_pmem_area(m);
255 if (err == 0) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700256 // PMEM buffers are always mmapped
257 base = m->pmem_master_base;
258 lockState |= private_handle_t::LOCK_STATE_MAPPED;
259
260 offset = sAllocator.allocate(size);
261 if (offset < 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700262 // no more pmem memory
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700263 err = -ENOMEM;
264 } else {
Mathias Agopian14784232009-07-02 17:32:15 -0700265 struct pmem_region sub = { offset, size };
266
267 // now create the "sub-heap"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700268 fd = open("/dev/pmem", O_RDWR, 0);
Mathias Agopian14784232009-07-02 17:32:15 -0700269 err = fd < 0 ? fd : 0;
270
271 // and connect to it
272 if (err == 0)
273 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
274
275 // and make it available to the client process
276 if (err == 0)
277 err = ioctl(fd, PMEM_MAP, &sub);
278
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700279 if (err < 0) {
280 err = -errno;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700281 close(fd);
282 sAllocator.deallocate(offset);
283 fd = -1;
284 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700285 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700286 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700287 } else {
288 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
289 // the caller didn't request PMEM, so we can try something else
290 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
291 err = 0;
292 goto try_ashmem;
293 } else {
294 LOGE("couldn't open pmem (%s)", strerror(-errno));
295 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700296 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700297 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700298
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700299#else // HAVE_ANDROID_OS
300
301 fd = ashmem_create_region("Buffer", size);
302 if (fd < 0) {
303 LOGE("couldn't create ashmem (%s)", strerror(-errno));
304 err = -errno;
305 }
306
307#endif // HAVE_ANDROID_OS
308
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700309 if (err == 0) {
310 private_handle_t* hnd = new private_handle_t(fd, size, flags);
311 hnd->offset = offset;
312 hnd->base = int(base)+offset;
313 hnd->lockState = lockState;
314 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700315 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700316
317 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
318
319 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700320}
321
322/*****************************************************************************/
323
324static int gralloc_alloc(alloc_device_t* dev,
325 int w, int h, int format, int usage,
326 buffer_handle_t* pHandle, int* pStride)
327{
328 if (!pHandle || !pStride)
329 return -EINVAL;
330
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700331 size_t size, stride;
332 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
333 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
334 {
335 // FIXME: there is no way to return the vstride
336 int vstride;
337 stride = (w + 1) & ~1;
338 switch (format) {
339 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
340 size = stride * h * 2;
341 break;
342 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
343 vstride = (h+1) & ~1;
344 size = (stride * vstride) + (w/2 * h/2) * 2;
345 break;
346 default:
347 return -EINVAL;
348 }
349 } else {
350 int align = 4;
351 int bpp = 0;
352 switch (format) {
353 case HAL_PIXEL_FORMAT_RGBA_8888:
354 case HAL_PIXEL_FORMAT_BGRA_8888:
355 bpp = 4;
356 break;
357 case HAL_PIXEL_FORMAT_RGB_565:
358 case HAL_PIXEL_FORMAT_RGBA_5551:
359 case HAL_PIXEL_FORMAT_RGBA_4444:
360 bpp = 2;
361 break;
362 default:
363 return -EINVAL;
364 }
365 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
366 size = bpr * h;
367 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700368 }
369
Mathias Agopiana8a75162009-04-10 14:24:31 -0700370 int err;
371 if (usage & GRALLOC_USAGE_HW_FB) {
372 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
373 } else {
374 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
375 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700376
Mathias Agopiana8a75162009-04-10 14:24:31 -0700377 if (err < 0) {
378 return err;
379 }
380
381 *pStride = stride;
382 return 0;
383}
384
385static int gralloc_free(alloc_device_t* dev,
386 buffer_handle_t handle)
387{
388 if (private_handle_t::validate(handle) < 0)
389 return -EINVAL;
390
391 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700392 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700393 // free this buffer
394 private_module_t* m = reinterpret_cast<private_module_t*>(
395 dev->common.module);
396 const size_t bufferSize = m->finfo.line_length * m->info.yres;
397 int index = (hnd->base - m->framebuffer->base) / bufferSize;
398 m->bufferMask &= ~(1<<index);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700399 } else {
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700400#if HAVE_ANDROID_OS
Mathias Agopianbd80b382009-07-07 17:53:43 -0700401 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) {
402 if (hnd->fd >= 0) {
403 struct pmem_region sub = { hnd->offset, hnd->size };
404 int err = ioctl(hnd->fd, PMEM_UNMAP, &sub);
405 LOGE_IF(err<0, "PMEM_UNMAP failed (%s), "
406 "fd=%d, sub.offset=%lu, sub.size=%lu",
407 strerror(errno), hnd->fd, hnd->offset, hnd->size);
408 if (err == 0) {
409 // we can't deallocate the memory in case of UNMAP failure
410 // because it would give that process access to someone else's
411 // surfaces, which would be a security breach.
412 sAllocator.deallocate(hnd->offset);
413 }
Mathias Agopian14784232009-07-02 17:32:15 -0700414 }
415 }
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700416#endif // HAVE_ANDROID_OS
Mathias Agopianbd80b382009-07-07 17:53:43 -0700417 gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
418 dev->common.module);
419 terminateBuffer(module, const_cast<private_handle_t*>(hnd));
420 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700421
Mathias Agopiana8a75162009-04-10 14:24:31 -0700422 close(hnd->fd);
423 delete hnd;
424 return 0;
425}
426
427/*****************************************************************************/
428
429static int gralloc_close(struct hw_device_t *dev)
430{
431 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
432 if (ctx) {
433 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700434 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700435 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700436 free(ctx);
437 }
438 return 0;
439}
440
441int gralloc_device_open(const hw_module_t* module, const char* name,
442 hw_device_t** device)
443{
444 int status = -EINVAL;
445 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
446 gralloc_context_t *dev;
447 dev = (gralloc_context_t*)malloc(sizeof(*dev));
448
449 /* initialize our state here */
450 memset(dev, 0, sizeof(*dev));
451
452 /* initialize the procs */
453 dev->device.common.tag = HARDWARE_DEVICE_TAG;
454 dev->device.common.version = 0;
455 dev->device.common.module = const_cast<hw_module_t*>(module);
456 dev->device.common.close = gralloc_close;
457
458 dev->device.alloc = gralloc_alloc;
459 dev->device.free = gralloc_free;
460
461 *device = &dev->device.common;
462 status = 0;
463 } else {
464 status = fb_device_open(module, name, device);
465 }
466 return status;
467}