blob: 8aa01817dd9a3a3d7f7dbaea0d71f150da4959b5 [file] [log] [blame]
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08003 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07004 *
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 Ahmed45a99602012-07-31 19:15:24 -070021#define DEBUG_COPYBIT 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070022#include <copybit.h>
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080023#include <utils/Timers.h>
Terence Hampson0124cc72013-04-18 23:45:56 -070024#include <mdp_version.h>
Naseer Ahmed45a99602012-07-31 19:15:24 -070025#include "hwc_copybit.h"
26#include "comptype.h"
Naseer Ahmed64b81212013-02-14 10:29:47 -050027#include "gr.h"
Naseer Ahmed74214722013-02-09 08:11:36 -050028
Naseer Ahmed31da0b12012-07-31 18:55:33 -070029namespace qhwc {
30
Naseer Ahmed31da0b12012-07-31 18:55:33 -070031struct range {
32 int current;
33 int end;
34};
35struct region_iterator : public copybit_region_t {
36
37 region_iterator(hwc_region_t region) {
38 mRegion = region;
39 r.end = region.numRects;
40 r.current = 0;
41 this->next = iterate;
42 }
43
44private:
45 static int iterate(copybit_region_t const * self, copybit_rect_t* rect){
46 if (!self || !rect) {
47 ALOGE("iterate invalid parameters");
48 return 0;
49 }
50
51 region_iterator const* me =
52 static_cast<region_iterator const*>(self);
53 if (me->r.current != me->r.end) {
54 rect->l = me->mRegion.rects[me->r.current].left;
55 rect->t = me->mRegion.rects[me->r.current].top;
56 rect->r = me->mRegion.rects[me->r.current].right;
57 rect->b = me->mRegion.rects[me->r.current].bottom;
58 me->r.current++;
59 return 1;
60 }
61 return 0;
62 }
63
64 hwc_region_t mRegion;
65 mutable range r;
66};
67
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080068void CopyBit::reset() {
69 mIsModeOn = false;
70 mCopyBitDraw = false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070071}
72
Naseer Ahmed45a99602012-07-31 19:15:24 -070073bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
74 // return true for non-overlay targets
Terence Hampsond0fd5792013-05-29 11:56:52 -040075 if(ctx->mMDP.hasOverlay && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070076 return false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070077 }
78 return true;
79}
Naseer Ahmed45a99602012-07-31 19:15:24 -070080
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080081bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
82 hwc_display_contents_1_t *list,
83 int dpy) {
84 int compositionType = qdutils::QCCompositionType::
85 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -070086
Naseer Ahmed45a99602012-07-31 19:15:24 -070087 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
88 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053089 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070090 // this is done based on perf inputs in ICS
91 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080092 int fbWidth = ctx->dpyAttr[dpy].xres;
93 int fbHeight = ctx->dpyAttr[dpy].yres;
94 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -070095 unsigned int renderArea = getRGBRenderingArea(list);
96 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
97 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053098 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070099 return true;
100 }
101 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
102 // MDP composition, use COPYBIT always
103 return true;
104 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
105 // C2D composition, use COPYBIT
106 return true;
107 }
108 return false;
109}
110
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800111unsigned int CopyBit::getRGBRenderingArea
112 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700113 //Calculates total rendering area for RGB layers
114 unsigned int renderArea = 0;
115 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400116 // Skipping last layer since FrameBuffer layer should not affect
117 // which composition to choose
118 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700119 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
120 if (hnd) {
121 if (BUFFER_TYPE_UI == hnd->bufferType) {
122 getLayerResolution(&list->hwLayers[i], w, h);
123 renderArea += (w*h);
124 }
125 }
126 }
127 return renderArea;
128}
129
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800130bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
131 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700132
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800133 if(mEngine == NULL) {
134 // No copybit device found - cannot use copybit
135 return false;
136 }
137 int compositionType = qdutils::QCCompositionType::
138 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700139
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800140 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
141 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700142 //GPU/CPU composition, don't change layer composition type
143 return true;
144 }
145
Naseer Ahmed45a99602012-07-31 19:15:24 -0700146 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800147 ALOGE("%s:Invalid Params", __FUNCTION__);
148 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700149 }
150
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800151 if(ctx->listStats[dpy].skipCount) {
152 //GPU will be anyways used
153 return false;
154 }
155
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700156 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700157 // Reached max layers supported by HWC.
158 return false;
159 }
160
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800161 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
162 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500163 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
Terence Hampsonc235bda2013-07-12 12:47:38 -0400169 // Avoid issue with mdp3 and rotation by using gpu composition
170 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
171 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
172 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
173 if ((layer->transform & (HAL_TRANSFORM_FLIP_H |
174 HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)) &&
175 ((layer->displayFrame.bottom - layer->displayFrame.top) % 16 ||
176 (layer->displayFrame.right - layer->displayFrame.left) % 16))
177 return true;
178 }
179 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500180
181 //Allocate render buffers if they're not allocated
182 if (useCopybitForYUV || useCopybitForRGB) {
183 int ret = allocRenderBuffers(fbHnd->width,
184 fbHnd->height,
185 fbHnd->format);
186 if (ret < 0) {
187 return false;
188 } else {
189 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
190 NUM_RENDER_BUFFERS;
191 }
192 }
193
Terence Hampsond0fd5792013-05-29 11:56:52 -0400194 // We cannot mix copybit layer with layers marked to be drawn on FB
195 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
196 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800197
198 // numAppLayers-1, as we iterate till 0th layer index
199 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700200 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
201
Naseer Ahmed64b81212013-02-14 10:29:47 -0500202 if ((hnd->bufferType == BUFFER_TYPE_VIDEO && useCopybitForYUV) ||
203 (hnd->bufferType == BUFFER_TYPE_UI && useCopybitForRGB)) {
204 layerProp[i].mFlags |= HWC_COPYBIT;
205 list->hwLayers[i].compositionType = HWC_OVERLAY;
206 mCopyBitDraw = true;
207 } else {
208 // We currently cannot mix copybit layers with layers marked to
209 // be drawn on the framebuffer or that are on the layer cache.
210 mCopyBitDraw = false;
211 //There is no need to reset layer properties here as we return in
212 //draw if mCopyBitDraw is false
Terence Hampsond0fd5792013-05-29 11:56:52 -0400213 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700214 }
215 }
216 return true;
217}
218
Naseer Ahmed183939d2013-03-14 20:44:17 -0400219int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
220{
221 int ret = 0;
222 copybit_rect_t clear_rect = {rect.left, rect.top,
223 rect.right,
224 rect.bottom};
225
226 copybit_image_t buf;
227 buf.w = ALIGN(hnd->width,32);
228 buf.h = hnd->height;
229 buf.format = hnd->format;
230 buf.base = (void *)hnd->base;
231 buf.handle = (native_handle_t *)hnd;
232
233 copybit_device_t *copybit = mEngine;
234 ret = copybit->clear(copybit, &buf, &clear_rect);
235 return ret;
236}
237
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800238bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
239 int dpy, int32_t *fd) {
240 // draw layers marked for COPYBIT
241 int retVal = true;
242 int copybitLayerCount = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500243 LayerProp *layerProp = ctx->layerProp[dpy];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800244
245 if(mCopyBitDraw == false) // there is no layer marked for copybit
Naseer Ahmed64b81212013-02-14 10:29:47 -0500246 return false ;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800247
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800248 //render buffer
Naseer Ahmed64b81212013-02-14 10:29:47 -0500249 private_handle_t *renderBuffer = getCurrentRenderBuffer();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800250 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500251 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800252 return false;
253 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500254
255 //Wait for the previous frame to complete before rendering onto it
256 if(mRelFd[0] >=0) {
257 sync_wait(mRelFd[0], 1000);
258 close(mRelFd[0]);
259 mRelFd[0] = -1;
260 }
Naseer Ahmed183939d2013-03-14 20:44:17 -0400261
Terence Hampson0124cc72013-04-18 23:45:56 -0700262 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
263 //Clear the visible region on the render buffer
264 //XXX: Do this only when needed.
265 hwc_rect_t clearRegion;
266 getNonWormholeRegion(list, clearRegion);
267 clear(renderBuffer, clearRegion);
268 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500269 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800270 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
271 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500272 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
273 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800274 continue;
275 }
276 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400277 if (list->hwLayers[i].acquireFenceFd != -1
278 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800279 // Wait for acquire Fence on the App buffers.
280 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
281 if(ret < 0) {
282 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
283 __FUNCTION__, errno, strerror(errno));
284 }
285 close(list->hwLayers[i].acquireFenceFd);
286 list->hwLayers[i].acquireFenceFd = -1;
287 }
288 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
289 renderBuffer, dpy);
290 copybitLayerCount++;
291 if(retVal < 0) {
292 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
293 }
294 }
295
296 if (copybitLayerCount) {
297 copybit_device_t *copybit = getCopyBitDevice();
298 // Async mode
299 copybit->flush_get_fence(copybit, fd);
300 }
301 return true;
302}
303
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700304int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800305 private_handle_t *renderBuffer, int dpy)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700306{
307 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400308 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700309 if(!ctx) {
310 ALOGE("%s: null context ", __FUNCTION__);
311 return -1;
312 }
313
314 private_handle_t *hnd = (private_handle_t *)layer->handle;
315 if(!hnd) {
316 ALOGE("%s: invalid handle", __FUNCTION__);
317 return -1;
318 }
319
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800320 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700321 if(!fbHandle) {
322 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700323 return -1;
324 }
325
326 // Set the copybit source:
327 copybit_image_t src;
328 src.w = hnd->width;
329 src.h = hnd->height;
330 src.format = hnd->format;
331 src.base = (void *)hnd->base;
332 src.handle = (native_handle_t *)layer->handle;
333 src.horiz_padding = src.w - hnd->width;
334 // Initialize vertical padding to zero for now,
335 // this needs to change to accomodate vertical stride
336 // if needed in the future
337 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700338
shuoy7c101f92013-08-26 14:48:57 +0800339 int layerTransform = layer->transform ;
340 // When flip and rotation(90) are present alter the flip,
341 // as GPU is doing the flip and rotation in opposite order
342 // to that of MDP3.0
343 // For 270 degrees, we get 90 + (H+V) which is same as doing
344 // flip first and then rotation (H+V) + 90
345 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
346 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
347 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
348 (layer->transform & HAL_TRANSFORM_ROT_90) &&
349 !(layer->transform == HAL_TRANSFORM_ROT_270)){
350 if(layer->transform & HAL_TRANSFORM_FLIP_H){
351 layerTransform ^= HAL_TRANSFORM_FLIP_H;
352 layerTransform |= HAL_TRANSFORM_FLIP_V;
353 }
354 if(layer->transform & HAL_TRANSFORM_FLIP_V){
355 layerTransform ^= HAL_TRANSFORM_FLIP_V;
356 layerTransform |= HAL_TRANSFORM_FLIP_H;
357 }
358 }
359 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700360 // Copybit source rect
361 hwc_rect_t sourceCrop = layer->sourceCrop;
362 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
363 sourceCrop.right,
364 sourceCrop.bottom};
365
366 // Copybit destination rect
367 hwc_rect_t displayFrame = layer->displayFrame;
368 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
369 displayFrame.right,
370 displayFrame.bottom};
371
372 // Copybit dst
373 copybit_image_t dst;
374 dst.w = ALIGN(fbHandle->width,32);
375 dst.h = fbHandle->height;
376 dst.format = fbHandle->format;
377 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800378 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700379
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800380 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700381
382 int32_t screen_w = displayFrame.right - displayFrame.left;
383 int32_t screen_h = displayFrame.bottom - displayFrame.top;
384 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
385 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
386
387 // Copybit dst
388 float copybitsMaxScale =
389 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
390 float copybitsMinScale =
391 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
392
393 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
394 (layer->transform == HWC_TRANSFORM_ROT_270)) {
395 //swap screen width and height
396 int tmp = screen_w;
397 screen_w = screen_h;
398 screen_h = tmp;
399 }
400 private_handle_t *tmpHnd = NULL;
401
402 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
403 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
404 screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
405 src_crop_width,screen_w,src_crop_width);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700406 return -1;
407 }
408
409 float dsdx = (float)screen_w/src_crop_width;
410 float dtdy = (float)screen_h/src_crop_height;
411
412 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
413 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
414 if(dsdx > scaleLimitMax ||
415 dtdy > scaleLimitMax ||
416 dsdx < 1/scaleLimitMin ||
417 dtdy < 1/scaleLimitMin) {
418 ALOGE("%s: greater than max supported size dsdx=%f dtdy=%f \
419 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
420 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700421 return -1;
422 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400423 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700424 if(dsdx > copybitsMaxScale ||
425 dtdy > copybitsMaxScale ||
426 dsdx < 1/copybitsMinScale ||
427 dtdy < 1/copybitsMinScale){
428 // The requested scale is out of the range the hardware
429 // can support.
430 ALOGE("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
431 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
432 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
433 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
434 src_crop_width,src_crop_height);
435
436 //Driver makes width and height as even
437 //that may cause wrong calculation of the ratio
438 //in display and crop.Hence we make
439 //crop width and height as even.
440 src_crop_width = (src_crop_width/2)*2;
441 src_crop_height = (src_crop_height/2)*2;
442
443 int tmp_w = src_crop_width;
444 int tmp_h = src_crop_height;
445
446 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
447 tmp_w = src_crop_width*copybitsMaxScale;
448 tmp_h = src_crop_height*copybitsMaxScale;
449 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
450 tmp_w = src_crop_width/copybitsMinScale;
451 tmp_h = src_crop_height/copybitsMinScale;
452 tmp_w = (tmp_w/2)*2;
453 tmp_h = (tmp_h/2)*2;
454 }
455 ALOGE("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
456
Naseer Ahmed64b81212013-02-14 10:29:47 -0500457 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400458 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700459
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400460 // We do not want copybit to generate alpha values from nothing
461 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
462 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
463 format = HAL_PIXEL_FORMAT_RGBX_8888;
464 }
465 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage)){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700466 copybit_image_t tmp_dst;
467 copybit_rect_t tmp_rect;
468 tmp_dst.w = tmp_w;
469 tmp_dst.h = tmp_h;
470 tmp_dst.format = tmpHnd->format;
471 tmp_dst.handle = tmpHnd;
472 tmp_dst.horiz_padding = src.horiz_padding;
473 tmp_dst.vert_padding = src.vert_padding;
474 tmp_rect.l = 0;
475 tmp_rect.t = 0;
476 tmp_rect.r = tmp_dst.w;
477 tmp_rect.b = tmp_dst.h;
478 //create one clip region
479 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
480 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
481 region_iterator tmp_it(tmp_hwc_reg);
482 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700483 //TODO: once, we are able to read layer alpha, update this
484 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400485 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700486 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
487 &srcRect, &tmp_it);
488 if(err < 0){
489 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
490 __LINE__);
491 if(tmpHnd)
492 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700493 return err;
494 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400495 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400496 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400497 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400498 close(acquireFd);
499 acquireFd = -1;
500 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700501 // copy new src and src rect crop
502 src = tmp_dst;
503 srcRect = tmp_rect;
504 }
505 }
506 // Copybit region
507 hwc_region_t region = layer->visibleRegionScreen;
508 region_iterator copybitRegion(region);
509
510 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
511 renderBuffer->width);
512 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
513 renderBuffer->height);
514 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800515 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700516 //TODO: once, we are able to read layer alpha, update this
517 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800518 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
519 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700520 copybit->set_parameter(copybit, COPYBIT_DITHER,
521 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
522 COPYBIT_ENABLE : COPYBIT_DISABLE);
523 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
524 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400525 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700526 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
527 &copybitRegion);
528 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
529 COPYBIT_DISABLE);
530
Terence Hampsonadf47302013-05-23 12:21:02 -0400531 if(tmpHnd) {
532 if (ctx->mMDP.version < qdutils::MDP_V4_0){
533 int ret = -1, releaseFd;
534 // we need to wait for the buffer before freeing
535 copybit->flush_get_fence(copybit, &releaseFd);
536 ret = sync_wait(releaseFd, 1000);
537 if(ret < 0) {
538 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
539 __FUNCTION__, errno, strerror(errno));
540 }
541 close(releaseFd);
542 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700543 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400544 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700545
546 if(err < 0)
547 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700548 return err;
549}
550
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700551void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700552 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700553{
554 hwc_rect_t displayFrame = layer->displayFrame;
555
556 width = displayFrame.right - displayFrame.left;
557 height = displayFrame.bottom - displayFrame.top;
558}
559
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800560bool CopyBit::validateParams(hwc_context_t *ctx,
561 const hwc_display_contents_1_t *list) {
562 //Validate parameters
563 if (!ctx) {
564 ALOGE("%s:Invalid HWC context", __FUNCTION__);
565 return false;
566 } else if (!list) {
567 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
568 return false;
569 }
570 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700571}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700572
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700573
Naseer Ahmed64b81212013-02-14 10:29:47 -0500574int CopyBit::allocRenderBuffers(int w, int h, int f)
575{
576 int ret = 0;
577 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
578 if (mRenderBuffer[i] == NULL) {
579 ret = alloc_buffer(&mRenderBuffer[i],
580 w, h, f,
581 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
582 }
583 if(ret < 0) {
584 freeRenderBuffers();
585 break;
586 }
587 }
588 return ret;
589}
590
591void CopyBit::freeRenderBuffers()
592{
593 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
594 if(mRenderBuffer[i]) {
595 free_buffer(mRenderBuffer[i]);
596 mRenderBuffer[i] = NULL;
597 }
598 }
599}
600
601private_handle_t * CopyBit::getCurrentRenderBuffer() {
602 return mRenderBuffer[mCurRenderBufferIndex];
603}
604
605void CopyBit::setReleaseFd(int fd) {
606 if(mRelFd[0] >=0)
607 close(mRelFd[0]);
608 mRelFd[0] = mRelFd[1];
609 mRelFd[1] = dup(fd);
610}
611
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800612struct copybit_device_t* CopyBit::getCopyBitDevice() {
613 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700614}
615
Naseer Ahmed64b81212013-02-14 10:29:47 -0500616CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
617 mCurRenderBufferIndex(0){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700618 hw_module_t const *module;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500619 for (int i = 0; i < NUM_RENDER_BUFFERS; i++)
620 mRenderBuffer[i] = NULL;
621 mRelFd[0] = -1;
622 mRelFd[1] = -1;
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530623
624 char value[PROPERTY_VALUE_MAX];
625 property_get("debug.hwc.dynThreshold", value, "2");
626 mDynThreshold = atof(value);
627
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700628 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800629 if(copybit_open(module, &mEngine) < 0) {
630 ALOGE("FATAL ERROR: copybit open failed.");
631 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700632 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800633 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700634 }
635}
Saurabh Shah661a58f2012-08-30 15:30:49 -0700636
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800637CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700638{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500639 freeRenderBuffers();
640 if(mRelFd[0] >=0)
641 close(mRelFd[0]);
642 if(mRelFd[1] >=0)
643 close(mRelFd[1]);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800644 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700645 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800646 copybit_close(mEngine);
647 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700648 }
649}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700650}; //namespace qhwc