blob: 257bb4ae4eab164b1119f79a333cd88cafa779e6 [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
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700225#if HAVE_ANDROID_OS // should probably define HAVE_PMEM somewhere
226
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700227 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
228 // enable pmem in that case, so our software GL can fallback to
229 // the copybit module.
230 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
231 }
232
Mathias Agopiana8a75162009-04-10 14:24:31 -0700233 if (usage & GRALLOC_USAGE_HW_2D) {
234 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
235 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700236
Mathias Agopiana8a75162009-04-10 14:24:31 -0700237 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700238try_ashmem:
Mathias Agopiana8a75162009-04-10 14:24:31 -0700239 fd = ashmem_create_region("Buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700240 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700241 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700242 err = -errno;
243 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700244 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700245 private_module_t* m = reinterpret_cast<private_module_t*>(
246 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700247
Mathias Agopian14784232009-07-02 17:32:15 -0700248 err = init_pmem_area(m);
249 if (err == 0) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700250 // PMEM buffers are always mmapped
251 base = m->pmem_master_base;
252 lockState |= private_handle_t::LOCK_STATE_MAPPED;
253
254 offset = sAllocator.allocate(size);
255 if (offset < 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700256 // no more pmem memory
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700257 err = -ENOMEM;
258 } else {
Mathias Agopian14784232009-07-02 17:32:15 -0700259 struct pmem_region sub = { offset, size };
260
261 // now create the "sub-heap"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700262 fd = open("/dev/pmem", O_RDWR, 0);
Mathias Agopian14784232009-07-02 17:32:15 -0700263 err = fd < 0 ? fd : 0;
264
265 // and connect to it
266 if (err == 0)
267 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
268
269 // and make it available to the client process
270 if (err == 0)
271 err = ioctl(fd, PMEM_MAP, &sub);
272
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700273 if (err < 0) {
274 err = -errno;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700275 close(fd);
276 sAllocator.deallocate(offset);
277 fd = -1;
278 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700279 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700280 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700281 } else {
282 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
283 // the caller didn't request PMEM, so we can try something else
284 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
285 err = 0;
286 goto try_ashmem;
287 } else {
288 LOGE("couldn't open pmem (%s)", strerror(-errno));
289 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700290 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700291 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700292
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700293#else // HAVE_ANDROID_OS
294
295 fd = ashmem_create_region("Buffer", size);
296 if (fd < 0) {
297 LOGE("couldn't create ashmem (%s)", strerror(-errno));
298 err = -errno;
299 }
300
301#endif // HAVE_ANDROID_OS
302
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700303 if (err == 0) {
304 private_handle_t* hnd = new private_handle_t(fd, size, flags);
305 hnd->offset = offset;
306 hnd->base = int(base)+offset;
307 hnd->lockState = lockState;
308 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700309 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700310
311 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
312
313 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700314}
315
316/*****************************************************************************/
317
318static int gralloc_alloc(alloc_device_t* dev,
319 int w, int h, int format, int usage,
320 buffer_handle_t* pHandle, int* pStride)
321{
322 if (!pHandle || !pStride)
323 return -EINVAL;
324
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700325 size_t size, stride;
326 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
327 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
328 {
329 // FIXME: there is no way to return the vstride
330 int vstride;
331 stride = (w + 1) & ~1;
332 switch (format) {
333 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
334 size = stride * h * 2;
335 break;
336 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
337 vstride = (h+1) & ~1;
338 size = (stride * vstride) + (w/2 * h/2) * 2;
339 break;
340 default:
341 return -EINVAL;
342 }
343 } else {
344 int align = 4;
345 int bpp = 0;
346 switch (format) {
347 case HAL_PIXEL_FORMAT_RGBA_8888:
348 case HAL_PIXEL_FORMAT_BGRA_8888:
349 bpp = 4;
350 break;
351 case HAL_PIXEL_FORMAT_RGB_565:
352 case HAL_PIXEL_FORMAT_RGBA_5551:
353 case HAL_PIXEL_FORMAT_RGBA_4444:
354 bpp = 2;
355 break;
356 default:
357 return -EINVAL;
358 }
359 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
360 size = bpr * h;
361 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700362 }
363
Mathias Agopiana8a75162009-04-10 14:24:31 -0700364 int err;
365 if (usage & GRALLOC_USAGE_HW_FB) {
366 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
367 } else {
368 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
369 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700370
Mathias Agopiana8a75162009-04-10 14:24:31 -0700371 if (err < 0) {
372 return err;
373 }
374
375 *pStride = stride;
376 return 0;
377}
378
379static int gralloc_free(alloc_device_t* dev,
380 buffer_handle_t handle)
381{
382 if (private_handle_t::validate(handle) < 0)
383 return -EINVAL;
384
385 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopian14784232009-07-02 17:32:15 -0700386 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
387 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700388 // free this buffer
389 private_module_t* m = reinterpret_cast<private_module_t*>(
390 dev->common.module);
391 const size_t bufferSize = m->finfo.line_length * m->info.yres;
392 int index = (hnd->base - m->framebuffer->base) / bufferSize;
393 m->bufferMask &= ~(1<<index);
Mathias Agopian14784232009-07-02 17:32:15 -0700394 }
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700395#if HAVE_ANDROID_OS
Mathias Agopian14784232009-07-02 17:32:15 -0700396 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM)
397 {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700398 if (hnd->fd >= 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700399 struct pmem_region sub = { hnd->offset, hnd->size };
400 int err = ioctl(hnd->fd, PMEM_UNMAP, &sub);
401 LOGE_IF(err<0, "PMEM_UNMAP failed (%s), "
402 "fd=%d, sub.offset=%lu, sub.size=%lu",
403 strerror(errno), hnd->fd, hnd->offset, hnd->size);
404 if (err == 0) {
405 // we can't deallocate the memory in case of UNMAP failure
406 // because it would give that process access to someone else's
407 // surfaces, which would be a security breach.
408 sAllocator.deallocate(hnd->offset);
409 }
410 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700411 }
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700412#endif // HAVE_ANDROID_OS
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700413
414 gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>(
415 dev->common.module);
416 gralloc_unregister_buffer(m, handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700417
418 close(hnd->fd);
419 delete hnd;
420 return 0;
421}
422
423/*****************************************************************************/
424
425static int gralloc_close(struct hw_device_t *dev)
426{
427 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
428 if (ctx) {
429 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700430 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700431 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700432 free(ctx);
433 }
434 return 0;
435}
436
437int gralloc_device_open(const hw_module_t* module, const char* name,
438 hw_device_t** device)
439{
440 int status = -EINVAL;
441 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
442 gralloc_context_t *dev;
443 dev = (gralloc_context_t*)malloc(sizeof(*dev));
444
445 /* initialize our state here */
446 memset(dev, 0, sizeof(*dev));
447
448 /* initialize the procs */
449 dev->device.common.tag = HARDWARE_DEVICE_TAG;
450 dev->device.common.version = 0;
451 dev->device.common.module = const_cast<hw_module_t*>(module);
452 dev->device.common.close = gralloc_close;
453
454 dev->device.alloc = gralloc_alloc;
455 dev->device.free = gralloc_free;
456
457 *device = &dev->device.common;
458 status = 0;
459 } else {
460 status = fb_device_open(module, name, device);
461 }
462 return status;
463}