blob: f7ea01cf779ee1ea405fe47ea122bec517482f87 [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
Mathias Agopiana8a75162009-04-10 14:24:31 -070017#include <errno.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070018#include <fcntl.h>
19#include <limits.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070020#include <pthread.h>
Marco Nelissena4b587c2009-07-07 09:29:00 -070021#include <stdlib.h>
22#include <string.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070023#include <sys/ioctl.h>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070024#include <sys/mman.h>
25#include <sys/stat.h>
26#include <sys/types.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070027#include <unistd.h>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070028
29#include <cutils/ashmem.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070030#include <cutils/atomic.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070031#include <log/log.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070032
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070033#include <hardware/gralloc.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070034#include <hardware/hardware.h>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070035
Mathias Agopiana8a75162009-04-10 14:24:31 -070036#include "gralloc_priv.h"
Mathias Agopianf96b2062009-12-14 18:27:09 -080037#include "gr.h"
Mathias Agopian3e1f89b2009-07-16 18:04:54 -070038
39/*****************************************************************************/
40
Mathias Agopiana8a75162009-04-10 14:24:31 -070041struct gralloc_context_t {
42 alloc_device_t device;
43 /* our private data here */
44};
45
46static int gralloc_alloc_buffer(alloc_device_t* dev,
47 size_t size, int usage, buffer_handle_t* pHandle);
48
49/*****************************************************************************/
50
51int fb_device_open(const hw_module_t* module, const char* name,
52 hw_device_t** device);
53
54static int gralloc_device_open(const hw_module_t* module, const char* name,
55 hw_device_t** device);
56
Mathias Agopiana8a75162009-04-10 14:24:31 -070057extern int gralloc_lock(gralloc_module_t const* module,
58 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -070059 int l, int t, int w, int h,
60 void** vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -070061
62extern int gralloc_unlock(gralloc_module_t const* module,
63 buffer_handle_t handle);
64
Mathias Agopian988b8bd2009-05-04 14:26:56 -070065extern int gralloc_register_buffer(gralloc_module_t const* module,
66 buffer_handle_t handle);
67
68extern int gralloc_unregister_buffer(gralloc_module_t const* module,
69 buffer_handle_t handle);
70
Mathias Agopiana8a75162009-04-10 14:24:31 -070071/*****************************************************************************/
72
73static struct hw_module_methods_t gralloc_module_methods = {
synergy dev6abbed52013-11-06 17:27:44 -080074 .open = gralloc_device_open
Mathias Agopiana8a75162009-04-10 14:24:31 -070075};
76
77struct private_module_t HAL_MODULE_INFO_SYM = {
synergy dev6abbed52013-11-06 17:27:44 -080078 .base = {
79 .common = {
80 .tag = HARDWARE_MODULE_TAG,
81 .version_major = 1,
82 .version_minor = 0,
83 .id = GRALLOC_HARDWARE_MODULE_ID,
84 .name = "Graphics Memory Allocator Module",
85 .author = "The Android Open Source Project",
86 .methods = &gralloc_module_methods
Mathias Agopiana8a75162009-04-10 14:24:31 -070087 },
synergy dev6abbed52013-11-06 17:27:44 -080088 .registerBuffer = gralloc_register_buffer,
89 .unregisterBuffer = gralloc_unregister_buffer,
90 .lock = gralloc_lock,
91 .unlock = gralloc_unlock,
Mathias Agopiana8a75162009-04-10 14:24:31 -070092 },
synergy dev6abbed52013-11-06 17:27:44 -080093 .framebuffer = 0,
94 .flags = 0,
95 .numBuffers = 0,
96 .bufferMask = 0,
97 .lock = PTHREAD_MUTEX_INITIALIZER,
98 .currentBuffer = 0,
Mathias Agopiana8a75162009-04-10 14:24:31 -070099};
100
101/*****************************************************************************/
102
Mathias Agopiana8a75162009-04-10 14:24:31 -0700103static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev,
Peter Collingbournef2ed2932019-10-15 17:29:20 -0700104 size_t size, int format, int usage, buffer_handle_t* pHandle)
Mathias Agopiana8a75162009-04-10 14:24:31 -0700105{
106 private_module_t* m = reinterpret_cast<private_module_t*>(
107 dev->common.module);
108
109 // allocate the framebuffer
110 if (m->framebuffer == NULL) {
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700111 // initialize the framebuffer, the framebuffer is mapped once
112 // and forever.
Peter Collingbournef2ed2932019-10-15 17:29:20 -0700113 int err = mapFrameBufferLocked(m, format);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700114 if (err < 0) {
115 return err;
116 }
117 }
118
Mathias Agopiana8a75162009-04-10 14:24:31 -0700119 const uint32_t bufferMask = m->bufferMask;
120 const uint32_t numBuffers = m->numBuffers;
121 const size_t bufferSize = m->finfo.line_length * m->info.yres;
122 if (numBuffers == 1) {
123 // If we have only one buffer, we never use page-flipping. Instead,
124 // we return a regular buffer which will be memcpy'ed to the main
125 // screen when post is called.
126 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
127 return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle);
128 }
129
130 if (bufferMask >= ((1LU<<numBuffers)-1)) {
131 // We ran out of buffers.
132 return -ENOMEM;
133 }
134
135 // create a "fake" handles for it
136 intptr_t vaddr = intptr_t(m->framebuffer->base);
137 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700138 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,
Peter Collingbournef2ed2932019-10-15 17:29:20 -0700157 size_t size, int format, int usage, buffer_handle_t* pHandle)
Mathias Agopiana8a75162009-04-10 14:24:31 -0700158{
159 private_module_t* m = reinterpret_cast<private_module_t*>(
160 dev->common.module);
161 pthread_mutex_lock(&m->lock);
Peter Collingbournef2ed2932019-10-15 17:29:20 -0700162 int err = gralloc_alloc_framebuffer_locked(dev, size, format, usage, pHandle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700163 pthread_mutex_unlock(&m->lock);
164 return err;
165}
166
Mathias Agopiana8a75162009-04-10 14:24:31 -0700167static int gralloc_alloc_buffer(alloc_device_t* dev,
Colin Crossfebaaa92014-02-05 18:21:09 -0800168 size_t size, int /*usage*/, buffer_handle_t* pHandle)
Mathias Agopiana8a75162009-04-10 14:24:31 -0700169{
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700170 int err = 0;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700171 int fd = -1;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700172
173 size = roundUpToPageSize(size);
174
Mathias Agopianf96b2062009-12-14 18:27:09 -0800175 fd = ashmem_create_region("gralloc-buffer", size);
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700176 if (fd < 0) {
Steve Block60d056b2012-01-08 10:17:53 +0000177 ALOGE("couldn't create ashmem (%s)", strerror(-errno));
Mathias Agopianbfc010a2009-07-07 12:43:35 -0700178 err = -errno;
179 }
180
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700181 if (err == 0) {
Mathias Agopianf96b2062009-12-14 18:27:09 -0800182 private_handle_t* hnd = new private_handle_t(fd, size, 0);
183 gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
184 dev->common.module);
185 err = mapBuffer(module, hnd);
186 if (err == 0) {
187 *pHandle = hnd;
188 }
Mathias Agopiana8a75162009-04-10 14:24:31 -0700189 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700190
Steve Block60d056b2012-01-08 10:17:53 +0000191 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -0700192
193 return err;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700194}
195
196/*****************************************************************************/
197
Nicolas Capensbd986012015-10-01 15:51:34 -0400198inline size_t align(size_t value, size_t alignment)
199{
200 return ((value + alignment - 1) / alignment) * alignment;
201}
202
Mathias Agopiana8a75162009-04-10 14:24:31 -0700203static int gralloc_alloc(alloc_device_t* dev,
Nicolas Capensbd986012015-10-01 15:51:34 -0400204 int width, int height, int format, int usage,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700205 buffer_handle_t* pHandle, int* pStride)
206{
207 if (!pHandle || !pStride)
208 return -EINVAL;
209
Nicolas Capensbd986012015-10-01 15:51:34 -0400210 int bytesPerPixel = 0;
Mathias Agopian9da751b2010-02-16 14:04:00 -0800211 switch (format) {
Romain Guyd0eb44c2016-12-16 09:22:53 -0800212 case HAL_PIXEL_FORMAT_RGBA_FP16:
213 bytesPerPixel = 8;
214 break;
Mathias Agopian9da751b2010-02-16 14:04:00 -0800215 case HAL_PIXEL_FORMAT_RGBA_8888:
216 case HAL_PIXEL_FORMAT_RGBX_8888:
217 case HAL_PIXEL_FORMAT_BGRA_8888:
Nicolas Capensbd986012015-10-01 15:51:34 -0400218 bytesPerPixel = 4;
Mathias Agopian9da751b2010-02-16 14:04:00 -0800219 break;
220 case HAL_PIXEL_FORMAT_RGB_888:
Nicolas Capensbd986012015-10-01 15:51:34 -0400221 bytesPerPixel = 3;
Mathias Agopian9da751b2010-02-16 14:04:00 -0800222 break;
223 case HAL_PIXEL_FORMAT_RGB_565:
Eino-Ville Talvalae69efbb2015-03-06 13:19:36 -0800224 case HAL_PIXEL_FORMAT_RAW16:
Nicolas Capensbd986012015-10-01 15:51:34 -0400225 bytesPerPixel = 2;
Mathias Agopian9da751b2010-02-16 14:04:00 -0800226 break;
Theodore Dubois46fd7e62022-12-12 10:49:08 -0800227 case HAL_PIXEL_FORMAT_BLOB:
228 bytesPerPixel = 1;
229 break;
Mathias Agopian9da751b2010-02-16 14:04:00 -0800230 default:
Theodore Dubois46fd7e62022-12-12 10:49:08 -0800231 ALOGE("gralloc_alloc bad format %d", format);
Mathias Agopian9da751b2010-02-16 14:04:00 -0800232 return -EINVAL;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700233 }
Nicolas Capensbd986012015-10-01 15:51:34 -0400234
235 const size_t tileWidth = 2;
236 const size_t tileHeight = 2;
237
238 size_t stride = align(width, tileWidth);
239 size_t size = align(height, tileHeight) * stride * bytesPerPixel + 4;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700240
Mathias Agopiana8a75162009-04-10 14:24:31 -0700241 int err;
242 if (usage & GRALLOC_USAGE_HW_FB) {
Peter Collingbournef2ed2932019-10-15 17:29:20 -0700243 err = gralloc_alloc_framebuffer(dev, size, format, usage, pHandle);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700244 } else {
245 err = gralloc_alloc_buffer(dev, size, usage, pHandle);
246 }
Mathias Agopian8bf1f752009-06-25 17:38:50 -0700247
Mathias Agopiana8a75162009-04-10 14:24:31 -0700248 if (err < 0) {
249 return err;
250 }
251
252 *pStride = stride;
253 return 0;
254}
255
256static int gralloc_free(alloc_device_t* dev,
257 buffer_handle_t handle)
258{
259 if (private_handle_t::validate(handle) < 0)
260 return -EINVAL;
261
262 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700263 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700264 // free this buffer
265 private_module_t* m = reinterpret_cast<private_module_t*>(
266 dev->common.module);
267 const size_t bufferSize = m->finfo.line_length * m->info.yres;
268 int index = (hnd->base - m->framebuffer->base) / bufferSize;
269 m->bufferMask &= ~(1<<index);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700270 } else {
Mathias Agopianbd80b382009-07-07 17:53:43 -0700271 gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
272 dev->common.module);
273 terminateBuffer(module, const_cast<private_handle_t*>(hnd));
274 }
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700275
Mathias Agopiana8a75162009-04-10 14:24:31 -0700276 close(hnd->fd);
277 delete hnd;
278 return 0;
279}
280
281/*****************************************************************************/
282
283static int gralloc_close(struct hw_device_t *dev)
284{
285 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
286 if (ctx) {
287 /* TODO: keep a list of all buffer_handle_t created, and free them
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700288 * all here.
Mathias Agopiana8a75162009-04-10 14:24:31 -0700289 */
Mathias Agopiana8a75162009-04-10 14:24:31 -0700290 free(ctx);
291 }
292 return 0;
293}
294
295int gralloc_device_open(const hw_module_t* module, const char* name,
296 hw_device_t** device)
297{
298 int status = -EINVAL;
299 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
300 gralloc_context_t *dev;
301 dev = (gralloc_context_t*)malloc(sizeof(*dev));
302
303 /* initialize our state here */
304 memset(dev, 0, sizeof(*dev));
305
306 /* initialize the procs */
307 dev->device.common.tag = HARDWARE_DEVICE_TAG;
308 dev->device.common.version = 0;
309 dev->device.common.module = const_cast<hw_module_t*>(module);
310 dev->device.common.close = gralloc_close;
311
312 dev->device.alloc = gralloc_alloc;
313 dev->device.free = gralloc_free;
314
315 *device = &dev->device.common;
316 status = 0;
317 } else {
318 status = fb_device_open(module, name, device);
319 }
320 return status;
321}