blob: 486e27abb29ec8ebe7226e63125d49304a2c48f5 [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
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +053045// Set TARGET_USE_PAN_DISPLAY to true at compile time if the
46// board uses FBIOPAN_DISPLAY to setup page flipping, otherwise
47// default ioctl to do page-flipping is FBIOPUT_VSCREENINFO.
48#ifndef USE_PAN_DISPLAY
49#define USE_PAN_DISPLAY 0
50#endif
51
Mathias Agopiancdb66fb2009-07-06 20:49:05 -070052// numbers of buffers for page flipping
Mathias Agopiana8a75162009-04-10 14:24:31 -070053#define NUM_BUFFERS 2
54
55
56enum {
57 PAGE_FLIP = 0x00000001,
58 LOCKED = 0x00000002
59};
60
61struct fb_context_t {
62 framebuffer_device_t device;
63};
64
65/*****************************************************************************/
66
67static int fb_setSwapInterval(struct framebuffer_device_t* dev,
68 int interval)
69{
70 fb_context_t* ctx = (fb_context_t*)dev;
71 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
72 return -EINVAL;
73 // FIXME: implement fb_setSwapInterval
74 return 0;
75}
76
77static int fb_setUpdateRect(struct framebuffer_device_t* dev,
78 int l, int t, int w, int h)
79{
80 if (((w|h) <= 0) || ((l|t)<0))
81 return -EINVAL;
82
83 fb_context_t* ctx = (fb_context_t*)dev;
84 private_module_t* m = reinterpret_cast<private_module_t*>(
85 dev->common.module);
86 m->info.reserved[0] = 0x54445055; // "UPDT";
87 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
88 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
89 return 0;
90}
91
92static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
93{
94 if (private_handle_t::validate(buffer) < 0)
95 return -EINVAL;
96
97 fb_context_t* ctx = (fb_context_t*)dev;
98
99 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
100 private_module_t* m = reinterpret_cast<private_module_t*>(
101 dev->common.module);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700102
103 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700104 const size_t offset = hnd->base - m->framebuffer->base;
105 m->info.activate = FB_ACTIVATE_VBL;
106 m->info.yoffset = offset / m->finfo.line_length;
107 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
Steve Block60d056b2012-01-08 10:17:53 +0000108 ALOGE("FBIOPUT_VSCREENINFO failed");
Mathias Agopiana8a75162009-04-10 14:24:31 -0700109 m->base.unlock(&m->base, buffer);
110 return -errno;
111 }
112 m->currentBuffer = buffer;
113
114 } else {
115 // If we can't do the page_flip, just copy the buffer to the front
116 // FIXME: use copybit HAL instead of memcpy
117
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700118 void* fb_vaddr;
119 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700120
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700121 m->base.lock(&m->base, m->framebuffer,
122 GRALLOC_USAGE_SW_WRITE_RARELY,
123 0, 0, m->info.xres, m->info.yres,
124 &fb_vaddr);
125
126 m->base.lock(&m->base, buffer,
127 GRALLOC_USAGE_SW_READ_RARELY,
128 0, 0, m->info.xres, m->info.yres,
129 &buffer_vaddr);
130
131 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700132
133 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700134 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700135 }
136
137 return 0;
138}
139
140/*****************************************************************************/
141
142int mapFrameBufferLocked(struct private_module_t* module)
143{
144 // already initialized...
145 if (module->framebuffer) {
146 return 0;
147 }
148
149 char const * const device_template[] = {
150 "/dev/graphics/fb%u",
151 "/dev/fb%u",
152 0 };
153
154 int fd = -1;
155 int i=0;
156 char name[64];
157
158 while ((fd==-1) && device_template[i]) {
159 snprintf(name, 64, device_template[i], 0);
160 fd = open(name, O_RDWR, 0);
161 i++;
162 }
163 if (fd < 0)
164 return -errno;
165
166 struct fb_fix_screeninfo finfo;
167 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
168 return -errno;
169
170 struct fb_var_screeninfo info;
171 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
172 return -errno;
173
174 info.reserved[0] = 0;
175 info.reserved[1] = 0;
176 info.reserved[2] = 0;
177 info.xoffset = 0;
178 info.yoffset = 0;
179 info.activate = FB_ACTIVATE_NOW;
180
181 /*
Mathias Agopiana8a75162009-04-10 14:24:31 -0700182 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
183 */
184 info.yres_virtual = info.yres * NUM_BUFFERS;
185
186
187 uint32_t flags = PAGE_FLIP;
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +0530188#if USE_PAN_DISPLAY
189 if (ioctl(fd, FBIOPAN_DISPLAY, &info) == -1) {
190 ALOGW("FBIOPAN_DISPLAY failed, page flipping not supported");
191#else
Mathias Agopiana8a75162009-04-10 14:24:31 -0700192 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +0530193 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
194#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700195 info.yres_virtual = info.yres;
196 flags &= ~PAGE_FLIP;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700197 }
198
199 if (info.yres_virtual < info.yres * 2) {
200 // we need at least 2 for page-flipping
201 info.yres_virtual = info.yres;
202 flags &= ~PAGE_FLIP;
Steve Block22d35132012-01-05 23:27:52 +0000203 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Mathias Agopiana8a75162009-04-10 14:24:31 -0700204 info.yres_virtual, info.yres*2);
205 }
206
207 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
208 return -errno;
209
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100210 uint64_t refreshQuotient =
Mathias Agopiana8a75162009-04-10 14:24:31 -0700211 (
212 uint64_t( info.upper_margin + info.lower_margin + info.yres )
213 * ( info.left_margin + info.right_margin + info.xres )
214 * info.pixclock
215 );
216
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100217 /* Beware, info.pixclock might be 0 under emulation, so avoid a
218 * division-by-0 here (SIGFPE on ARM) */
219 int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
220
Mathias Agopiana8a75162009-04-10 14:24:31 -0700221 if (refreshRate == 0) {
222 // bleagh, bad info from the driver
223 refreshRate = 60*1000; // 60 Hz
224 }
225
226 if (int(info.width) <= 0 || int(info.height) <= 0) {
227 // the driver doesn't return that information
228 // default to 160 dpi
229 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
230 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
231 }
232
233 float xdpi = (info.xres * 25.4f) / info.width;
234 float ydpi = (info.yres * 25.4f) / info.height;
235 float fps = refreshRate / 1000.0f;
236
Steve Blockb224b782012-01-04 20:07:12 +0000237 ALOGI( "using (fd=%d)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700238 "id = %s\n"
239 "xres = %d px\n"
240 "yres = %d px\n"
241 "xres_virtual = %d px\n"
242 "yres_virtual = %d px\n"
243 "bpp = %d\n"
244 "r = %2u:%u\n"
245 "g = %2u:%u\n"
246 "b = %2u:%u\n",
247 fd,
248 finfo.id,
249 info.xres,
250 info.yres,
251 info.xres_virtual,
252 info.yres_virtual,
253 info.bits_per_pixel,
254 info.red.offset, info.red.length,
255 info.green.offset, info.green.length,
256 info.blue.offset, info.blue.length
257 );
258
Steve Blockb224b782012-01-04 20:07:12 +0000259 ALOGI( "width = %d mm (%f dpi)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700260 "height = %d mm (%f dpi)\n"
261 "refresh rate = %.2f Hz\n",
262 info.width, xdpi,
263 info.height, ydpi,
264 fps
265 );
266
267
268 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
269 return -errno;
270
271 if (finfo.smem_len <= 0)
272 return -errno;
273
274
275 module->flags = flags;
276 module->info = info;
277 module->finfo = finfo;
278 module->xdpi = xdpi;
279 module->ydpi = ydpi;
280 module->fps = fps;
281
282 /*
283 * map the framebuffer
284 */
285
286 int err;
287 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
Mathias Agopianf96b2062009-12-14 18:27:09 -0800288 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700289
290 module->numBuffers = info.yres_virtual / info.yres;
291 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700292
Mathias Agopian51156652009-06-09 18:55:49 -0700293 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
294 if (vaddr == MAP_FAILED) {
Steve Block60d056b2012-01-08 10:17:53 +0000295 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -0700296 return -errno;
297 }
298 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700299 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700300 return 0;
301}
302
303static int mapFrameBuffer(struct private_module_t* module)
304{
305 pthread_mutex_lock(&module->lock);
306 int err = mapFrameBufferLocked(module);
307 pthread_mutex_unlock(&module->lock);
308 return err;
309}
310
311/*****************************************************************************/
312
313static int fb_close(struct hw_device_t *dev)
314{
315 fb_context_t* ctx = (fb_context_t*)dev;
316 if (ctx) {
317 free(ctx);
318 }
319 return 0;
320}
321
322int fb_device_open(hw_module_t const* module, const char* name,
323 hw_device_t** device)
324{
325 int status = -EINVAL;
326 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700327 /* initialize our state here */
328 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
329 memset(dev, 0, sizeof(*dev));
330
331 /* initialize the procs */
332 dev->device.common.tag = HARDWARE_DEVICE_TAG;
333 dev->device.common.version = 0;
334 dev->device.common.module = const_cast<hw_module_t*>(module);
335 dev->device.common.close = fb_close;
336 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700337 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700338 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700339
340 private_module_t* m = (private_module_t*)module;
341 status = mapFrameBuffer(m);
342 if (status >= 0) {
343 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100344 int format = (m->info.bits_per_pixel == 32)
Bernhard Rosenkraenzer5e3ac6b2014-02-21 01:38:44 +0530345 ? (m->info.red.offset ? HAL_PIXEL_FORMAT_BGRA_8888 : HAL_PIXEL_FORMAT_RGBX_8888)
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100346 : HAL_PIXEL_FORMAT_RGB_565;
Mathias Agopian295190f2009-05-05 18:30:52 -0700347 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700348 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
349 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
350 const_cast<int&>(dev->device.stride) = stride;
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100351 const_cast<int&>(dev->device.format) = format;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700352 const_cast<float&>(dev->device.xdpi) = m->xdpi;
353 const_cast<float&>(dev->device.ydpi) = m->ydpi;
354 const_cast<float&>(dev->device.fps) = m->fps;
355 const_cast<int&>(dev->device.minSwapInterval) = 1;
356 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700357 *device = &dev->device.common;
358 }
359 }
360 return status;
361}