2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <cutils/ashmem.h>
22 #include <cutils/log.h>
24 #include <hardware/hardware.h>
25 #include <hardware/gralloc.h>
29 #include <sys/ioctl.h>
33 #include <cutils/log.h>
34 #include <cutils/atomic.h>
40 #include "gralloc_priv.h"
43 /*****************************************************************************/
45 // 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
52 // numbers of buffers for page flipping
57 PAGE_FLIP = 0x00000001,
62 framebuffer_device_t device;
65 /*****************************************************************************/
67 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
70 fb_context_t* ctx = (fb_context_t*)dev;
71 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
73 // FIXME: implement fb_setSwapInterval
77 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
78 int l, int t, int w, int h)
80 if (((w|h) <= 0) || ((l|t)<0))
83 fb_context_t* ctx = (fb_context_t*)dev;
84 private_module_t* m = reinterpret_cast<private_module_t*>(
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);
92 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
94 if (private_handle_t::validate(buffer) < 0)
97 fb_context_t* ctx = (fb_context_t*)dev;
99 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
100 private_module_t* m = reinterpret_cast<private_module_t*>(
103 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
104 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) {
108 ALOGE("FBIOPUT_VSCREENINFO failed");
109 m->base.unlock(&m->base, buffer);
112 m->currentBuffer = buffer;
115 // If we can't do the page_flip, just copy the buffer to the front
116 // FIXME: use copybit HAL instead of memcpy
121 m->base.lock(&m->base, m->framebuffer,
122 GRALLOC_USAGE_SW_WRITE_RARELY,
123 0, 0, m->info.xres, m->info.yres,
126 m->base.lock(&m->base, buffer,
127 GRALLOC_USAGE_SW_READ_RARELY,
128 0, 0, m->info.xres, m->info.yres,
131 memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
133 m->base.unlock(&m->base, buffer);
134 m->base.unlock(&m->base, m->framebuffer);
140 /*****************************************************************************/
142 int mapFrameBufferLocked(struct private_module_t* module)
144 // already initialized...
145 if (module->framebuffer) {
149 char const * const device_template[] = {
150 "/dev/graphics/fb%u",
158 while ((fd==-1) && device_template[i]) {
159 snprintf(name, 64, device_template[i], 0);
160 fd = open(name, O_RDWR, 0);
166 struct fb_fix_screeninfo finfo;
167 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
170 struct fb_var_screeninfo info;
171 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
174 info.reserved[0] = 0;
175 info.reserved[1] = 0;
176 info.reserved[2] = 0;
179 info.activate = FB_ACTIVATE_NOW;
182 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
184 info.yres_virtual = info.yres * NUM_BUFFERS;
187 uint32_t flags = PAGE_FLIP;
189 if (ioctl(fd, FBIOPAN_DISPLAY, &info) == -1) {
190 ALOGW("FBIOPAN_DISPLAY failed, page flipping not supported");
192 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
193 ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
195 info.yres_virtual = info.yres;
199 if (info.yres_virtual < info.yres * 2) {
200 // we need at least 2 for page-flipping
201 info.yres_virtual = info.yres;
203 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
204 info.yres_virtual, info.yres*2);
207 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
210 uint64_t refreshQuotient =
212 uint64_t( info.upper_margin + info.lower_margin + info.yres )
213 * ( info.left_margin + info.right_margin + info.xres )
217 /* 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;
221 if (refreshRate == 0) {
222 // bleagh, bad info from the driver
223 refreshRate = 60*1000; // 60 Hz
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);
233 float xdpi = (info.xres * 25.4f) / info.width;
234 float ydpi = (info.yres * 25.4f) / info.height;
235 float fps = refreshRate / 1000.0f;
237 ALOGI( "using (fd=%d)\n"
241 "xres_virtual = %d px\n"
242 "yres_virtual = %d px\n"
254 info.red.offset, info.red.length,
255 info.green.offset, info.green.length,
256 info.blue.offset, info.blue.length
259 ALOGI( "width = %d mm (%f dpi)\n"
260 "height = %d mm (%f dpi)\n"
261 "refresh rate = %.2f Hz\n",
268 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
271 if (finfo.smem_len <= 0)
275 module->flags = flags;
277 module->finfo = finfo;
283 * map the framebuffer
287 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
288 module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
290 module->numBuffers = info.yres_virtual / info.yres;
291 module->bufferMask = 0;
293 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
294 if (vaddr == MAP_FAILED) {
295 ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
298 module->framebuffer->base = intptr_t(vaddr);
299 memset(vaddr, 0, fbSize);
303 static int mapFrameBuffer(struct private_module_t* module)
305 pthread_mutex_lock(&module->lock);
306 int err = mapFrameBufferLocked(module);
307 pthread_mutex_unlock(&module->lock);
311 /*****************************************************************************/
313 static int fb_close(struct hw_device_t *dev)
315 fb_context_t* ctx = (fb_context_t*)dev;
322 int fb_device_open(hw_module_t const* module, const char* name,
323 hw_device_t** device)
325 int status = -EINVAL;
326 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
327 /* initialize our state here */
328 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
329 memset(dev, 0, sizeof(*dev));
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;
337 dev->device.post = fb_post;
338 dev->device.setUpdateRect = 0;
340 private_module_t* m = (private_module_t*)module;
341 status = mapFrameBuffer(m);
343 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
345 * Auto detect current depth and select mode
348 if (m->info.bits_per_pixel == 32) {
349 format = (m->info.red.offset == 16) ? HAL_PIXEL_FORMAT_BGRA_8888
350 : (m->info.red.offset == 24) ? HAL_PIXEL_FORMAT_RGBA_8888
351 : HAL_PIXEL_FORMAT_RGBX_8888;
352 } else if (m->info.bits_per_pixel == 16) {
353 format = HAL_PIXEL_FORMAT_RGB_565;
355 ALOGE("Unsupported format %d", m->info.bits_per_pixel);
358 const_cast<uint32_t&>(dev->device.flags) = 0;
359 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
360 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
361 const_cast<int&>(dev->device.stride) = stride;
362 const_cast<int&>(dev->device.format) = format;
363 const_cast<float&>(dev->device.xdpi) = m->xdpi;
364 const_cast<float&>(dev->device.ydpi) = m->ydpi;
365 const_cast<float&>(dev->device.fps) = m->fps;
366 const_cast<int&>(dev->device.minSwapInterval) = 1;
367 const_cast<int&>(dev->device.maxSwapInterval) = 1;
368 *device = &dev->device.common;