blob: 5f97032a73de9e12d7655bbf485cbf92bd2d5fb4 [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 Agopiancdb66fb2009-07-06 20:49:05 -070044// numbers of buffers for page flipping
Mathias Agopiana8a75162009-04-10 14:24:31 -070045#define NUM_BUFFERS 2
46
47
48enum {
49 PAGE_FLIP = 0x00000001,
50 LOCKED = 0x00000002
51};
52
53struct fb_context_t {
54 framebuffer_device_t device;
55};
56
57/*****************************************************************************/
58
59static int fb_setSwapInterval(struct framebuffer_device_t* dev,
60 int interval)
61{
62 fb_context_t* ctx = (fb_context_t*)dev;
63 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
64 return -EINVAL;
65 // FIXME: implement fb_setSwapInterval
66 return 0;
67}
68
69static int fb_setUpdateRect(struct framebuffer_device_t* dev,
70 int l, int t, int w, int h)
71{
72 if (((w|h) <= 0) || ((l|t)<0))
73 return -EINVAL;
74
75 fb_context_t* ctx = (fb_context_t*)dev;
76 private_module_t* m = reinterpret_cast<private_module_t*>(
77 dev->common.module);
78 m->info.reserved[0] = 0x54445055; // "UPDT";
79 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
80 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
81 return 0;
82}
83
84static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
85{
86 if (private_handle_t::validate(buffer) < 0)
87 return -EINVAL;
88
89 fb_context_t* ctx = (fb_context_t*)dev;
90
91 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
92 private_module_t* m = reinterpret_cast<private_module_t*>(
93 dev->common.module);
94
95 if (m->currentBuffer) {
96 m->base.unlock(&m->base, m->currentBuffer);
97 m->currentBuffer = 0;
98 }
99
100 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
101
102 m->base.lock(&m->base, buffer,
103 private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700104 0, 0, m->info.xres, m->info.yres, NULL);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700105
106 const size_t offset = hnd->base - m->framebuffer->base;
107 m->info.activate = FB_ACTIVATE_VBL;
108 m->info.yoffset = offset / m->finfo.line_length;
109 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
110 LOGE("FBIOPUT_VSCREENINFO failed");
111 m->base.unlock(&m->base, buffer);
112 return -errno;
113 }
114 m->currentBuffer = buffer;
115
116 } else {
117 // If we can't do the page_flip, just copy the buffer to the front
118 // FIXME: use copybit HAL instead of memcpy
119
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700120 void* fb_vaddr;
121 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700122
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700123 m->base.lock(&m->base, m->framebuffer,
124 GRALLOC_USAGE_SW_WRITE_RARELY,
125 0, 0, m->info.xres, m->info.yres,
126 &fb_vaddr);
127
128 m->base.lock(&m->base, buffer,
129 GRALLOC_USAGE_SW_READ_RARELY,
130 0, 0, m->info.xres, m->info.yres,
131 &buffer_vaddr);
132
133 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700134
135 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700136 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700137 }
138
139 return 0;
140}
141
142/*****************************************************************************/
143
144int mapFrameBufferLocked(struct private_module_t* module)
145{
146 // already initialized...
147 if (module->framebuffer) {
148 return 0;
149 }
150
151 char const * const device_template[] = {
152 "/dev/graphics/fb%u",
153 "/dev/fb%u",
154 0 };
155
156 int fd = -1;
157 int i=0;
158 char name[64];
159
160 while ((fd==-1) && device_template[i]) {
161 snprintf(name, 64, device_template[i], 0);
162 fd = open(name, O_RDWR, 0);
163 i++;
164 }
165 if (fd < 0)
166 return -errno;
167
168 struct fb_fix_screeninfo finfo;
169 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
170 return -errno;
171
172 struct fb_var_screeninfo info;
173 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
174 return -errno;
175
176 info.reserved[0] = 0;
177 info.reserved[1] = 0;
178 info.reserved[2] = 0;
179 info.xoffset = 0;
180 info.yoffset = 0;
181 info.activate = FB_ACTIVATE_NOW;
182
183 /*
184 * Explicitly request 5/6/5
185 */
186 info.bits_per_pixel = 16;
187 info.red.offset = 11;
188 info.red.length = 5;
189 info.green.offset = 5;
190 info.green.length = 6;
191 info.blue.offset = 0;
192 info.blue.length = 5;
193 info.transp.offset = 0;
194 info.transp.length = 0;
195
196 /*
197 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
198 */
199 info.yres_virtual = info.yres * NUM_BUFFERS;
200
201
202 uint32_t flags = PAGE_FLIP;
203 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
204 info.yres_virtual = info.yres;
205 flags &= ~PAGE_FLIP;
206 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
207 }
208
209 if (info.yres_virtual < info.yres * 2) {
210 // we need at least 2 for page-flipping
211 info.yres_virtual = info.yres;
212 flags &= ~PAGE_FLIP;
213 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
214 info.yres_virtual, info.yres*2);
215 }
216
217 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
218 return -errno;
219
220 int refreshRate = 1000000000000000LLU /
221 (
222 uint64_t( info.upper_margin + info.lower_margin + info.yres )
223 * ( info.left_margin + info.right_margin + info.xres )
224 * info.pixclock
225 );
226
227 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);
294 module->framebuffer = new private_handle_t(dup(fd), fbSize,
295 private_handle_t::PRIV_FLAGS_USES_PMEM);
296
297 module->numBuffers = info.yres_virtual / info.yres;
298 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700299
Mathias Agopian51156652009-06-09 18:55:49 -0700300 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
301 if (vaddr == MAP_FAILED) {
302 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
303 return -errno;
304 }
305 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700306 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700307 return 0;
308}
309
310static int mapFrameBuffer(struct private_module_t* module)
311{
312 pthread_mutex_lock(&module->lock);
313 int err = mapFrameBufferLocked(module);
314 pthread_mutex_unlock(&module->lock);
315 return err;
316}
317
318/*****************************************************************************/
319
320static int fb_close(struct hw_device_t *dev)
321{
322 fb_context_t* ctx = (fb_context_t*)dev;
323 if (ctx) {
324 free(ctx);
325 }
326 return 0;
327}
328
329int fb_device_open(hw_module_t const* module, const char* name,
330 hw_device_t** device)
331{
332 int status = -EINVAL;
333 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700334 alloc_device_t* gralloc_device;
335 status = gralloc_open(module, &gralloc_device);
336 if (status < 0)
337 return status;
338
339 /* initialize our state here */
340 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
341 memset(dev, 0, sizeof(*dev));
342
343 /* initialize the procs */
344 dev->device.common.tag = HARDWARE_DEVICE_TAG;
345 dev->device.common.version = 0;
346 dev->device.common.module = const_cast<hw_module_t*>(module);
347 dev->device.common.close = fb_close;
348 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700349 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700350 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700351
352 private_module_t* m = (private_module_t*)module;
353 status = mapFrameBuffer(m);
354 if (status >= 0) {
355 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
Mathias Agopian295190f2009-05-05 18:30:52 -0700356 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700357 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
358 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
359 const_cast<int&>(dev->device.stride) = stride;
360 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
361 const_cast<float&>(dev->device.xdpi) = m->xdpi;
362 const_cast<float&>(dev->device.ydpi) = m->ydpi;
363 const_cast<float&>(dev->device.fps) = m->fps;
364 const_cast<int&>(dev->device.minSwapInterval) = 1;
365 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700366 *device = &dev->device.common;
367 }
368 }
369 return status;
370}