blob: e73724f49fbee03453e7f8ebfe0ed562a6ea4d6c [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"
41
42/*****************************************************************************/
43
Mathias Agopianf5cf8f82009-05-07 17:39:31 -070044// should be a build option
45#define SUPPORTS_UPDATE_ON_DEMAND 1
46
Mathias Agopiancdb66fb2009-07-06 20:49:05 -070047// fb_fix_screeninfo::id returned on the emulator
48#define EMULATOR_DEV_FB_ID ""
49
50// numbers of buffers for page flipping
Mathias Agopiana8a75162009-04-10 14:24:31 -070051#define NUM_BUFFERS 2
52
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);
100
101 if (m->currentBuffer) {
102 m->base.unlock(&m->base, m->currentBuffer);
103 m->currentBuffer = 0;
104 }
105
106 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
107
108 m->base.lock(&m->base, buffer,
109 private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700110 0, 0, m->info.xres, m->info.yres, NULL);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700111
112 const size_t offset = hnd->base - m->framebuffer->base;
113 m->info.activate = FB_ACTIVATE_VBL;
114 m->info.yoffset = offset / m->finfo.line_length;
115 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
116 LOGE("FBIOPUT_VSCREENINFO failed");
117 m->base.unlock(&m->base, buffer);
118 return -errno;
119 }
120 m->currentBuffer = buffer;
121
122 } else {
123 // If we can't do the page_flip, just copy the buffer to the front
124 // FIXME: use copybit HAL instead of memcpy
125
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700126 void* fb_vaddr;
127 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700128
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700129 m->base.lock(&m->base, m->framebuffer,
130 GRALLOC_USAGE_SW_WRITE_RARELY,
131 0, 0, m->info.xres, m->info.yres,
132 &fb_vaddr);
133
134 m->base.lock(&m->base, buffer,
135 GRALLOC_USAGE_SW_READ_RARELY,
136 0, 0, m->info.xres, m->info.yres,
137 &buffer_vaddr);
138
139 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700140
141 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700142 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700143 }
144
145 return 0;
146}
147
148/*****************************************************************************/
149
150int mapFrameBufferLocked(struct private_module_t* module)
151{
152 // already initialized...
153 if (module->framebuffer) {
154 return 0;
155 }
156
157 char const * const device_template[] = {
158 "/dev/graphics/fb%u",
159 "/dev/fb%u",
160 0 };
161
162 int fd = -1;
163 int i=0;
164 char name[64];
165
166 while ((fd==-1) && device_template[i]) {
167 snprintf(name, 64, device_template[i], 0);
168 fd = open(name, O_RDWR, 0);
169 i++;
170 }
171 if (fd < 0)
172 return -errno;
173
174 struct fb_fix_screeninfo finfo;
175 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
176 return -errno;
177
178 struct fb_var_screeninfo info;
179 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
180 return -errno;
181
182 info.reserved[0] = 0;
183 info.reserved[1] = 0;
184 info.reserved[2] = 0;
185 info.xoffset = 0;
186 info.yoffset = 0;
187 info.activate = FB_ACTIVATE_NOW;
188
189 /*
190 * Explicitly request 5/6/5
191 */
192 info.bits_per_pixel = 16;
193 info.red.offset = 11;
194 info.red.length = 5;
195 info.green.offset = 5;
196 info.green.length = 6;
197 info.blue.offset = 0;
198 info.blue.length = 5;
199 info.transp.offset = 0;
200 info.transp.length = 0;
201
202 /*
203 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
204 */
205 info.yres_virtual = info.yres * NUM_BUFFERS;
206
207
208 uint32_t flags = PAGE_FLIP;
209 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
210 info.yres_virtual = info.yres;
211 flags &= ~PAGE_FLIP;
212 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
213 }
214
215 if (info.yres_virtual < info.yres * 2) {
216 // we need at least 2 for page-flipping
217 info.yres_virtual = info.yres;
218 flags &= ~PAGE_FLIP;
219 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
220 info.yres_virtual, info.yres*2);
221 }
222
223 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
224 return -errno;
225
226 int refreshRate = 1000000000000000LLU /
227 (
228 uint64_t( info.upper_margin + info.lower_margin + info.yres )
229 * ( info.left_margin + info.right_margin + info.xres )
230 * info.pixclock
231 );
232
233 if (refreshRate == 0) {
234 // bleagh, bad info from the driver
235 refreshRate = 60*1000; // 60 Hz
236 }
237
238 if (int(info.width) <= 0 || int(info.height) <= 0) {
239 // the driver doesn't return that information
240 // default to 160 dpi
241 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
242 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
243 }
244
245 float xdpi = (info.xres * 25.4f) / info.width;
246 float ydpi = (info.yres * 25.4f) / info.height;
247 float fps = refreshRate / 1000.0f;
248
249 LOGI( "using (fd=%d)\n"
250 "id = %s\n"
251 "xres = %d px\n"
252 "yres = %d px\n"
253 "xres_virtual = %d px\n"
254 "yres_virtual = %d px\n"
255 "bpp = %d\n"
256 "r = %2u:%u\n"
257 "g = %2u:%u\n"
258 "b = %2u:%u\n",
259 fd,
260 finfo.id,
261 info.xres,
262 info.yres,
263 info.xres_virtual,
264 info.yres_virtual,
265 info.bits_per_pixel,
266 info.red.offset, info.red.length,
267 info.green.offset, info.green.length,
268 info.blue.offset, info.blue.length
269 );
270
271 LOGI( "width = %d mm (%f dpi)\n"
272 "height = %d mm (%f dpi)\n"
273 "refresh rate = %.2f Hz\n",
274 info.width, xdpi,
275 info.height, ydpi,
276 fps
277 );
278
279
280 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
281 return -errno;
282
283 if (finfo.smem_len <= 0)
284 return -errno;
285
286
287 module->flags = flags;
288 module->info = info;
289 module->finfo = finfo;
290 module->xdpi = xdpi;
291 module->ydpi = ydpi;
292 module->fps = fps;
293
294 /*
295 * map the framebuffer
296 */
297
298 int err;
299 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
300 module->framebuffer = new private_handle_t(dup(fd), fbSize,
301 private_handle_t::PRIV_FLAGS_USES_PMEM);
302
303 module->numBuffers = info.yres_virtual / info.yres;
304 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700305
Mathias Agopian51156652009-06-09 18:55:49 -0700306 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
307 if (vaddr == MAP_FAILED) {
308 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
309 return -errno;
310 }
311 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700312 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700313 return 0;
314}
315
316static int mapFrameBuffer(struct private_module_t* module)
317{
318 pthread_mutex_lock(&module->lock);
319 int err = mapFrameBufferLocked(module);
320 pthread_mutex_unlock(&module->lock);
321 return err;
322}
323
324/*****************************************************************************/
325
326static int fb_close(struct hw_device_t *dev)
327{
328 fb_context_t* ctx = (fb_context_t*)dev;
329 if (ctx) {
330 free(ctx);
331 }
332 return 0;
333}
334
335int fb_device_open(hw_module_t const* module, const char* name,
336 hw_device_t** device)
337{
338 int status = -EINVAL;
339 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
340
341 alloc_device_t* gralloc_device;
342 status = gralloc_open(module, &gralloc_device);
343 if (status < 0)
344 return status;
345
346 /* initialize our state here */
347 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
348 memset(dev, 0, sizeof(*dev));
349
350 /* initialize the procs */
351 dev->device.common.tag = HARDWARE_DEVICE_TAG;
352 dev->device.common.version = 0;
353 dev->device.common.module = const_cast<hw_module_t*>(module);
354 dev->device.common.close = fb_close;
355 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700356#if SUPPORTS_UPDATE_ON_DEMAND
Mathias Agopiana8a75162009-04-10 14:24:31 -0700357 dev->device.setUpdateRect = fb_setUpdateRect;
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700358#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700359 dev->device.post = fb_post;
360
361 private_module_t* m = (private_module_t*)module;
362 status = mapFrameBuffer(m);
363 if (status >= 0) {
364 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
Mathias Agopian295190f2009-05-05 18:30:52 -0700365 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700366 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
367 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
368 const_cast<int&>(dev->device.stride) = stride;
369 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
370 const_cast<float&>(dev->device.xdpi) = m->xdpi;
371 const_cast<float&>(dev->device.ydpi) = m->ydpi;
372 const_cast<float&>(dev->device.fps) = m->fps;
373 const_cast<int&>(dev->device.minSwapInterval) = 1;
374 const_cast<int&>(dev->device.maxSwapInterval) = 1;
375
Mathias Agopiancdb66fb2009-07-06 20:49:05 -0700376 if (!strcmp(m->finfo.id, EMULATOR_DEV_FB_ID)) {
377 LOGD("I think we're running on the emulator, "
378 "turning UPDATE_ON_DEMAND off");
379 dev->device.setUpdateRect = 0;
380 }
381
Mathias Agopiana8a75162009-04-10 14:24:31 -0700382 *device = &dev->device.common;
383 }
384 }
385 return status;
386}