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 #include <cutils/properties.h> 24 25 #include <hardware/hardware.h> 26 #include <hardware/gralloc.h> 27 28 #include <fcntl.h> 29 #include <errno.h> 30 #include <sys/ioctl.h> 31 #include <string.h> 32 #include <stdlib.h> 33 34 #include <cutils/log.h> 35 #include <cutils/atomic.h> 36 37 #include <linux/fb.h> 38 #include <linux/msm_mdp.h> 39 40 #include <GLES/gl.h> 41 42 #include "gralloc_priv.h" 43 #include "gr.h" 44 45 /*****************************************************************************/ 46 47 // numbers of buffers for page flipping 48 #define NUM_BUFFERS 2 49 50 51 enum { 52 PAGE_FLIP = 0x00000001, 53 LOCKED = 0x00000002 54 }; 55 56 struct fb_context_t { 57 framebuffer_device_t device; 58 }; 59 60 /*****************************************************************************/ 61 62 static void 63 msm_copy_buffer(buffer_handle_t handle, int fd, 64 int width, int height, int format, 65 int x, int y, int w, int h); 66 67 static int fb_setSwapInterval(struct framebuffer_device_t* dev, 68 int interval) 69 { 70 fb_context_t* ctx = (fb_context_t*)dev; 71 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) 72 return -EINVAL; 73 // FIXME: implement fb_setSwapInterval 74 return 0; 75 } 76 77 static int fb_setUpdateRect(struct framebuffer_device_t* dev, 78 int l, int t, int w, int h) 79 { 80 if (((w|h) <= 0) || ((l|t)<0)) 81 return -EINVAL; 82 83 fb_context_t* ctx = (fb_context_t*)dev; 84 private_module_t* m = reinterpret_cast<private_module_t*>( 85 dev->common.module); 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); 89 return 0; 90 } 91 92 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) 93 { 94 if (private_handle_t::validate(buffer) < 0) 95 return -EINVAL; 96 97 fb_context_t* ctx = (fb_context_t*)dev; 98 99 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); 100 private_module_t* m = reinterpret_cast<private_module_t*>( 101 dev->common.module); 102 103 if (m->currentBuffer) { 104 m->base.unlock(&m->base, m->currentBuffer); 105 m->currentBuffer = 0; 106 } 107 108 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { 109 110 m->base.lock(&m->base, buffer, 111 private_module_t::PRIV_USAGE_LOCKED_FOR_POST, 112 0, 0, m->info.xres, m->info.yres, NULL); 113 114 const size_t offset = hnd->base - m->framebuffer->base; 115 m->info.activate = FB_ACTIVATE_VBL; 116 m->info.yoffset = offset / m->finfo.line_length; 117 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { 118 LOGE("FBIOPUT_VSCREENINFO failed"); 119 m->base.unlock(&m->base, buffer); 120 return -errno; 121 } 122 m->currentBuffer = buffer; 123 124 } else { 125 void* fb_vaddr; 126 void* buffer_vaddr; 127 128 m->base.lock(&m->base, m->framebuffer, 129 GRALLOC_USAGE_SW_WRITE_RARELY, 130 0, 0, m->info.xres, m->info.yres, 131 &fb_vaddr); 132 133 m->base.lock(&m->base, buffer, 134 GRALLOC_USAGE_SW_READ_RARELY, 135 0, 0, m->info.xres, m->info.yres, 136 &buffer_vaddr); 137 138 //memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres); 139 140 msm_copy_buffer( 141 m->framebuffer, m->framebuffer->fd, 142 m->info.xres, m->info.yres, m->fbFormat, 143 m->info.xoffset, m->info.yoffset, 144 m->info.width, m->info.height); 145 146 m->base.unlock(&m->base, buffer); 147 m->base.unlock(&m->base, m->framebuffer); 148 } 149 150 return 0; 151 } 152 153 static int fb_compositionComplete(struct framebuffer_device_t* dev) 154 { 155 // TODO: Properly implement composition complete callback 156 glFinish(); 157 158 return 0; 159 } 160 161 /*****************************************************************************/ 162 163 int mapFrameBufferLocked(struct private_module_t* module) 164 { 165 // already initialized... 166 if (module->framebuffer) { 167 return 0; 168 } 169 170 char const * const device_template[] = { 171 "/dev/graphics/fb%u", 172 "/dev/fb%u", 173 0 }; 174 175 int fd = -1; 176 int i=0; 177 char name[64]; 178 179 while ((fd==-1) && device_template[i]) { 180 snprintf(name, 64, device_template[i], 0); 181 fd = open(name, O_RDWR, 0); 182 i++; 183 } 184 if (fd < 0) 185 return -errno; 186 187 struct fb_fix_screeninfo finfo; 188 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) 189 return -errno; 190 191 struct fb_var_screeninfo info; 192 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) 193 return -errno; 194 195 info.reserved[0] = 0; 196 info.reserved[1] = 0; 197 info.reserved[2] = 0; 198 info.xoffset = 0; 199 info.yoffset = 0; 200 info.activate = FB_ACTIVATE_NOW; 201 202 /* Interpretation of offset for color fields: All offsets are from the right, 203 * inside a "pixel" value, which is exactly 'bits_per_pixel' wide (means: you 204 * can use the offset as right argument to <<). A pixel afterwards is a bit 205 * stream and is written to video memory as that unmodified. This implies 206 * big-endian byte order if bits_per_pixel is greater than 8. 207 */ 208 209 /* 210 * Explicitly request RGBA_8888 211 */ 212 info.bits_per_pixel = 32; 213 info.red.offset = 24; 214 info.red.length = 8; 215 info.green.offset = 16; 216 info.green.length = 8; 217 info.blue.offset = 8; 218 info.blue.length = 8; 219 info.transp.offset = 0; 220 info.transp.length = 0; 221 222 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we do 223 * not use the MDP for composition (i.e. hw composition == 0), ask for 224 * RGBA instead of RGBX. */ 225 char property[PROPERTY_VALUE_MAX]; 226 if (property_get("debug.sf.hw", property, NULL) > 0 && atoi(property) == 0) 227 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; 228 else 229 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888; 230 231 /* 232 * Request NUM_BUFFERS screens (at lest 2 for page flipping) 233 */ 234 info.yres_virtual = info.yres * NUM_BUFFERS; 235 236 237 uint32_t flags = PAGE_FLIP; 238 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) { 239 info.yres_virtual = info.yres; 240 flags &= ~PAGE_FLIP; 241 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported"); 242 } 243 244 if (info.yres_virtual < info.yres * 2) { 245 // we need at least 2 for page-flipping 246 info.yres_virtual = info.yres; 247 flags &= ~PAGE_FLIP; 248 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)", 249 info.yres_virtual, info.yres*2); 250 } 251 252 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) 253 return -errno; 254 255 int refreshRate = 1000000000000000LLU / 256 ( 257 uint64_t( info.upper_margin + info.lower_margin + info.yres ) 258 * ( info.left_margin + info.right_margin + info.xres ) 259 * info.pixclock 260 ); 261 262 if (refreshRate == 0) { 263 // bleagh, bad info from the driver 264 refreshRate = 60*1000; // 60 Hz 265 } 266 267 if (int(info.width) <= 0 || int(info.height) <= 0) { 268 // the driver doesn't return that information 269 // default to 160 dpi 270 info.width = ((info.xres * 25.4f)/160.0f + 0.5f); 271 info.height = ((info.yres * 25.4f)/160.0f + 0.5f); 272 } 273 274 float xdpi = (info.xres * 25.4f) / info.width; 275 float ydpi = (info.yres * 25.4f) / info.height; 276 float fps = refreshRate / 1000.0f; 277 278 LOGI( "using (fd=%d)\n" 279 "id = %s\n" 280 "xres = %d px\n" 281 "yres = %d px\n" 282 "xres_virtual = %d px\n" 283 "yres_virtual = %d px\n" 284 "bpp = %d\n" 285 "r = %2u:%u\n" 286 "g = %2u:%u\n" 287 "b = %2u:%u\n", 288 fd, 289 finfo.id, 290 info.xres, 291 info.yres, 292 info.xres_virtual, 293 info.yres_virtual, 294 info.bits_per_pixel, 295 info.red.offset, info.red.length, 296 info.green.offset, info.green.length, 297 info.blue.offset, info.blue.length 298 ); 299 300 LOGI( "width = %d mm (%f dpi)\n" 301 "height = %d mm (%f dpi)\n" 302 "refresh rate = %.2f Hz\n", 303 info.width, xdpi, 304 info.height, ydpi, 305 fps 306 ); 307 308 309 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) 310 return -errno; 311 312 if (finfo.smem_len <= 0) 313 return -errno; 314 315 316 module->flags = flags; 317 module->info = info; 318 module->finfo = finfo; 319 module->xdpi = xdpi; 320 module->ydpi = ydpi; 321 module->fps = fps; 322 323 /* 324 * map the framebuffer 325 */ 326 327 int err; 328 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual); 329 module->framebuffer = new private_handle_t(dup(fd), fbSize, 330 private_handle_t::PRIV_FLAGS_USES_PMEM); 331 332 module->numBuffers = info.yres_virtual / info.yres; 333 module->bufferMask = 0; 334 335 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 336 if (vaddr == MAP_FAILED) { 337 LOGE("Error mapping the framebuffer (%s)", strerror(errno)); 338 return -errno; 339 } 340 module->framebuffer->base = intptr_t(vaddr); 341 memset(vaddr, 0, fbSize); 342 return 0; 343 } 344 345 static int mapFrameBuffer(struct private_module_t* module) 346 { 347 pthread_mutex_lock(&module->lock); 348 int err = mapFrameBufferLocked(module); 349 pthread_mutex_unlock(&module->lock); 350 return err; 351 } 352 353 /*****************************************************************************/ 354 355 static int fb_close(struct hw_device_t *dev) 356 { 357 fb_context_t* ctx = (fb_context_t*)dev; 358 if (ctx) { 359 free(ctx); 360 } 361 return 0; 362 } 363 364 int fb_device_open(hw_module_t const* module, const char* name, 365 hw_device_t** device) 366 { 367 int status = -EINVAL; 368 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 369 alloc_device_t* gralloc_device; 370 status = gralloc_open(module, &gralloc_device); 371 if (status < 0) 372 return status; 373 374 /* initialize our state here */ 375 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); 376 memset(dev, 0, sizeof(*dev)); 377 378 /* initialize the procs */ 379 dev->device.common.tag = HARDWARE_DEVICE_TAG; 380 dev->device.common.version = 0; 381 dev->device.common.module = const_cast<hw_module_t*>(module); 382 dev->device.common.close = fb_close; 383 dev->device.setSwapInterval = fb_setSwapInterval; 384 dev->device.post = fb_post; 385 dev->device.setUpdateRect = 0; 386 dev->device.compositionComplete = fb_compositionComplete; 387 388 private_module_t* m = (private_module_t*)module; 389 status = mapFrameBuffer(m); 390 if (status >= 0) { 391 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); 392 const_cast<uint32_t&>(dev->device.flags) = 0; 393 const_cast<uint32_t&>(dev->device.width) = m->info.xres; 394 const_cast<uint32_t&>(dev->device.height) = m->info.yres; 395 const_cast<int&>(dev->device.stride) = stride; 396 const_cast<int&>(dev->device.format) = m->fbFormat; 397 const_cast<float&>(dev->device.xdpi) = m->xdpi; 398 const_cast<float&>(dev->device.ydpi) = m->ydpi; 399 const_cast<float&>(dev->device.fps) = m->fps; 400 const_cast<int&>(dev->device.minSwapInterval) = 1; 401 const_cast<int&>(dev->device.maxSwapInterval) = 1; 402 403 if (m->finfo.reserved[0] == 0x5444 && 404 m->finfo.reserved[1] == 0x5055) { 405 dev->device.setUpdateRect = fb_setUpdateRect; 406 LOGD("UPDATE_ON_DEMAND supported"); 407 } 408 409 *device = &dev->device.common; 410 } 411 } 412 return status; 413 } 414 415 /* Copy a pmem buffer to the framebuffer */ 416 417 static void 418 msm_copy_buffer(buffer_handle_t handle, int fd, 419 int width, int height, int format, 420 int x, int y, int w, int h) 421 { 422 struct { 423 unsigned int count; 424 mdp_blit_req req; 425 } blit; 426 private_handle_t *priv = (private_handle_t*) handle; 427 428 memset(&blit, 0, sizeof(blit)); 429 blit.count = 1; 430 431 blit.req.flags = 0; 432 blit.req.alpha = 0xff; 433 blit.req.transp_mask = 0xffffffff; 434 435 blit.req.src.width = width; 436 blit.req.src.height = height; 437 blit.req.src.offset = 0; 438 blit.req.src.memory_id = priv->fd; 439 440 blit.req.dst.width = width; 441 blit.req.dst.height = height; 442 blit.req.dst.offset = 0; 443 blit.req.dst.memory_id = fd; 444 blit.req.dst.format = format; 445 446 blit.req.src_rect.x = blit.req.dst_rect.x = x; 447 blit.req.src_rect.y = blit.req.dst_rect.y = y; 448 blit.req.src_rect.w = blit.req.dst_rect.w = w; 449 blit.req.src_rect.h = blit.req.dst_rect.h = h; 450 451 if (ioctl(fd, MSMFB_BLIT, &blit)) 452 LOGE("MSMFB_BLIT failed = %d", -errno); 453 } 454