blob: 2d0fa255cc9fa0a5664457e473b08e30f612039d [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>
29
30#include <cutils/log.h>
31#include <cutils/atomic.h>
32
33#if HAVE_ANDROID_OS
34#include <linux/fb.h>
35#endif
36
37#include "gralloc_priv.h"
38
39/*****************************************************************************/
40
Mathias Agopianf5cf8f82009-05-07 17:39:31 -070041// should be a build option
42#define SUPPORTS_UPDATE_ON_DEMAND 1
43
Mathias Agopiancdb66fb2009-07-06 20:49:05 -070044// fb_fix_screeninfo::id returned on the emulator
45#define EMULATOR_DEV_FB_ID ""
46
47// numbers of buffers for page flipping
Mathias Agopiana8a75162009-04-10 14:24:31 -070048#define NUM_BUFFERS 2
49
50
51enum {
52 PAGE_FLIP = 0x00000001,
53 LOCKED = 0x00000002
54};
55
56struct fb_context_t {
57 framebuffer_device_t device;
58};
59
60/*****************************************************************************/
61
62static int fb_setSwapInterval(struct framebuffer_device_t* dev,
63 int interval)
64{
65 fb_context_t* ctx = (fb_context_t*)dev;
66 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
67 return -EINVAL;
68 // FIXME: implement fb_setSwapInterval
69 return 0;
70}
71
72static int fb_setUpdateRect(struct framebuffer_device_t* dev,
73 int l, int t, int w, int h)
74{
75 if (((w|h) <= 0) || ((l|t)<0))
76 return -EINVAL;
77
78 fb_context_t* ctx = (fb_context_t*)dev;
79 private_module_t* m = reinterpret_cast<private_module_t*>(
80 dev->common.module);
81 m->info.reserved[0] = 0x54445055; // "UPDT";
82 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
83 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
84 return 0;
85}
86
87static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
88{
89 if (private_handle_t::validate(buffer) < 0)
90 return -EINVAL;
91
92 fb_context_t* ctx = (fb_context_t*)dev;
93
94 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
95 private_module_t* m = reinterpret_cast<private_module_t*>(
96 dev->common.module);
97
98 if (m->currentBuffer) {
99 m->base.unlock(&m->base, m->currentBuffer);
100 m->currentBuffer = 0;
101 }
102
103 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
104
105 m->base.lock(&m->base, buffer,
106 private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700107 0, 0, m->info.xres, m->info.yres, NULL);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700108
109 const size_t offset = hnd->base - m->framebuffer->base;
110 m->info.activate = FB_ACTIVATE_VBL;
111 m->info.yoffset = offset / m->finfo.line_length;
112 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
113 LOGE("FBIOPUT_VSCREENINFO failed");
114 m->base.unlock(&m->base, buffer);
115 return -errno;
116 }
117 m->currentBuffer = buffer;
118
119 } else {
120 // If we can't do the page_flip, just copy the buffer to the front
121 // FIXME: use copybit HAL instead of memcpy
122
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700123 void* fb_vaddr;
124 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700125
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700126 m->base.lock(&m->base, m->framebuffer,
127 GRALLOC_USAGE_SW_WRITE_RARELY,
128 0, 0, m->info.xres, m->info.yres,
129 &fb_vaddr);
130
131 m->base.lock(&m->base, buffer,
132 GRALLOC_USAGE_SW_READ_RARELY,
133 0, 0, m->info.xres, m->info.yres,
134 &buffer_vaddr);
135
136 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700137
138 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700139 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700140 }
141
142 return 0;
143}
144
145/*****************************************************************************/
146
147int mapFrameBufferLocked(struct private_module_t* module)
148{
149 // already initialized...
150 if (module->framebuffer) {
151 return 0;
152 }
153
154 char const * const device_template[] = {
155 "/dev/graphics/fb%u",
156 "/dev/fb%u",
157 0 };
158
159 int fd = -1;
160 int i=0;
161 char name[64];
162
163 while ((fd==-1) && device_template[i]) {
164 snprintf(name, 64, device_template[i], 0);
165 fd = open(name, O_RDWR, 0);
166 i++;
167 }
168 if (fd < 0)
169 return -errno;
170
171 struct fb_fix_screeninfo finfo;
172 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
173 return -errno;
174
175 struct fb_var_screeninfo info;
176 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
177 return -errno;
178
179 info.reserved[0] = 0;
180 info.reserved[1] = 0;
181 info.reserved[2] = 0;
182 info.xoffset = 0;
183 info.yoffset = 0;
184 info.activate = FB_ACTIVATE_NOW;
185
186 /*
187 * Explicitly request 5/6/5
188 */
189 info.bits_per_pixel = 16;
190 info.red.offset = 11;
191 info.red.length = 5;
192 info.green.offset = 5;
193 info.green.length = 6;
194 info.blue.offset = 0;
195 info.blue.length = 5;
196 info.transp.offset = 0;
197 info.transp.length = 0;
198
199 /*
200 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
201 */
202 info.yres_virtual = info.yres * NUM_BUFFERS;
203
204
205 uint32_t flags = PAGE_FLIP;
206 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
207 info.yres_virtual = info.yres;
208 flags &= ~PAGE_FLIP;
209 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
210 }
211
212 if (info.yres_virtual < info.yres * 2) {
213 // we need at least 2 for page-flipping
214 info.yres_virtual = info.yres;
215 flags &= ~PAGE_FLIP;
216 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
217 info.yres_virtual, info.yres*2);
218 }
219
220 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
221 return -errno;
222
223 int refreshRate = 1000000000000000LLU /
224 (
225 uint64_t( info.upper_margin + info.lower_margin + info.yres )
226 * ( info.left_margin + info.right_margin + info.xres )
227 * info.pixclock
228 );
229
230 if (refreshRate == 0) {
231 // bleagh, bad info from the driver
232 refreshRate = 60*1000; // 60 Hz
233 }
234
235 if (int(info.width) <= 0 || int(info.height) <= 0) {
236 // the driver doesn't return that information
237 // default to 160 dpi
238 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
239 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
240 }
241
242 float xdpi = (info.xres * 25.4f) / info.width;
243 float ydpi = (info.yres * 25.4f) / info.height;
244 float fps = refreshRate / 1000.0f;
245
246 LOGI( "using (fd=%d)\n"
247 "id = %s\n"
248 "xres = %d px\n"
249 "yres = %d px\n"
250 "xres_virtual = %d px\n"
251 "yres_virtual = %d px\n"
252 "bpp = %d\n"
253 "r = %2u:%u\n"
254 "g = %2u:%u\n"
255 "b = %2u:%u\n",
256 fd,
257 finfo.id,
258 info.xres,
259 info.yres,
260 info.xres_virtual,
261 info.yres_virtual,
262 info.bits_per_pixel,
263 info.red.offset, info.red.length,
264 info.green.offset, info.green.length,
265 info.blue.offset, info.blue.length
266 );
267
268 LOGI( "width = %d mm (%f dpi)\n"
269 "height = %d mm (%f dpi)\n"
270 "refresh rate = %.2f Hz\n",
271 info.width, xdpi,
272 info.height, ydpi,
273 fps
274 );
275
276
277 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
278 return -errno;
279
280 if (finfo.smem_len <= 0)
281 return -errno;
282
283
284 module->flags = flags;
285 module->info = info;
286 module->finfo = finfo;
287 module->xdpi = xdpi;
288 module->ydpi = ydpi;
289 module->fps = fps;
290
291 /*
292 * map the framebuffer
293 */
294
295 int err;
296 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
297 module->framebuffer = new private_handle_t(dup(fd), fbSize,
298 private_handle_t::PRIV_FLAGS_USES_PMEM);
299
300 module->numBuffers = info.yres_virtual / info.yres;
301 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700302
Mathias Agopian51156652009-06-09 18:55:49 -0700303 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
304 if (vaddr == MAP_FAILED) {
305 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
306 return -errno;
307 }
308 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700309 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700310 return 0;
311}
312
313static int mapFrameBuffer(struct private_module_t* module)
314{
315 pthread_mutex_lock(&module->lock);
316 int err = mapFrameBufferLocked(module);
317 pthread_mutex_unlock(&module->lock);
318 return err;
319}
320
321/*****************************************************************************/
322
323static int fb_close(struct hw_device_t *dev)
324{
325 fb_context_t* ctx = (fb_context_t*)dev;
326 if (ctx) {
327 free(ctx);
328 }
329 return 0;
330}
331
332int fb_device_open(hw_module_t const* module, const char* name,
333 hw_device_t** device)
334{
335 int status = -EINVAL;
336 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
337
338 alloc_device_t* gralloc_device;
339 status = gralloc_open(module, &gralloc_device);
340 if (status < 0)
341 return status;
342
343 /* initialize our state here */
344 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
345 memset(dev, 0, sizeof(*dev));
346
347 /* initialize the procs */
348 dev->device.common.tag = HARDWARE_DEVICE_TAG;
349 dev->device.common.version = 0;
350 dev->device.common.module = const_cast<hw_module_t*>(module);
351 dev->device.common.close = fb_close;
352 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700353#if SUPPORTS_UPDATE_ON_DEMAND
Mathias Agopiana8a75162009-04-10 14:24:31 -0700354 dev->device.setUpdateRect = fb_setUpdateRect;
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700355#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700356 dev->device.post = fb_post;
357
358 private_module_t* m = (private_module_t*)module;
359 status = mapFrameBuffer(m);
360 if (status >= 0) {
361 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
Mathias Agopian295190f2009-05-05 18:30:52 -0700362 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700363 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
364 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
365 const_cast<int&>(dev->device.stride) = stride;
366 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
367 const_cast<float&>(dev->device.xdpi) = m->xdpi;
368 const_cast<float&>(dev->device.ydpi) = m->ydpi;
369 const_cast<float&>(dev->device.fps) = m->fps;
370 const_cast<int&>(dev->device.minSwapInterval) = 1;
371 const_cast<int&>(dev->device.maxSwapInterval) = 1;
372
Mathias Agopiancdb66fb2009-07-06 20:49:05 -0700373 if (!strcmp(m->finfo.id, EMULATOR_DEV_FB_ID)) {
374 LOGD("I think we're running on the emulator, "
375 "turning UPDATE_ON_DEMAND off");
376 dev->device.setUpdateRect = 0;
377 }
378
Mathias Agopiana8a75162009-04-10 14:24:31 -0700379 *device = &dev->device.common;
380 }
381 }
382 return status;
383}