blob: 2cf6e029c93eb650bce0f22b73e563dda1eb5c20 [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) {
220 fd = ashmem_create_region("Buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700221 if (fd < 0) {
222 err = -errno;
223 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700224 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700225 private_module_t* m = reinterpret_cast<private_module_t*>(
226 dev->common.module);
227
228 pthread_mutex_lock(&m->lock);
229 if (m->pmem_master == -1)
230 err = init_pmem_area(m);
231 pthread_mutex_unlock(&m->lock);
232
233 if (m->pmem_master >= 0) {
234 // PMEM buffers are always mmapped
235 base = m->pmem_master_base;
236 lockState |= private_handle_t::LOCK_STATE_MAPPED;
237
238 offset = sAllocator.allocate(size);
239 if (offset < 0) {
240 err = -ENOMEM;
241 } else {
242 fd = open("/dev/pmem", O_RDWR, 0);
243 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
244 if (err < 0) {
245 err = -errno;
246 } else {
247 struct pmem_region sub = { offset, size };
248 err = ioctl(fd, PMEM_MAP, &sub);
249 }
250 if (err < 0) {
251 close(fd);
252 sAllocator.deallocate(offset);
253 fd = -1;
254 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700255 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700256 }
257 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700258 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700259
260 if (err == 0) {
261 private_handle_t* hnd = new private_handle_t(fd, size, flags);
262 hnd->offset = offset;
263 hnd->base = int(base)+offset;
264 hnd->lockState = lockState;
265 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700266 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700267
268 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
269
270 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700271}
272
273/*****************************************************************************/
274
275static int gralloc_alloc(alloc_device_t* dev,
276 int w, int h, int format, int usage,
277 buffer_handle_t* pHandle, int* pStride)
278{
279 if (!pHandle || !pStride)
280 return -EINVAL;
281
282 int align = 4;
283 int bpp = 0;
284 switch (format) {
285 case HAL_PIXEL_FORMAT_RGBA_8888:
286 case HAL_PIXEL_FORMAT_BGRA_8888:
287 bpp = 4;
288 break;
289 case HAL_PIXEL_FORMAT_RGB_565:
290 case HAL_PIXEL_FORMAT_RGBA_5551:
291 case HAL_PIXEL_FORMAT_RGBA_4444:
292 bpp = 2;
293 break;
294 default:
295 return -EINVAL;
296 }
297
298 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
299 size_t size = bpr * h;
300 size_t stride = bpr / bpp;
301
302 int err;
303 if (usage & GRALLOC_USAGE_HW_FB) {
304 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
305 } else {
306 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
307 }
308 if (err < 0) {
309 return err;
310 }
311
312 *pStride = stride;
313 return 0;
314}
315
316static int gralloc_free(alloc_device_t* dev,
317 buffer_handle_t handle)
318{
319 if (private_handle_t::validate(handle) < 0)
320 return -EINVAL;
321
322 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
323 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
324 // free this buffer
325 private_module_t* m = reinterpret_cast<private_module_t*>(
326 dev->common.module);
327 const size_t bufferSize = m->finfo.line_length * m->info.yres;
328 int index = (hnd->base - m->framebuffer->base) / bufferSize;
329 m->bufferMask &= ~(1<<index);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700330 } else if (true || hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) {
331 if (hnd->fd >= 0) {
332 sAllocator.deallocate(hnd->offset);
333 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700334 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700335
336 gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>(
337 dev->common.module);
338 gralloc_unregister_buffer(m, handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700339
340 close(hnd->fd);
341 delete hnd;
342 return 0;
343}
344
345/*****************************************************************************/
346
347static int gralloc_close(struct hw_device_t *dev)
348{
349 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
350 if (ctx) {
351 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700352 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700353 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700354 free(ctx);
355 }
356 return 0;
357}
358
359int gralloc_device_open(const hw_module_t* module, const char* name,
360 hw_device_t** device)
361{
362 int status = -EINVAL;
363 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
364 gralloc_context_t *dev;
365 dev = (gralloc_context_t*)malloc(sizeof(*dev));
366
367 /* initialize our state here */
368 memset(dev, 0, sizeof(*dev));
369
370 /* initialize the procs */
371 dev->device.common.tag = HARDWARE_DEVICE_TAG;
372 dev->device.common.version = 0;
373 dev->device.common.module = const_cast<hw_module_t*>(module);
374 dev->device.common.close = gralloc_close;
375
376 dev->device.alloc = gralloc_alloc;
377 dev->device.free = gralloc_free;
378
379 *device = &dev->device.common;
380 status = 0;
381 } else {
382 status = fb_device_open(module, name, device);
383 }
384 return status;
385}