blob: af1ed66687bbf0a477f58cce8be5d377f5082f4b [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
Mathias Agopian14784232009-07-02 17:32:15 -0700175static int init_pmem_area_locked(private_module_t* m)
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700176{
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
Mathias Agopian14784232009-07-02 17:32:15 -0700196static int init_pmem_area(private_module_t* m)
197{
198 int err = 0;
199 pthread_mutex_lock(&m->lock);
200 if (m->pmem_master == -1) {
201 err = init_pmem_area_locked(m);
202 if (err) {
203 m->pmem_master = err;
204 }
205 }
206 pthread_mutex_unlock(&m->lock);
207 return err;
208}
209
Mathias Agopiana8a75162009-04-10 14:24:31 -0700210static int gralloc_alloc_buffer(alloc_device_t* dev,
211 size_t size, int usage, buffer_handle_t* pHandle)
212{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700213 int err = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700214 int flags = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700215
216 int fd = -1;
217 void* base = 0;
218 int offset = 0;
219 int lockState = 0;
220
221 size = roundUpToPageSize(size);
222
223 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
224 // enable pmem in that case, so our software GL can fallback to
225 // the copybit module.
226 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
227 }
228
Mathias Agopiana8a75162009-04-10 14:24:31 -0700229 if (usage & GRALLOC_USAGE_HW_2D) {
230 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
231 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700232
Mathias Agopiana8a75162009-04-10 14:24:31 -0700233 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700234try_ashmem:
Mathias Agopiana8a75162009-04-10 14:24:31 -0700235 fd = ashmem_create_region("Buffer", size);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700236 if (fd < 0) {
Mathias Agopian31802ca2009-06-19 16:14:09 -0700237 LOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700238 err = -errno;
239 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700240 } else {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700241 private_module_t* m = reinterpret_cast<private_module_t*>(
242 dev->common.module);
Mathias Agopian31802ca2009-06-19 16:14:09 -0700243
Mathias Agopian14784232009-07-02 17:32:15 -0700244 err = init_pmem_area(m);
245 if (err == 0) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700246 // PMEM buffers are always mmapped
247 base = m->pmem_master_base;
248 lockState |= private_handle_t::LOCK_STATE_MAPPED;
249
250 offset = sAllocator.allocate(size);
251 if (offset < 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700252 // no more pmem memory
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700253 err = -ENOMEM;
254 } else {
Mathias Agopian14784232009-07-02 17:32:15 -0700255 struct pmem_region sub = { offset, size };
256
257 // now create the "sub-heap"
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700258 fd = open("/dev/pmem", O_RDWR, 0);
Mathias Agopian14784232009-07-02 17:32:15 -0700259 err = fd < 0 ? fd : 0;
260
261 // and connect to it
262 if (err == 0)
263 err = ioctl(fd, PMEM_CONNECT, m->pmem_master);
264
265 // and make it available to the client process
266 if (err == 0)
267 err = ioctl(fd, PMEM_MAP, &sub);
268
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700269 if (err < 0) {
270 err = -errno;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700271 close(fd);
272 sAllocator.deallocate(offset);
273 fd = -1;
274 }
Mathias Agopianed93e8b2009-06-16 18:22:45 -0700275 //LOGD_IF(!err, "allocating pmem size=%d, offset=%d", size, offset);
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700276 }
Mathias Agopian31802ca2009-06-19 16:14:09 -0700277 } else {
278 if ((usage & GRALLOC_USAGE_HW_2D) == 0) {
279 // the caller didn't request PMEM, so we can try something else
280 flags &= ~private_handle_t::PRIV_FLAGS_USES_PMEM;
281 err = 0;
282 goto try_ashmem;
283 } else {
284 LOGE("couldn't open pmem (%s)", strerror(-errno));
285 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700286 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700287 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700288
289 if (err == 0) {
290 private_handle_t* hnd = new private_handle_t(fd, size, flags);
291 hnd->offset = offset;
292 hnd->base = int(base)+offset;
293 hnd->lockState = lockState;
294 *pHandle = hnd;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700295 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700296
297 LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
298
299 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700300}
301
302/*****************************************************************************/
303
304static int gralloc_alloc(alloc_device_t* dev,
305 int w, int h, int format, int usage,
306 buffer_handle_t* pHandle, int* pStride)
307{
308 if (!pHandle || !pStride)
309 return -EINVAL;
310
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700311 size_t size, stride;
312 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP ||
313 format == HAL_PIXEL_FORMAT_YCbCr_422_SP)
314 {
315 // FIXME: there is no way to return the vstride
316 int vstride;
317 stride = (w + 1) & ~1;
318 switch (format) {
319 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
320 size = stride * h * 2;
321 break;
322 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
323 vstride = (h+1) & ~1;
324 size = (stride * vstride) + (w/2 * h/2) * 2;
325 break;
326 default:
327 return -EINVAL;
328 }
329 } else {
330 int align = 4;
331 int bpp = 0;
332 switch (format) {
333 case HAL_PIXEL_FORMAT_RGBA_8888:
334 case HAL_PIXEL_FORMAT_BGRA_8888:
335 bpp = 4;
336 break;
337 case HAL_PIXEL_FORMAT_RGB_565:
338 case HAL_PIXEL_FORMAT_RGBA_5551:
339 case HAL_PIXEL_FORMAT_RGBA_4444:
340 bpp = 2;
341 break;
342 default:
343 return -EINVAL;
344 }
345 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
346 size = bpr * h;
347 stride = bpr / bpp;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700348 }
349
Mathias Agopiana8a75162009-04-10 14:24:31 -0700350 int err;
351 if (usage & GRALLOC_USAGE_HW_FB) {
352 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
353 } else {
354 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
355 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700356
Mathias Agopiana8a75162009-04-10 14:24:31 -0700357 if (err < 0) {
358 return err;
359 }
360
361 *pStride = stride;
362 return 0;
363}
364
365static int gralloc_free(alloc_device_t* dev,
366 buffer_handle_t handle)
367{
368 if (private_handle_t::validate(handle) < 0)
369 return -EINVAL;
370
371 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopian14784232009-07-02 17:32:15 -0700372 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
373 {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700374 // free this buffer
375 private_module_t* m = reinterpret_cast<private_module_t*>(
376 dev->common.module);
377 const size_t bufferSize = m->finfo.line_length * m->info.yres;
378 int index = (hnd->base - m->framebuffer->base) / bufferSize;
379 m->bufferMask &= ~(1<<index);
Mathias Agopian14784232009-07-02 17:32:15 -0700380 }
381 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM)
382 {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700383 if (hnd->fd >= 0) {
Mathias Agopian14784232009-07-02 17:32:15 -0700384 struct pmem_region sub = { hnd->offset, hnd->size };
385 int err = ioctl(hnd->fd, PMEM_UNMAP, &sub);
386 LOGE_IF(err<0, "PMEM_UNMAP failed (%s), "
387 "fd=%d, sub.offset=%lu, sub.size=%lu",
388 strerror(errno), hnd->fd, hnd->offset, hnd->size);
389 if (err == 0) {
390 // we can't deallocate the memory in case of UNMAP failure
391 // because it would give that process access to someone else's
392 // surfaces, which would be a security breach.
393 sAllocator.deallocate(hnd->offset);
394 }
395 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700396 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700397
398 gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>(
399 dev->common.module);
400 gralloc_unregister_buffer(m, handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700401
402 close(hnd->fd);
403 delete hnd;
404 return 0;
405}
406
407/*****************************************************************************/
408
409static int gralloc_close(struct hw_device_t *dev)
410{
411 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
412 if (ctx) {
413 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700414 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700415 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700416 free(ctx);
417 }
418 return 0;
419}
420
421int gralloc_device_open(const hw_module_t* module, const char* name,
422 hw_device_t** device)
423{
424 int status = -EINVAL;
425 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
426 gralloc_context_t *dev;
427 dev = (gralloc_context_t*)malloc(sizeof(*dev));
428
429 /* initialize our state here */
430 memset(dev, 0, sizeof(*dev));
431
432 /* initialize the procs */
433 dev->device.common.tag = HARDWARE_DEVICE_TAG;
434 dev->device.common.version = 0;
435 dev->device.common.module = const_cast<hw_module_t*>(module);
436 dev->device.common.close = gralloc_close;
437
438 dev->device.alloc = gralloc_alloc;
439 dev->device.free = gralloc_free;
440
441 *device = &dev->device.common;
442 status = 0;
443 } else {
444 status = fb_device_open(module, name, device);
445 }
446 return status;
447}