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