hwc_utils.cpp revision bfa005e8c51529d99b9c360a444d351ebe0885f7
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * Copyright (C) 2012, 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/ioctl.h> 19#include <EGL/egl.h> 20#include <cutils/properties.h> 21#include <gralloc_priv.h> 22#include <fb_priv.h> 23#include <overlay.h> 24#include "hwc_utils.h" 25#include "hwc_mdpcomp.h" 26#include "hwc_fbupdate.h" 27#include "mdp_version.h" 28#include "external.h" 29#include "QService.h" 30 31namespace qhwc { 32 33// Opens Framebuffer device 34static void openFramebufferDevice(hwc_context_t *ctx) 35{ 36 hw_module_t const *module; 37 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) { 38 framebuffer_open(module, &(ctx->mFbDev)); 39 private_module_t* m = reinterpret_cast<private_module_t*>( 40 ctx->mFbDev->common.module); 41 //xres, yres may not be 32 aligned 42 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = m->finfo.line_length / 43 (m->info.xres/8); 44 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres; 45 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres; 46 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi; 47 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi; 48 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 49 1000000000l / ctx->mFbDev->fps; 50 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY); 51 } 52} 53 54void initContext(hwc_context_t *ctx) 55{ 56 openFramebufferDevice(ctx); 57 overlay::Overlay::initOverlay(); 58 ctx->mOverlay = overlay::Overlay::getInstance(); 59 ctx->mQService = qService::QService::getInstance(ctx); 60 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion(); 61 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay(); 62 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType(); 63 //Is created and destroyed only once for primary 64 //For external it could get created and destroyed multiple times depending 65 //on what external we connect to. 66 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] = 67 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres, 68 HWC_DISPLAY_PRIMARY); 69 ctx->mExtDisplay = new ExternalDisplay(ctx); 70 for (uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) 71 ctx->mLayerCache[i] = new LayerCache(); 72 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres); 73 MDPComp::init(ctx); 74 75 pthread_mutex_init(&(ctx->vstate.lock), NULL); 76 pthread_cond_init(&(ctx->vstate.cond), NULL); 77 ctx->vstate.enable = false; 78 79 ALOGI("Initializing Qualcomm Hardware Composer"); 80 ALOGI("MDP version: %d", ctx->mMDP.version); 81} 82 83void closeContext(hwc_context_t *ctx) 84{ 85 if(ctx->mOverlay) { 86 delete ctx->mOverlay; 87 ctx->mOverlay = NULL; 88 } 89 90 if(ctx->mFbDev) { 91 framebuffer_close(ctx->mFbDev); 92 ctx->mFbDev = NULL; 93 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd); 94 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1; 95 } 96 97 if(ctx->mExtDisplay) { 98 delete ctx->mExtDisplay; 99 ctx->mExtDisplay = NULL; 100 } 101 102 for(int i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) { 103 if(ctx->mFBUpdate[i]) { 104 delete ctx->mFBUpdate[i]; 105 ctx->mFBUpdate[i] = NULL; 106 } 107 } 108 109 if(ctx->mMDPComp) { 110 delete ctx->mMDPComp; 111 ctx->mMDPComp = NULL; 112 } 113 114 pthread_mutex_destroy(&(ctx->vstate.lock)); 115 pthread_cond_destroy(&(ctx->vstate.cond)); 116} 117 118 119void dumpsys_log(android::String8& buf, const char* fmt, ...) 120{ 121 va_list varargs; 122 va_start(varargs, fmt); 123 buf.appendFormatV(fmt, varargs); 124 va_end(varargs); 125} 126 127static inline bool isAlphaScaled(hwc_layer_1_t const* layer) { 128 int dst_w, dst_h, src_w, src_h; 129 130 hwc_rect_t displayFrame = layer->displayFrame; 131 hwc_rect_t sourceCrop = layer->sourceCrop; 132 133 dst_w = displayFrame.right - displayFrame.left; 134 dst_h = displayFrame.bottom - displayFrame.top; 135 136 src_w = sourceCrop.right - sourceCrop.left; 137 src_h = sourceCrop.bottom - sourceCrop.top; 138 139 if(((src_w != dst_w) || (src_h != dst_h))) { 140 if(layer->blending != HWC_BLENDING_NONE) 141 return true; 142 } 143 return false; 144} 145 146void setListStats(hwc_context_t *ctx, 147 const hwc_display_contents_1_t *list, int dpy) { 148 149 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1; 150 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1; 151 ctx->listStats[dpy].yuvCount = 0; 152 ctx->listStats[dpy].yuvIndex = -1; 153 ctx->listStats[dpy].skipCount = 0; 154 ctx->listStats[dpy].needsAlphaScale = false; 155 156 for (size_t i = 0; i < list->numHwLayers; i++) { 157 hwc_layer_1_t const* layer = &list->hwLayers[i]; 158 private_handle_t *hnd = (private_handle_t *)layer->handle; 159 160 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 161 continue; 162 //We disregard FB being skip for now! so the else if 163 } else if (isSkipLayer(&list->hwLayers[i])) { 164 ctx->listStats[dpy].skipCount++; 165 } 166 167 if(!ctx->listStats[dpy].needsAlphaScale) 168 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer); 169 170 if (UNLIKELY(isYuvBuffer(hnd))) { 171 ctx->listStats[dpy].yuvCount++; 172 ctx->listStats[dpy].yuvIndex = i; 173 } 174 } 175} 176 177 178static inline void calc_cut(float& leftCutRatio, float& topCutRatio, 179 float& rightCutRatio, float& bottomCutRatio, int orient) { 180 if(orient & HAL_TRANSFORM_FLIP_H) { 181 swap(leftCutRatio, rightCutRatio); 182 } 183 if(orient & HAL_TRANSFORM_FLIP_V) { 184 swap(topCutRatio, bottomCutRatio); 185 } 186 if(orient & HAL_TRANSFORM_ROT_90) { 187 //Anti clock swapping 188 float tmpCutRatio = leftCutRatio; 189 leftCutRatio = topCutRatio; 190 topCutRatio = rightCutRatio; 191 rightCutRatio = bottomCutRatio; 192 bottomCutRatio = tmpCutRatio; 193 } 194} 195 196bool isSecuring(hwc_context_t* ctx) { 197 if((ctx->mMDP.version < qdutils::MDSS_V5) && 198 (ctx->mMDP.version > qdutils::MDP_V3_0) && 199 ctx->mSecuring) { 200 return true; 201 } 202 return false; 203} 204 205 206//Crops source buffer against destination and FB boundaries 207void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, 208 const int fbWidth, const int fbHeight, int orient) { 209 int& crop_l = crop.left; 210 int& crop_t = crop.top; 211 int& crop_r = crop.right; 212 int& crop_b = crop.bottom; 213 int crop_w = crop.right - crop.left; 214 int crop_h = crop.bottom - crop.top; 215 216 int& dst_l = dst.left; 217 int& dst_t = dst.top; 218 int& dst_r = dst.right; 219 int& dst_b = dst.bottom; 220 int dst_w = abs(dst.right - dst.left); 221 int dst_h = abs(dst.bottom - dst.top); 222 223 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f, 224 bottomCutRatio = 0.0f; 225 226 if(dst_l < 0) { 227 leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w; 228 dst_l = 0; 229 } 230 if(dst_r > fbWidth) { 231 rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w; 232 dst_r = fbWidth; 233 } 234 if(dst_t < 0) { 235 topCutRatio = (float)(0 - dst_t) / (float)dst_h; 236 dst_t = 0; 237 } 238 if(dst_b > fbHeight) { 239 bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h; 240 dst_b = fbHeight; 241 } 242 243 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient); 244 crop_l += crop_w * leftCutRatio; 245 crop_t += crop_h * topCutRatio; 246 crop_r -= crop_w * rightCutRatio; 247 crop_b -= crop_h * bottomCutRatio; 248} 249 250bool isExternalActive(hwc_context_t* ctx) { 251 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive; 252} 253 254int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) { 255 int ret = 0; 256 struct mdp_buf_sync data; 257 int acquireFd[MAX_NUM_LAYERS]; 258 int count = 0; 259 int releaseFd = -1; 260 int fbFd = -1; 261 bool swapzero = false; 262 data.flags = MDP_BUF_SYNC_FLAG_WAIT; 263 data.acq_fen_fd = acquireFd; 264 data.rel_fen_fd = &releaseFd; 265 char property[PROPERTY_VALUE_MAX]; 266 if(property_get("debug.egl.swapinterval", property, "1") > 0) { 267 if(atoi(property) == 0) 268 swapzero = true; 269 } 270 271 //Accumulate acquireFenceFds 272 for(uint32_t i = 0; i < list->numHwLayers; i++) { 273 if((list->hwLayers[i].compositionType == HWC_OVERLAY || 274 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) && 275 list->hwLayers[i].acquireFenceFd != -1 ){ 276 if(UNLIKELY(swapzero)) 277 acquireFd[count++] = -1; 278 else 279 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 280 } 281 } 282 283 data.acq_fen_fd_cnt = count; 284 fbFd = ctx->dpyAttr[dpy].fd; 285 286 //Waits for acquire fences, returns a release fence 287 if(LIKELY(!swapzero)) 288 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data); 289 if(ret < 0) { 290 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s", 291 strerror(errno)); 292 } 293 294 for(uint32_t i = 0; i < list->numHwLayers; i++) { 295 if((list->hwLayers[i].compositionType == HWC_OVERLAY || 296 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) { 297 //Close the acquireFenceFds 298 if(list->hwLayers[i].acquireFenceFd > 0) { 299 close(list->hwLayers[i].acquireFenceFd); 300 list->hwLayers[i].acquireFenceFd = -1; 301 } 302 //Populate releaseFenceFds. 303 if(UNLIKELY(swapzero)) 304 list->hwLayers[i].releaseFenceFd = -1; 305 else 306 list->hwLayers[i].releaseFenceFd = dup(releaseFd); 307 } 308 } 309 if(UNLIKELY(swapzero)){ 310 list->retireFenceFd = -1; 311 close(releaseFd); 312 } else { 313 list->retireFenceFd = releaseFd; 314 } 315 return ret; 316} 317 318void LayerCache::resetLayerCache(int num) { 319 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) { 320 hnd[i] = NULL; 321 } 322 numHwLayers = num; 323} 324 325void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) { 326 327 int numFbLayers = 0; 328 int numCacheableLayers = 0; 329 330 canUseLayerCache = false; 331 //Bail if geometry changed or num of layers changed 332 if(list->flags & HWC_GEOMETRY_CHANGED || 333 list->numHwLayers != numHwLayers ) { 334 resetLayerCache(list->numHwLayers); 335 return; 336 } 337 338 for(uint32_t i = 0; i < list->numHwLayers; i++) { 339 //Bail on skip layers 340 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) { 341 resetLayerCache(list->numHwLayers); 342 return; 343 } 344 345 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) { 346 numFbLayers++; 347 if(hnd[i] == NULL) { 348 hnd[i] = list->hwLayers[i].handle; 349 } else if (hnd[i] == 350 list->hwLayers[i].handle) { 351 numCacheableLayers++; 352 } else { 353 hnd[i] = NULL; 354 return; 355 } 356 } else { 357 hnd[i] = NULL; 358 } 359 } 360 if(numFbLayers == numCacheableLayers) 361 canUseLayerCache = true; 362 363 //XXX: The marking part is separate, if MDP comp wants 364 // to use it in the future. Right now getting MDP comp 365 // to use this is more trouble than it is worth. 366 markCachedLayersAsOverlay(list); 367} 368 369void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) { 370 //This optimization only works if ALL the layer handles 371 //that were on the framebuffer didn't change. 372 if(canUseLayerCache){ 373 for(uint32_t i = 0; i < list->numHwLayers; i++) { 374 if (list->hwLayers[i].handle && 375 list->hwLayers[i].handle == hnd[i] && 376 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET) 377 { 378 list->hwLayers[i].compositionType = HWC_OVERLAY; 379 } 380 } 381 } 382 383} 384 385};//namespace 386