blob: 248a2fc5b9e954e1e5d8441e5e4e597d7edfc83e [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 Agopiana8a75162009-04-10 14:24:31 -070044#define NUM_BUFFERS 2
45
46
47enum {
48 PAGE_FLIP = 0x00000001,
49 LOCKED = 0x00000002
50};
51
52struct fb_context_t {
53 framebuffer_device_t device;
54};
55
56/*****************************************************************************/
57
58static int fb_setSwapInterval(struct framebuffer_device_t* dev,
59 int interval)
60{
61 fb_context_t* ctx = (fb_context_t*)dev;
62 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
63 return -EINVAL;
64 // FIXME: implement fb_setSwapInterval
65 return 0;
66}
67
68static int fb_setUpdateRect(struct framebuffer_device_t* dev,
69 int l, int t, int w, int h)
70{
71 if (((w|h) <= 0) || ((l|t)<0))
72 return -EINVAL;
73
74 fb_context_t* ctx = (fb_context_t*)dev;
75 private_module_t* m = reinterpret_cast<private_module_t*>(
76 dev->common.module);
77 m->info.reserved[0] = 0x54445055; // "UPDT";
78 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
79 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
80 return 0;
81}
82
83static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
84{
85 if (private_handle_t::validate(buffer) < 0)
86 return -EINVAL;
87
88 fb_context_t* ctx = (fb_context_t*)dev;
89
90 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
91 private_module_t* m = reinterpret_cast<private_module_t*>(
92 dev->common.module);
93
94 if (m->currentBuffer) {
95 m->base.unlock(&m->base, m->currentBuffer);
96 m->currentBuffer = 0;
97 }
98
99 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
100
101 m->base.lock(&m->base, buffer,
102 private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700103 0, 0, m->info.xres, m->info.yres, NULL);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700104
105 const size_t offset = hnd->base - m->framebuffer->base;
106 m->info.activate = FB_ACTIVATE_VBL;
107 m->info.yoffset = offset / m->finfo.line_length;
108 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
109 LOGE("FBIOPUT_VSCREENINFO failed");
110 m->base.unlock(&m->base, buffer);
111 return -errno;
112 }
113 m->currentBuffer = buffer;
114
115 } else {
116 // If we can't do the page_flip, just copy the buffer to the front
117 // FIXME: use copybit HAL instead of memcpy
118
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700119 void* fb_vaddr;
120 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700121
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700122 m->base.lock(&m->base, m->framebuffer,
123 GRALLOC_USAGE_SW_WRITE_RARELY,
124 0, 0, m->info.xres, m->info.yres,
125 &fb_vaddr);
126
127 m->base.lock(&m->base, buffer,
128 GRALLOC_USAGE_SW_READ_RARELY,
129 0, 0, m->info.xres, m->info.yres,
130 &buffer_vaddr);
131
132 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700133
134 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700135 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700136 }
137
138 return 0;
139}
140
141/*****************************************************************************/
142
143int mapFrameBufferLocked(struct private_module_t* module)
144{
145 // already initialized...
146 if (module->framebuffer) {
147 return 0;
148 }
149
150 char const * const device_template[] = {
151 "/dev/graphics/fb%u",
152 "/dev/fb%u",
153 0 };
154
155 int fd = -1;
156 int i=0;
157 char name[64];
158
159 while ((fd==-1) && device_template[i]) {
160 snprintf(name, 64, device_template[i], 0);
161 fd = open(name, O_RDWR, 0);
162 i++;
163 }
164 if (fd < 0)
165 return -errno;
166
167 struct fb_fix_screeninfo finfo;
168 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
169 return -errno;
170
171 struct fb_var_screeninfo info;
172 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
173 return -errno;
174
175 info.reserved[0] = 0;
176 info.reserved[1] = 0;
177 info.reserved[2] = 0;
178 info.xoffset = 0;
179 info.yoffset = 0;
180 info.activate = FB_ACTIVATE_NOW;
181
182 /*
183 * Explicitly request 5/6/5
184 */
185 info.bits_per_pixel = 16;
186 info.red.offset = 11;
187 info.red.length = 5;
188 info.green.offset = 5;
189 info.green.length = 6;
190 info.blue.offset = 0;
191 info.blue.length = 5;
192 info.transp.offset = 0;
193 info.transp.length = 0;
194
195 /*
196 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
197 */
198 info.yres_virtual = info.yres * NUM_BUFFERS;
199
200
201 uint32_t flags = PAGE_FLIP;
202 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
203 info.yres_virtual = info.yres;
204 flags &= ~PAGE_FLIP;
205 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
206 }
207
208 if (info.yres_virtual < info.yres * 2) {
209 // we need at least 2 for page-flipping
210 info.yres_virtual = info.yres;
211 flags &= ~PAGE_FLIP;
212 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
213 info.yres_virtual, info.yres*2);
214 }
215
216 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
217 return -errno;
218
219 int refreshRate = 1000000000000000LLU /
220 (
221 uint64_t( info.upper_margin + info.lower_margin + info.yres )
222 * ( info.left_margin + info.right_margin + info.xres )
223 * info.pixclock
224 );
225
226 if (refreshRate == 0) {
227 // bleagh, bad info from the driver
228 refreshRate = 60*1000; // 60 Hz
229 }
230
231 if (int(info.width) <= 0 || int(info.height) <= 0) {
232 // the driver doesn't return that information
233 // default to 160 dpi
234 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
235 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
236 }
237
238 float xdpi = (info.xres * 25.4f) / info.width;
239 float ydpi = (info.yres * 25.4f) / info.height;
240 float fps = refreshRate / 1000.0f;
241
242 LOGI( "using (fd=%d)\n"
243 "id = %s\n"
244 "xres = %d px\n"
245 "yres = %d px\n"
246 "xres_virtual = %d px\n"
247 "yres_virtual = %d px\n"
248 "bpp = %d\n"
249 "r = %2u:%u\n"
250 "g = %2u:%u\n"
251 "b = %2u:%u\n",
252 fd,
253 finfo.id,
254 info.xres,
255 info.yres,
256 info.xres_virtual,
257 info.yres_virtual,
258 info.bits_per_pixel,
259 info.red.offset, info.red.length,
260 info.green.offset, info.green.length,
261 info.blue.offset, info.blue.length
262 );
263
264 LOGI( "width = %d mm (%f dpi)\n"
265 "height = %d mm (%f dpi)\n"
266 "refresh rate = %.2f Hz\n",
267 info.width, xdpi,
268 info.height, ydpi,
269 fps
270 );
271
272
273 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
274 return -errno;
275
276 if (finfo.smem_len <= 0)
277 return -errno;
278
279
280 module->flags = flags;
281 module->info = info;
282 module->finfo = finfo;
283 module->xdpi = xdpi;
284 module->ydpi = ydpi;
285 module->fps = fps;
286
287 /*
288 * map the framebuffer
289 */
290
291 int err;
292 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
293 module->framebuffer = new private_handle_t(dup(fd), fbSize,
294 private_handle_t::PRIV_FLAGS_USES_PMEM);
295
296 module->numBuffers = info.yres_virtual / info.yres;
297 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700298
Mathias Agopian51156652009-06-09 18:55:49 -0700299 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
300 if (vaddr == MAP_FAILED) {
301 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
302 return -errno;
303 }
304 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700305 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700306 return 0;
307}
308
309static int mapFrameBuffer(struct private_module_t* module)
310{
311 pthread_mutex_lock(&module->lock);
312 int err = mapFrameBufferLocked(module);
313 pthread_mutex_unlock(&module->lock);
314 return err;
315}
316
317/*****************************************************************************/
318
319static int fb_close(struct hw_device_t *dev)
320{
321 fb_context_t* ctx = (fb_context_t*)dev;
322 if (ctx) {
323 free(ctx);
324 }
325 return 0;
326}
327
328int fb_device_open(hw_module_t const* module, const char* name,
329 hw_device_t** device)
330{
331 int status = -EINVAL;
332 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
333
334 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 Agopianf5cf8f82009-05-07 17:39:31 -0700349#if SUPPORTS_UPDATE_ON_DEMAND
Mathias Agopiana8a75162009-04-10 14:24:31 -0700350 dev->device.setUpdateRect = fb_setUpdateRect;
Mathias Agopianf5cf8f82009-05-07 17:39:31 -0700351#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700352 dev->device.post = fb_post;
353
354 private_module_t* m = (private_module_t*)module;
355 status = mapFrameBuffer(m);
356 if (status >= 0) {
357 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
Mathias Agopian295190f2009-05-05 18:30:52 -0700358 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700359 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
360 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
361 const_cast<int&>(dev->device.stride) = stride;
362 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
363 const_cast<float&>(dev->device.xdpi) = m->xdpi;
364 const_cast<float&>(dev->device.ydpi) = m->ydpi;
365 const_cast<float&>(dev->device.fps) = m->fps;
366 const_cast<int&>(dev->device.minSwapInterval) = 1;
367 const_cast<int&>(dev->device.maxSwapInterval) = 1;
368
369 *device = &dev->device.common;
370 }
371 }
372 return status;
373}