blob: 01951034190ae560dca75d1c593958cf13160fc2 [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 /*
175 * Explicitly request 5/6/5
176 */
177 info.bits_per_pixel = 16;
178 info.red.offset = 11;
179 info.red.length = 5;
180 info.green.offset = 5;
181 info.green.length = 6;
182 info.blue.offset = 0;
183 info.blue.length = 5;
184 info.transp.offset = 0;
185 info.transp.length = 0;
186
187 /*
188 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
189 */
190 info.yres_virtual = info.yres * NUM_BUFFERS;
191
192
193 uint32_t flags = PAGE_FLIP;
194 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
195 info.yres_virtual = info.yres;
196 flags &= ~PAGE_FLIP;
197 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
198 }
199
200 if (info.yres_virtual < info.yres * 2) {
201 // we need at least 2 for page-flipping
202 info.yres_virtual = info.yres;
203 flags &= ~PAGE_FLIP;
204 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
205 info.yres_virtual, info.yres*2);
206 }
207
208 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
209 return -errno;
210
211 int refreshRate = 1000000000000000LLU /
212 (
213 uint64_t( info.upper_margin + info.lower_margin + info.yres )
214 * ( info.left_margin + info.right_margin + info.xres )
215 * info.pixclock
216 );
217
218 if (refreshRate == 0) {
219 // bleagh, bad info from the driver
220 refreshRate = 60*1000; // 60 Hz
221 }
222
223 if (int(info.width) <= 0 || int(info.height) <= 0) {
224 // the driver doesn't return that information
225 // default to 160 dpi
226 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
227 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
228 }
229
230 float xdpi = (info.xres * 25.4f) / info.width;
231 float ydpi = (info.yres * 25.4f) / info.height;
232 float fps = refreshRate / 1000.0f;
233
234 LOGI( "using (fd=%d)\n"
235 "id = %s\n"
236 "xres = %d px\n"
237 "yres = %d px\n"
238 "xres_virtual = %d px\n"
239 "yres_virtual = %d px\n"
240 "bpp = %d\n"
241 "r = %2u:%u\n"
242 "g = %2u:%u\n"
243 "b = %2u:%u\n",
244 fd,
245 finfo.id,
246 info.xres,
247 info.yres,
248 info.xres_virtual,
249 info.yres_virtual,
250 info.bits_per_pixel,
251 info.red.offset, info.red.length,
252 info.green.offset, info.green.length,
253 info.blue.offset, info.blue.length
254 );
255
256 LOGI( "width = %d mm (%f dpi)\n"
257 "height = %d mm (%f dpi)\n"
258 "refresh rate = %.2f Hz\n",
259 info.width, xdpi,
260 info.height, ydpi,
261 fps
262 );
263
264
265 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
266 return -errno;
267
268 if (finfo.smem_len <= 0)
269 return -errno;
270
271
272 module->flags = flags;
273 module->info = info;
274 module->finfo = finfo;
275 module->xdpi = xdpi;
276 module->ydpi = ydpi;
277 module->fps = fps;
278
279 /*
280 * map the framebuffer
281 */
282
283 int err;
284 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
Mathias Agopianf96b2062009-12-14 18:27:09 -0800285 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700286
287 module->numBuffers = info.yres_virtual / info.yres;
288 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700289
Mathias Agopian51156652009-06-09 18:55:49 -0700290 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
291 if (vaddr == MAP_FAILED) {
292 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
293 return -errno;
294 }
295 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700296 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700297 return 0;
298}
299
300static int mapFrameBuffer(struct private_module_t* module)
301{
302 pthread_mutex_lock(&module->lock);
303 int err = mapFrameBufferLocked(module);
304 pthread_mutex_unlock(&module->lock);
305 return err;
306}
307
308/*****************************************************************************/
309
310static int fb_close(struct hw_device_t *dev)
311{
312 fb_context_t* ctx = (fb_context_t*)dev;
313 if (ctx) {
314 free(ctx);
315 }
316 return 0;
317}
318
319int fb_device_open(hw_module_t const* module, const char* name,
320 hw_device_t** device)
321{
322 int status = -EINVAL;
323 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700324 alloc_device_t* gralloc_device;
325 status = gralloc_open(module, &gralloc_device);
326 if (status < 0)
327 return status;
328
329 /* initialize our state here */
330 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
331 memset(dev, 0, sizeof(*dev));
332
333 /* initialize the procs */
334 dev->device.common.tag = HARDWARE_DEVICE_TAG;
335 dev->device.common.version = 0;
336 dev->device.common.module = const_cast<hw_module_t*>(module);
337 dev->device.common.close = fb_close;
338 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700339 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700340 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700341
342 private_module_t* m = (private_module_t*)module;
343 status = mapFrameBuffer(m);
344 if (status >= 0) {
345 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
Mathias Agopian295190f2009-05-05 18:30:52 -0700346 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700347 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
348 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
349 const_cast<int&>(dev->device.stride) = stride;
350 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
351 const_cast<float&>(dev->device.xdpi) = m->xdpi;
352 const_cast<float&>(dev->device.ydpi) = m->ydpi;
353 const_cast<float&>(dev->device.fps) = m->fps;
354 const_cast<int&>(dev->device.minSwapInterval) = 1;
355 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700356 *device = &dev->device.common;
357 }
358 }
359 return status;
360}