blob: 979645721b8c1364b4db8ab6300b932cf8aa96b6 [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{
Mathias Agopiana8a75162009-04-10 14:24:31 -070066 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
67 return -EINVAL;
68 // FIXME: implement fb_setSwapInterval
69 return 0;
70}
71
Mathias Agopiana8a75162009-04-10 14:24:31 -070072static 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
Mathias Agopiana8a75162009-04-10 14:24:31 -070077 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);
Mathias Agopiana8a75162009-04-10 14:24:31 -070080
81 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Mathias Agopiana8a75162009-04-10 14:24:31 -070082 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) {
Steve Block60d056b2012-01-08 10:17:53 +000086 ALOGE("FBIOPUT_VSCREENINFO failed");
Mathias Agopiana8a75162009-04-10 14:24:31 -070087 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
Mathias Agopian988b8bd2009-05-04 14:26:56 -070096 void* fb_vaddr;
97 void* buffer_vaddr;
Mathias Agopiana8a75162009-04-10 14:24:31 -070098
Mathias Agopian988b8bd2009-05-04 14:26:56 -070099 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);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700110
111 m->base.unlock(&m->base, buffer);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700112 m->base.unlock(&m->base, m->framebuffer);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700113 }
114
115 return 0;
116}
117
118/*****************************************************************************/
119
120int mapFrameBufferLocked(struct private_module_t* module)
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 /*
Mathias Agopiana8a75162009-04-10 14:24:31 -0700160 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
161 */
162 info.yres_virtual = info.yres * NUM_BUFFERS;
163
164
165 uint32_t flags = PAGE_FLIP;
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +0530166#if USE_PAN_DISPLAY
167 if (ioctl(fd, FBIOPAN_DISPLAY, &info) == -1) {
168 ALOGW("FBIOPAN_DISPLAY failed, page flipping not supported");
169#else
Mathias Agopiana8a75162009-04-10 14:24:31 -0700170 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
Bernhard Rosenkraenzer56416422014-02-25 13:50:34 +0530171 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
172#endif
Mathias Agopiana8a75162009-04-10 14:24:31 -0700173 info.yres_virtual = info.yres;
174 flags &= ~PAGE_FLIP;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700175 }
176
177 if (info.yres_virtual < info.yres * 2) {
178 // we need at least 2 for page-flipping
179 info.yres_virtual = info.yres;
180 flags &= ~PAGE_FLIP;
Steve Block22d35132012-01-05 23:27:52 +0000181 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
Mathias Agopiana8a75162009-04-10 14:24:31 -0700182 info.yres_virtual, info.yres*2);
183 }
184
185 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
186 return -errno;
187
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100188 uint64_t refreshQuotient =
Mathias Agopiana8a75162009-04-10 14:24:31 -0700189 (
190 uint64_t( info.upper_margin + info.lower_margin + info.yres )
191 * ( info.left_margin + info.right_margin + info.xres )
192 * info.pixclock
193 );
194
David 'Digit' Turner80d36992011-01-15 21:06:12 +0100195 /* Beware, info.pixclock might be 0 under emulation, so avoid a
196 * division-by-0 here (SIGFPE on ARM) */
197 int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
198
Mathias Agopiana8a75162009-04-10 14:24:31 -0700199 if (refreshRate == 0) {
200 // bleagh, bad info from the driver
201 refreshRate = 60*1000; // 60 Hz
202 }
203
204 if (int(info.width) <= 0 || int(info.height) <= 0) {
205 // the driver doesn't return that information
206 // default to 160 dpi
207 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
208 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
209 }
210
211 float xdpi = (info.xres * 25.4f) / info.width;
212 float ydpi = (info.yres * 25.4f) / info.height;
213 float fps = refreshRate / 1000.0f;
214
Steve Blockb224b782012-01-04 20:07:12 +0000215 ALOGI( "using (fd=%d)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700216 "id = %s\n"
217 "xres = %d px\n"
218 "yres = %d px\n"
219 "xres_virtual = %d px\n"
220 "yres_virtual = %d px\n"
221 "bpp = %d\n"
222 "r = %2u:%u\n"
223 "g = %2u:%u\n"
224 "b = %2u:%u\n",
225 fd,
226 finfo.id,
227 info.xres,
228 info.yres,
229 info.xres_virtual,
230 info.yres_virtual,
231 info.bits_per_pixel,
232 info.red.offset, info.red.length,
233 info.green.offset, info.green.length,
234 info.blue.offset, info.blue.length
235 );
236
Steve Blockb224b782012-01-04 20:07:12 +0000237 ALOGI( "width = %d mm (%f dpi)\n"
Mathias Agopiana8a75162009-04-10 14:24:31 -0700238 "height = %d mm (%f dpi)\n"
239 "refresh rate = %.2f Hz\n",
240 info.width, xdpi,
241 info.height, ydpi,
242 fps
243 );
244
245
246 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
247 return -errno;
248
249 if (finfo.smem_len <= 0)
250 return -errno;
251
252
253 module->flags = flags;
254 module->info = info;
255 module->finfo = finfo;
256 module->xdpi = xdpi;
257 module->ydpi = ydpi;
258 module->fps = fps;
259
260 /*
261 * map the framebuffer
262 */
263
Mathias Agopiana8a75162009-04-10 14:24:31 -0700264 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
Mathias Agopianf96b2062009-12-14 18:27:09 -0800265 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700266
267 module->numBuffers = info.yres_virtual / info.yres;
268 module->bufferMask = 0;
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700269
Mathias Agopian51156652009-06-09 18:55:49 -0700270 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
271 if (vaddr == MAP_FAILED) {
Steve Block60d056b2012-01-08 10:17:53 +0000272 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -0700273 return -errno;
274 }
275 module->framebuffer->base = intptr_t(vaddr);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700276 memset(vaddr, 0, fbSize);
Mathias Agopiana8a75162009-04-10 14:24:31 -0700277 return 0;
278}
279
280static int mapFrameBuffer(struct private_module_t* module)
281{
282 pthread_mutex_lock(&module->lock);
283 int err = mapFrameBufferLocked(module);
284 pthread_mutex_unlock(&module->lock);
285 return err;
286}
287
288/*****************************************************************************/
289
290static int fb_close(struct hw_device_t *dev)
291{
292 fb_context_t* ctx = (fb_context_t*)dev;
293 if (ctx) {
294 free(ctx);
295 }
296 return 0;
297}
298
299int fb_device_open(hw_module_t const* module, const char* name,
300 hw_device_t** device)
301{
302 int status = -EINVAL;
303 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
Mathias Agopiana8a75162009-04-10 14:24:31 -0700304 /* initialize our state here */
305 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
306 memset(dev, 0, sizeof(*dev));
307
308 /* initialize the procs */
309 dev->device.common.tag = HARDWARE_DEVICE_TAG;
310 dev->device.common.version = 0;
311 dev->device.common.module = const_cast<hw_module_t*>(module);
312 dev->device.common.close = fb_close;
313 dev->device.setSwapInterval = fb_setSwapInterval;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700314 dev->device.post = fb_post;
Mathias Agopiand28d7e82009-07-07 12:20:31 -0700315 dev->device.setUpdateRect = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700316
317 private_module_t* m = (private_module_t*)module;
318 status = mapFrameBuffer(m);
319 if (status >= 0) {
320 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100321 int format = (m->info.bits_per_pixel == 32)
Bernhard Rosenkraenzer5e3ac6b2014-02-21 01:38:44 +0530322 ? (m->info.red.offset ? HAL_PIXEL_FORMAT_BGRA_8888 : HAL_PIXEL_FORMAT_RGBX_8888)
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100323 : HAL_PIXEL_FORMAT_RGB_565;
Mathias Agopian295190f2009-05-05 18:30:52 -0700324 const_cast<uint32_t&>(dev->device.flags) = 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700325 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
326 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
327 const_cast<int&>(dev->device.stride) = stride;
David 'Digit' Turner25b68b52011-01-16 19:52:22 +0100328 const_cast<int&>(dev->device.format) = format;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700329 const_cast<float&>(dev->device.xdpi) = m->xdpi;
330 const_cast<float&>(dev->device.ydpi) = m->ydpi;
331 const_cast<float&>(dev->device.fps) = m->fps;
332 const_cast<int&>(dev->device.minSwapInterval) = 1;
333 const_cast<int&>(dev->device.maxSwapInterval) = 1;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700334 *device = &dev->device.common;
335 }
336 }
337 return status;
338}