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