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