blob: 33d460bc25af79e27eb1d3f647dda923d6b16085 [file] [log] [blame]
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -08003 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07004 *
radhakrishna2f00c8f2013-10-21 16:49:21 +05305 * Not a Contribution.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Naseer Ahmed45a99602012-07-31 19:15:24 -070020#define DEBUG_COPYBIT 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070021#include <copybit.h>
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080022#include <utils/Timers.h>
Terence Hampson0124cc72013-04-18 23:45:56 -070023#include <mdp_version.h>
Naseer Ahmed45a99602012-07-31 19:15:24 -070024#include "hwc_copybit.h"
25#include "comptype.h"
Naseer Ahmed64b81212013-02-14 10:29:47 -050026#include "gr.h"
radhakrishna2f00c8f2013-10-21 16:49:21 +053027#include "cb_utils.h"
Ramakant Singh762ae082013-11-28 18:45:34 +053028#include "cb_swap_rect.h"
Ramakant Singh80f36f22014-02-25 14:11:35 +053029#include "math.h"
Saurabh Shah74eff4d2014-03-28 16:33:13 -070030#include "sync/sync.h"
31
radhakrishna2f00c8f2013-10-21 16:49:21 +053032using namespace qdutils;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070033namespace qhwc {
34
Naseer Ahmed31da0b12012-07-31 18:55:33 -070035struct range {
36 int current;
37 int end;
38};
39struct region_iterator : public copybit_region_t {
40
41 region_iterator(hwc_region_t region) {
42 mRegion = region;
Praveena Pachipulusud9443c72014-02-17 10:42:28 +053043 r.end = (int)region.numRects;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070044 r.current = 0;
45 this->next = iterate;
46 }
47
48private:
49 static int iterate(copybit_region_t const * self, copybit_rect_t* rect){
50 if (!self || !rect) {
51 ALOGE("iterate invalid parameters");
52 return 0;
53 }
54
55 region_iterator const* me =
56 static_cast<region_iterator const*>(self);
57 if (me->r.current != me->r.end) {
58 rect->l = me->mRegion.rects[me->r.current].left;
59 rect->t = me->mRegion.rects[me->r.current].top;
60 rect->r = me->mRegion.rects[me->r.current].right;
61 rect->b = me->mRegion.rects[me->r.current].bottom;
62 me->r.current++;
63 return 1;
64 }
65 return 0;
66 }
67
68 hwc_region_t mRegion;
69 mutable range r;
70};
71
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080072void CopyBit::reset() {
73 mIsModeOn = false;
74 mCopyBitDraw = false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070075}
76
Naseer Ahmed45a99602012-07-31 19:15:24 -070077bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
78 // return true for non-overlay targets
Terence Hampsond0fd5792013-05-29 11:56:52 -040079 if(ctx->mMDP.hasOverlay && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070080 return false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070081 }
82 return true;
83}
Naseer Ahmed45a99602012-07-31 19:15:24 -070084
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080085bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
86 hwc_display_contents_1_t *list,
87 int dpy) {
88 int compositionType = qdutils::QCCompositionType::
89 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -070090
Naseer Ahmed45a99602012-07-31 19:15:24 -070091 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
92 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053093 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070094 // this is done based on perf inputs in ICS
95 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080096 int fbWidth = ctx->dpyAttr[dpy].xres;
97 int fbHeight = ctx->dpyAttr[dpy].yres;
98 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -070099 unsigned int renderArea = getRGBRenderingArea(list);
100 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
101 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530102 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700103 return true;
104 }
105 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
106 // MDP composition, use COPYBIT always
107 return true;
108 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
109 // C2D composition, use COPYBIT
110 return true;
111 }
112 return false;
113}
114
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800115unsigned int CopyBit::getRGBRenderingArea
116 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700117 //Calculates total rendering area for RGB layers
118 unsigned int renderArea = 0;
119 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400120 // Skipping last layer since FrameBuffer layer should not affect
121 // which composition to choose
122 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700123 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
124 if (hnd) {
125 if (BUFFER_TYPE_UI == hnd->bufferType) {
126 getLayerResolution(&list->hwLayers[i], w, h);
127 renderArea += (w*h);
128 }
129 }
130 }
131 return renderArea;
132}
133
Ramakant Singh21cec722014-03-07 19:11:45 +0530134int CopyBit::getLayersChanging(hwc_context_t *ctx,
135 hwc_display_contents_1_t *list,
136 int dpy){
137
138 int changingLayerIndex = -1;
139 if(mLayerCache.layerCount != ctx->listStats[dpy].numAppLayers) {
140 mLayerCache.reset();
141 mFbCache.reset();
142 mLayerCache.updateCounts(ctx,list,dpy);
143 return -1;
144 }
145
146 int updatingLayerCount = 0;
147 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--){
148 //swap rect will kick in only for single updating layer
149 if(mLayerCache.hnd[k] != list->hwLayers[k].handle){
150 updatingLayerCount ++;
151 if(updatingLayerCount == 1)
152 changingLayerIndex = k;
153 }
154 }
155 //since we are using more than one framebuffers,we have to
156 //kick in swap rect only if we are getting continuous same
157 //dirty rect for same layer at least equal of number of
158 //framebuffers
159
160 if ( updatingLayerCount == 1 ) {
Saurabh Shah93c69052014-04-24 10:27:10 -0700161 hwc_rect_t dirtyRect = list->hwLayers[changingLayerIndex].displayFrame;
162#ifdef QCOM_BSP
163 dirtyRect = list->hwLayers[changingLayerIndex].dirtyRect;
164#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530165
166 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--){
167 //disable swap rect for overlapping visible layer(s)
168 hwc_rect_t displayFrame = list->hwLayers[k].displayFrame;
169 hwc_rect_t result = getIntersection(displayFrame,dirtyRect);
170 if((k != changingLayerIndex) && isValidRect(result)){
171 return -1;
172 }
173 }
174 mFbCache.insertAndUpdateFbCache(dirtyRect);
175 if(mFbCache.getUnchangedFbDRCount(dirtyRect) <
176 NUM_RENDER_BUFFERS)
177 changingLayerIndex = -1;
178 }else {
179 mFbCache.reset();
180 changingLayerIndex = -1;
181 }
182 mLayerCache.updateCounts(ctx,list,dpy);
183 return changingLayerIndex;
184}
185
186int CopyBit::checkDirtyRect(hwc_context_t *ctx,
187 hwc_display_contents_1_t *list,
188 int dpy) {
189
190 //dirty rect will enable only if
191 //1.Only single layer is updating.
192 //2.No overlapping
193 //3.No scaling
194 //4.No video layer
195 if(mSwapRectEnable == false)
196 return -1;
197 int changingLayerIndex = getLayersChanging(ctx, list, dpy);
198 //swap rect will kick in only for single updating layer
199 if(changingLayerIndex == -1){
200 return -1;
201 }
202 if(!needsScaling(&list->hwLayers[changingLayerIndex])){
203 private_handle_t *hnd =
204 (private_handle_t *)list->hwLayers[changingLayerIndex].handle;
205 if( hnd && !isYuvBuffer(hnd))
206 return changingLayerIndex;
207 }
208 return -1;
209}
210
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700211bool CopyBit::prepareOverlap(hwc_context_t *ctx,
212 hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700213
214 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
215 ALOGE("%s: Invalid request", __FUNCTION__);
216 return false;
217 }
218
219 if (mEngine == NULL || !(validateParams(ctx, list))) {
220 ALOGE("%s: Invalid Params", __FUNCTION__);
221 return false;
222 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700223 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700224
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700225 // Allocate render buffers if they're not allocated
226 int alignW = 0, alignH = 0;
227 int finalW = 0, finalH = 0;
228 for (int i = 0; i < ptorInfo->count; i++) {
229 int ovlapIndex = ptorInfo->layerIndex[i];
230 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
231 // render buffer width will be the max of two layers
232 // Align Widht and height to 32, Mdp would be configured
233 // with Aligned overlap w/h
234 finalW = max(finalW, ALIGN((overlap.right - overlap.left), 32));
235 finalH += ALIGN((overlap.bottom - overlap.top), 32);
236 if(finalH > ALIGN((overlap.bottom - overlap.top), 32)) {
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700237 // Calculate the dest top, left will always be zero
238 ptorInfo->displayFrame[i].top = (finalH -
239 (ALIGN((overlap.bottom - overlap.top), 32)));
240 }
241 // calculate the right and bottom values
242 ptorInfo->displayFrame[i].right = ptorInfo->displayFrame[i].left +
243 (overlap.right - overlap.left);
244 ptorInfo->displayFrame[i].bottom = ptorInfo->displayFrame[i].top +
245 (overlap.bottom - overlap.top);
246 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700247
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700248 getBufferSizeAndDimensions(finalW, finalH, HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -0700249 alignW, alignH);
250
Sushil Chauhanfb8845c2014-07-22 12:42:07 -0700251 int heightFromTop = 0;
252 for (int i = 0; i < ptorInfo->count; i++) {
253 // Offset depends on stride calculated by gralloc for RGBA (4 bpp)
254 ptorInfo->mRenderBuffOffset[i] = alignW * heightFromTop * 4;
255 heightFromTop += ALIGN((ptorInfo->displayFrame[i].bottom -
256 ptorInfo->displayFrame[i].top), 32);
257 }
258
Sushil Chauhandefd3522014-05-13 18:17:12 -0700259 if ((mAlignedWidth != alignW) || (mAlignedHeight != alignH)) {
260 // Overlap rect has changed, so free render buffers
261 freeRenderBuffers();
262 }
263
264 int ret = allocRenderBuffers(alignW, alignH, HAL_PIXEL_FORMAT_RGBA_8888);
265
266 if (ret < 0) {
267 ALOGE("%s: Render buffer allocation failed", __FUNCTION__);
268 return false;
269 }
270
271 mAlignedWidth = alignW;
272 mAlignedHeight = alignH;
273 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
274 return true;
275}
276
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800277bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
278 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700279
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800280 if(mEngine == NULL) {
281 // No copybit device found - cannot use copybit
282 return false;
283 }
284 int compositionType = qdutils::QCCompositionType::
285 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700286
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800287 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
288 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700289 //GPU/CPU composition, don't change layer composition type
290 return true;
291 }
292
Naseer Ahmed45a99602012-07-31 19:15:24 -0700293 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800294 ALOGE("%s:Invalid Params", __FUNCTION__);
295 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700296 }
297
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800298 if(ctx->listStats[dpy].skipCount) {
299 //GPU will be anyways used
300 return false;
301 }
302
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700303 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700304 // Reached max layers supported by HWC.
305 return false;
306 }
307
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800308 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
309 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500310 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500311
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530312 // Following are MDP3 limitations for which we
313 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400314 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500315 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400316 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
317 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500318 int dst_h, dst_w, src_h, src_w;
319 float dx, dy;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400320 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530321 if (layer->planeAlpha != 0xFF)
322 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530323 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500324
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700325 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530326 src_h = sourceCrop.right - sourceCrop.left;
327 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500328 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530329 src_h = sourceCrop.bottom - sourceCrop.top;
330 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500331 }
332 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
333 dst_w = layer->displayFrame.right - layer->displayFrame.left;
334
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530335 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
336 ALOGE("%s: wrong params for display screen_w=%d \
337 src_crop_width=%d screen_h=%d src_crop_height=%d",
338 __FUNCTION__, dst_w,src_w,dst_h,src_h);
339 return false;
340 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530341 dx = (float)dst_w/(float)src_w;
342 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530343
Terence Hampson6b0a4272013-11-06 16:04:51 -0500344 if (dx > MAX_SCALE_FACTOR || dx < MIN_SCALE_FACTOR)
345 return false;
346
347 if (dy > MAX_SCALE_FACTOR || dy < MIN_SCALE_FACTOR)
348 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400349 }
350 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500351
352 //Allocate render buffers if they're not allocated
Terence Hampsonc67b0372013-10-09 11:32:21 -0400353 if (ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
354 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700355 int ret = allocRenderBuffers(mAlignedWidth,
356 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700357 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500358 if (ret < 0) {
359 return false;
360 } else {
361 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
362 NUM_RENDER_BUFFERS;
363 }
364 }
365
Terence Hampsond0fd5792013-05-29 11:56:52 -0400366 // We cannot mix copybit layer with layers marked to be drawn on FB
367 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
368 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800369
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530370 mCopyBitDraw = false;
371 if (useCopybitForRGB &&
372 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
373 mCopyBitDraw = true;
374 // numAppLayers-1, as we iterate till 0th layer index
375 // Mark all layers to be drawn by copybit
376 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500377 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700378#ifdef QCOM_BSP
Terence Hampsonc67b0372013-10-09 11:32:21 -0400379 if (ctx->mMDP.version == qdutils::MDP_V3_0_4)
380 list->hwLayers[i].compositionType = HWC_BLIT;
381 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700382#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400383 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700384 }
385 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530386
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700387 return true;
388}
389
Naseer Ahmed183939d2013-03-14 20:44:17 -0400390int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
391{
392 int ret = 0;
393 copybit_rect_t clear_rect = {rect.left, rect.top,
394 rect.right,
395 rect.bottom};
396
397 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700398 buf.w = ALIGN(getWidth(hnd),32);
399 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400400 buf.format = hnd->format;
401 buf.base = (void *)hnd->base;
402 buf.handle = (native_handle_t *)hnd;
403
404 copybit_device_t *copybit = mEngine;
405 ret = copybit->clear(copybit, &buf, &clear_rect);
406 return ret;
407}
408
Ramakant Singh0def28c2014-03-28 20:43:13 +0530409bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
410 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530411 int dpy, int *copybitFd) {
412 int layerCount = 0;
Shalaj Jaina70b4352014-06-15 13:47:47 -0700413 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530414 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
415 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530416
417 if(ctx->enableABC == false)
418 return false;
419
Ramakant Singh467759f2014-04-16 11:09:40 +0530420 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530421 return false;
422
423 layerCount = ctx->listStats[dpy].numAppLayers;
424 //bottom most layer should
425 //equal to FB
426 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
427 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
428 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
429 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
430 if(tmpLayer->transform ||
431 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
432 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
433 (needsScaling(tmpLayer) == true)) {
434 return false;
435 }else {
436 ctx->listStats[dpy].renderBufIndexforABC = 0;
437 }
438 }
439
Ramakant Singh467759f2014-04-16 11:09:40 +0530440 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
441 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530442 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530443
Ramakant Singhcc326612014-06-19 14:19:18 +0530444 if(layerCount == MAX_LAYERS_FOR_ABC) {
Ramakant Singh467759f2014-04-16 11:09:40 +0530445 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
Ramakant Singhcc326612014-06-19 14:19:18 +0530446 //enable ABC only for non intersecting layers.
447 hwc_rect_t displayFrame =
448 list->hwLayers[abcRenderBufIdx].displayFrame;
449 for (int i = abcRenderBufIdx + 1; i < layerCount; i++) {
450 hwc_rect_t tmpDisplayFrame = list->hwLayers[i].displayFrame;
451 hwc_rect_t result = getIntersection(displayFrame,tmpDisplayFrame);
452 if (isValidRect(result)) {
453 ctx->listStats[dpy].renderBufIndexforABC = -1;
454 return false;
455 }
456 }
457 // Pass the Acquire Fence FD to driver for base layer
Ramakant Singh467759f2014-04-16 11:09:40 +0530458 private_handle_t *renderBuffer =
459 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
460 copybit_device_t *copybit = getCopyBitDevice();
461 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
462 copybit->set_sync(copybit,
463 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
464 }
Ramakant Singhcc326612014-06-19 14:19:18 +0530465 for(int i = abcRenderBufIdx + 1; i < layerCount; i++){
Ramakant Singh467759f2014-04-16 11:09:40 +0530466 int retVal = drawLayerUsingCopybit(ctx,
467 &(list->hwLayers[i]),renderBuffer, 0);
468 if(retVal < 0) {
469 ALOGE("%s : Copybit failed", __FUNCTION__);
470 }
471 }
472 // Get Release Fence FD of copybit for the App layer(s)
473 copybit->flush_get_fence(copybit, copybitFd);
474 close(list->hwLayers[abcRenderBufIdx].acquireFenceFd);
475 list->hwLayers[abcRenderBufIdx].acquireFenceFd = -1;
476 return true;
477 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530478 }
479 return false;
480}
481
482bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
483 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800484 // draw layers marked for COPYBIT
485 int retVal = true;
486 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400487 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500488 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400489 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800490
Ramakant Singh21cec722014-03-07 19:11:45 +0530491 if(mCopyBitDraw == false){
492 mFbCache.reset(); // there is no layer marked for copybit
493 return false ;
494 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530495
Ramakant Singh467759f2014-04-16 11:09:40 +0530496 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530497 return true;
498 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800499 //render buffer
Terence Hampsonc67b0372013-10-09 11:32:21 -0400500 if (ctx->mMDP.version == qdutils::MDP_V3_0_4) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530501 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400502 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
503 } else {
504 renderBuffer = getCurrentRenderBuffer();
505 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800506 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500507 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800508 return false;
509 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500510
Terence Hampson0124cc72013-04-18 23:45:56 -0700511 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400512 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530513 if(mRelFd[mCurRenderBufferIndex] >=0) {
514 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
515 close(mRelFd[mCurRenderBufferIndex]);
516 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400517 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400518 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400519 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400520 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400521 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400522 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700523 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530524
Ramakant Singh21cec722014-03-07 19:11:45 +0530525 mDirtyLayerIndex = checkDirtyRect(ctx, list, dpy);
526 if( mDirtyLayerIndex != -1){
527 hwc_layer_1_t *layer = &list->hwLayers[mDirtyLayerIndex];
Saurabh Shah93c69052014-04-24 10:27:10 -0700528#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530529 clear(renderBuffer,layer->dirtyRect);
Saurabh Shah93c69052014-04-24 10:27:10 -0700530#else
531 clear(renderBuffer,layer->displayFrame);
532#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530533 } else {
534 hwc_rect_t clearRegion = {0,0,0,0};
535 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
536 clear(renderBuffer, clearRegion);
537 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530538
Naseer Ahmed64b81212013-02-14 10:29:47 -0500539 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800540 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500541 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
542 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800543 continue;
544 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530545 //skip non updating layers
546 if((mDirtyLayerIndex != -1) && (mDirtyLayerIndex != i) )
Ramakant Singh762ae082013-11-28 18:45:34 +0530547 continue;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800548 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400549 if (list->hwLayers[i].acquireFenceFd != -1
550 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800551 // Wait for acquire Fence on the App buffers.
552 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
553 if(ret < 0) {
554 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
555 __FUNCTION__, errno, strerror(errno));
556 }
557 close(list->hwLayers[i].acquireFenceFd);
558 list->hwLayers[i].acquireFenceFd = -1;
559 }
560 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530561 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800562 copybitLayerCount++;
563 if(retVal < 0) {
564 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
565 }
566 }
567
568 if (copybitLayerCount) {
569 copybit_device_t *copybit = getCopyBitDevice();
570 // Async mode
571 copybit->flush_get_fence(copybit, fd);
Terence Hampsonc67b0372013-10-09 11:32:21 -0400572 if(ctx->mMDP.version == qdutils::MDP_V3_0_4 &&
573 list->hwLayers[last].acquireFenceFd >= 0) {
574 close(list->hwLayers[last].acquireFenceFd);
575 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400576 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800577 }
578 return true;
579}
580
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700581int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700582 int fd = -1;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700583 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700584
585 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
586 ALOGE("%s: Invalid request", __FUNCTION__);
587 return fd;
588 }
589
590 private_handle_t *renderBuffer = getCurrentRenderBuffer();
591
592 if (!renderBuffer) {
593 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
594 return fd;
595 }
596
597 int copybitLayerCount = 0;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700598 for(int j = 0; j < ptorInfo->count; j++) {
599 int ovlapIndex = ptorInfo->layerIndex[j];
600 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700601
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700602 // Draw overlapped content of layers on render buffer
603 for (int i = 0; i <= ovlapIndex; i++) {
604 hwc_layer_1_t *layer = &list->hwLayers[i];
605 if(!isValidRect(getIntersection(layer->displayFrame,
606 overlap))) {
607 continue;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700608 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700609 if ((list->hwLayers[i].acquireFenceFd != -1)) {
610 // Wait for acquire fence on the App buffers.
611 if(sync_wait(list->hwLayers[i].acquireFenceFd, 1000) < 0) {
612 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
613 __FUNCTION__, errno, strerror(errno));
614 }
615 close(list->hwLayers[i].acquireFenceFd);
616 list->hwLayers[i].acquireFenceFd = -1;
617 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700618
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700619 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer, overlap,
620 ptorInfo->displayFrame[j]);
621 copybitLayerCount++;
622 if(retVal < 0) {
623 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
624 copybitLayerCount = 0;
625 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700626 }
627 }
628
629 if (copybitLayerCount) {
630 copybit_device_t *copybit = getCopyBitDevice();
631 copybit->flush_get_fence(copybit, &fd);
632 }
633
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700634 ALOGD_IF(DEBUG_COPYBIT, "%s: done! copybitLayerCount = %d", __FUNCTION__,
635 copybitLayerCount);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700636 return fd;
637}
638
639int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700640 private_handle_t *renderBuffer, hwc_rect_t overlap,
641 hwc_rect_t destRect)
Sushil Chauhandefd3522014-05-13 18:17:12 -0700642{
643 hwc_context_t* ctx = (hwc_context_t*)(dev);
644 if (!ctx) {
645 ALOGE("%s: null context ", __FUNCTION__);
646 return -1;
647 }
648
649 private_handle_t *hnd = (private_handle_t *)layer->handle;
650 if (!hnd) {
651 ALOGE("%s: invalid handle", __FUNCTION__);
652 return -1;
653 }
654
655 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
656 if (!dstHandle) {
657 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
658 return -1;
659 }
660
661 // Set the Copybit Source
662 copybit_image_t src;
663 src.handle = (native_handle_t *)layer->handle;
664 src.w = hnd->width;
665 src.h = hnd->height;
666 src.base = (void *)hnd->base;
667 src.format = hnd->format;
668 src.horiz_padding = 0;
669 src.vert_padding = 0;
670
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700671
Sushil Chauhandefd3522014-05-13 18:17:12 -0700672 hwc_rect_t dispFrame = layer->displayFrame;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700673 hwc_rect_t iRect = getIntersection(dispFrame, overlap);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700674 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700675 qhwc::calculate_crop_rects(crop, dispFrame, iRect,
676 layer->transform);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700677
678 // Copybit source rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700679 copybit_rect_t srcRect = {crop.left, crop.top, crop.right,
680 crop.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700681
682 // Copybit destination rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700683 copybit_rect_t dstRect = {destRect.left, destRect.top, destRect.right,
684 destRect.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700685
686 // Copybit dst
687 copybit_image_t dst;
688 dst.handle = (native_handle_t *)dstHandle;
689 dst.w = ALIGN(dstHandle->width, 32);
690 dst.h = dstHandle->height;
691 dst.base = (void *)dstHandle->base;
692 dst.format = dstHandle->format;
693
694 copybit_device_t *copybit = mEngine;
695
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700696 // Copybit region is the destRect
697 hwc_rect_t regRect = {dstRect.l,dstRect.t, dstRect.r, dstRect.b};
698 hwc_region_t region;
699 region.numRects = 1;
700 region.rects = &regRect;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700701 region_iterator copybitRegion(region);
702 int acquireFd = layer->acquireFenceFd;
703
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700704 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
705 renderBuffer->width);
706 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
707 renderBuffer->height);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700708 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
709 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
710 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
711 copybit->set_parameter(copybit, COPYBIT_DITHER,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700712 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE :
713 COPYBIT_DISABLE);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700714 copybit->set_sync(copybit, acquireFd);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700715 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
716 &copybitRegion);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700717
718 if (err < 0)
719 ALOGE("%s: copybit stretch failed",__FUNCTION__);
720
721 return err;
722}
723
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700724int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800725 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700726{
727 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400728 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700729 if(!ctx) {
730 ALOGE("%s: null context ", __FUNCTION__);
731 return -1;
732 }
733
734 private_handle_t *hnd = (private_handle_t *)layer->handle;
735 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700736 if (layer->flags & HWC_COLOR_FILL) { // Color layer
737 return fillColorUsingCopybit(layer, renderBuffer);
738 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700739 ALOGE("%s: invalid handle", __FUNCTION__);
740 return -1;
741 }
742
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800743 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700744 if(!fbHandle) {
745 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700746 return -1;
747 }
748
749 // Set the copybit source:
750 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700751 src.w = getWidth(hnd);
752 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700753 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700754
755 // Handle R/B swap
756 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
757 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
758 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
759 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
760 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
761 }
762 }
763
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700764 src.base = (void *)hnd->base;
765 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700766 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700767 // Initialize vertical padding to zero for now,
768 // this needs to change to accomodate vertical stride
769 // if needed in the future
770 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700771
shuoy7c101f92013-08-26 14:48:57 +0800772 int layerTransform = layer->transform ;
773 // When flip and rotation(90) are present alter the flip,
774 // as GPU is doing the flip and rotation in opposite order
775 // to that of MDP3.0
776 // For 270 degrees, we get 90 + (H+V) which is same as doing
777 // flip first and then rotation (H+V) + 90
778 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
779 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
780 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
781 (layer->transform & HAL_TRANSFORM_ROT_90) &&
782 !(layer->transform == HAL_TRANSFORM_ROT_270)){
783 if(layer->transform & HAL_TRANSFORM_FLIP_H){
784 layerTransform ^= HAL_TRANSFORM_FLIP_H;
785 layerTransform |= HAL_TRANSFORM_FLIP_V;
786 }
787 if(layer->transform & HAL_TRANSFORM_FLIP_V){
788 layerTransform ^= HAL_TRANSFORM_FLIP_V;
789 layerTransform |= HAL_TRANSFORM_FLIP_H;
790 }
791 }
792 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700793 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700794 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700795 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
796 sourceCrop.right,
797 sourceCrop.bottom};
798
799 // Copybit destination rect
800 hwc_rect_t displayFrame = layer->displayFrame;
801 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
802 displayFrame.right,
803 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700804#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530805 //change src and dst with dirtyRect
806 if(mDirtyLayerIndex != -1) {
807 srcRect.l = layer->dirtyRect.left;
808 srcRect.t = layer->dirtyRect.top;
809 srcRect.r = layer->dirtyRect.right;
810 srcRect.b = layer->dirtyRect.bottom;
811 dstRect = srcRect;
812 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700813#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700814 // Copybit dst
815 copybit_image_t dst;
816 dst.w = ALIGN(fbHandle->width,32);
817 dst.h = fbHandle->height;
818 dst.format = fbHandle->format;
819 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800820 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700821
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800822 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700823
824 int32_t screen_w = displayFrame.right - displayFrame.left;
825 int32_t screen_h = displayFrame.bottom - displayFrame.top;
826 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
827 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
828
829 // Copybit dst
830 float copybitsMaxScale =
831 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
832 float copybitsMinScale =
833 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
834
Sushil Chauhandffbf432013-12-09 15:33:57 -0800835 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700836 //swap screen width and height
837 int tmp = screen_w;
838 screen_w = screen_h;
839 screen_h = tmp;
840 }
841 private_handle_t *tmpHnd = NULL;
842
843 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
844 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530845 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
846 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700847 return -1;
848 }
849
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530850 float dsdx = (float)screen_w/(float)src_crop_width;
851 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700852
853 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
854 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
855 if(dsdx > scaleLimitMax ||
856 dtdy > scaleLimitMax ||
857 dsdx < 1/scaleLimitMin ||
858 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400859 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700860 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
861 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700862 return -1;
863 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400864 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700865 if(dsdx > copybitsMaxScale ||
866 dtdy > copybitsMaxScale ||
867 dsdx < 1/copybitsMinScale ||
868 dtdy < 1/copybitsMinScale){
869 // The requested scale is out of the range the hardware
870 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400871 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700872 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
873 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
874 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
875 src_crop_width,src_crop_height);
876
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700877
878 int tmp_w = src_crop_width;
879 int tmp_h = src_crop_height;
880
881 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530882 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
883 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700884 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
Ramakant Singh80f36f22014-02-25 14:11:35 +0530885 // ceil the tmp_w and tmp_h value to maintain proper ratio
886 // b/w src and dst (should not cross the desired scale limit
887 // due to float -> int )
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530888 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
889 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700890 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400891 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700892
Naseer Ahmed64b81212013-02-14 10:29:47 -0500893 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400894 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700895
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400896 // We do not want copybit to generate alpha values from nothing
897 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
898 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
899 format = HAL_PIXEL_FORMAT_RGBX_8888;
900 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800901 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700902 copybit_image_t tmp_dst;
903 copybit_rect_t tmp_rect;
904 tmp_dst.w = tmp_w;
905 tmp_dst.h = tmp_h;
906 tmp_dst.format = tmpHnd->format;
907 tmp_dst.handle = tmpHnd;
908 tmp_dst.horiz_padding = src.horiz_padding;
909 tmp_dst.vert_padding = src.vert_padding;
910 tmp_rect.l = 0;
911 tmp_rect.t = 0;
912 tmp_rect.r = tmp_dst.w;
913 tmp_rect.b = tmp_dst.h;
914 //create one clip region
915 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
916 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
917 region_iterator tmp_it(tmp_hwc_reg);
918 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700919 //TODO: once, we are able to read layer alpha, update this
920 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400921 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700922 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
923 &srcRect, &tmp_it);
924 if(err < 0){
925 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
926 __LINE__);
927 if(tmpHnd)
928 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700929 return err;
930 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400931 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400932 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400933 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400934 close(acquireFd);
935 acquireFd = -1;
936 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700937 // copy new src and src rect crop
938 src = tmp_dst;
939 srcRect = tmp_rect;
940 }
941 }
942 // Copybit region
943 hwc_region_t region = layer->visibleRegionScreen;
944 region_iterator copybitRegion(region);
945
946 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
947 renderBuffer->width);
948 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
949 renderBuffer->height);
950 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800951 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700952 //TODO: once, we are able to read layer alpha, update this
953 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800954 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
955 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700956 copybit->set_parameter(copybit, COPYBIT_DITHER,
957 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
958 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530959 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
960 COPYBIT_ENABLE : COPYBIT_DISABLE);
961
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700962 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
963 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400964 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700965 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
966 &copybitRegion);
967 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
968 COPYBIT_DISABLE);
969
Terence Hampsonadf47302013-05-23 12:21:02 -0400970 if(tmpHnd) {
971 if (ctx->mMDP.version < qdutils::MDP_V4_0){
972 int ret = -1, releaseFd;
973 // we need to wait for the buffer before freeing
974 copybit->flush_get_fence(copybit, &releaseFd);
975 ret = sync_wait(releaseFd, 1000);
976 if(ret < 0) {
977 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
978 __FUNCTION__, errno, strerror(errno));
979 }
980 close(releaseFd);
981 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700982 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400983 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700984
985 if(err < 0)
986 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700987 return err;
988}
989
Sushil Chauhan943797c2013-10-21 17:35:55 -0700990int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
991 private_handle_t *renderBuffer)
992{
993 if (!renderBuffer) {
994 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
995 return -1;
996 }
997
998 // Copybit dst
999 copybit_image_t dst;
1000 dst.w = ALIGN(renderBuffer->width, 32);
1001 dst.h = renderBuffer->height;
1002 dst.format = renderBuffer->format;
1003 dst.base = (void *)renderBuffer->base;
1004 dst.handle = (native_handle_t *)renderBuffer;
1005
1006 // Copybit dst rect
1007 hwc_rect_t displayFrame = layer->displayFrame;
1008 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
1009 displayFrame.right, displayFrame.bottom};
1010
1011 uint32_t color = layer->transform;
1012 copybit_device_t *copybit = mEngine;
1013 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1014 renderBuffer->width);
1015 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1016 renderBuffer->height);
1017 copybit->set_parameter(copybit, COPYBIT_DITHER,
1018 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
1019 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -08001020 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -07001021 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
1022 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
1023 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
1024 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
1025 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
1026 return res;
1027}
1028
Naseer Ahmed5b6708a2012-08-02 13:46:08 -07001029void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -07001030 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001031{
1032 hwc_rect_t displayFrame = layer->displayFrame;
1033
1034 width = displayFrame.right - displayFrame.left;
1035 height = displayFrame.bottom - displayFrame.top;
1036}
1037
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001038bool CopyBit::validateParams(hwc_context_t *ctx,
1039 const hwc_display_contents_1_t *list) {
1040 //Validate parameters
1041 if (!ctx) {
1042 ALOGE("%s:Invalid HWC context", __FUNCTION__);
1043 return false;
1044 } else if (!list) {
1045 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
1046 return false;
1047 }
1048 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001049}
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001050
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001051
Naseer Ahmed64b81212013-02-14 10:29:47 -05001052int CopyBit::allocRenderBuffers(int w, int h, int f)
1053{
1054 int ret = 0;
1055 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1056 if (mRenderBuffer[i] == NULL) {
1057 ret = alloc_buffer(&mRenderBuffer[i],
1058 w, h, f,
1059 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
1060 }
1061 if(ret < 0) {
1062 freeRenderBuffers();
1063 break;
1064 }
1065 }
1066 return ret;
1067}
1068
1069void CopyBit::freeRenderBuffers()
1070{
1071 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1072 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301073 //Since we are freeing buffer close the fence if it has a valid one.
1074 if(mRelFd[i] >= 0) {
1075 close(mRelFd[i]);
1076 mRelFd[i] = -1;
1077 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001078 free_buffer(mRenderBuffer[i]);
1079 mRenderBuffer[i] = NULL;
1080 }
1081 }
1082}
1083
1084private_handle_t * CopyBit::getCurrentRenderBuffer() {
1085 return mRenderBuffer[mCurRenderBufferIndex];
1086}
1087
1088void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301089 if(mRelFd[mCurRenderBufferIndex] >=0)
1090 close(mRelFd[mCurRenderBufferIndex]);
1091 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001092}
1093
Sushil Chauhandefd3522014-05-13 18:17:12 -07001094void CopyBit::setReleaseFdSync(int fd) {
1095 if (mRelFd[mCurRenderBufferIndex] >=0) {
1096 int ret = -1;
1097 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1098 if (ret < 0)
1099 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1100 __FUNCTION__, errno, strerror(errno));
1101 close(mRelFd[mCurRenderBufferIndex]);
1102 }
1103 mRelFd[mCurRenderBufferIndex] = dup(fd);
1104}
1105
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001106struct copybit_device_t* CopyBit::getCopyBitDevice() {
1107 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001108}
1109
Shalaj Jaina70b4352014-06-15 13:47:47 -07001110CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mEngine(0),
1111 mIsModeOn(false), mCopyBitDraw(false), mCurRenderBufferIndex(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001112
1113 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1114 ctx->dpyAttr[dpy].yres,
1115 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001116 mAlignedWidth,
1117 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001118
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001119 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301120 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001121 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301122 mRelFd[i] = -1;
1123 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301124
1125 char value[PROPERTY_VALUE_MAX];
1126 property_get("debug.hwc.dynThreshold", value, "2");
1127 mDynThreshold = atof(value);
1128
Ramakant Singh21cec722014-03-07 19:11:45 +05301129 property_get("debug.sf.swaprect", value, "0");
1130 mSwapRectEnable = atoi(value) ? true:false ;
1131 mDirtyLayerIndex = -1;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001132 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001133 if(copybit_open(module, &mEngine) < 0) {
1134 ALOGE("FATAL ERROR: copybit open failed.");
1135 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001136 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001137 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001138 }
1139}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001140
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001141CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001142{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001143 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001144 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001145 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001146 copybit_close(mEngine);
1147 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001148 }
1149}
Ramakant Singh21cec722014-03-07 19:11:45 +05301150CopyBit::LayerCache::LayerCache() {
1151 reset();
1152}
1153void CopyBit::LayerCache::reset() {
1154 memset(&hnd, 0, sizeof(hnd));
1155 layerCount = 0;
1156}
1157void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1158 hwc_display_contents_1_t *list, int dpy)
1159{
1160 layerCount = ctx->listStats[dpy].numAppLayers;
1161 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1162 hnd[i] = list->hwLayers[i].handle;
1163 }
1164}
1165
1166CopyBit::FbCache::FbCache() {
1167 reset();
1168}
1169void CopyBit::FbCache::reset() {
1170 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
1171 FbIndex =0;
1172}
1173
1174void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect) {
1175 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1176 FbdirtyRect[FbIndex] = dirtyRect;
1177 FbIndex++;
1178}
1179
1180int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect){
1181 int sameDirtyCount = 0;
1182 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
1183 if( FbdirtyRect[i] == dirtyRect)
1184 sameDirtyCount++;
1185 }
1186 return sameDirtyCount;
1187}
1188
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001189}; //namespace qhwc