blob: cf18ed2eb5a744e694eaed08e5061347fe9df6d4 [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 <sys/mman.h>
18
19#include <dlfcn.h>
20
21#include <cutils/ashmem.h>
22#include <cutils/log.h>
23
24#include <hardware/hardware.h>
25#include <hardware/gralloc.h>
26
27#include <fcntl.h>
28#include <errno.h>
Marco Nelissena4b587c2009-07-07 09:29:00 -070029#include <sys/ioctl.h>
30#include <string.h>
31#include <stdlib.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070032
33#include <cutils/log.h>
34#include <cutils/atomic.h>
35
36#if HAVE_ANDROID_OS
37#include <linux/fb.h>
38#endif
39
40#include "gralloc_priv.h"
Mathias Agopianfc054132009-08-18 17:35:44 -070041#include "gr.h"
Mathias Agopiana8a75162009-04-10 14:24:31 -070042
43/*****************************************************************************/
44
Mathias Agopiancdb66fb2009-07-06 20:49:05 -070045// numbers of buffers for page flipping
Mathias Agopiana8a75162009-04-10 14:24:31 -070046#define NUM_BUFFERS 2
47
48
49enum {
50 PAGE_FLIP = 0x00000001,
51 LOCKED = 0x00000002
52};
53
54struct fb_context_t {
55 framebuffer_device_t device;
56};
57
58/*****************************************************************************/
59
60static int fb_setSwapInterval(struct framebuffer_device_t* dev,
61 int interval)
62{
63 fb_context_t* ctx = (fb_context_t*)dev;
64 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
65 return -EINVAL;
66 // FIXME: implement fb_setSwapInterval
67 return 0;
68}
69
70static int fb_setUpdateRect(struct framebuffer_device_t* dev,
71 int l, int t, int w, int h)
72{
73 if (((w|h) <= 0) || ((l|t)<0))
74 return -EINVAL;
75
76 fb_context_t* ctx = (fb_context_t*)dev;
77 private_module_t* m = reinterpret_cast<private_module_t*>(
78 dev->common.module);
79 m->info.reserved[0] = 0x54445055; // "UPDT";
80 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
81 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
82 return 0;
83}
84
85static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
86{
87 if (private_handle_t::validate(buffer) < 0)
88 return -EINVAL;
89
90 fb_context_t* ctx = (fb_context_t*)dev;
91
92 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
93 private_module_t* m = reinterpret_cast<private_module_t*>(
94 dev->common.module);
Mathias Agopiana8a75162009-04-10 14:24:31 -070095
96 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -070097 const size_t offset = hnd->base - m->framebuffer->base;
98 m->info.activate = FB_ACTIVATE_VBL;
99 m->info.yoffset = offset / m->finfo.line_length;
100 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
101 LOGE("FBIOPUT_VSCREENINFO failed");
102 m->base.unlock(&m->base, buffer);
103 return -errno;
104 }
105 m->currentBuffer = buffer;
106
107 } else {
108 // If we can't do the page_flip, just copy the buffer to the front
109 // FIXME: use copybit HAL instead of memcpy
110
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700111 void* fb_vaddr;
112 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700113
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700114 m->base.lock(&m->base, m->framebuffer,
115 GRALLOC_USAGE_SW_WRITE_RARELY,
116 0, 0, m->info.xres, m->info.yres,
117 &fb_vaddr);
118
119 m->base.lock(&m->base, buffer,
120 GRALLOC_USAGE_SW_READ_RARELY,
121 0, 0, m->info.xres, m->info.yres,
122 &buffer_vaddr);
123
124 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700125
126 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700127 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700128 }
129
130 return 0;
131}
132
133/*****************************************************************************/
134
135int mapFrameBufferLocked(struct private_module_t* module)
136{
137 // already initialized...
138 if (module->framebuffer) {
139 return 0;
140 }
141
142 char const * const device_template[] = {
143 "/dev/graphics/fb%u",
144 "/dev/fb%u",
145 0 };
146
147 int fd = -1;
148 int i=0;
149 char name[64];
150
151 while ((fd==-1) && device_template[i]) {
152 snprintf(name, 64, device_template[i], 0);
153 fd = open(name, O_RDWR, 0);
154 i++;
155 }
156 if (fd < 0)
157 return -errno;
158
159 struct fb_fix_screeninfo finfo;
160 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
161 return -errno;
162
163 struct fb_var_screeninfo info;
164 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
165 return -errno;
166
167 info.reserved[0] = 0;
168 info.reserved[1] = 0;
169 info.reserved[2] = 0;
170 info.xoffset = 0;
171 info.yoffset = 0;
172 info.activate = FB_ACTIVATE_NOW;
173
174 /*
Mathias Agopiana8a75162009-04-10 14:24:31 -0700175 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
176 */
177 info.yres_virtual = info.yres * NUM_BUFFERS;
178
179
180 uint32_t flags = PAGE_FLIP;
181 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
182 info.yres_virtual = info.yres;
183 flags &= ~PAGE_FLIP;
Steve Block22d35132012-01-05 23:27:52 +0000184 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
Mathias Agopiana8a75162009-04-10 14:24:31 -0700185 }
186
187 if (info.yres_virtual < info.yres * 2) {
188 // we need at least 2 for page-flipping
189 info.yres_virtual = info.yres;
190 flags &= ~PAGE_FLIP;
Steve Block22d35132012-01-05 23:27:52 +0000191 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Mathias Agopiana8a75162009-04-10 14:24:31 -0700192 info.yres_virtual, info.yres*2);
193 }
194
195 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
196 return -errno;
197
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100198 uint64_t refreshQuotient =
Mathias Agopiana8a75162009-04-10 14:24:31 -0700199 (
200 uint64_t( info.upper_margin + info.lower_margin + info.yres )
201 * ( info.left_margin + info.right_margin + info.xres )
202 * info.pixclock
203 );
204
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100205 /* Beware, info.pixclock might be 0 under emulation, so avoid a
206 * division-by-0 here (SIGFPE on ARM) */
207 int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
208
Mathias Agopiana8a75162009-04-10 14:24:31 -0700209 if (refreshRate == 0) {
210 // bleagh, bad info from the driver
211 refreshRate = 60*1000; // 60 Hz
212 }
213
214 if (int(info.width) <= 0 || int(info.height) <= 0) {
215 // the driver doesn't return that information
216 // default to 160 dpi
217 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
218 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
219 }
220
221 float xdpi = (info.xres * 25.4f) / info.width;
222 float ydpi = (info.yres * 25.4f) / info.height;
223 float fps = refreshRate / 1000.0f;
224
Steve Blockb224b782012-01-04 20:07:12 +0000225 ALOGI( "using (fd=%d)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700226 "id = %s\n"
227 "xres = %d px\n"
228 "yres = %d px\n"
229 "xres_virtual = %d px\n"
230 "yres_virtual = %d px\n"
231 "bpp = %d\n"
232 "r = %2u:%u\n"
233 "g = %2u:%u\n"
234 "b = %2u:%u\n",
235 fd,
236 finfo.id,
237 info.xres,
238 info.yres,
239 info.xres_virtual,
240 info.yres_virtual,
241 info.bits_per_pixel,
242 info.red.offset, info.red.length,
243 info.green.offset, info.green.length,
244 info.blue.offset, info.blue.length
245 );
246
Steve Blockb224b782012-01-04 20:07:12 +0000247 ALOGI( "width = %d mm (%f dpi)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700248 "height = %d mm (%f dpi)\n"
249 "refresh rate = %.2f Hz\n",
250 info.width, xdpi,
251 info.height, ydpi,
252 fps
253 );
254
255
256 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
257 return -errno;
258
259 if (finfo.smem_len <= 0)
260 return -errno;
261
262
263 module->flags = flags;
264 module->info = info;
265 module->finfo = finfo;
266 module->xdpi = xdpi;
267 module->ydpi = ydpi;
268 module->fps = fps;
269
270 /*
271 * map the framebuffer
272 */
273
274 int err;
275 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
Mathias Agopianf96b2062009-12-14 18:27:09 -0800276 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700277
278 module->numBuffers = info.yres_virtual / info.yres;
279 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700280
Mathias Agopian51156652009-06-09 18:55:49 -0700281 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
282 if (vaddr == MAP_FAILED) {
283 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
284 return -errno;
285 }
286 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700287 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700288 return 0;
289}
290
291static int mapFrameBuffer(struct private_module_t* module)
292{
293 pthread_mutex_lock(&module->lock);
294 int err = mapFrameBufferLocked(module);
295 pthread_mutex_unlock(&module->lock);
296 return err;
297}
298
299/*****************************************************************************/
300
301static int fb_close(struct hw_device_t *dev)
302{
303 fb_context_t* ctx = (fb_context_t*)dev;
304 if (ctx) {
305 free(ctx);
306 }
307 return 0;
308}
309
310int fb_device_open(hw_module_t const* module, const char* name,
311 hw_device_t** device)
312{
313 int status = -EINVAL;
314 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700315 alloc_device_t* gralloc_device;
316 status = gralloc_open(module, &gralloc_device);
317 if (status < 0)
318 return status;
319
320 /* initialize our state here */
321 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
322 memset(dev, 0, sizeof(*dev));
323
324 /* initialize the procs */
325 dev->device.common.tag = HARDWARE_DEVICE_TAG;
326 dev->device.common.version = 0;
327 dev->device.common.module = const_cast<hw_module_t*>(module);
328 dev->device.common.close = fb_close;
329 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700330 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700331 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700332
333 private_module_t* m = (private_module_t*)module;
334 status = mapFrameBuffer(m);
335 if (status >= 0) {
336 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100337 int format = (m->info.bits_per_pixel == 32)
338 ? HAL_PIXEL_FORMAT_RGBX_8888
339 : HAL_PIXEL_FORMAT_RGB_565;
Mathias Agopian295190f2009-05-05 18:30:52 -0700340 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700341 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
342 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
343 const_cast<int&>(dev->device.stride) = stride;
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100344 const_cast<int&>(dev->device.format) = format;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700345 const_cast<float&>(dev->device.xdpi) = m->xdpi;
346 const_cast<float&>(dev->device.ydpi) = m->ydpi;
347 const_cast<float&>(dev->device.fps) = m->fps;
348 const_cast<int&>(dev->device.minSwapInterval) = 1;
349 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700350 *device = &dev->device.common;
351 }
352 }
353 return status;
354}