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 #include <linux/fb.h> 37 #include <linux/msm_mdp.h> 38 39 #include "gralloc_priv.h" 40 #include "gr.h" 41 42 /*****************************************************************************/ 43 44 // numbers of buffers for page flipping 45 #define NUM_BUFFERS 2 46 47 48 enum { 49 PAGE_FLIP = 0x00000001, 50 LOCKED = 0x00000002 51 }; 52 53 struct fb_context_t { 54 framebuffer_device_t device; 55 }; 56 57 /*****************************************************************************/ 58 59 static void 60 msm_copy_buffer(buffer_handle_t handle, int fd, int width, int height, 61 int x, int y, int w, int h); 62 63 static 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 73 static 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 88 static 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); 98 99 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { 100 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) { 104 LOGE("FBIOPUT_VSCREENINFO failed"); 105 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 114 void* fb_vaddr; 115 void* buffer_vaddr; 116 117 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); 128 129 msm_copy_buffer(m->framebuffer, m->framebuffer->fd, 130 m->info.xres, m->info.yres, 131 m->info.xoffset, m->info.yoffset, 132 m->info.width, m->info.height); 133 134 m->base.unlock(&m->base, buffer); 135 m->base.unlock(&m->base, m->framebuffer); 136 } 137 138 return 0; 139 } 140 141 /*****************************************************************************/ 142 143 int mapFrameBufferLocked(struct private_module_t* module) 144 { 145 // already initialized... 146 if (module->framebuffer) { 147 return 0; 148 } 149 150 char const * const device_template[] = { 151 "/dev/graphics/fb%u", 152 "/dev/fb%u", 153 0 }; 154 155 int fd = -1; 156 int i=0; 157 char name[64]; 158 159 while ((fd==-1) && device_template[i]) { 160 snprintf(name, 64, device_template[i], 0); 161 fd = open(name, O_RDWR, 0); 162 i++; 163 } 164 if (fd < 0) 165 return -errno; 166 167 struct fb_fix_screeninfo finfo; 168 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) 169 return -errno; 170 171 struct fb_var_screeninfo info; 172 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) 173 return -errno; 174 175 info.reserved[0] = 0; 176 info.reserved[1] = 0; 177 info.reserved[2] = 0; 178 info.xoffset = 0; 179 info.yoffset = 0; 180 info.activate = FB_ACTIVATE_NOW; 181 182 /* 183 * Explicitly request 5/6/5 184 */ 185 info.bits_per_pixel = 16; 186 info.red.offset = 11; 187 info.red.length = 5; 188 info.green.offset = 5; 189 info.green.length = 6; 190 info.blue.offset = 0; 191 info.blue.length = 5; 192 info.transp.offset = 0; 193 info.transp.length = 0; 194 195 /* 196 * Request NUM_BUFFERS screens (at lest 2 for page flipping) 197 */ 198 info.yres_virtual = info.yres * NUM_BUFFERS; 199 200 201 uint32_t flags = PAGE_FLIP; 202 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) { 203 info.yres_virtual = info.yres; 204 flags &= ~PAGE_FLIP; 205 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported"); 206 } 207 208 if (info.yres_virtual < info.yres * 2) { 209 // we need at least 2 for page-flipping 210 info.yres_virtual = info.yres; 211 flags &= ~PAGE_FLIP; 212 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)", 213 info.yres_virtual, info.yres*2); 214 } 215 216 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) 217 return -errno; 218 219 int refreshRate = 1000000000000000LLU / 220 ( 221 uint64_t( info.upper_margin + info.lower_margin + info.yres ) 222 * ( info.left_margin + info.right_margin + info.xres ) 223 * info.pixclock 224 ); 225 226 if (refreshRate == 0) { 227 // bleagh, bad info from the driver 228 refreshRate = 60*1000; // 60 Hz 229 } 230 231 if (int(info.width) <= 0 || int(info.height) <= 0) { 232 // the driver doesn't return that information 233 // default to 160 dpi 234 info.width = ((info.xres * 25.4f)/160.0f + 0.5f); 235 info.height = ((info.yres * 25.4f)/160.0f + 0.5f); 236 } 237 238 float xdpi = (info.xres * 25.4f) / info.width; 239 float ydpi = (info.yres * 25.4f) / info.height; 240 float fps = refreshRate / 1000.0f; 241 242 LOGI( "using (fd=%d)\n" 243 "id = %s\n" 244 "xres = %d px\n" 245 "yres = %d px\n" 246 "xres_virtual = %d px\n" 247 "yres_virtual = %d px\n" 248 "bpp = %d\n" 249 "r = %2u:%u\n" 250 "g = %2u:%u\n" 251 "b = %2u:%u\n", 252 fd, 253 finfo.id, 254 info.xres, 255 info.yres, 256 info.xres_virtual, 257 info.yres_virtual, 258 info.bits_per_pixel, 259 info.red.offset, info.red.length, 260 info.green.offset, info.green.length, 261 info.blue.offset, info.blue.length 262 ); 263 264 LOGI( "width = %d mm (%f dpi)\n" 265 "height = %d mm (%f dpi)\n" 266 "refresh rate = %.2f Hz\n", 267 info.width, xdpi, 268 info.height, ydpi, 269 fps 270 ); 271 272 273 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) 274 return -errno; 275 276 if (finfo.smem_len <= 0) 277 return -errno; 278 279 280 module->flags = flags; 281 module->info = info; 282 module->finfo = finfo; 283 module->xdpi = xdpi; 284 module->ydpi = ydpi; 285 module->fps = fps; 286 287 /* 288 * map the framebuffer 289 */ 290 291 int err; 292 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual); 293 module->framebuffer = new private_handle_t(dup(fd), fbSize, 294 private_handle_t::PRIV_FLAGS_USES_PMEM); 295 296 module->numBuffers = info.yres_virtual / info.yres; 297 module->bufferMask = 0; 298 299 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 300 if (vaddr == MAP_FAILED) { 301 LOGE("Error mapping the framebuffer (%s)", strerror(errno)); 302 return -errno; 303 } 304 module->framebuffer->base = intptr_t(vaddr); 305 306 memset(vaddr, 0, fbSize); 307 return 0; 308 } 309 310 static int mapFrameBuffer(struct private_module_t* module) 311 { 312 pthread_mutex_lock(&module->lock); 313 int err = mapFrameBufferLocked(module); 314 pthread_mutex_unlock(&module->lock); 315 return err; 316 } 317 318 /*****************************************************************************/ 319 320 static int fb_close(struct hw_device_t *dev) 321 { 322 fb_context_t* ctx = (fb_context_t*)dev; 323 if (ctx) { 324 free(ctx); 325 } 326 return 0; 327 } 328 329 int fb_device_open(hw_module_t const* module, const char* name, 330 hw_device_t** device) 331 { 332 int status = -EINVAL; 333 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 334 alloc_device_t* gralloc_device; 335 status = gralloc_open(module, &gralloc_device); 336 if (status < 0) 337 return status; 338 339 /* initialize our state here */ 340 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); 341 memset(dev, 0, sizeof(*dev)); 342 343 /* initialize the procs */ 344 dev->device.common.tag = HARDWARE_DEVICE_TAG; 345 dev->device.common.version = 0; 346 dev->device.common.module = const_cast<hw_module_t*>(module); 347 dev->device.common.close = fb_close; 348 dev->device.setSwapInterval = fb_setSwapInterval; 349 dev->device.post = fb_post; 350 dev->device.setUpdateRect = 0; 351 352 private_module_t* m = (private_module_t*)module; 353 status = mapFrameBuffer(m); 354 if (status >= 0) { 355 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); 356 const_cast<uint32_t&>(dev->device.flags) = 0; 357 const_cast<uint32_t&>(dev->device.width) = m->info.xres; 358 const_cast<uint32_t&>(dev->device.height) = m->info.yres; 359 const_cast<int&>(dev->device.stride) = stride; 360 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565; 361 const_cast<float&>(dev->device.xdpi) = m->xdpi; 362 const_cast<float&>(dev->device.ydpi) = m->ydpi; 363 const_cast<float&>(dev->device.fps) = m->fps; 364 const_cast<int&>(dev->device.minSwapInterval) = 1; 365 const_cast<int&>(dev->device.maxSwapInterval) = 1; 366 367 if (m->finfo.reserved[0] == 0x5444 && 368 m->finfo.reserved[1] == 0x5055) { 369 dev->device.setUpdateRect = fb_setUpdateRect; 370 LOGD("UPDATE_ON_DEMAND supported"); 371 } 372 373 *device = &dev->device.common; 374 } 375 } 376 return status; 377 } 378 379 /* Copy a pmem buffer to the framebuffer */ 380 381 static void 382 msm_copy_buffer(buffer_handle_t handle, int fd, int width, int height, 383 int x, int y, int w, int h) 384 { 385 struct { 386 unsigned int count; 387 mdp_blit_req req; 388 } blit; 389 private_handle_t *priv = (private_handle_t*) handle; 390 391 memset(&blit, 0, sizeof(blit)); 392 blit.count = 1; 393 394 blit.req.flags = 0; 395 blit.req.alpha = 0xff; 396 blit.req.transp_mask = 0xffffffff; 397 398 blit.req.src.width = width; 399 blit.req.src.height = height; 400 blit.req.src.offset = 0; 401 blit.req.src.memory_id = priv->fd; 402 403 blit.req.dst.width = width; 404 blit.req.dst.height = height; 405 blit.req.dst.offset = 0; 406 blit.req.dst.memory_id = fd; 407 blit.req.dst.format = MDP_RGB_565; 408 409 blit.req.src_rect.x = blit.req.dst_rect.x = x; 410 blit.req.src_rect.y = blit.req.dst_rect.y = y; 411 blit.req.src_rect.w = blit.req.dst_rect.w = w; 412 blit.req.src_rect.h = blit.req.dst_rect.h = h; 413 414 if (ioctl(fd, MSMFB_BLIT, &blit)) 415 LOGE("MSMFB_BLIT failed = %d", -errno); 416 } 417