Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 3 | * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved. |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 4 | * |
| 5 | * Not a Contribution, Apache license notifications and license are retained |
| 6 | * for attribution purposes only. |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 21 | #define DEBUG_COPYBIT 0 |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 22 | #include <copybit.h> |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 23 | #include <utils/Timers.h> |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 24 | #include "hwc_copybit.h" |
| 25 | #include "comptype.h" |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 26 | #include "gr.h" |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 27 | |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 28 | namespace qhwc { |
| 29 | |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 30 | struct range { |
| 31 | int current; |
| 32 | int end; |
| 33 | }; |
| 34 | struct region_iterator : public copybit_region_t { |
| 35 | |
| 36 | region_iterator(hwc_region_t region) { |
| 37 | mRegion = region; |
| 38 | r.end = region.numRects; |
| 39 | r.current = 0; |
| 40 | this->next = iterate; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | static int iterate(copybit_region_t const * self, copybit_rect_t* rect){ |
| 45 | if (!self || !rect) { |
| 46 | ALOGE("iterate invalid parameters"); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | region_iterator const* me = |
| 51 | static_cast<region_iterator const*>(self); |
| 52 | if (me->r.current != me->r.end) { |
| 53 | rect->l = me->mRegion.rects[me->r.current].left; |
| 54 | rect->t = me->mRegion.rects[me->r.current].top; |
| 55 | rect->r = me->mRegion.rects[me->r.current].right; |
| 56 | rect->b = me->mRegion.rects[me->r.current].bottom; |
| 57 | me->r.current++; |
| 58 | return 1; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | hwc_region_t mRegion; |
| 64 | mutable range r; |
| 65 | }; |
| 66 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 67 | void CopyBit::reset() { |
| 68 | mIsModeOn = false; |
| 69 | mCopyBitDraw = false; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 72 | bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) { |
| 73 | // return true for non-overlay targets |
| 74 | if(ctx->mMDP.hasOverlay) { |
| 75 | return false; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 76 | } |
| 77 | return true; |
| 78 | } |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 79 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 80 | bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx, |
| 81 | hwc_display_contents_1_t *list, |
| 82 | int dpy) { |
| 83 | int compositionType = qdutils::QCCompositionType:: |
| 84 | getInstance().getCompositionType(); |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 85 | |
| 86 | if ((compositionType & qdutils::COMPOSITION_TYPE_C2D) || |
| 87 | (compositionType & qdutils::COMPOSITION_TYPE_DYN)) { |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 88 | if(ctx->listStats[dpy].yuvCount) { |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 89 | //Overlay up & running. Dont use COPYBIT for RGB layers. |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (compositionType & qdutils::COMPOSITION_TYPE_DYN) { |
| 95 | // DYN Composition: |
| 96 | // use copybit, if (TotalRGBRenderArea < 2 * FB Area) |
| 97 | // this is done based on perf inputs in ICS |
| 98 | // TODO: Above condition needs to be re-evaluated in JB |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 99 | int fbWidth = ctx->dpyAttr[dpy].xres; |
| 100 | int fbHeight = ctx->dpyAttr[dpy].yres; |
| 101 | unsigned int fbArea = (fbWidth * fbHeight); |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 102 | unsigned int renderArea = getRGBRenderingArea(list); |
| 103 | ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u", |
| 104 | __FUNCTION__, renderArea, fbArea); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 105 | if (renderArea <= (2 * fbArea)) { |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) { |
| 109 | // MDP composition, use COPYBIT always |
| 110 | return true; |
| 111 | } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) { |
| 112 | // C2D composition, use COPYBIT |
| 113 | return true; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 118 | unsigned int CopyBit::getRGBRenderingArea |
| 119 | (const hwc_display_contents_1_t *list) { |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 120 | //Calculates total rendering area for RGB layers |
| 121 | unsigned int renderArea = 0; |
| 122 | unsigned int w=0, h=0; |
| 123 | for (unsigned int i=0; i<list->numHwLayers; i++) { |
| 124 | private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle; |
| 125 | if (hnd) { |
| 126 | if (BUFFER_TYPE_UI == hnd->bufferType) { |
| 127 | getLayerResolution(&list->hwLayers[i], w, h); |
| 128 | renderArea += (w*h); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return renderArea; |
| 133 | } |
| 134 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 135 | bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list, |
| 136 | int dpy) { |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 137 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 138 | if(mEngine == NULL) { |
| 139 | // No copybit device found - cannot use copybit |
| 140 | return false; |
| 141 | } |
| 142 | int compositionType = qdutils::QCCompositionType:: |
| 143 | getInstance().getCompositionType(); |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 144 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 145 | if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) || |
| 146 | (compositionType == qdutils::COMPOSITION_TYPE_CPU)) { |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 147 | //GPU/CPU composition, don't change layer composition type |
| 148 | return true; |
| 149 | } |
| 150 | |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 151 | if(!(validateParams(ctx, list))) { |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 152 | ALOGE("%s:Invalid Params", __FUNCTION__); |
| 153 | return false; |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 156 | if(ctx->listStats[dpy].skipCount) { |
| 157 | //GPU will be anyways used |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | bool useCopybitForYUV = canUseCopybitForYUV(ctx); |
| 162 | bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy); |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 163 | LayerProp *layerProp = ctx->layerProp[dpy]; |
| 164 | size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex; |
| 165 | hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex]; |
| 166 | private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle; |
| 167 | |
| 168 | |
| 169 | |
| 170 | //Allocate render buffers if they're not allocated |
| 171 | if (useCopybitForYUV || useCopybitForRGB) { |
| 172 | int ret = allocRenderBuffers(fbHnd->width, |
| 173 | fbHnd->height, |
| 174 | fbHnd->format); |
| 175 | if (ret < 0) { |
| 176 | return false; |
| 177 | } else { |
| 178 | mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % |
| 179 | NUM_RENDER_BUFFERS; |
| 180 | } |
| 181 | } |
| 182 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 183 | |
| 184 | // numAppLayers-1, as we iterate till 0th layer index |
| 185 | for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) { |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 186 | private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle; |
| 187 | |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 188 | if ((hnd->bufferType == BUFFER_TYPE_VIDEO && useCopybitForYUV) || |
| 189 | (hnd->bufferType == BUFFER_TYPE_UI && useCopybitForRGB)) { |
| 190 | layerProp[i].mFlags |= HWC_COPYBIT; |
| 191 | list->hwLayers[i].compositionType = HWC_OVERLAY; |
| 192 | mCopyBitDraw = true; |
| 193 | } else { |
| 194 | // We currently cannot mix copybit layers with layers marked to |
| 195 | // be drawn on the framebuffer or that are on the layer cache. |
| 196 | mCopyBitDraw = false; |
| 197 | //There is no need to reset layer properties here as we return in |
| 198 | //draw if mCopyBitDraw is false |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 204 | bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list, |
| 205 | int dpy, int32_t *fd) { |
| 206 | // draw layers marked for COPYBIT |
| 207 | int retVal = true; |
| 208 | int copybitLayerCount = 0; |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 209 | LayerProp *layerProp = ctx->layerProp[dpy]; |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 210 | |
| 211 | if(mCopyBitDraw == false) // there is no layer marked for copybit |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 212 | return false ; |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 213 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 214 | //render buffer |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 215 | private_handle_t *renderBuffer = getCurrentRenderBuffer(); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 216 | if (!renderBuffer) { |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 217 | ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 218 | return false; |
| 219 | } |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 220 | |
| 221 | //Wait for the previous frame to complete before rendering onto it |
| 222 | if(mRelFd[0] >=0) { |
| 223 | sync_wait(mRelFd[0], 1000); |
| 224 | close(mRelFd[0]); |
| 225 | mRelFd[0] = -1; |
| 226 | } |
| 227 | // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 228 | for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) { |
| 229 | hwc_layer_1_t *layer = &list->hwLayers[i]; |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 230 | if(!(layerProp[i].mFlags & HWC_COPYBIT)) { |
| 231 | ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 232 | continue; |
| 233 | } |
| 234 | int ret = -1; |
| 235 | if (list->hwLayers[i].acquireFenceFd != -1 ) { |
| 236 | // Wait for acquire Fence on the App buffers. |
| 237 | ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000); |
| 238 | if(ret < 0) { |
| 239 | ALOGE("%s: sync_wait error!! error no = %d err str = %s", |
| 240 | __FUNCTION__, errno, strerror(errno)); |
| 241 | } |
| 242 | close(list->hwLayers[i].acquireFenceFd); |
| 243 | list->hwLayers[i].acquireFenceFd = -1; |
| 244 | } |
| 245 | retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]), |
| 246 | renderBuffer, dpy); |
| 247 | copybitLayerCount++; |
| 248 | if(retVal < 0) { |
| 249 | ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (copybitLayerCount) { |
| 254 | copybit_device_t *copybit = getCopyBitDevice(); |
| 255 | // Async mode |
| 256 | copybit->flush_get_fence(copybit, fd); |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
Naseer Ahmed | 5b6708a | 2012-08-02 13:46:08 -0700 | [diff] [blame] | 261 | int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer, |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 262 | private_handle_t *renderBuffer, int dpy) |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 263 | { |
| 264 | hwc_context_t* ctx = (hwc_context_t*)(dev); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 265 | int err = 0; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 266 | if(!ctx) { |
| 267 | ALOGE("%s: null context ", __FUNCTION__); |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | private_handle_t *hnd = (private_handle_t *)layer->handle; |
| 272 | if(!hnd) { |
| 273 | ALOGE("%s: invalid handle", __FUNCTION__); |
| 274 | return -1; |
| 275 | } |
| 276 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 277 | private_handle_t *fbHandle = (private_handle_t *)renderBuffer; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 278 | if(!fbHandle) { |
| 279 | ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | // Set the copybit source: |
| 284 | copybit_image_t src; |
| 285 | src.w = hnd->width; |
| 286 | src.h = hnd->height; |
| 287 | src.format = hnd->format; |
| 288 | src.base = (void *)hnd->base; |
| 289 | src.handle = (native_handle_t *)layer->handle; |
| 290 | src.horiz_padding = src.w - hnd->width; |
| 291 | // Initialize vertical padding to zero for now, |
| 292 | // this needs to change to accomodate vertical stride |
| 293 | // if needed in the future |
| 294 | src.vert_padding = 0; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 295 | |
| 296 | // Copybit source rect |
| 297 | hwc_rect_t sourceCrop = layer->sourceCrop; |
| 298 | copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top, |
| 299 | sourceCrop.right, |
| 300 | sourceCrop.bottom}; |
| 301 | |
| 302 | // Copybit destination rect |
| 303 | hwc_rect_t displayFrame = layer->displayFrame; |
| 304 | copybit_rect_t dstRect = {displayFrame.left, displayFrame.top, |
| 305 | displayFrame.right, |
| 306 | displayFrame.bottom}; |
| 307 | |
| 308 | // Copybit dst |
| 309 | copybit_image_t dst; |
| 310 | dst.w = ALIGN(fbHandle->width,32); |
| 311 | dst.h = fbHandle->height; |
| 312 | dst.format = fbHandle->format; |
| 313 | dst.base = (void *)fbHandle->base; |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 314 | dst.handle = (native_handle_t *)fbHandle; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 315 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 316 | copybit_device_t *copybit = mEngine; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 317 | |
| 318 | int32_t screen_w = displayFrame.right - displayFrame.left; |
| 319 | int32_t screen_h = displayFrame.bottom - displayFrame.top; |
| 320 | int32_t src_crop_width = sourceCrop.right - sourceCrop.left; |
| 321 | int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top; |
| 322 | |
| 323 | // Copybit dst |
| 324 | float copybitsMaxScale = |
| 325 | (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT); |
| 326 | float copybitsMinScale = |
| 327 | (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT); |
| 328 | |
| 329 | if((layer->transform == HWC_TRANSFORM_ROT_90) || |
| 330 | (layer->transform == HWC_TRANSFORM_ROT_270)) { |
| 331 | //swap screen width and height |
| 332 | int tmp = screen_w; |
| 333 | screen_w = screen_h; |
| 334 | screen_h = tmp; |
| 335 | } |
| 336 | private_handle_t *tmpHnd = NULL; |
| 337 | |
| 338 | if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) { |
| 339 | ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \ |
| 340 | screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w, |
| 341 | src_crop_width,screen_w,src_crop_width); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | float dsdx = (float)screen_w/src_crop_width; |
| 346 | float dtdy = (float)screen_h/src_crop_height; |
| 347 | |
| 348 | float scaleLimitMax = copybitsMaxScale * copybitsMaxScale; |
| 349 | float scaleLimitMin = copybitsMinScale * copybitsMinScale; |
| 350 | if(dsdx > scaleLimitMax || |
| 351 | dtdy > scaleLimitMax || |
| 352 | dsdx < 1/scaleLimitMin || |
| 353 | dtdy < 1/scaleLimitMin) { |
| 354 | ALOGE("%s: greater than max supported size dsdx=%f dtdy=%f \ |
| 355 | scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy, |
| 356 | scaleLimitMax,1/scaleLimitMin); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 357 | return -1; |
| 358 | } |
| 359 | if(dsdx > copybitsMaxScale || |
| 360 | dtdy > copybitsMaxScale || |
| 361 | dsdx < 1/copybitsMinScale || |
| 362 | dtdy < 1/copybitsMinScale){ |
| 363 | // The requested scale is out of the range the hardware |
| 364 | // can support. |
| 365 | ALOGE("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\ |
| 366 | copybitsMinScale=%f,screen_w=%d,screen_h=%d \ |
| 367 | src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__, |
| 368 | dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h, |
| 369 | src_crop_width,src_crop_height); |
| 370 | |
| 371 | //Driver makes width and height as even |
| 372 | //that may cause wrong calculation of the ratio |
| 373 | //in display and crop.Hence we make |
| 374 | //crop width and height as even. |
| 375 | src_crop_width = (src_crop_width/2)*2; |
| 376 | src_crop_height = (src_crop_height/2)*2; |
| 377 | |
| 378 | int tmp_w = src_crop_width; |
| 379 | int tmp_h = src_crop_height; |
| 380 | |
| 381 | if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){ |
| 382 | tmp_w = src_crop_width*copybitsMaxScale; |
| 383 | tmp_h = src_crop_height*copybitsMaxScale; |
| 384 | }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){ |
| 385 | tmp_w = src_crop_width/copybitsMinScale; |
| 386 | tmp_h = src_crop_height/copybitsMinScale; |
| 387 | tmp_w = (tmp_w/2)*2; |
| 388 | tmp_h = (tmp_h/2)*2; |
| 389 | } |
| 390 | ALOGE("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h); |
| 391 | |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 392 | int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 393 | |
| 394 | if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, fbHandle->format, usage)){ |
| 395 | copybit_image_t tmp_dst; |
| 396 | copybit_rect_t tmp_rect; |
| 397 | tmp_dst.w = tmp_w; |
| 398 | tmp_dst.h = tmp_h; |
| 399 | tmp_dst.format = tmpHnd->format; |
| 400 | tmp_dst.handle = tmpHnd; |
| 401 | tmp_dst.horiz_padding = src.horiz_padding; |
| 402 | tmp_dst.vert_padding = src.vert_padding; |
| 403 | tmp_rect.l = 0; |
| 404 | tmp_rect.t = 0; |
| 405 | tmp_rect.r = tmp_dst.w; |
| 406 | tmp_rect.b = tmp_dst.h; |
| 407 | //create one clip region |
| 408 | hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b}; |
| 409 | hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect}; |
| 410 | region_iterator tmp_it(tmp_hwc_reg); |
| 411 | copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0); |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 412 | //TODO: once, we are able to read layer alpha, update this |
| 413 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 414 | err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect, |
| 415 | &srcRect, &tmp_it); |
| 416 | if(err < 0){ |
| 417 | ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__, |
| 418 | __LINE__); |
| 419 | if(tmpHnd) |
| 420 | free_buffer(tmpHnd); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 421 | return err; |
| 422 | } |
| 423 | // copy new src and src rect crop |
| 424 | src = tmp_dst; |
| 425 | srcRect = tmp_rect; |
| 426 | } |
| 427 | } |
| 428 | // Copybit region |
| 429 | hwc_region_t region = layer->visibleRegionScreen; |
| 430 | region_iterator copybitRegion(region); |
| 431 | |
| 432 | copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH, |
| 433 | renderBuffer->width); |
| 434 | copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT, |
| 435 | renderBuffer->height); |
| 436 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, |
| 437 | layer->transform); |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 438 | //TODO: once, we are able to read layer alpha, update this |
| 439 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 440 | copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, |
| 441 | layer->blending); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 442 | copybit->set_parameter(copybit, COPYBIT_DITHER, |
| 443 | (dst.format == HAL_PIXEL_FORMAT_RGB_565)? |
| 444 | COPYBIT_ENABLE : COPYBIT_DISABLE); |
| 445 | copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER, |
| 446 | COPYBIT_ENABLE); |
| 447 | err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect, |
| 448 | ©bitRegion); |
| 449 | copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER, |
| 450 | COPYBIT_DISABLE); |
| 451 | |
| 452 | if(tmpHnd) |
| 453 | free_buffer(tmpHnd); |
| 454 | |
| 455 | if(err < 0) |
| 456 | ALOGE("%s: copybit stretch failed",__FUNCTION__); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 457 | return err; |
| 458 | } |
| 459 | |
Naseer Ahmed | 5b6708a | 2012-08-02 13:46:08 -0700 | [diff] [blame] | 460 | void CopyBit::getLayerResolution(const hwc_layer_1_t* layer, |
Naseer Ahmed | 45a9960 | 2012-07-31 19:15:24 -0700 | [diff] [blame] | 461 | unsigned int& width, unsigned int& height) |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 462 | { |
| 463 | hwc_rect_t displayFrame = layer->displayFrame; |
| 464 | |
| 465 | width = displayFrame.right - displayFrame.left; |
| 466 | height = displayFrame.bottom - displayFrame.top; |
| 467 | } |
| 468 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 469 | bool CopyBit::validateParams(hwc_context_t *ctx, |
| 470 | const hwc_display_contents_1_t *list) { |
| 471 | //Validate parameters |
| 472 | if (!ctx) { |
| 473 | ALOGE("%s:Invalid HWC context", __FUNCTION__); |
| 474 | return false; |
| 475 | } else if (!list) { |
| 476 | ALOGE("%s:Invalid HWC layer list", __FUNCTION__); |
| 477 | return false; |
| 478 | } |
| 479 | return true; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 480 | } |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 481 | |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 482 | |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 483 | int CopyBit::allocRenderBuffers(int w, int h, int f) |
| 484 | { |
| 485 | int ret = 0; |
| 486 | for (int i = 0; i < NUM_RENDER_BUFFERS; i++) { |
| 487 | if (mRenderBuffer[i] == NULL) { |
| 488 | ret = alloc_buffer(&mRenderBuffer[i], |
| 489 | w, h, f, |
| 490 | GRALLOC_USAGE_PRIVATE_IOMMU_HEAP); |
| 491 | } |
| 492 | if(ret < 0) { |
| 493 | freeRenderBuffers(); |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | void CopyBit::freeRenderBuffers() |
| 501 | { |
| 502 | for (int i = 0; i < NUM_RENDER_BUFFERS; i++) { |
| 503 | if(mRenderBuffer[i]) { |
| 504 | free_buffer(mRenderBuffer[i]); |
| 505 | mRenderBuffer[i] = NULL; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | private_handle_t * CopyBit::getCurrentRenderBuffer() { |
| 511 | return mRenderBuffer[mCurRenderBufferIndex]; |
| 512 | } |
| 513 | |
| 514 | void CopyBit::setReleaseFd(int fd) { |
| 515 | if(mRelFd[0] >=0) |
| 516 | close(mRelFd[0]); |
| 517 | mRelFd[0] = mRelFd[1]; |
| 518 | mRelFd[1] = dup(fd); |
| 519 | } |
| 520 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 521 | struct copybit_device_t* CopyBit::getCopyBitDevice() { |
| 522 | return mEngine; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 525 | CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false), |
| 526 | mCurRenderBufferIndex(0){ |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 527 | hw_module_t const *module; |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 528 | for (int i = 0; i < NUM_RENDER_BUFFERS; i++) |
| 529 | mRenderBuffer[i] = NULL; |
| 530 | mRelFd[0] = -1; |
| 531 | mRelFd[1] = -1; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 532 | if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) { |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 533 | if(copybit_open(module, &mEngine) < 0) { |
| 534 | ALOGE("FATAL ERROR: copybit open failed."); |
| 535 | } |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 536 | } else { |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 537 | ALOGE("FATAL ERROR: copybit hw module not found"); |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
Saurabh Shah | 661a58f | 2012-08-30 15:30:49 -0700 | [diff] [blame] | 540 | |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 541 | CopyBit::~CopyBit() |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 542 | { |
Naseer Ahmed | 64b8121 | 2013-02-14 10:29:47 -0500 | [diff] [blame^] | 543 | freeRenderBuffers(); |
| 544 | if(mRelFd[0] >=0) |
| 545 | close(mRelFd[0]); |
| 546 | if(mRelFd[1] >=0) |
| 547 | close(mRelFd[1]); |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 548 | if(mEngine) |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 549 | { |
Arun Kumar K.R | 361da4f | 2012-11-28 10:42:59 -0800 | [diff] [blame] | 550 | copybit_close(mEngine); |
| 551 | mEngine = NULL; |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
Naseer Ahmed | 31da0b1 | 2012-07-31 18:55:33 -0700 | [diff] [blame] | 554 | }; //namespace qhwc |