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