blob: 59c5001f7c8f90f973820e5e1baa8c5150d99a79 [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
Jean-Baptiste Queru118b5d72010-10-14 18:04:30 -070046#if defined(NO_PAGE_FLIPPING)
47// page-flipping is buggy on some devices
48#define NUM_BUFFERS 1
49#else
Mathias Agopiana8a75162009-04-10 14:24:31 -070050#define NUM_BUFFERS 2
Jean-Baptiste Queru118b5d72010-10-14 18:04:30 -070051#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -070052
53
54enum {
55 PAGE_FLIP = 0x00000001,
56 LOCKED = 0x00000002
57};
58
59struct fb_context_t {
60 framebuffer_device_t device;
61};
62
63/*****************************************************************************/
64
65static int fb_setSwapInterval(struct framebuffer_device_t* dev,
66 int interval)
67{
68 fb_context_t* ctx = (fb_context_t*)dev;
69 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
70 return -EINVAL;
71 // FIXME: implement fb_setSwapInterval
72 return 0;
73}
74
75static int fb_setUpdateRect(struct framebuffer_device_t* dev,
76 int l, int t, int w, int h)
77{
78 if (((w|h) <= 0) || ((l|t)<0))
79 return -EINVAL;
80
81 fb_context_t* ctx = (fb_context_t*)dev;
82 private_module_t* m = reinterpret_cast<private_module_t*>(
83 dev->common.module);
84 m->info.reserved[0] = 0x54445055; // "UPDT";
85 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
86 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
87 return 0;
88}
89
90static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
91{
92 if (private_handle_t::validate(buffer) < 0)
93 return -EINVAL;
94
95 fb_context_t* ctx = (fb_context_t*)dev;
96
97 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
98 private_module_t* m = reinterpret_cast<private_module_t*>(
99 dev->common.module);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700100
101 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700102 const size_t offset = hnd->base - m->framebuffer->base;
103 m->info.activate = FB_ACTIVATE_VBL;
104 m->info.yoffset = offset / m->finfo.line_length;
105 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
106 LOGE("FBIOPUT_VSCREENINFO failed");
107 m->base.unlock(&m->base, buffer);
108 return -errno;
109 }
110 m->currentBuffer = buffer;
111
112 } else {
113 // If we can't do the page_flip, just copy the buffer to the front
114 // FIXME: use copybit HAL instead of memcpy
115
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700116 void* fb_vaddr;
117 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700118
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700119 m->base.lock(&m->base, m->framebuffer,
120 GRALLOC_USAGE_SW_WRITE_RARELY,
121 0, 0, m->info.xres, m->info.yres,
122 &fb_vaddr);
123
124 m->base.lock(&m->base, buffer,
125 GRALLOC_USAGE_SW_READ_RARELY,
126 0, 0, m->info.xres, m->info.yres,
127 &buffer_vaddr);
128
129 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700130
131 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700132 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700133 }
134
135 return 0;
136}
137
138/*****************************************************************************/
139
140int mapFrameBufferLocked(struct private_module_t* module)
141{
142 // already initialized...
143 if (module->framebuffer) {
144 return 0;
145 }
146
147 char const * const device_template[] = {
148 "/dev/graphics/fb%u",
149 "/dev/fb%u",
150 0 };
151
152 int fd = -1;
153 int i=0;
154 char name[64];
155
156 while ((fd==-1) && device_template[i]) {
157 snprintf(name, 64, device_template[i], 0);
158 fd = open(name, O_RDWR, 0);
159 i++;
160 }
161 if (fd < 0)
162 return -errno;
163
164 struct fb_fix_screeninfo finfo;
165 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
166 return -errno;
167
168 struct fb_var_screeninfo info;
169 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
170 return -errno;
171
172 info.reserved[0] = 0;
173 info.reserved[1] = 0;
174 info.reserved[2] = 0;
175 info.xoffset = 0;
176 info.yoffset = 0;
177 info.activate = FB_ACTIVATE_NOW;
178
179 /*
180 * Explicitly request 5/6/5
181 */
182 info.bits_per_pixel = 16;
183 info.red.offset = 11;
184 info.red.length = 5;
185 info.green.offset = 5;
186 info.green.length = 6;
187 info.blue.offset = 0;
188 info.blue.length = 5;
189 info.transp.offset = 0;
190 info.transp.length = 0;
191
192 /*
193 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
194 */
195 info.yres_virtual = info.yres * NUM_BUFFERS;
196
197
198 uint32_t flags = PAGE_FLIP;
199 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
200 info.yres_virtual = info.yres;
201 flags &= ~PAGE_FLIP;
202 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
203 }
204
205 if (info.yres_virtual < info.yres * 2) {
206 // we need at least 2 for page-flipping
207 info.yres_virtual = info.yres;
208 flags &= ~PAGE_FLIP;
209 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
210 info.yres_virtual, info.yres*2);
211 }
212
213 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
214 return -errno;
215
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100216 uint64_t refreshQuotient =
Mathias Agopiana8a75162009-04-10 14:24:31 -0700217 (
218 uint64_t( info.upper_margin + info.lower_margin + info.yres )
219 * ( info.left_margin + info.right_margin + info.xres )
220 * info.pixclock
221 );
222
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100223 /* Beware, info.pixclock might be 0 under emulation, so avoid a
224 * division-by-0 here (SIGFPE on ARM) */
225 int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
226
Mathias Agopiana8a75162009-04-10 14:24:31 -0700227 if (refreshRate == 0) {
228 // bleagh, bad info from the driver
229 refreshRate = 60*1000; // 60 Hz
230 }
231
232 if (int(info.width) <= 0 || int(info.height) <= 0) {
233 // the driver doesn't return that information
234 // default to 160 dpi
235 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
236 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
237 }
238
239 float xdpi = (info.xres * 25.4f) / info.width;
240 float ydpi = (info.yres * 25.4f) / info.height;
241 float fps = refreshRate / 1000.0f;
242
243 LOGI( "using (fd=%d)\n"
244 "id = %s\n"
245 "xres = %d px\n"
246 "yres = %d px\n"
247 "xres_virtual = %d px\n"
248 "yres_virtual = %d px\n"
249 "bpp = %d\n"
250 "r = %2u:%u\n"
251 "g = %2u:%u\n"
252 "b = %2u:%u\n",
253 fd,
254 finfo.id,
255 info.xres,
256 info.yres,
257 info.xres_virtual,
258 info.yres_virtual,
259 info.bits_per_pixel,
260 info.red.offset, info.red.length,
261 info.green.offset, info.green.length,
262 info.blue.offset, info.blue.length
263 );
264
265 LOGI( "width = %d mm (%f dpi)\n"
266 "height = %d mm (%f dpi)\n"
267 "refresh rate = %.2f Hz\n",
268 info.width, xdpi,
269 info.height, ydpi,
270 fps
271 );
272
273
274 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
275 return -errno;
276
277 if (finfo.smem_len <= 0)
278 return -errno;
279
280
281 module->flags = flags;
282 module->info = info;
283 module->finfo = finfo;
284 module->xdpi = xdpi;
285 module->ydpi = ydpi;
286 module->fps = fps;
287
288 /*
289 * map the framebuffer
290 */
291
292 int err;
293 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
Mathias Agopianf96b2062009-12-14 18:27:09 -0800294 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700295
296 module->numBuffers = info.yres_virtual / info.yres;
297 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700298
Mathias Agopian51156652009-06-09 18:55:49 -0700299 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
300 if (vaddr == MAP_FAILED) {
301 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
302 return -errno;
303 }
304 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700305 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700306 return 0;
307}
308
309static int mapFrameBuffer(struct private_module_t* module)
310{
311 pthread_mutex_lock(&module->lock);
312 int err = mapFrameBufferLocked(module);
313 pthread_mutex_unlock(&module->lock);
314 return err;
315}
316
317/*****************************************************************************/
318
319static int fb_close(struct hw_device_t *dev)
320{
321 fb_context_t* ctx = (fb_context_t*)dev;
322 if (ctx) {
323 free(ctx);
324 }
325 return 0;
326}
327
328int fb_device_open(hw_module_t const* module, const char* name,
329 hw_device_t** device)
330{
331 int status = -EINVAL;
332 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700333 alloc_device_t* gralloc_device;
334 status = gralloc_open(module, &gralloc_device);
335 if (status < 0)
336 return status;
337
338 /* initialize our state here */
339 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
340 memset(dev, 0, sizeof(*dev));
341
342 /* initialize the procs */
343 dev->device.common.tag = HARDWARE_DEVICE_TAG;
344 dev->device.common.version = 0;
345 dev->device.common.module = const_cast<hw_module_t*>(module);
346 dev->device.common.close = fb_close;
347 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700348 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700349 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700350
351 private_module_t* m = (private_module_t*)module;
352 status = mapFrameBuffer(m);
353 if (status >= 0) {
354 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
Mathias Agopian295190f2009-05-05 18:30:52 -0700355 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700356 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
357 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
358 const_cast<int&>(dev->device.stride) = stride;
359 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
360 const_cast<float&>(dev->device.xdpi) = m->xdpi;
361 const_cast<float&>(dev->device.ydpi) = m->ydpi;
362 const_cast<float&>(dev->device.fps) = m->fps;
363 const_cast<int&>(dev->device.minSwapInterval) = 1;
364 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700365 *device = &dev->device.common;
366 }
367 }
368 return status;
369}