1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * Copyright (c) 2010-2012,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 }; 62 63 64 static int fb_setSwapInterval(struct framebuffer_device_t* dev, 65 int interval) 66 { 67 #ifdef DEBUG_SWAPINTERVAL 68 //XXX: Get the value here and implement along with 69 //single vsync in HWC 70 char pval[PROPERTY_VALUE_MAX]; 71 property_get("debug.egl.swapinterval", pval, "-1"); 72 int property_interval = atoi(pval); 73 if (property_interval >= 0) 74 interval = property_interval; 75 #endif 76 77 private_module_t* m = reinterpret_cast<private_module_t*>( 78 dev->common.module); 79 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) 80 return -EINVAL; 81 82 m->swapInterval = interval; 83 return 0; 84 } 85 86 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) 87 { 88 private_module_t* m = 89 reinterpret_cast<private_module_t*>(dev->common.module); 90 private_handle_t *hnd = static_cast<private_handle_t*> 91 (const_cast<native_handle_t*>(buffer)); 92 const unsigned int offset = (unsigned int) (hnd->base - 93 m->framebuffer->base); 94 m->info.activate = FB_ACTIVATE_VBL; 95 m->info.yoffset = (int)(offset / m->finfo.line_length); 96 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { 97 ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s", 98 __FUNCTION__, strerror(errno)); 99 return -errno; 100 } 101 return 0; 102 } 103 104 static int fb_compositionComplete(struct framebuffer_device_t* dev) 105 { 106 // TODO: Properly implement composition complete callback 107 if(!dev) { 108 return -1; 109 } 110 glFinish(); 111 112 return 0; 113 } 114 115 int mapFrameBufferLocked(struct private_module_t* module) 116 { 117 // already initialized... 118 if (module->framebuffer) { 119 return 0; 120 } 121 char const * const device_template[] = { 122 "/dev/graphics/fb%u", 123 "/dev/fb%u", 124 0 }; 125 126 int fd = -1; 127 int i=0; 128 char name[64]; 129 char property[PROPERTY_VALUE_MAX]; 130 131 while ((fd==-1) && device_template[i]) { 132 snprintf(name, 64, device_template[i], 0); 133 fd = open(name, O_RDWR, 0); 134 i++; 135 } 136 if (fd < 0) 137 return -errno; 138 139 memset(&module->commit, 0, sizeof(struct mdp_display_commit)); 140 141 struct fb_fix_screeninfo finfo; 142 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) { 143 close(fd); 144 return -errno; 145 } 146 147 struct fb_var_screeninfo info; 148 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { 149 close(fd); 150 return -errno; 151 } 152 153 info.reserved[0] = 0; 154 info.reserved[1] = 0; 155 info.reserved[2] = 0; 156 info.xoffset = 0; 157 info.yoffset = 0; 158 info.activate = FB_ACTIVATE_NOW; 159 160 /* Interpretation of offset for color fields: All offsets are from the 161 * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide 162 * (means: you can use the offset as right argument to <<). A pixel 163 * afterwards is a bit stream and is written to video memory as that 164 * unmodified. This implies big-endian byte order if bits_per_pixel is 165 * greater than 8. 166 */ 167 168 if(info.bits_per_pixel == 32) { 169 /* 170 * Explicitly request RGBA_8888 171 */ 172 info.bits_per_pixel = 32; 173 info.red.offset = 24; 174 info.red.length = 8; 175 info.green.offset = 16; 176 info.green.length = 8; 177 info.blue.offset = 8; 178 info.blue.length = 8; 179 info.transp.offset = 0; 180 info.transp.length = 8; 181 182 /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we 183 * do not use the MDP for composition (i.e. hw composition == 0), ask 184 * for RGBA instead of RGBX. */ 185 if (property_get("debug.sf.hw", property, NULL) > 0 && 186 atoi(property) == 0) 187 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; 188 else if(property_get("debug.composition.type", property, NULL) > 0 && 189 (strncmp(property, "mdp", 3) == 0)) 190 module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; 191 else 192 module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888; 193 } else { 194 /* 195 * Explicitly request 5/6/5 196 */ 197 info.bits_per_pixel = 16; 198 info.red.offset = 11; 199 info.red.length = 5; 200 info.green.offset = 5; 201 info.green.length = 6; 202 info.blue.offset = 0; 203 info.blue.length = 5; 204 info.transp.offset = 0; 205 info.transp.length = 0; 206 module->fbFormat = HAL_PIXEL_FORMAT_RGB_565; 207 } 208 209 //adreno needs 4k aligned offsets. Max hole size is 4096-1 210 unsigned int size = roundUpToPageSize(info.yres * info.xres * 211 (info.bits_per_pixel/8)); 212 213 /* 214 * Request NUM_BUFFERS screens (at least 2 for page flipping) 215 */ 216 int numberOfBuffers = (int)(finfo.smem_len/size); 217 ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers); 218 219 if (property_get("debug.gr.numframebuffers", property, NULL) > 0) { 220 int num = atoi(property); 221 if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) { 222 numberOfBuffers = num; 223 } 224 } 225 if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX) 226 numberOfBuffers = NUM_FRAMEBUFFERS_MAX; 227 228 ALOGV("We support %d buffers", numberOfBuffers); 229 230 //consider the included hole by 4k alignment 231 uint32_t line_length = (info.xres * info.bits_per_pixel / 8); 232 info.yres_virtual = (uint32_t) ((size * numberOfBuffers) / line_length); 233 234 uint32_t flags = PAGE_FLIP; 235 236 if (info.yres_virtual < ((size * 2) / line_length) ) { 237 // we need at least 2 for page-flipping 238 info.yres_virtual = (int)(size / line_length); 239 flags &= ~PAGE_FLIP; 240 ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)", 241 info.yres_virtual, info.yres*2); 242 } 243 244 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { 245 close(fd); 246 return -errno; 247 } 248 249 if (int(info.width) <= 0 || int(info.height) <= 0) { 250 // the driver doesn't return that information 251 // default to 160 dpi 252 info.width = (uint32_t)(((float)(info.xres) * 25.4f)/160.0f + 0.5f); 253 info.height = (uint32_t)(((float)(info.yres) * 25.4f)/160.0f + 0.5f); 254 } 255 256 float xdpi = ((float)(info.xres) * 25.4f) / (float)info.width; 257 float ydpi = ((float)(info.yres) * 25.4f) / (float)info.height; 258 259 #ifdef MSMFB_METADATA_GET 260 struct msmfb_metadata metadata; 261 memset(&metadata, 0 , sizeof(metadata)); 262 metadata.op = metadata_op_frame_rate; 263 if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) { 264 ALOGE("Error retrieving panel frame rate"); 265 close(fd); 266 return -errno; 267 } 268 float fps = (float)metadata.data.panel_frame_rate; 269 #else 270 //XXX: Remove reserved field usage on all baselines 271 //The reserved[3] field is used to store FPS by the driver. 272 float fps = info.reserved[3] & 0xFF; 273 #endif 274 ALOGI("using (fd=%d)\n" 275 "id = %s\n" 276 "xres = %d px\n" 277 "yres = %d px\n" 278 "xres_virtual = %d px\n" 279 "yres_virtual = %d px\n" 280 "bpp = %d\n" 281 "r = %2u:%u\n" 282 "g = %2u:%u\n" 283 "b = %2u:%u\n", 284 fd, 285 finfo.id, 286 info.xres, 287 info.yres, 288 info.xres_virtual, 289 info.yres_virtual, 290 info.bits_per_pixel, 291 info.red.offset, info.red.length, 292 info.green.offset, info.green.length, 293 info.blue.offset, info.blue.length 294 ); 295 296 ALOGI("width = %d mm (%f dpi)\n" 297 "height = %d mm (%f dpi)\n" 298 "refresh rate = %.2f Hz\n", 299 info.width, xdpi, 300 info.height, ydpi, 301 fps 302 ); 303 304 305 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) { 306 close(fd); 307 return -errno; 308 } 309 310 if (finfo.smem_len <= 0) { 311 close(fd); 312 return -errno; 313 } 314 315 module->flags = flags; 316 module->info = info; 317 module->finfo = finfo; 318 module->xdpi = xdpi; 319 module->ydpi = ydpi; 320 module->fps = fps; 321 module->swapInterval = 1; 322 323 CALC_INIT(); 324 325 /* 326 * map the framebuffer 327 */ 328 329 module->numBuffers = info.yres_virtual / info.yres; 330 module->bufferMask = 0; 331 //adreno needs page aligned offsets. Align the fbsize to pagesize. 332 unsigned int fbSize = roundUpToPageSize(finfo.line_length * info.yres)* 333 module->numBuffers; 334 module->framebuffer = new private_handle_t(fd, fbSize, 335 private_handle_t::PRIV_FLAGS_USES_ION, 336 BUFFER_TYPE_UI, 337 module->fbFormat, info.xres, info.yres); 338 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 339 if (vaddr == MAP_FAILED) { 340 ALOGE("Error mapping the framebuffer (%s)", strerror(errno)); 341 close(fd); 342 return -errno; 343 } 344 module->framebuffer->base = uint64_t(vaddr); 345 memset(vaddr, 0, fbSize); 346 //Enable vsync 347 int enable = 1; 348 ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL, 349 &enable); 350 return 0; 351 } 352 353 static int mapFrameBuffer(struct private_module_t* module) 354 { 355 int err = -1; 356 char property[PROPERTY_VALUE_MAX]; 357 if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && 358 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) || 359 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) { 360 pthread_mutex_lock(&module->lock); 361 err = mapFrameBufferLocked(module); 362 pthread_mutex_unlock(&module->lock); 363 } 364 return err; 365 } 366 367 /*****************************************************************************/ 368 369 static int fb_close(struct hw_device_t *dev) 370 { 371 fb_context_t* ctx = (fb_context_t*)dev; 372 if (ctx) { 373 //Hack until fbdev is removed. Framework could close this causing hwc a 374 //pain. 375 //free(ctx); 376 } 377 return 0; 378 } 379 380 int fb_device_open(hw_module_t const* module, const char* name, 381 hw_device_t** device) 382 { 383 int status = -EINVAL; 384 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 385 alloc_device_t* gralloc_device; 386 status = gralloc_open(module, &gralloc_device); 387 if (status < 0) 388 return status; 389 390 /* initialize our state here */ 391 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); 392 if(dev == NULL) { 393 gralloc_close(gralloc_device); 394 return status; 395 } 396 memset(dev, 0, sizeof(*dev)); 397 398 /* initialize the procs */ 399 dev->device.common.tag = HARDWARE_DEVICE_TAG; 400 dev->device.common.version = 0; 401 dev->device.common.module = const_cast<hw_module_t*>(module); 402 dev->device.common.close = fb_close; 403 dev->device.setSwapInterval = fb_setSwapInterval; 404 dev->device.post = fb_post; 405 dev->device.setUpdateRect = 0; 406 dev->device.compositionComplete = fb_compositionComplete; 407 408 private_module_t* m = (private_module_t*)module; 409 status = mapFrameBuffer(m); 410 if (status >= 0) { 411 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); 412 const_cast<uint32_t&>(dev->device.flags) = 0; 413 const_cast<uint32_t&>(dev->device.width) = m->info.xres; 414 const_cast<uint32_t&>(dev->device.height) = m->info.yres; 415 const_cast<int&>(dev->device.stride) = stride; 416 const_cast<int&>(dev->device.format) = m->fbFormat; 417 const_cast<float&>(dev->device.xdpi) = m->xdpi; 418 const_cast<float&>(dev->device.ydpi) = m->ydpi; 419 const_cast<float&>(dev->device.fps) = m->fps; 420 const_cast<int&>(dev->device.minSwapInterval) = 421 PRIV_MIN_SWAP_INTERVAL; 422 const_cast<int&>(dev->device.maxSwapInterval) = 423 PRIV_MAX_SWAP_INTERVAL; 424 const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers; 425 dev->device.setUpdateRect = 0; 426 427 *device = &dev->device.common; 428 } 429 430 // Close the gralloc module 431 gralloc_close(gralloc_device); 432 } 433 return status; 434 } 435