blob: d6e5e8a4b2340617a9d9b9c2850f24cb79d89448 [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
251 if ((mAlignedWidth != alignW) || (mAlignedHeight != alignH)) {
252 // Overlap rect has changed, so free render buffers
253 freeRenderBuffers();
254 }
255
256 int ret = allocRenderBuffers(alignW, alignH, HAL_PIXEL_FORMAT_RGBA_8888);
257
258 if (ret < 0) {
259 ALOGE("%s: Render buffer allocation failed", __FUNCTION__);
260 return false;
261 }
262
263 mAlignedWidth = alignW;
264 mAlignedHeight = alignH;
265 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
266 return true;
267}
268
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800269bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
270 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700271
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800272 if(mEngine == NULL) {
273 // No copybit device found - cannot use copybit
274 return false;
275 }
276 int compositionType = qdutils::QCCompositionType::
277 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700278
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800279 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
280 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700281 //GPU/CPU composition, don't change layer composition type
282 return true;
283 }
284
Naseer Ahmed45a99602012-07-31 19:15:24 -0700285 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800286 ALOGE("%s:Invalid Params", __FUNCTION__);
287 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700288 }
289
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800290 if(ctx->listStats[dpy].skipCount) {
291 //GPU will be anyways used
292 return false;
293 }
294
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700295 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700296 // Reached max layers supported by HWC.
297 return false;
298 }
299
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800300 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
301 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500302 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500303
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530304 // Following are MDP3 limitations for which we
305 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400306 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500307 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400308 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
309 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500310 int dst_h, dst_w, src_h, src_w;
311 float dx, dy;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400312 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530313 if (layer->planeAlpha != 0xFF)
314 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530315 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500316
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700317 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530318 src_h = sourceCrop.right - sourceCrop.left;
319 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500320 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530321 src_h = sourceCrop.bottom - sourceCrop.top;
322 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500323 }
324 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
325 dst_w = layer->displayFrame.right - layer->displayFrame.left;
326
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530327 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
328 ALOGE("%s: wrong params for display screen_w=%d \
329 src_crop_width=%d screen_h=%d src_crop_height=%d",
330 __FUNCTION__, dst_w,src_w,dst_h,src_h);
331 return false;
332 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530333 dx = (float)dst_w/(float)src_w;
334 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530335
Terence Hampson6b0a4272013-11-06 16:04:51 -0500336 if (dx > MAX_SCALE_FACTOR || dx < MIN_SCALE_FACTOR)
337 return false;
338
339 if (dy > MAX_SCALE_FACTOR || dy < MIN_SCALE_FACTOR)
340 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400341 }
342 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500343
344 //Allocate render buffers if they're not allocated
Terence Hampsonc67b0372013-10-09 11:32:21 -0400345 if (ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
346 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700347 int ret = allocRenderBuffers(mAlignedWidth,
348 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700349 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500350 if (ret < 0) {
351 return false;
352 } else {
353 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
354 NUM_RENDER_BUFFERS;
355 }
356 }
357
Terence Hampsond0fd5792013-05-29 11:56:52 -0400358 // We cannot mix copybit layer with layers marked to be drawn on FB
359 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
360 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800361
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530362 mCopyBitDraw = false;
363 if (useCopybitForRGB &&
364 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
365 mCopyBitDraw = true;
366 // numAppLayers-1, as we iterate till 0th layer index
367 // Mark all layers to be drawn by copybit
368 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500369 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700370#ifdef QCOM_BSP
Terence Hampsonc67b0372013-10-09 11:32:21 -0400371 if (ctx->mMDP.version == qdutils::MDP_V3_0_4)
372 list->hwLayers[i].compositionType = HWC_BLIT;
373 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700374#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400375 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700376 }
377 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530378
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700379 return true;
380}
381
Naseer Ahmed183939d2013-03-14 20:44:17 -0400382int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
383{
384 int ret = 0;
385 copybit_rect_t clear_rect = {rect.left, rect.top,
386 rect.right,
387 rect.bottom};
388
389 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700390 buf.w = ALIGN(getWidth(hnd),32);
391 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400392 buf.format = hnd->format;
393 buf.base = (void *)hnd->base;
394 buf.handle = (native_handle_t *)hnd;
395
396 copybit_device_t *copybit = mEngine;
397 ret = copybit->clear(copybit, &buf, &clear_rect);
398 return ret;
399}
400
Ramakant Singh0def28c2014-03-28 20:43:13 +0530401bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
402 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530403 int dpy, int *copybitFd) {
404 int layerCount = 0;
Shalaj Jaina70b4352014-06-15 13:47:47 -0700405 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530406 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
407 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530408
409 if(ctx->enableABC == false)
410 return false;
411
Ramakant Singh467759f2014-04-16 11:09:40 +0530412 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530413 return false;
414
415 layerCount = ctx->listStats[dpy].numAppLayers;
416 //bottom most layer should
417 //equal to FB
418 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
419 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
420 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
421 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
422 if(tmpLayer->transform ||
423 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
424 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
425 (needsScaling(tmpLayer) == true)) {
426 return false;
427 }else {
428 ctx->listStats[dpy].renderBufIndexforABC = 0;
429 }
430 }
431
Ramakant Singh467759f2014-04-16 11:09:40 +0530432 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
433 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530434 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530435
Ramakant Singhcc326612014-06-19 14:19:18 +0530436 if(layerCount == MAX_LAYERS_FOR_ABC) {
Ramakant Singh467759f2014-04-16 11:09:40 +0530437 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
Ramakant Singhcc326612014-06-19 14:19:18 +0530438 //enable ABC only for non intersecting layers.
439 hwc_rect_t displayFrame =
440 list->hwLayers[abcRenderBufIdx].displayFrame;
441 for (int i = abcRenderBufIdx + 1; i < layerCount; i++) {
442 hwc_rect_t tmpDisplayFrame = list->hwLayers[i].displayFrame;
443 hwc_rect_t result = getIntersection(displayFrame,tmpDisplayFrame);
444 if (isValidRect(result)) {
445 ctx->listStats[dpy].renderBufIndexforABC = -1;
446 return false;
447 }
448 }
449 // Pass the Acquire Fence FD to driver for base layer
Ramakant Singh467759f2014-04-16 11:09:40 +0530450 private_handle_t *renderBuffer =
451 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
452 copybit_device_t *copybit = getCopyBitDevice();
453 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
454 copybit->set_sync(copybit,
455 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
456 }
Ramakant Singhcc326612014-06-19 14:19:18 +0530457 for(int i = abcRenderBufIdx + 1; i < layerCount; i++){
Ramakant Singh467759f2014-04-16 11:09:40 +0530458 int retVal = drawLayerUsingCopybit(ctx,
459 &(list->hwLayers[i]),renderBuffer, 0);
460 if(retVal < 0) {
461 ALOGE("%s : Copybit failed", __FUNCTION__);
462 }
463 }
464 // Get Release Fence FD of copybit for the App layer(s)
465 copybit->flush_get_fence(copybit, copybitFd);
466 close(list->hwLayers[abcRenderBufIdx].acquireFenceFd);
467 list->hwLayers[abcRenderBufIdx].acquireFenceFd = -1;
468 return true;
469 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530470 }
471 return false;
472}
473
474bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
475 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800476 // draw layers marked for COPYBIT
477 int retVal = true;
478 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400479 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500480 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400481 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800482
Ramakant Singh21cec722014-03-07 19:11:45 +0530483 if(mCopyBitDraw == false){
484 mFbCache.reset(); // there is no layer marked for copybit
485 return false ;
486 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530487
Ramakant Singh467759f2014-04-16 11:09:40 +0530488 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530489 return true;
490 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800491 //render buffer
Terence Hampsonc67b0372013-10-09 11:32:21 -0400492 if (ctx->mMDP.version == qdutils::MDP_V3_0_4) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530493 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400494 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
495 } else {
496 renderBuffer = getCurrentRenderBuffer();
497 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800498 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500499 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800500 return false;
501 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500502
Terence Hampson0124cc72013-04-18 23:45:56 -0700503 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400504 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530505 if(mRelFd[mCurRenderBufferIndex] >=0) {
506 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
507 close(mRelFd[mCurRenderBufferIndex]);
508 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400509 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400510 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400511 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400512 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400513 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400514 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700515 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530516
Ramakant Singh21cec722014-03-07 19:11:45 +0530517 mDirtyLayerIndex = checkDirtyRect(ctx, list, dpy);
518 if( mDirtyLayerIndex != -1){
519 hwc_layer_1_t *layer = &list->hwLayers[mDirtyLayerIndex];
Saurabh Shah93c69052014-04-24 10:27:10 -0700520#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530521 clear(renderBuffer,layer->dirtyRect);
Saurabh Shah93c69052014-04-24 10:27:10 -0700522#else
523 clear(renderBuffer,layer->displayFrame);
524#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530525 } else {
526 hwc_rect_t clearRegion = {0,0,0,0};
527 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
528 clear(renderBuffer, clearRegion);
529 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530530
Naseer Ahmed64b81212013-02-14 10:29:47 -0500531 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800532 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500533 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
534 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800535 continue;
536 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530537 //skip non updating layers
538 if((mDirtyLayerIndex != -1) && (mDirtyLayerIndex != i) )
Ramakant Singh762ae082013-11-28 18:45:34 +0530539 continue;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800540 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400541 if (list->hwLayers[i].acquireFenceFd != -1
542 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800543 // Wait for acquire Fence on the App buffers.
544 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
545 if(ret < 0) {
546 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
547 __FUNCTION__, errno, strerror(errno));
548 }
549 close(list->hwLayers[i].acquireFenceFd);
550 list->hwLayers[i].acquireFenceFd = -1;
551 }
552 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530553 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800554 copybitLayerCount++;
555 if(retVal < 0) {
556 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
557 }
558 }
559
560 if (copybitLayerCount) {
561 copybit_device_t *copybit = getCopyBitDevice();
562 // Async mode
563 copybit->flush_get_fence(copybit, fd);
Terence Hampsonc67b0372013-10-09 11:32:21 -0400564 if(ctx->mMDP.version == qdutils::MDP_V3_0_4 &&
565 list->hwLayers[last].acquireFenceFd >= 0) {
566 close(list->hwLayers[last].acquireFenceFd);
567 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400568 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800569 }
570 return true;
571}
572
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700573int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700574 int fd = -1;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700575 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700576
577 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
578 ALOGE("%s: Invalid request", __FUNCTION__);
579 return fd;
580 }
581
582 private_handle_t *renderBuffer = getCurrentRenderBuffer();
583
584 if (!renderBuffer) {
585 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
586 return fd;
587 }
588
589 int copybitLayerCount = 0;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700590 for(int j = 0; j < ptorInfo->count; j++) {
591 int ovlapIndex = ptorInfo->layerIndex[j];
592 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700593
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700594 // Draw overlapped content of layers on render buffer
595 for (int i = 0; i <= ovlapIndex; i++) {
596 hwc_layer_1_t *layer = &list->hwLayers[i];
597 if(!isValidRect(getIntersection(layer->displayFrame,
598 overlap))) {
599 continue;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700600 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700601 if ((list->hwLayers[i].acquireFenceFd != -1)) {
602 // Wait for acquire fence on the App buffers.
603 if(sync_wait(list->hwLayers[i].acquireFenceFd, 1000) < 0) {
604 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
605 __FUNCTION__, errno, strerror(errno));
606 }
607 close(list->hwLayers[i].acquireFenceFd);
608 list->hwLayers[i].acquireFenceFd = -1;
609 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700610
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700611 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer, overlap,
612 ptorInfo->displayFrame[j]);
613 copybitLayerCount++;
614 if(retVal < 0) {
615 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
616 copybitLayerCount = 0;
617 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700618 }
619 }
620
621 if (copybitLayerCount) {
622 copybit_device_t *copybit = getCopyBitDevice();
623 copybit->flush_get_fence(copybit, &fd);
624 }
625
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700626 ALOGD_IF(DEBUG_COPYBIT, "%s: done! copybitLayerCount = %d", __FUNCTION__,
627 copybitLayerCount);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700628 return fd;
629}
630
631int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700632 private_handle_t *renderBuffer, hwc_rect_t overlap,
633 hwc_rect_t destRect)
Sushil Chauhandefd3522014-05-13 18:17:12 -0700634{
635 hwc_context_t* ctx = (hwc_context_t*)(dev);
636 if (!ctx) {
637 ALOGE("%s: null context ", __FUNCTION__);
638 return -1;
639 }
640
641 private_handle_t *hnd = (private_handle_t *)layer->handle;
642 if (!hnd) {
643 ALOGE("%s: invalid handle", __FUNCTION__);
644 return -1;
645 }
646
647 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
648 if (!dstHandle) {
649 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
650 return -1;
651 }
652
653 // Set the Copybit Source
654 copybit_image_t src;
655 src.handle = (native_handle_t *)layer->handle;
656 src.w = hnd->width;
657 src.h = hnd->height;
658 src.base = (void *)hnd->base;
659 src.format = hnd->format;
660 src.horiz_padding = 0;
661 src.vert_padding = 0;
662
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700663
Sushil Chauhandefd3522014-05-13 18:17:12 -0700664 hwc_rect_t dispFrame = layer->displayFrame;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700665 hwc_rect_t iRect = getIntersection(dispFrame, overlap);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700666 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700667 qhwc::calculate_crop_rects(crop, dispFrame, iRect,
668 layer->transform);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700669
670 // Copybit source rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700671 copybit_rect_t srcRect = {crop.left, crop.top, crop.right,
672 crop.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700673
674 // Copybit destination rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700675 copybit_rect_t dstRect = {destRect.left, destRect.top, destRect.right,
676 destRect.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700677
678 // Copybit dst
679 copybit_image_t dst;
680 dst.handle = (native_handle_t *)dstHandle;
681 dst.w = ALIGN(dstHandle->width, 32);
682 dst.h = dstHandle->height;
683 dst.base = (void *)dstHandle->base;
684 dst.format = dstHandle->format;
685
686 copybit_device_t *copybit = mEngine;
687
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700688 // Copybit region is the destRect
689 hwc_rect_t regRect = {dstRect.l,dstRect.t, dstRect.r, dstRect.b};
690 hwc_region_t region;
691 region.numRects = 1;
692 region.rects = &regRect;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700693 region_iterator copybitRegion(region);
694 int acquireFd = layer->acquireFenceFd;
695
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700696 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
697 renderBuffer->width);
698 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
699 renderBuffer->height);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700700 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
701 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
702 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
703 copybit->set_parameter(copybit, COPYBIT_DITHER,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700704 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE :
705 COPYBIT_DISABLE);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700706 copybit->set_sync(copybit, acquireFd);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700707 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
708 &copybitRegion);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700709
710 if (err < 0)
711 ALOGE("%s: copybit stretch failed",__FUNCTION__);
712
713 return err;
714}
715
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700716int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800717 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700718{
719 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400720 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700721 if(!ctx) {
722 ALOGE("%s: null context ", __FUNCTION__);
723 return -1;
724 }
725
726 private_handle_t *hnd = (private_handle_t *)layer->handle;
727 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700728 if (layer->flags & HWC_COLOR_FILL) { // Color layer
729 return fillColorUsingCopybit(layer, renderBuffer);
730 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700731 ALOGE("%s: invalid handle", __FUNCTION__);
732 return -1;
733 }
734
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800735 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700736 if(!fbHandle) {
737 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700738 return -1;
739 }
740
741 // Set the copybit source:
742 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700743 src.w = getWidth(hnd);
744 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700745 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700746
747 // Handle R/B swap
748 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
749 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
750 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
751 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
752 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
753 }
754 }
755
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700756 src.base = (void *)hnd->base;
757 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700758 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700759 // Initialize vertical padding to zero for now,
760 // this needs to change to accomodate vertical stride
761 // if needed in the future
762 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700763
shuoy7c101f92013-08-26 14:48:57 +0800764 int layerTransform = layer->transform ;
765 // When flip and rotation(90) are present alter the flip,
766 // as GPU is doing the flip and rotation in opposite order
767 // to that of MDP3.0
768 // For 270 degrees, we get 90 + (H+V) which is same as doing
769 // flip first and then rotation (H+V) + 90
770 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
771 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
772 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
773 (layer->transform & HAL_TRANSFORM_ROT_90) &&
774 !(layer->transform == HAL_TRANSFORM_ROT_270)){
775 if(layer->transform & HAL_TRANSFORM_FLIP_H){
776 layerTransform ^= HAL_TRANSFORM_FLIP_H;
777 layerTransform |= HAL_TRANSFORM_FLIP_V;
778 }
779 if(layer->transform & HAL_TRANSFORM_FLIP_V){
780 layerTransform ^= HAL_TRANSFORM_FLIP_V;
781 layerTransform |= HAL_TRANSFORM_FLIP_H;
782 }
783 }
784 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700785 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700786 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700787 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
788 sourceCrop.right,
789 sourceCrop.bottom};
790
791 // Copybit destination rect
792 hwc_rect_t displayFrame = layer->displayFrame;
793 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
794 displayFrame.right,
795 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700796#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530797 //change src and dst with dirtyRect
798 if(mDirtyLayerIndex != -1) {
799 srcRect.l = layer->dirtyRect.left;
800 srcRect.t = layer->dirtyRect.top;
801 srcRect.r = layer->dirtyRect.right;
802 srcRect.b = layer->dirtyRect.bottom;
803 dstRect = srcRect;
804 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700805#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700806 // Copybit dst
807 copybit_image_t dst;
808 dst.w = ALIGN(fbHandle->width,32);
809 dst.h = fbHandle->height;
810 dst.format = fbHandle->format;
811 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800812 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700813
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800814 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700815
816 int32_t screen_w = displayFrame.right - displayFrame.left;
817 int32_t screen_h = displayFrame.bottom - displayFrame.top;
818 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
819 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
820
821 // Copybit dst
822 float copybitsMaxScale =
823 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
824 float copybitsMinScale =
825 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
826
Sushil Chauhandffbf432013-12-09 15:33:57 -0800827 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700828 //swap screen width and height
829 int tmp = screen_w;
830 screen_w = screen_h;
831 screen_h = tmp;
832 }
833 private_handle_t *tmpHnd = NULL;
834
835 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
836 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530837 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
838 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700839 return -1;
840 }
841
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530842 float dsdx = (float)screen_w/(float)src_crop_width;
843 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700844
845 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
846 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
847 if(dsdx > scaleLimitMax ||
848 dtdy > scaleLimitMax ||
849 dsdx < 1/scaleLimitMin ||
850 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400851 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700852 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
853 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700854 return -1;
855 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400856 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700857 if(dsdx > copybitsMaxScale ||
858 dtdy > copybitsMaxScale ||
859 dsdx < 1/copybitsMinScale ||
860 dtdy < 1/copybitsMinScale){
861 // The requested scale is out of the range the hardware
862 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400863 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700864 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
865 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
866 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
867 src_crop_width,src_crop_height);
868
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700869
870 int tmp_w = src_crop_width;
871 int tmp_h = src_crop_height;
872
873 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530874 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
875 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700876 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
Ramakant Singh80f36f22014-02-25 14:11:35 +0530877 // ceil the tmp_w and tmp_h value to maintain proper ratio
878 // b/w src and dst (should not cross the desired scale limit
879 // due to float -> int )
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530880 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
881 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700882 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400883 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700884
Naseer Ahmed64b81212013-02-14 10:29:47 -0500885 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400886 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700887
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400888 // We do not want copybit to generate alpha values from nothing
889 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
890 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
891 format = HAL_PIXEL_FORMAT_RGBX_8888;
892 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800893 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700894 copybit_image_t tmp_dst;
895 copybit_rect_t tmp_rect;
896 tmp_dst.w = tmp_w;
897 tmp_dst.h = tmp_h;
898 tmp_dst.format = tmpHnd->format;
899 tmp_dst.handle = tmpHnd;
900 tmp_dst.horiz_padding = src.horiz_padding;
901 tmp_dst.vert_padding = src.vert_padding;
902 tmp_rect.l = 0;
903 tmp_rect.t = 0;
904 tmp_rect.r = tmp_dst.w;
905 tmp_rect.b = tmp_dst.h;
906 //create one clip region
907 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
908 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
909 region_iterator tmp_it(tmp_hwc_reg);
910 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700911 //TODO: once, we are able to read layer alpha, update this
912 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400913 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700914 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
915 &srcRect, &tmp_it);
916 if(err < 0){
917 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
918 __LINE__);
919 if(tmpHnd)
920 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700921 return err;
922 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400923 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400924 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400925 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400926 close(acquireFd);
927 acquireFd = -1;
928 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700929 // copy new src and src rect crop
930 src = tmp_dst;
931 srcRect = tmp_rect;
932 }
933 }
934 // Copybit region
935 hwc_region_t region = layer->visibleRegionScreen;
936 region_iterator copybitRegion(region);
937
938 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
939 renderBuffer->width);
940 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
941 renderBuffer->height);
942 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800943 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700944 //TODO: once, we are able to read layer alpha, update this
945 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800946 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
947 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700948 copybit->set_parameter(copybit, COPYBIT_DITHER,
949 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
950 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530951 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
952 COPYBIT_ENABLE : COPYBIT_DISABLE);
953
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700954 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
955 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400956 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700957 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
958 &copybitRegion);
959 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
960 COPYBIT_DISABLE);
961
Terence Hampsonadf47302013-05-23 12:21:02 -0400962 if(tmpHnd) {
963 if (ctx->mMDP.version < qdutils::MDP_V4_0){
964 int ret = -1, releaseFd;
965 // we need to wait for the buffer before freeing
966 copybit->flush_get_fence(copybit, &releaseFd);
967 ret = sync_wait(releaseFd, 1000);
968 if(ret < 0) {
969 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
970 __FUNCTION__, errno, strerror(errno));
971 }
972 close(releaseFd);
973 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700974 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400975 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700976
977 if(err < 0)
978 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700979 return err;
980}
981
Sushil Chauhan943797c2013-10-21 17:35:55 -0700982int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
983 private_handle_t *renderBuffer)
984{
985 if (!renderBuffer) {
986 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
987 return -1;
988 }
989
990 // Copybit dst
991 copybit_image_t dst;
992 dst.w = ALIGN(renderBuffer->width, 32);
993 dst.h = renderBuffer->height;
994 dst.format = renderBuffer->format;
995 dst.base = (void *)renderBuffer->base;
996 dst.handle = (native_handle_t *)renderBuffer;
997
998 // Copybit dst rect
999 hwc_rect_t displayFrame = layer->displayFrame;
1000 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
1001 displayFrame.right, displayFrame.bottom};
1002
1003 uint32_t color = layer->transform;
1004 copybit_device_t *copybit = mEngine;
1005 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1006 renderBuffer->width);
1007 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1008 renderBuffer->height);
1009 copybit->set_parameter(copybit, COPYBIT_DITHER,
1010 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
1011 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -08001012 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -07001013 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
1014 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
1015 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
1016 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
1017 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
1018 return res;
1019}
1020
Naseer Ahmed5b6708a2012-08-02 13:46:08 -07001021void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -07001022 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001023{
1024 hwc_rect_t displayFrame = layer->displayFrame;
1025
1026 width = displayFrame.right - displayFrame.left;
1027 height = displayFrame.bottom - displayFrame.top;
1028}
1029
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001030bool CopyBit::validateParams(hwc_context_t *ctx,
1031 const hwc_display_contents_1_t *list) {
1032 //Validate parameters
1033 if (!ctx) {
1034 ALOGE("%s:Invalid HWC context", __FUNCTION__);
1035 return false;
1036 } else if (!list) {
1037 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
1038 return false;
1039 }
1040 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001041}
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001042
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001043
Naseer Ahmed64b81212013-02-14 10:29:47 -05001044int CopyBit::allocRenderBuffers(int w, int h, int f)
1045{
1046 int ret = 0;
1047 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1048 if (mRenderBuffer[i] == NULL) {
1049 ret = alloc_buffer(&mRenderBuffer[i],
1050 w, h, f,
1051 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
1052 }
1053 if(ret < 0) {
1054 freeRenderBuffers();
1055 break;
1056 }
1057 }
1058 return ret;
1059}
1060
1061void CopyBit::freeRenderBuffers()
1062{
1063 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1064 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301065 //Since we are freeing buffer close the fence if it has a valid one.
1066 if(mRelFd[i] >= 0) {
1067 close(mRelFd[i]);
1068 mRelFd[i] = -1;
1069 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001070 free_buffer(mRenderBuffer[i]);
1071 mRenderBuffer[i] = NULL;
1072 }
1073 }
1074}
1075
1076private_handle_t * CopyBit::getCurrentRenderBuffer() {
1077 return mRenderBuffer[mCurRenderBufferIndex];
1078}
1079
1080void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301081 if(mRelFd[mCurRenderBufferIndex] >=0)
1082 close(mRelFd[mCurRenderBufferIndex]);
1083 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001084}
1085
Sushil Chauhandefd3522014-05-13 18:17:12 -07001086void CopyBit::setReleaseFdSync(int fd) {
1087 if (mRelFd[mCurRenderBufferIndex] >=0) {
1088 int ret = -1;
1089 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1090 if (ret < 0)
1091 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1092 __FUNCTION__, errno, strerror(errno));
1093 close(mRelFd[mCurRenderBufferIndex]);
1094 }
1095 mRelFd[mCurRenderBufferIndex] = dup(fd);
1096}
1097
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001098struct copybit_device_t* CopyBit::getCopyBitDevice() {
1099 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001100}
1101
Shalaj Jaina70b4352014-06-15 13:47:47 -07001102CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mEngine(0),
1103 mIsModeOn(false), mCopyBitDraw(false), mCurRenderBufferIndex(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001104
1105 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1106 ctx->dpyAttr[dpy].yres,
1107 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001108 mAlignedWidth,
1109 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001110
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001111 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301112 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001113 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301114 mRelFd[i] = -1;
1115 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301116
1117 char value[PROPERTY_VALUE_MAX];
1118 property_get("debug.hwc.dynThreshold", value, "2");
1119 mDynThreshold = atof(value);
1120
Ramakant Singh21cec722014-03-07 19:11:45 +05301121 property_get("debug.sf.swaprect", value, "0");
1122 mSwapRectEnable = atoi(value) ? true:false ;
1123 mDirtyLayerIndex = -1;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001124 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001125 if(copybit_open(module, &mEngine) < 0) {
1126 ALOGE("FATAL ERROR: copybit open failed.");
1127 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001128 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001129 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001130 }
1131}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001132
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001133CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001134{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001135 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001136 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001137 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001138 copybit_close(mEngine);
1139 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001140 }
1141}
Ramakant Singh21cec722014-03-07 19:11:45 +05301142CopyBit::LayerCache::LayerCache() {
1143 reset();
1144}
1145void CopyBit::LayerCache::reset() {
1146 memset(&hnd, 0, sizeof(hnd));
1147 layerCount = 0;
1148}
1149void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1150 hwc_display_contents_1_t *list, int dpy)
1151{
1152 layerCount = ctx->listStats[dpy].numAppLayers;
1153 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1154 hnd[i] = list->hwLayers[i].handle;
1155 }
1156}
1157
1158CopyBit::FbCache::FbCache() {
1159 reset();
1160}
1161void CopyBit::FbCache::reset() {
1162 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
1163 FbIndex =0;
1164}
1165
1166void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect) {
1167 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1168 FbdirtyRect[FbIndex] = dirtyRect;
1169 FbIndex++;
1170}
1171
1172int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect){
1173 int sameDirtyCount = 0;
1174 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
1175 if( FbdirtyRect[i] == dirtyRect)
1176 sameDirtyCount++;
1177 }
1178 return sameDirtyCount;
1179}
1180
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001181}; //namespace qhwc