blob: 3441effd86b984ce165dfdc03f2aadd3cd05b975 [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>
22
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070023#include <sys/mman.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/ioctl.h>
27
28#include <cutils/ashmem.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070029#include <cutils/log.h>
30#include <cutils/atomic.h>
31
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070032#include <hardware/hardware.h>
33#include <hardware/gralloc.h>
34
Mathias Agopiana8a75162009-04-10 14:24:31 -070035#include "gralloc_priv.h"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070036#include "allocator.h"
37
38#if HAVE_ANDROID_OS
39#include <linux/android_pmem.h>
40#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -070041
42/*****************************************************************************/
43
44struct gralloc_context_t {
45 alloc_device_t device;
46 /* our private data here */
47};
48
49static int gralloc_alloc_buffer(alloc_device_t* dev,
50 size_t size, int usage, buffer_handle_t* pHandle);
51
52/*****************************************************************************/
53
54int fb_device_open(const hw_module_t* module, const char* name,
55 hw_device_t** device);
56
57static int gralloc_device_open(const hw_module_t* module, const char* name,
58 hw_device_t** device);
59
Mathias Agopiana8a75162009-04-10 14:24:31 -070060extern int gralloc_lock(gralloc_module_t const* module,
61 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070062 int l, int t, int w, int h,
63 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -070064
65extern int gralloc_unlock(gralloc_module_t const* module,
66 buffer_handle_t handle);
67
Mathias Agopian988b8bd2009-05-04 14:26:56 -070068extern int gralloc_register_buffer(gralloc_module_t const* module,
69 buffer_handle_t handle);
70
71extern int gralloc_unregister_buffer(gralloc_module_t const* module,
72 buffer_handle_t handle);
73
Mathias Agopiana8a75162009-04-10 14:24:31 -070074/*****************************************************************************/
75
76static struct hw_module_methods_t gralloc_module_methods = {
77 open: gralloc_device_open
78};
79
80struct private_module_t HAL_MODULE_INFO_SYM = {
81 base: {
82 common: {
83 tag: HARDWARE_MODULE_TAG,
84 version_major: 1,
85 version_minor: 0,
86 id: GRALLOC_HARDWARE_MODULE_ID,
87 name: "Graphics Memory Allocator Module",
88 author: "The Android Open Source Project",
89 methods: &gralloc_module_methods
90 },
Mathias Agopian988b8bd2009-05-04 14:26:56 -070091 registerBuffer: gralloc_register_buffer,
92 unregisterBuffer: gralloc_unregister_buffer,
Mathias Agopiana8a75162009-04-10 14:24:31 -070093 lock: gralloc_lock,
94 unlock: gralloc_unlock,
95 },
96 framebuffer: 0,
97 flags: 0,
98 numBuffers: 0,
99 bufferMask: 0,
100 lock: PTHREAD_MUTEX_INITIALIZER,
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700101 currentBuffer: 0,
102 pmem_master: -1,
103 pmem_master_base: 0
Mathias Agopiana8a75162009-04-10 14:24:31 -0700104};
105
106/*****************************************************************************/
107
Mathias Agopiana8a75162009-04-10 14:24:31 -0700108static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
109 size_t size, int usage, buffer_handle_t* pHandle)
110{
111 private_module_t* m = reinterpret_cast<private_module_t*>(
112 dev->common.module);
113
114 // allocate the framebuffer
115 if (m->framebuffer == NULL) {
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700116 // initialize the framebuffer, the framebuffer is mapped once
117 // and forever.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700118 int err = mapFrameBufferLocked(m);
119 if (err < 0) {
120 return err;
121 }
122 }
123
Mathias Agopiana8a75162009-04-10 14:24:31 -0700124 const uint32_t bufferMask = m->bufferMask;
125 const uint32_t numBuffers = m->numBuffers;
126 const size_t bufferSize = m->finfo.line_length * m->info.yres;
127 if (numBuffers == 1) {
128 // If we have only one buffer, we never use page-flipping. Instead,
129 // we return a regular buffer which will be memcpy'ed to the main
130 // screen when post is called.
131 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
132 return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
133 }
134
135 if (bufferMask >= ((1LU<<numBuffers)-1)) {
136 // We ran out of buffers.
137 return -ENOMEM;
138 }
139
140 // create a "fake" handles for it
141 intptr_t vaddr = intptr_t(m->framebuffer->base);
142 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size,
143 private_handle_t::PRIV_FLAGS_USES_PMEM |
144 private_handle_t::PRIV_FLAGS_FRAMEBUFFER);
145
146 // find a free slot
147 for (uint32_t i=0 ; i<numBuffers ; i++) {
148 if ((bufferMask & (1LU<<i)) == 0) {
149 m->bufferMask |= (1LU<<i);
150 break;
151 }
152 vaddr += bufferSize;
153 }
154
155 hnd->base = vaddr;
Mathias Agopian72c85082009-06-10 16:06:28 -0700156 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700157 *pHandle = hnd;
158
159 return 0;
160}
161
162static int gralloc_alloc_framebuffer(alloc_device_t* dev,
163 size_t size, int usage, buffer_handle_t* pHandle)
164{
165 private_module_t* m = reinterpret_cast<private_module_t*>(
166 dev->common.module);
167 pthread_mutex_lock(&m->lock);
168 int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle);
169 pthread_mutex_unlock(&m->lock);
170 return err;
171}
172
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700173static SimpleBestFitAllocator sAllocator(8*1024*1024);
174
175static int init_pmem_area(private_module_t* m)
176{
177 int err = 0;
178 int master_fd = open("/dev/pmem", O_RDWR, 0);
179 if (master_fd >= 0) {
180 void* base = mmap(0, sAllocator.size(),
181 PROT_READ|PROT_WRITE, MAP_SHARED, master_fd, 0);
182 if (base == MAP_FAILED) {
183 err = -errno;
184 base = 0;
185 close(master_fd);
186 master_fd = -1;
187 }
188 m->pmem_master = master_fd;
189 m->pmem_master_base = base;
190 } else {
191 err = -errno;
192 }
193 return err;
194}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700195
196static int gralloc_alloc_buffer(alloc_device_t* dev,
197 size_t size, int usage, buffer_handle_t* pHandle)
198{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700199 int err = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700200 int flags = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700201
202 int fd = -1;
203 void* base = 0;
204 int offset = 0;
205 int lockState = 0;
206
207 size = roundUpToPageSize(size);
208
209 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
210 // enable pmem in that case, so our software GL can fallback to
211 // the copybit module.
212 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
213 }
214
Mathias Agopiana8a75162009-04-10 14:24:31 -0700215 if (usage & GRALLOC_USAGE_HW_2D) {
216 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
217 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700218
Mathias Agopiana8a75162009-04-10 14:24:31 -0700219 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700220try_ashmem:
Mathias Agopiana8a75162009-04-10 14:24:31 -0700221 fd = ashmem_create_region("Buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700222 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700223 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700224 err = -errno;
225 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700226 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700227 private_module_t* m = reinterpret_cast<private_module_t*>(
228 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700229
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700230 pthread_mutex_lock(&m->lock);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700231 if (m->pmem_master == -1) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700232 err = init_pmem_area(m);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700233 if (err) {
234 m->pmem_master = err;
235 }
236 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700237 pthread_mutex_unlock(&m->lock);
238
239 if (m->pmem_master >= 0) {
240 // PMEM buffers are always mmapped
241 base = m->pmem_master_base;
242 lockState |= private_handle_t::LOCK_STATE_MAPPED;
243
244 offset = sAllocator.allocate(size);
245 if (offset < 0) {
246 err = -ENOMEM;
247 } else {
248 fd = open("/dev/pmem", O_RDWR, 0);
249 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
250 if (err < 0) {
251 err = -errno;
252 } else {
253 struct pmem_region sub = { offset, size };
254 err = ioctl(fd, PMEM_MAP, &sub);
255 }
256 if (err < 0) {
257 close(fd);
258 sAllocator.deallocate(offset);
259 fd = -1;
260 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700261 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700262 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700263 } else {
264 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
265 // the caller didn't request PMEM, so we can try something else
266 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
267 err = 0;
268 goto try_ashmem;
269 } else {
270 LOGE("couldn't open pmem (%s)", strerror(-errno));
271 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700272 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700273 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700274
275 if (err == 0) {
276 private_handle_t* hnd = new private_handle_t(fd, size, flags);
277 hnd->offset = offset;
278 hnd->base = int(base)+offset;
279 hnd->lockState = lockState;
280 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700281 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700282
283 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
284
285 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700286}
287
288/*****************************************************************************/
289
290static int gralloc_alloc(alloc_device_t* dev,
291 int w, int h, int format, int usage,
292 buffer_handle_t* pHandle, int* pStride)
293{
294 if (!pHandle || !pStride)
295 return -EINVAL;
296
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700297 size_t size, stride;
298 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
299 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
300 {
301 // FIXME: there is no way to return the vstride
302 int vstride;
303 stride = (w + 1) & ~1;
304 switch (format) {
305 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
306 size = stride * h * 2;
307 break;
308 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
309 vstride = (h+1) & ~1;
310 size = (stride * vstride) + (w/2 * h/2) * 2;
311 break;
312 default:
313 return -EINVAL;
314 }
315 } else {
316 int align = 4;
317 int bpp = 0;
318 switch (format) {
319 case HAL_PIXEL_FORMAT_RGBA_8888:
320 case HAL_PIXEL_FORMAT_BGRA_8888:
321 bpp = 4;
322 break;
323 case HAL_PIXEL_FORMAT_RGB_565:
324 case HAL_PIXEL_FORMAT_RGBA_5551:
325 case HAL_PIXEL_FORMAT_RGBA_4444:
326 bpp = 2;
327 break;
328 default:
329 return -EINVAL;
330 }
331 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
332 size = bpr * h;
333 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700334 }
335
Mathias Agopiana8a75162009-04-10 14:24:31 -0700336 int err;
337 if (usage & GRALLOC_USAGE_HW_FB) {
338 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
339 } else {
340 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
341 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700342
Mathias Agopiana8a75162009-04-10 14:24:31 -0700343 if (err < 0) {
344 return err;
345 }
346
347 *pStride = stride;
348 return 0;
349}
350
351static int gralloc_free(alloc_device_t* dev,
352 buffer_handle_t handle)
353{
354 if (private_handle_t::validate(handle) < 0)
355 return -EINVAL;
356
357 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
358 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
359 // free this buffer
360 private_module_t* m = reinterpret_cast<private_module_t*>(
361 dev->common.module);
362 const size_t bufferSize = m->finfo.line_length * m->info.yres;
363 int index = (hnd->base - m->framebuffer->base) / bufferSize;
364 m->bufferMask &= ~(1<<index);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700365 } else if (true || hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) {
366 if (hnd->fd >= 0) {
367 sAllocator.deallocate(hnd->offset);
368 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700369 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700370
371 gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>(
372 dev->common.module);
373 gralloc_unregister_buffer(m, handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700374
375 close(hnd->fd);
376 delete hnd;
377 return 0;
378}
379
380/*****************************************************************************/
381
382static int gralloc_close(struct hw_device_t *dev)
383{
384 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
385 if (ctx) {
386 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700387 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700388 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700389 free(ctx);
390 }
391 return 0;
392}
393
394int gralloc_device_open(const hw_module_t* module, const char* name,
395 hw_device_t** device)
396{
397 int status = -EINVAL;
398 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
399 gralloc_context_t *dev;
400 dev = (gralloc_context_t*)malloc(sizeof(*dev));
401
402 /* initialize our state here */
403 memset(dev, 0, sizeof(*dev));
404
405 /* initialize the procs */
406 dev->device.common.tag = HARDWARE_DEVICE_TAG;
407 dev->device.common.version = 0;
408 dev->device.common.module = const_cast<hw_module_t*>(module);
409 dev->device.common.close = gralloc_close;
410
411 dev->device.alloc = gralloc_alloc;
412 dev->device.free = gralloc_free;
413
414 *device = &dev->device.common;
415 status = 0;
416 } else {
417 status = fb_device_open(module, name, device);
418 }
419 return status;
420}