blob: 44e42d8778acc0d4a72eebe5151bf5cc3bdc1ae0 [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 Agopian14784232009-07-02 17:32:15 -0700203 err = init_pmem_area_locked(m);
204 if (err) {
205 m->pmem_master = err;
206 }
207 }
208 pthread_mutex_unlock(&m->lock);
209 return err;
210}
211
Mathias Agopiana8a75162009-04-10 14:24:31 -0700212static int gralloc_alloc_buffer(alloc_device_t* dev,
213 size_t size, int usage, buffer_handle_t* pHandle)
214{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700215 int err = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700216 int flags = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700217
218 int fd = -1;
219 void* base = 0;
220 int offset = 0;
221 int lockState = 0;
222
223 size = roundUpToPageSize(size);
224
225 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
226 // enable pmem in that case, so our software GL can fallback to
227 // the copybit module.
228 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
229 }
230
Mathias Agopiana8a75162009-04-10 14:24:31 -0700231 if (usage & GRALLOC_USAGE_HW_2D) {
232 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
233 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700234
Mathias Agopiana8a75162009-04-10 14:24:31 -0700235 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700236try_ashmem:
Mathias Agopiana8a75162009-04-10 14:24:31 -0700237 fd = ashmem_create_region("Buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700238 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700239 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700240 err = -errno;
241 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700242 } else {
Marco Nelissena4b587c2009-07-07 09:29:00 -0700243#ifndef HAVE_ANDROID_OS // should probably define HAVE_PMEM somewhere
244 LOGE("pmem not available on this target");
245 err = -1;
246#else
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700247 private_module_t* m = reinterpret_cast<private_module_t*>(
248 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700249
Mathias Agopian14784232009-07-02 17:32:15 -0700250 err = init_pmem_area(m);
251 if (err == 0) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700252 // PMEM buffers are always mmapped
253 base = m->pmem_master_base;
254 lockState |= private_handle_t::LOCK_STATE_MAPPED;
255
256 offset = sAllocator.allocate(size);
257 if (offset < 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700258 // no more pmem memory
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700259 err = -ENOMEM;
260 } else {
Mathias Agopian14784232009-07-02 17:32:15 -0700261 struct pmem_region sub = { offset, size };
262
263 // now create the "sub-heap"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700264 fd = open("/dev/pmem", O_RDWR, 0);
Mathias Agopian14784232009-07-02 17:32:15 -0700265 err = fd < 0 ? fd : 0;
266
267 // and connect to it
268 if (err == 0)
269 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
270
271 // and make it available to the client process
272 if (err == 0)
273 err = ioctl(fd, PMEM_MAP, &sub);
274
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700275 if (err < 0) {
276 err = -errno;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700277 close(fd);
278 sAllocator.deallocate(offset);
279 fd = -1;
280 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700281 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700282 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700283 } else {
284 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
285 // the caller didn't request PMEM, so we can try something else
286 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
287 err = 0;
288 goto try_ashmem;
289 } else {
290 LOGE("couldn't open pmem (%s)", strerror(-errno));
291 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700292 }
Marco Nelissena4b587c2009-07-07 09:29:00 -0700293#endif // HAVE_ANDROID_OS
Mathias Agopiana8a75162009-04-10 14:24:31 -0700294 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700295
296 if (err == 0) {
297 private_handle_t* hnd = new private_handle_t(fd, size, flags);
298 hnd->offset = offset;
299 hnd->base = int(base)+offset;
300 hnd->lockState = lockState;
301 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700302 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700303
304 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
305
306 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700307}
308
309/*****************************************************************************/
310
311static int gralloc_alloc(alloc_device_t* dev,
312 int w, int h, int format, int usage,
313 buffer_handle_t* pHandle, int* pStride)
314{
315 if (!pHandle || !pStride)
316 return -EINVAL;
317
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700318 size_t size, stride;
319 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
320 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
321 {
322 // FIXME: there is no way to return the vstride
323 int vstride;
324 stride = (w + 1) & ~1;
325 switch (format) {
326 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
327 size = stride * h * 2;
328 break;
329 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
330 vstride = (h+1) & ~1;
331 size = (stride * vstride) + (w/2 * h/2) * 2;
332 break;
333 default:
334 return -EINVAL;
335 }
336 } else {
337 int align = 4;
338 int bpp = 0;
339 switch (format) {
340 case HAL_PIXEL_FORMAT_RGBA_8888:
341 case HAL_PIXEL_FORMAT_BGRA_8888:
342 bpp = 4;
343 break;
344 case HAL_PIXEL_FORMAT_RGB_565:
345 case HAL_PIXEL_FORMAT_RGBA_5551:
346 case HAL_PIXEL_FORMAT_RGBA_4444:
347 bpp = 2;
348 break;
349 default:
350 return -EINVAL;
351 }
352 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
353 size = bpr * h;
354 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700355 }
356
Mathias Agopiana8a75162009-04-10 14:24:31 -0700357 int err;
358 if (usage & GRALLOC_USAGE_HW_FB) {
359 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
360 } else {
361 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
362 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700363
Mathias Agopiana8a75162009-04-10 14:24:31 -0700364 if (err < 0) {
365 return err;
366 }
367
368 *pStride = stride;
369 return 0;
370}
371
372static int gralloc_free(alloc_device_t* dev,
373 buffer_handle_t handle)
374{
375 if (private_handle_t::validate(handle) < 0)
376 return -EINVAL;
377
378 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopian14784232009-07-02 17:32:15 -0700379 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
380 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700381 // free this buffer
382 private_module_t* m = reinterpret_cast<private_module_t*>(
383 dev->common.module);
384 const size_t bufferSize = m->finfo.line_length * m->info.yres;
385 int index = (hnd->base - m->framebuffer->base) / bufferSize;
386 m->bufferMask &= ~(1<<index);
Mathias Agopian14784232009-07-02 17:32:15 -0700387 }
388 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM)
389 {
Marco Nelissena4b587c2009-07-07 09:29:00 -0700390#ifndef HAVE_ANDROID_OS
391 LOGE("pmem not available on this target");
392 return -EINVAL;
393#else
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700394 if (hnd->fd >= 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700395 struct pmem_region sub = { hnd->offset, hnd->size };
396 int err = ioctl(hnd->fd, PMEM_UNMAP, &sub);
397 LOGE_IF(err<0, "PMEM_UNMAP failed (%s), "
398 "fd=%d, sub.offset=%lu, sub.size=%lu",
399 strerror(errno), hnd->fd, hnd->offset, hnd->size);
400 if (err == 0) {
401 // we can't deallocate the memory in case of UNMAP failure
402 // because it would give that process access to someone else's
403 // surfaces, which would be a security breach.
404 sAllocator.deallocate(hnd->offset);
405 }
406 }
Marco Nelissena4b587c2009-07-07 09:29:00 -0700407#endif // HAVE_ANDROID_OS
Mathias Agopiana8a75162009-04-10 14:24:31 -0700408 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700409
410 gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>(
411 dev->common.module);
412 gralloc_unregister_buffer(m, handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700413
414 close(hnd->fd);
415 delete hnd;
416 return 0;
417}
418
419/*****************************************************************************/
420
421static int gralloc_close(struct hw_device_t *dev)
422{
423 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
424 if (ctx) {
425 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700426 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700427 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700428 free(ctx);
429 }
430 return 0;
431}
432
433int gralloc_device_open(const hw_module_t* module, const char* name,
434 hw_device_t** device)
435{
436 int status = -EINVAL;
437 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
438 gralloc_context_t *dev;
439 dev = (gralloc_context_t*)malloc(sizeof(*dev));
440
441 /* initialize our state here */
442 memset(dev, 0, sizeof(*dev));
443
444 /* initialize the procs */
445 dev->device.common.tag = HARDWARE_DEVICE_TAG;
446 dev->device.common.version = 0;
447 dev->device.common.module = const_cast<hw_module_t*>(module);
448 dev->device.common.close = gralloc_close;
449
450 dev->device.alloc = gralloc_alloc;
451 dev->device.free = gralloc_free;
452
453 *device = &dev->device.common;
454 status = 0;
455 } else {
456 status = fb_device_open(module, name, device);
457 }
458 return status;
459}