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