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