1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * Copyright (c) 2010-2014 The Linux Foundation. All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <sys/mman.h> 19 20 #include <cutils/log.h> 21 #include <cutils/properties.h> 22 #include <dlfcn.h> 23 24 #include <hardware/hardware.h> 25 26 #include <fcntl.h> 27 #include <errno.h> 28 #include <sys/ioctl.h> 29 #include <string.h> 30 #include <stdlib.h> 31 #include <pthread.h> 32 #include <cutils/atomic.h> 33 34 #include <linux/fb.h> 35 #include <linux/msm_mdp.h> 36 37 #include <GLES/gl.h> 38 39 #include "gralloc_priv.h" 40 #include "fb_priv.h" 41 #include "gr.h" 42 #include <cutils/properties.h> 43 #include <profiler.h> 44 45 #define EVEN_OUT(x) if (x & 0x0001) {x--;} 46 /** min of int a, b */ 47 static inline int min(int a, int b) { 48 return (a<b) ? a : b; 49 } 50 /** max of int a, b */ 51 static inline int max(int a, int b) { 52 return (a>b) ? a : b; 53 } 54 55 enum { 56 PAGE_FLIP = 0x00000001, 57 }; 58 59 struct fb_context_t { 60 framebuffer_device_t device; 61 //fd - which is returned on open 62 int fbFd; 63 }; 64 65 static int fb_setSwapInterval(struct framebuffer_device_t* dev, 66 int interval) 67 { 68 #ifdef DEBUG_SWAPINTERVAL 69 //XXX: Get the value here and implement along with 70 //single vsync in HWC 71 char pval[PROPERTY_VALUE_MAX]; 72 property_get("debug.egl.swapinterval", pval, "-1"); 73 int property_interval = atoi(pval); 74 if (property_interval >= 0) 75 interval = property_interval; 76 #endif 77 78 private_module_t* m = reinterpret_cast<private_module_t*>( 79 dev->common.module); 80 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) 81 return -EINVAL; 82 83 m->swapInterval = interval; 84 return 0; 85 } 86 87 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) 88 { 89 private_module_t* m = 90 reinterpret_cast<private_module_t*>(dev->common.module); 91 private_handle_t *hnd = static_cast<private_handle_t*> 92 (const_cast<native_handle_t*>(buffer)); 93 fb_context_t *ctx = reinterpret_cast<fb_context_t*>(dev); 94 const unsigned int offset = (unsigned int) (hnd->base - 95 m->framebuffer->base); 96 m->info.activate = FB_ACTIVATE_VBL; 97 m->info.yoffset = (int)(offset / m->finfo.line_length); 98 if (ioctl(ctx->fbFd, FBIOPUT_VSCREENINFO, &m->info) == -1) { 99 ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s", 100 __FUNCTION__, strerror(errno)); 101 return -errno; 102 } 103 return 0; 104 } 105 106 static int fb_compositionComplete(struct framebuffer_device_t* dev) 107 { 108 // TODO: Properly implement composition complete callback 109 if(!dev) { 110 return -1; 111 } 112 glFinish(); 113 114 return 0; 115 } 116 117 int mapFrameBufferLocked(framebuffer_device_t *dev) 118 { 119 private_module_t* module = 120 reinterpret_cast<private_module_t*>(dev->common.module); 121 fb_context_t *ctx = reinterpret_cast<fb_context_t*>(dev); 122 // already initialized... 123 if (module->framebuffer) { 124 return 0; 125 } 126 char const * const device_template[] = { 127 "/dev/graphics/fb%u", 128 "/dev/fb%u", 129 0 }; 130 131 int fd = -1; 132 int i=0; 133 char name[64]; 134 char property[PROPERTY_VALUE_MAX]; 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 close(fd); 147 return -errno; 148 } 149 150 struct fb_var_screeninfo info; 151 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { 152 close(fd); 153 return -errno; 154 } 155 156 info.reserved[0] = 0; 157 info.reserved[1] = 0; 158 info.reserved[2] = 0; 159 info.xoffset = 0; 160 info.yoffset = 0; 161 info.activate = FB_ACTIVATE_NOW; 162 163 /* Interpretation of offset for color fields: All offsets are from the 164 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide 165 * (means: you can use the offset as right argument to <<). A pixel 166 * afterwards is a bit stream and is written to video memory as that 167 * unmodified. This implies big-endian byte order if bits_per_pixel is 168 * greater than 8. 169 */ 170 171 if(info.bits_per_pixel == 32) { 172 /* 173 * Explicitly request RGBA_8888 174 */ 175 info.bits_per_pixel = 32; 176 info.red.offset = 24; 177 info.red.length = 8; 178 info.green.offset = 16; 179 info.green.length = 8; 180 info.blue.offset = 8; 181 info.blue.length = 8; 182 info.transp.offset = 0; 183 info.transp.length = 8; 184 185 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we 186 * do not use the MDP for composition (i.e. hw composition == 0), ask 187 * for RGBA instead of RGBX. */ 188 if (property_get("debug.sf.hw", property, NULL) > 0 && 189 atoi(property) == 0) 190 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; 191 else if(property_get("debug.composition.type", property, NULL) > 0 && 192 (strncmp(property, "mdp", 3) == 0)) 193 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; 194 else 195 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888; 196 } else { 197 /* 198 * Explicitly request 5/6/5 199 */ 200 info.bits_per_pixel = 16; 201 info.red.offset = 11; 202 info.red.length = 5; 203 info.green.offset = 5; 204 info.green.length = 6; 205 info.blue.offset = 0; 206 info.blue.length = 5; 207 info.transp.offset = 0; 208 info.transp.length = 0; 209 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565; 210 } 211 212 //adreno needs 4k aligned offsets. Max hole size is 4096-1 213 unsigned int size = roundUpToPageSize(info.yres * info.xres * 214 (info.bits_per_pixel/8)); 215 216 /* 217 * Request NUM_BUFFERS screens (at least 2 for page flipping) 218 */ 219 int numberOfBuffers = (int)(finfo.smem_len/size); 220 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers); 221 222 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) { 223 int num = atoi(property); 224 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) { 225 numberOfBuffers = num; 226 } 227 } 228 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX) 229 numberOfBuffers = NUM_FRAMEBUFFERS_MAX; 230 231 ALOGV("We support %d buffers", numberOfBuffers); 232 233 //consider the included hole by 4k alignment 234 uint32_t line_length = (info.xres * info.bits_per_pixel / 8); 235 info.yres_virtual = (uint32_t) ((size * numberOfBuffers) / line_length); 236 237 uint32_t flags = PAGE_FLIP; 238 239 if (info.yres_virtual < ((size * 2) / line_length) ) { 240 // we need at least 2 for page-flipping 241 info.yres_virtual = (int)(size / line_length); 242 flags &= ~PAGE_FLIP; 243 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)", 244 info.yres_virtual, info.yres*2); 245 } 246 247 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { 248 close(fd); 249 return -errno; 250 } 251 252 if (int(info.width) <= 0 || int(info.height) <= 0) { 253 // the driver doesn't return that information 254 // default to 160 dpi 255 info.width = (uint32_t)(((float)(info.xres) * 25.4f)/160.0f + 0.5f); 256 info.height = (uint32_t)(((float)(info.yres) * 25.4f)/160.0f + 0.5f); 257 } 258 259 float xdpi = ((float)(info.xres) * 25.4f) / (float)info.width; 260 float ydpi = ((float)(info.yres) * 25.4f) / (float)info.height; 261 262 #ifdef MSMFB_METADATA_GET 263 struct msmfb_metadata metadata; 264 memset(&metadata, 0 , sizeof(metadata)); 265 metadata.op = metadata_op_frame_rate; 266 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) { 267 ALOGE("Error retrieving panel frame rate"); 268 close(fd); 269 return -errno; 270 } 271 float fps = (float)metadata.data.panel_frame_rate; 272 #else 273 //XXX: Remove reserved field usage on all baselines 274 //The reserved[3] field is used to store FPS by the driver. 275 float fps = info.reserved[3] & 0xFF; 276 #endif 277 ALOGI("using (fd=%d)\n" 278 "id = %s\n" 279 "xres = %d px\n" 280 "yres = %d px\n" 281 "xres_virtual = %d px\n" 282 "yres_virtual = %d px\n" 283 "bpp = %d\n" 284 "r = %2u:%u\n" 285 "g = %2u:%u\n" 286 "b = %2u:%u\n", 287 fd, 288 finfo.id, 289 info.xres, 290 info.yres, 291 info.xres_virtual, 292 info.yres_virtual, 293 info.bits_per_pixel, 294 info.red.offset, info.red.length, 295 info.green.offset, info.green.length, 296 info.blue.offset, info.blue.length 297 ); 298 299 ALOGI("width = %d mm (%f dpi)\n" 300 "height = %d mm (%f dpi)\n" 301 "refresh rate = %.2f Hz\n", 302 info.width, xdpi, 303 info.height, ydpi, 304 fps 305 ); 306 307 308 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) { 309 close(fd); 310 return -errno; 311 } 312 313 if (finfo.smem_len <= 0) { 314 close(fd); 315 return -errno; 316 } 317 318 module->flags = flags; 319 module->info = info; 320 module->finfo = finfo; 321 module->xdpi = xdpi; 322 module->ydpi = ydpi; 323 module->fps = fps; 324 module->swapInterval = 1; 325 326 CALC_INIT(); 327 328 /* 329 * map the framebuffer 330 */ 331 332 module->numBuffers = info.yres_virtual / info.yres; 333 module->bufferMask = 0; 334 //adreno needs page aligned offsets. Align the fbsize to pagesize. 335 unsigned int fbSize = roundUpToPageSize(finfo.line_length * info.yres)* 336 module->numBuffers; 337 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 338 if (vaddr == MAP_FAILED) { 339 ALOGE("Error mapping the framebuffer (%s)", strerror(errno)); 340 close(fd); 341 return -errno; 342 } 343 //store the framebuffer fd in the ctx 344 ctx->fbFd = fd; 345 #ifdef MSMFB_METADATA_GET 346 memset(&metadata, 0 , sizeof(metadata)); 347 metadata.op = metadata_op_get_ion_fd; 348 // get the ION fd for the framebuffer, as GPU needs ION fd 349 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) { 350 ALOGE("Error getting ION fd (%s)", strerror(errno)); 351 close(fd); 352 return -errno; 353 } 354 if(metadata.data.fbmem_ionfd < 0) { 355 ALOGE("Error: Ioctl returned invalid ION fd = %d", 356 metadata.data.fbmem_ionfd); 357 close(fd); 358 return -errno; 359 } 360 fd = metadata.data.fbmem_ionfd; 361 #endif 362 // Create framebuffer handle using the ION fd 363 module->framebuffer = new private_handle_t(fd, fbSize, 364 private_handle_t::PRIV_FLAGS_USES_ION, 365 BUFFER_TYPE_UI, 366 module->fbFormat, info.xres, info.yres); 367 module->framebuffer->base = uint64_t(vaddr); 368 memset(vaddr, 0, fbSize); 369 //Enable vsync 370 int enable = 1; 371 ioctl(ctx->fbFd, MSMFB_OVERLAY_VSYNC_CTRL, &enable); 372 return 0; 373 } 374 375 static int mapFrameBuffer(framebuffer_device_t *dev) 376 { 377 int err = -1; 378 char property[PROPERTY_VALUE_MAX]; 379 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && 380 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) || 381 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) { 382 private_module_t* module = 383 reinterpret_cast<private_module_t*>(dev->common.module); 384 pthread_mutex_lock(&module->lock); 385 err = mapFrameBufferLocked(dev); 386 pthread_mutex_unlock(&module->lock); 387 } 388 return err; 389 } 390 391 /*****************************************************************************/ 392 393 static int fb_close(struct hw_device_t *dev) 394 { 395 fb_context_t* ctx = (fb_context_t*)dev; 396 if (ctx) { 397 #ifdef MSMFB_METADATA_GET 398 if(ctx->fbFd >=0) { 399 close(ctx->fbFd); 400 } 401 #endif 402 //Hack until fbdev is removed. Framework could close this causing hwc a 403 //pain. 404 //free(ctx); 405 } 406 return 0; 407 } 408 409 int fb_device_open(hw_module_t const* module, const char* name, 410 hw_device_t** device) 411 { 412 int status = -EINVAL; 413 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 414 alloc_device_t* gralloc_device; 415 status = gralloc_open(module, &gralloc_device); 416 if (status < 0) 417 return status; 418 419 /* initialize our state here */ 420 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); 421 if(dev == NULL) { 422 gralloc_close(gralloc_device); 423 return status; 424 } 425 memset(dev, 0, sizeof(*dev)); 426 427 /* initialize the procs */ 428 dev->device.common.tag = HARDWARE_DEVICE_TAG; 429 dev->device.common.version = 0; 430 dev->device.common.module = const_cast<hw_module_t*>(module); 431 dev->device.common.close = fb_close; 432 dev->device.setSwapInterval = fb_setSwapInterval; 433 dev->device.post = fb_post; 434 dev->device.setUpdateRect = 0; 435 dev->device.compositionComplete = fb_compositionComplete; 436 437 status = mapFrameBuffer((framebuffer_device_t*)dev); 438 private_module_t* m = (private_module_t*)dev->device.common.module; 439 if (status >= 0) { 440 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); 441 const_cast<uint32_t&>(dev->device.flags) = 0; 442 const_cast<uint32_t&>(dev->device.width) = m->info.xres; 443 const_cast<uint32_t&>(dev->device.height) = m->info.yres; 444 const_cast<int&>(dev->device.stride) = stride; 445 const_cast<int&>(dev->device.format) = m->fbFormat; 446 const_cast<float&>(dev->device.xdpi) = m->xdpi; 447 const_cast<float&>(dev->device.ydpi) = m->ydpi; 448 const_cast<float&>(dev->device.fps) = m->fps; 449 const_cast<int&>(dev->device.minSwapInterval) = 450 PRIV_MIN_SWAP_INTERVAL; 451 const_cast<int&>(dev->device.maxSwapInterval) = 452 PRIV_MAX_SWAP_INTERVAL; 453 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers; 454 dev->device.setUpdateRect = 0; 455 456 *device = &dev->device.common; 457 } 458 459 // Close the gralloc module 460 gralloc_close(gralloc_device); 461 } 462 return status; 463 } 464