blob: 6ec65e9bc8662a8d456479c053a05b212e1cd4be [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>
18
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <cutils/ashmem.h>
24#include <cutils/log.h>
25
26#include <hardware/hardware.h>
27#include <hardware/gralloc.h>
28
29#include <fcntl.h>
30#include <errno.h>
31#include <pthread.h>
32
33#include <cutils/log.h>
34#include <cutils/atomic.h>
35
36#include "gralloc_priv.h"
37
38/*****************************************************************************/
39
40struct gralloc_context_t {
41 alloc_device_t device;
42 /* our private data here */
43};
44
45static int gralloc_alloc_buffer(alloc_device_t* dev,
46 size_t size, int usage, buffer_handle_t* pHandle);
47
48/*****************************************************************************/
49
50int fb_device_open(const hw_module_t* module, const char* name,
51 hw_device_t** device);
52
53static int gralloc_device_open(const hw_module_t* module, const char* name,
54 hw_device_t** device);
55
Mathias Agopiana8a75162009-04-10 14:24:31 -070056extern int gralloc_lock(gralloc_module_t const* module,
57 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070058 int l, int t, int w, int h,
59 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -070060
61extern int gralloc_unlock(gralloc_module_t const* module,
62 buffer_handle_t handle);
63
Mathias Agopian988b8bd2009-05-04 14:26:56 -070064extern int gralloc_register_buffer(gralloc_module_t const* module,
65 buffer_handle_t handle);
66
67extern int gralloc_unregister_buffer(gralloc_module_t const* module,
68 buffer_handle_t handle);
69
Mathias Agopiana8a75162009-04-10 14:24:31 -070070/*****************************************************************************/
71
72static struct hw_module_methods_t gralloc_module_methods = {
73 open: gralloc_device_open
74};
75
76struct private_module_t HAL_MODULE_INFO_SYM = {
77 base: {
78 common: {
79 tag: HARDWARE_MODULE_TAG,
80 version_major: 1,
81 version_minor: 0,
82 id: GRALLOC_HARDWARE_MODULE_ID,
83 name: "Graphics Memory Allocator Module",
84 author: "The Android Open Source Project",
85 methods: &gralloc_module_methods
86 },
Mathias Agopian988b8bd2009-05-04 14:26:56 -070087 registerBuffer: gralloc_register_buffer,
88 unregisterBuffer: gralloc_unregister_buffer,
Mathias Agopiana8a75162009-04-10 14:24:31 -070089 lock: gralloc_lock,
90 unlock: gralloc_unlock,
91 },
92 framebuffer: 0,
93 flags: 0,
94 numBuffers: 0,
95 bufferMask: 0,
96 lock: PTHREAD_MUTEX_INITIALIZER,
97 currentBuffer: 0
98};
99
100/*****************************************************************************/
101
Mathias Agopiana8a75162009-04-10 14:24:31 -0700102static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
103 size_t size, int usage, buffer_handle_t* pHandle)
104{
105 private_module_t* m = reinterpret_cast<private_module_t*>(
106 dev->common.module);
107
108 // allocate the framebuffer
109 if (m->framebuffer == NULL) {
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700110 // initialize the framebuffer, the framebuffer is mapped once
111 // and forever.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700112 int err = mapFrameBufferLocked(m);
113 if (err < 0) {
114 return err;
115 }
116 }
117
Mathias Agopiana8a75162009-04-10 14:24:31 -0700118 const uint32_t bufferMask = m->bufferMask;
119 const uint32_t numBuffers = m->numBuffers;
120 const size_t bufferSize = m->finfo.line_length * m->info.yres;
121 if (numBuffers == 1) {
122 // If we have only one buffer, we never use page-flipping. Instead,
123 // we return a regular buffer which will be memcpy'ed to the main
124 // screen when post is called.
125 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
126 return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
127 }
128
129 if (bufferMask >= ((1LU<<numBuffers)-1)) {
130 // We ran out of buffers.
131 return -ENOMEM;
132 }
133
134 // create a "fake" handles for it
135 intptr_t vaddr = intptr_t(m->framebuffer->base);
136 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size,
137 private_handle_t::PRIV_FLAGS_USES_PMEM |
138 private_handle_t::PRIV_FLAGS_FRAMEBUFFER);
139
140 // find a free slot
141 for (uint32_t i=0 ; i<numBuffers ; i++) {
142 if ((bufferMask & (1LU<<i)) == 0) {
143 m->bufferMask |= (1LU<<i);
144 break;
145 }
146 vaddr += bufferSize;
147 }
148
149 hnd->base = vaddr;
Mathias Agopian72c85082009-06-10 16:06:28 -0700150 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700151 *pHandle = hnd;
152
153 return 0;
154}
155
156static int gralloc_alloc_framebuffer(alloc_device_t* dev,
157 size_t size, int usage, buffer_handle_t* pHandle)
158{
159 private_module_t* m = reinterpret_cast<private_module_t*>(
160 dev->common.module);
161 pthread_mutex_lock(&m->lock);
162 int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle);
163 pthread_mutex_unlock(&m->lock);
164 return err;
165}
166
167
168static int gralloc_alloc_buffer(alloc_device_t* dev,
169 size_t size, int usage, buffer_handle_t* pHandle)
170{
171 size = roundUpToPageSize(size);
172 int flags = 0;
173 if (usage & GRALLOC_USAGE_HW_2D) {
174 flags |= private_handle_t::PRIV_FLAGS_USES_PMEM;
175 }
176 int fd;
177 if ((flags & private_handle_t::PRIV_FLAGS_USES_PMEM) == 0) {
178 fd = ashmem_create_region("Buffer", size);
179 } else {
180 fd = open("/dev/pmem", O_RDWR, 0);
181 // Note: Currently pmem get sized when it is mmaped.
182 // This means that just doing the open doesn't actually allocate
183 // anything. We basically need to do an implicit "mmap"
184 // (under the hood) for pmem regions. However, the current
185 // code will work okay for now thanks to the reference-counting.
186 }
187 if (fd < 0) {
188 return -errno;
189 }
190 private_handle_t* hnd = new private_handle_t(fd, size, flags);
191 *pHandle = hnd;
192 return 0;
193}
194
195/*****************************************************************************/
196
197static int gralloc_alloc(alloc_device_t* dev,
198 int w, int h, int format, int usage,
199 buffer_handle_t* pHandle, int* pStride)
200{
201 if (!pHandle || !pStride)
202 return -EINVAL;
203
204 int align = 4;
205 int bpp = 0;
206 switch (format) {
207 case HAL_PIXEL_FORMAT_RGBA_8888:
208 case HAL_PIXEL_FORMAT_BGRA_8888:
209 bpp = 4;
210 break;
211 case HAL_PIXEL_FORMAT_RGB_565:
212 case HAL_PIXEL_FORMAT_RGBA_5551:
213 case HAL_PIXEL_FORMAT_RGBA_4444:
214 bpp = 2;
215 break;
216 default:
217 return -EINVAL;
218 }
219
220 size_t bpr = (w*bpp + (align-1)) & ~(align-1);
221 size_t size = bpr * h;
222 size_t stride = bpr / bpp;
223
224 int err;
225 if (usage & GRALLOC_USAGE_HW_FB) {
226 err = gralloc_alloc_framebuffer(dev, size, usage, pHandle);
227 } else {
228 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
229 }
230 if (err < 0) {
231 return err;
232 }
233
234 *pStride = stride;
235 return 0;
236}
237
238static int gralloc_free(alloc_device_t* dev,
239 buffer_handle_t handle)
240{
241 if (private_handle_t::validate(handle) < 0)
242 return -EINVAL;
243
244 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
245 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
246 // free this buffer
247 private_module_t* m = reinterpret_cast<private_module_t*>(
248 dev->common.module);
249 const size_t bufferSize = m->finfo.line_length * m->info.yres;
250 int index = (hnd->base - m->framebuffer->base) / bufferSize;
251 m->bufferMask &= ~(1<<index);
252 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700253
254 gralloc_module_t* m = reinterpret_cast<gralloc_module_t*>(
255 dev->common.module);
256 gralloc_unregister_buffer(m, handle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700257
258 close(hnd->fd);
259 delete hnd;
260 return 0;
261}
262
263/*****************************************************************************/
264
265static int gralloc_close(struct hw_device_t *dev)
266{
267 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
268 if (ctx) {
269 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700270 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700271 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700272 free(ctx);
273 }
274 return 0;
275}
276
277int gralloc_device_open(const hw_module_t* module, const char* name,
278 hw_device_t** device)
279{
280 int status = -EINVAL;
281 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
282 gralloc_context_t *dev;
283 dev = (gralloc_context_t*)malloc(sizeof(*dev));
284
285 /* initialize our state here */
286 memset(dev, 0, sizeof(*dev));
287
288 /* initialize the procs */
289 dev->device.common.tag = HARDWARE_DEVICE_TAG;
290 dev->device.common.version = 0;
291 dev->device.common.module = const_cast<hw_module_t*>(module);
292 dev->device.common.close = gralloc_close;
293
294 dev->device.alloc = gralloc_alloc;
295 dev->device.free = gralloc_free;
296
297 *device = &dev->device.common;
298 status = 0;
299 } else {
300 status = fb_device_open(module, name, device);
301 }
302 return status;
303}