blob: 7ef8098df1dfcba1617fa9a08a46abe2cf15d704 [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
Mark Salyzynd88dfe82017-04-11 08:56:09 -070017#include <dlfcn.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/ioctl.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070023#include <sys/mman.h>
24
Mathias Agopiana8a75162009-04-10 14:24:31 -070025#include <cutils/ashmem.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070026#include <cutils/atomic.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070027#include <log/log.h>
28
29#include <hardware/gralloc.h>
30#include <hardware/hardware.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070031
Elliott Hughesca6ccd92015-08-12 16:42:13 -070032#ifdef __ANDROID__
Mathias Agopiana8a75162009-04-10 14:24:31 -070033#include <linux/fb.h>
34#endif
35
36#include "gralloc_priv.h"
Mathias Agopianfc054132009-08-18 17:35:44 -070037#include "gr.h"
Mathias Agopiana8a75162009-04-10 14:24:31 -070038
39/*****************************************************************************/
40
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +053041// 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
Mathias Agopiancdb66fb2009-07-06 20:49:05 -070048// numbers of buffers for page flipping
Mathias Agopiana8a75162009-04-10 14:24:31 -070049#define NUM_BUFFERS 2
50
51
52enum {
53 PAGE_FLIP = 0x00000001,
54 LOCKED = 0x00000002
55};
56
57struct fb_context_t {
58 framebuffer_device_t device;
59};
60
61/*****************************************************************************/
62
63static int fb_setSwapInterval(struct framebuffer_device_t* dev,
64 int interval)
65{
66 fb_context_t* ctx = (fb_context_t*)dev;
67 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
68 return -EINVAL;
69 // FIXME: implement fb_setSwapInterval
70 return 0;
71}
72
73static int fb_setUpdateRect(struct framebuffer_device_t* dev,
74 int l, int t, int w, int h)
75{
76 if (((w|h) <= 0) || ((l|t)<0))
77 return -EINVAL;
78
79 fb_context_t* ctx = (fb_context_t*)dev;
80 private_module_t* m = reinterpret_cast<private_module_t*>(
81 dev->common.module);
82 m->info.reserved[0] = 0x54445055; // "UPDT";
83 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
84 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
85 return 0;
86}
87
88static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
89{
90 if (private_handle_t::validate(buffer) < 0)
91 return -EINVAL;
92
93 fb_context_t* ctx = (fb_context_t*)dev;
94
95 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
96 private_module_t* m = reinterpret_cast<private_module_t*>(
97 dev->common.module);
Mathias Agopiana8a75162009-04-10 14:24:31 -070098
99 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700100 const size_t offset = hnd->base - m->framebuffer->base;
101 m->info.activate = FB_ACTIVATE_VBL;
102 m->info.yoffset = offset / m->finfo.line_length;
103 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
Steve Block60d056b2012-01-08 10:17:53 +0000104 ALOGE("FBIOPUT_VSCREENINFO failed");
Mathias Agopiana8a75162009-04-10 14:24:31 -0700105 m->base.unlock(&m->base, buffer);
106 return -errno;
107 }
108 m->currentBuffer = buffer;
109
110 } else {
111 // If we can't do the page_flip, just copy the buffer to the front
112 // FIXME: use copybit HAL instead of memcpy
113
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700114 void* fb_vaddr;
115 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700116
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700117 m->base.lock(&m->base, m->framebuffer,
118 GRALLOC_USAGE_SW_WRITE_RARELY,
119 0, 0, m->info.xres, m->info.yres,
120 &fb_vaddr);
121
122 m->base.lock(&m->base, buffer,
123 GRALLOC_USAGE_SW_READ_RARELY,
124 0, 0, m->info.xres, m->info.yres,
125 &buffer_vaddr);
126
127 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700128
129 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700130 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700131 }
132
133 return 0;
134}
135
136/*****************************************************************************/
137
138int mapFrameBufferLocked(struct private_module_t* module)
139{
140 // already initialized...
141 if (module->framebuffer) {
142 return 0;
143 }
144
145 char const * const device_template[] = {
146 "/dev/graphics/fb%u",
147 "/dev/fb%u",
148 0 };
149
150 int fd = -1;
151 int i=0;
152 char name[64];
153
154 while ((fd==-1) && device_template[i]) {
155 snprintf(name, 64, device_template[i], 0);
156 fd = open(name, O_RDWR, 0);
157 i++;
158 }
159 if (fd < 0)
160 return -errno;
161
162 struct fb_fix_screeninfo finfo;
163 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
164 return -errno;
165
166 struct fb_var_screeninfo info;
167 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
168 return -errno;
169
170 info.reserved[0] = 0;
171 info.reserved[1] = 0;
172 info.reserved[2] = 0;
173 info.xoffset = 0;
174 info.yoffset = 0;
175 info.activate = FB_ACTIVATE_NOW;
176
177 /*
Mathias Agopiana8a75162009-04-10 14:24:31 -0700178 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
179 */
180 info.yres_virtual = info.yres * NUM_BUFFERS;
181
182
183 uint32_t flags = PAGE_FLIP;
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +0530184#if USE_PAN_DISPLAY
185 if (ioctl(fd, FBIOPAN_DISPLAY, &info) == -1) {
186 ALOGW("FBIOPAN_DISPLAY failed, page flipping not supported");
187#else
Mathias Agopiana8a75162009-04-10 14:24:31 -0700188 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +0530189 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
190#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700191 info.yres_virtual = info.yres;
192 flags &= ~PAGE_FLIP;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700193 }
194
195 if (info.yres_virtual < info.yres * 2) {
196 // we need at least 2 for page-flipping
197 info.yres_virtual = info.yres;
198 flags &= ~PAGE_FLIP;
Steve Block22d35132012-01-05 23:27:52 +0000199 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Mathias Agopiana8a75162009-04-10 14:24:31 -0700200 info.yres_virtual, info.yres*2);
201 }
202
203 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
204 return -errno;
205
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100206 uint64_t refreshQuotient =
Mathias Agopiana8a75162009-04-10 14:24:31 -0700207 (
208 uint64_t( info.upper_margin + info.lower_margin + info.yres )
209 * ( info.left_margin + info.right_margin + info.xres )
210 * info.pixclock
211 );
212
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100213 /* Beware, info.pixclock might be 0 under emulation, so avoid a
214 * division-by-0 here (SIGFPE on ARM) */
215 int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
216
Mathias Agopiana8a75162009-04-10 14:24:31 -0700217 if (refreshRate == 0) {
218 // bleagh, bad info from the driver
219 refreshRate = 60*1000; // 60 Hz
220 }
221
222 if (int(info.width) <= 0 || int(info.height) <= 0) {
223 // the driver doesn't return that information
224 // default to 160 dpi
225 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
226 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
227 }
228
229 float xdpi = (info.xres * 25.4f) / info.width;
230 float ydpi = (info.yres * 25.4f) / info.height;
231 float fps = refreshRate / 1000.0f;
232
Steve Blockb224b782012-01-04 20:07:12 +0000233 ALOGI( "using (fd=%d)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700234 "id = %s\n"
235 "xres = %d px\n"
236 "yres = %d px\n"
237 "xres_virtual = %d px\n"
238 "yres_virtual = %d px\n"
239 "bpp = %d\n"
240 "r = %2u:%u\n"
241 "g = %2u:%u\n"
242 "b = %2u:%u\n",
243 fd,
244 finfo.id,
245 info.xres,
246 info.yres,
247 info.xres_virtual,
248 info.yres_virtual,
249 info.bits_per_pixel,
250 info.red.offset, info.red.length,
251 info.green.offset, info.green.length,
252 info.blue.offset, info.blue.length
253 );
254
Steve Blockb224b782012-01-04 20:07:12 +0000255 ALOGI( "width = %d mm (%f dpi)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700256 "height = %d mm (%f dpi)\n"
257 "refresh rate = %.2f Hz\n",
258 info.width, xdpi,
259 info.height, ydpi,
260 fps
261 );
262
263
264 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
265 return -errno;
266
267 if (finfo.smem_len <= 0)
268 return -errno;
269
270
271 module->flags = flags;
272 module->info = info;
273 module->finfo = finfo;
274 module->xdpi = xdpi;
275 module->ydpi = ydpi;
276 module->fps = fps;
277
278 /*
279 * map the framebuffer
280 */
281
282 int err;
283 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
Mathias Agopianf96b2062009-12-14 18:27:09 -0800284 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700285
286 module->numBuffers = info.yres_virtual / info.yres;
287 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700288
Mathias Agopian51156652009-06-09 18:55:49 -0700289 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
290 if (vaddr == MAP_FAILED) {
Steve Block60d056b2012-01-08 10:17:53 +0000291 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -0700292 return -errno;
293 }
294 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700295 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700296 return 0;
297}
298
299static int mapFrameBuffer(struct private_module_t* module)
300{
301 pthread_mutex_lock(&module->lock);
302 int err = mapFrameBufferLocked(module);
303 pthread_mutex_unlock(&module->lock);
304 return err;
305}
306
307/*****************************************************************************/
308
309static int fb_close(struct hw_device_t *dev)
310{
311 fb_context_t* ctx = (fb_context_t*)dev;
312 if (ctx) {
313 free(ctx);
314 }
315 return 0;
316}
317
318int fb_device_open(hw_module_t const* module, const char* name,
319 hw_device_t** device)
320{
321 int status = -EINVAL;
322 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700323 /* initialize our state here */
324 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
325 memset(dev, 0, sizeof(*dev));
326
327 /* initialize the procs */
328 dev->device.common.tag = HARDWARE_DEVICE_TAG;
329 dev->device.common.version = 0;
330 dev->device.common.module = const_cast<hw_module_t*>(module);
331 dev->device.common.close = fb_close;
332 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700333 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700334 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700335
336 private_module_t* m = (private_module_t*)module;
337 status = mapFrameBuffer(m);
338 if (status >= 0) {
339 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100340 int format = (m->info.bits_per_pixel == 32)
Bernhard Rosenkraenzer5e3ac6b2014-02-21 01:38:44 +0530341 ? (m->info.red.offset ? HAL_PIXEL_FORMAT_BGRA_8888 : HAL_PIXEL_FORMAT_RGBX_8888)
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100342 : HAL_PIXEL_FORMAT_RGB_565;
Mathias Agopian295190f2009-05-05 18:30:52 -0700343 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700344 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
345 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
346 const_cast<int&>(dev->device.stride) = stride;
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100347 const_cast<int&>(dev->device.format) = format;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700348 const_cast<float&>(dev->device.xdpi) = m->xdpi;
349 const_cast<float&>(dev->device.ydpi) = m->ydpi;
350 const_cast<float&>(dev->device.fps) = m->fps;
351 const_cast<int&>(dev->device.minSwapInterval) = 1;
352 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700353 *device = &dev->device.common;
354 }
355 }
356 return status;
357}