blob: 24fd77192dbf06d6e646eceefc63b87ba5be1763 [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 }
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +0530276
277 if(ctx->mThermalBurstMode) {
278 ALOGD_IF (DEBUG_COPYBIT, "%s:Copybit failed,"
279 "Running in Thermal Burst mode",__FUNCTION__);
280 return false;
281 }
282
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800283 int compositionType = qdutils::QCCompositionType::
284 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700285
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800286 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
287 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700288 //GPU/CPU composition, don't change layer composition type
289 return true;
290 }
291
Naseer Ahmed45a99602012-07-31 19:15:24 -0700292 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800293 ALOGE("%s:Invalid Params", __FUNCTION__);
294 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700295 }
296
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800297 if(ctx->listStats[dpy].skipCount) {
298 //GPU will be anyways used
299 return false;
300 }
301
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700302 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700303 // Reached max layers supported by HWC.
304 return false;
305 }
306
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800307 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
308 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500309 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500310
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530311 // Following are MDP3 limitations for which we
312 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400313 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500314 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400315 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
316 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500317 int dst_h, dst_w, src_h, src_w;
318 float dx, dy;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400319 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530320 if (layer->planeAlpha != 0xFF)
321 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530322 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500323
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700324 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530325 src_h = sourceCrop.right - sourceCrop.left;
326 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500327 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530328 src_h = sourceCrop.bottom - sourceCrop.top;
329 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500330 }
331 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
332 dst_w = layer->displayFrame.right - layer->displayFrame.left;
333
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530334 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
335 ALOGE("%s: wrong params for display screen_w=%d \
336 src_crop_width=%d screen_h=%d src_crop_height=%d",
337 __FUNCTION__, dst_w,src_w,dst_h,src_h);
338 return false;
339 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530340 dx = (float)dst_w/(float)src_w;
341 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530342
Terence Hampson6b0a4272013-11-06 16:04:51 -0500343 if (dx > MAX_SCALE_FACTOR || dx < MIN_SCALE_FACTOR)
344 return false;
345
346 if (dy > MAX_SCALE_FACTOR || dy < MIN_SCALE_FACTOR)
347 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400348 }
349 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500350
351 //Allocate render buffers if they're not allocated
Terence Hampsonc67b0372013-10-09 11:32:21 -0400352 if (ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
353 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700354 int ret = allocRenderBuffers(mAlignedWidth,
355 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700356 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500357 if (ret < 0) {
358 return false;
359 } else {
360 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
361 NUM_RENDER_BUFFERS;
362 }
363 }
364
Terence Hampsond0fd5792013-05-29 11:56:52 -0400365 // We cannot mix copybit layer with layers marked to be drawn on FB
366 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
367 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800368
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530369 mCopyBitDraw = false;
370 if (useCopybitForRGB &&
371 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
372 mCopyBitDraw = true;
373 // numAppLayers-1, as we iterate till 0th layer index
374 // Mark all layers to be drawn by copybit
375 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500376 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700377#ifdef QCOM_BSP
Terence Hampsonc67b0372013-10-09 11:32:21 -0400378 if (ctx->mMDP.version == qdutils::MDP_V3_0_4)
379 list->hwLayers[i].compositionType = HWC_BLIT;
380 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700381#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400382 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700383 }
384 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530385
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700386 return true;
387}
388
Naseer Ahmed183939d2013-03-14 20:44:17 -0400389int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
390{
391 int ret = 0;
392 copybit_rect_t clear_rect = {rect.left, rect.top,
393 rect.right,
394 rect.bottom};
395
396 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700397 buf.w = ALIGN(getWidth(hnd),32);
398 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400399 buf.format = hnd->format;
400 buf.base = (void *)hnd->base;
401 buf.handle = (native_handle_t *)hnd;
402
403 copybit_device_t *copybit = mEngine;
404 ret = copybit->clear(copybit, &buf, &clear_rect);
405 return ret;
406}
407
Ramakant Singh0def28c2014-03-28 20:43:13 +0530408bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
409 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530410 int dpy, int *copybitFd) {
411 int layerCount = 0;
Shalaj Jaina70b4352014-06-15 13:47:47 -0700412 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530413 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
414 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530415
416 if(ctx->enableABC == false)
417 return false;
418
Ramakant Singh467759f2014-04-16 11:09:40 +0530419 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530420 return false;
421
422 layerCount = ctx->listStats[dpy].numAppLayers;
423 //bottom most layer should
424 //equal to FB
425 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
426 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
427 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
428 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
429 if(tmpLayer->transform ||
430 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
431 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
432 (needsScaling(tmpLayer) == true)) {
433 return false;
434 }else {
435 ctx->listStats[dpy].renderBufIndexforABC = 0;
436 }
437 }
438
Ramakant Singh467759f2014-04-16 11:09:40 +0530439 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
440 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530441 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530442
Ramakant Singhcc326612014-06-19 14:19:18 +0530443 if(layerCount == MAX_LAYERS_FOR_ABC) {
Ramakant Singh467759f2014-04-16 11:09:40 +0530444 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
Ramakant Singhcc326612014-06-19 14:19:18 +0530445 //enable ABC only for non intersecting layers.
446 hwc_rect_t displayFrame =
447 list->hwLayers[abcRenderBufIdx].displayFrame;
448 for (int i = abcRenderBufIdx + 1; i < layerCount; i++) {
449 hwc_rect_t tmpDisplayFrame = list->hwLayers[i].displayFrame;
450 hwc_rect_t result = getIntersection(displayFrame,tmpDisplayFrame);
451 if (isValidRect(result)) {
452 ctx->listStats[dpy].renderBufIndexforABC = -1;
453 return false;
454 }
455 }
456 // Pass the Acquire Fence FD to driver for base layer
Ramakant Singh467759f2014-04-16 11:09:40 +0530457 private_handle_t *renderBuffer =
458 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
459 copybit_device_t *copybit = getCopyBitDevice();
460 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
461 copybit->set_sync(copybit,
462 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
463 }
Ramakant Singhcc326612014-06-19 14:19:18 +0530464 for(int i = abcRenderBufIdx + 1; i < layerCount; i++){
Ramakant Singh467759f2014-04-16 11:09:40 +0530465 int retVal = drawLayerUsingCopybit(ctx,
466 &(list->hwLayers[i]),renderBuffer, 0);
467 if(retVal < 0) {
468 ALOGE("%s : Copybit failed", __FUNCTION__);
469 }
470 }
471 // Get Release Fence FD of copybit for the App layer(s)
472 copybit->flush_get_fence(copybit, copybitFd);
473 close(list->hwLayers[abcRenderBufIdx].acquireFenceFd);
474 list->hwLayers[abcRenderBufIdx].acquireFenceFd = -1;
475 return true;
476 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530477 }
478 return false;
479}
480
481bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
482 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800483 // draw layers marked for COPYBIT
484 int retVal = true;
485 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400486 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500487 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400488 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800489
Ramakant Singh21cec722014-03-07 19:11:45 +0530490 if(mCopyBitDraw == false){
491 mFbCache.reset(); // there is no layer marked for copybit
492 return false ;
493 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530494
Ramakant Singh467759f2014-04-16 11:09:40 +0530495 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530496 return true;
497 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800498 //render buffer
Terence Hampsonc67b0372013-10-09 11:32:21 -0400499 if (ctx->mMDP.version == qdutils::MDP_V3_0_4) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530500 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400501 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
502 } else {
503 renderBuffer = getCurrentRenderBuffer();
504 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800505 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500506 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800507 return false;
508 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500509
Terence Hampson0124cc72013-04-18 23:45:56 -0700510 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400511 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530512 if(mRelFd[mCurRenderBufferIndex] >=0) {
513 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
514 close(mRelFd[mCurRenderBufferIndex]);
515 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400516 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400517 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400518 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400519 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400520 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400521 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700522 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530523
Ramakant Singh21cec722014-03-07 19:11:45 +0530524 mDirtyLayerIndex = checkDirtyRect(ctx, list, dpy);
525 if( mDirtyLayerIndex != -1){
526 hwc_layer_1_t *layer = &list->hwLayers[mDirtyLayerIndex];
Saurabh Shah93c69052014-04-24 10:27:10 -0700527#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530528 clear(renderBuffer,layer->dirtyRect);
Saurabh Shah93c69052014-04-24 10:27:10 -0700529#else
530 clear(renderBuffer,layer->displayFrame);
531#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530532 } else {
533 hwc_rect_t clearRegion = {0,0,0,0};
534 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
535 clear(renderBuffer, clearRegion);
536 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530537
Naseer Ahmed64b81212013-02-14 10:29:47 -0500538 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800539 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500540 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
541 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800542 continue;
543 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530544 //skip non updating layers
545 if((mDirtyLayerIndex != -1) && (mDirtyLayerIndex != i) )
Ramakant Singh762ae082013-11-28 18:45:34 +0530546 continue;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800547 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400548 if (list->hwLayers[i].acquireFenceFd != -1
549 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800550 // Wait for acquire Fence on the App buffers.
551 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
552 if(ret < 0) {
553 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
554 __FUNCTION__, errno, strerror(errno));
555 }
556 close(list->hwLayers[i].acquireFenceFd);
557 list->hwLayers[i].acquireFenceFd = -1;
558 }
559 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530560 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800561 copybitLayerCount++;
562 if(retVal < 0) {
563 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
564 }
565 }
566
567 if (copybitLayerCount) {
568 copybit_device_t *copybit = getCopyBitDevice();
569 // Async mode
570 copybit->flush_get_fence(copybit, fd);
Terence Hampsonc67b0372013-10-09 11:32:21 -0400571 if(ctx->mMDP.version == qdutils::MDP_V3_0_4 &&
572 list->hwLayers[last].acquireFenceFd >= 0) {
573 close(list->hwLayers[last].acquireFenceFd);
574 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400575 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800576 }
577 return true;
578}
579
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700580int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700581 int fd = -1;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700582 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700583
584 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
585 ALOGE("%s: Invalid request", __FUNCTION__);
586 return fd;
587 }
588
589 private_handle_t *renderBuffer = getCurrentRenderBuffer();
590
591 if (!renderBuffer) {
592 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
593 return fd;
594 }
595
596 int copybitLayerCount = 0;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700597 for(int j = 0; j < ptorInfo->count; j++) {
598 int ovlapIndex = ptorInfo->layerIndex[j];
599 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700600
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700601 // Draw overlapped content of layers on render buffer
602 for (int i = 0; i <= ovlapIndex; i++) {
603 hwc_layer_1_t *layer = &list->hwLayers[i];
604 if(!isValidRect(getIntersection(layer->displayFrame,
605 overlap))) {
606 continue;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700607 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700608 if ((list->hwLayers[i].acquireFenceFd != -1)) {
609 // Wait for acquire fence on the App buffers.
610 if(sync_wait(list->hwLayers[i].acquireFenceFd, 1000) < 0) {
611 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
612 __FUNCTION__, errno, strerror(errno));
613 }
614 close(list->hwLayers[i].acquireFenceFd);
615 list->hwLayers[i].acquireFenceFd = -1;
616 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700617
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700618 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer, overlap,
619 ptorInfo->displayFrame[j]);
620 copybitLayerCount++;
621 if(retVal < 0) {
622 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
623 copybitLayerCount = 0;
624 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700625 }
626 }
627
628 if (copybitLayerCount) {
629 copybit_device_t *copybit = getCopyBitDevice();
630 copybit->flush_get_fence(copybit, &fd);
631 }
632
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700633 ALOGD_IF(DEBUG_COPYBIT, "%s: done! copybitLayerCount = %d", __FUNCTION__,
634 copybitLayerCount);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700635 return fd;
636}
637
638int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700639 private_handle_t *renderBuffer, hwc_rect_t overlap,
640 hwc_rect_t destRect)
Sushil Chauhandefd3522014-05-13 18:17:12 -0700641{
642 hwc_context_t* ctx = (hwc_context_t*)(dev);
643 if (!ctx) {
644 ALOGE("%s: null context ", __FUNCTION__);
645 return -1;
646 }
647
648 private_handle_t *hnd = (private_handle_t *)layer->handle;
649 if (!hnd) {
650 ALOGE("%s: invalid handle", __FUNCTION__);
651 return -1;
652 }
653
654 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
655 if (!dstHandle) {
656 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
657 return -1;
658 }
659
660 // Set the Copybit Source
661 copybit_image_t src;
662 src.handle = (native_handle_t *)layer->handle;
663 src.w = hnd->width;
664 src.h = hnd->height;
665 src.base = (void *)hnd->base;
666 src.format = hnd->format;
667 src.horiz_padding = 0;
668 src.vert_padding = 0;
669
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700670
Sushil Chauhandefd3522014-05-13 18:17:12 -0700671 hwc_rect_t dispFrame = layer->displayFrame;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700672 hwc_rect_t iRect = getIntersection(dispFrame, overlap);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700673 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700674 qhwc::calculate_crop_rects(crop, dispFrame, iRect,
675 layer->transform);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700676
677 // Copybit source rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700678 copybit_rect_t srcRect = {crop.left, crop.top, crop.right,
679 crop.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700680
681 // Copybit destination rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700682 copybit_rect_t dstRect = {destRect.left, destRect.top, destRect.right,
683 destRect.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700684
685 // Copybit dst
686 copybit_image_t dst;
687 dst.handle = (native_handle_t *)dstHandle;
688 dst.w = ALIGN(dstHandle->width, 32);
689 dst.h = dstHandle->height;
690 dst.base = (void *)dstHandle->base;
691 dst.format = dstHandle->format;
692
693 copybit_device_t *copybit = mEngine;
694
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700695 // Copybit region is the destRect
696 hwc_rect_t regRect = {dstRect.l,dstRect.t, dstRect.r, dstRect.b};
697 hwc_region_t region;
698 region.numRects = 1;
699 region.rects = &regRect;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700700 region_iterator copybitRegion(region);
701 int acquireFd = layer->acquireFenceFd;
702
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700703 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
704 renderBuffer->width);
705 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
706 renderBuffer->height);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700707 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
708 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
709 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
710 copybit->set_parameter(copybit, COPYBIT_DITHER,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700711 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE :
712 COPYBIT_DISABLE);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700713 copybit->set_sync(copybit, acquireFd);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700714 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
715 &copybitRegion);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700716
717 if (err < 0)
718 ALOGE("%s: copybit stretch failed",__FUNCTION__);
719
720 return err;
721}
722
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700723int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800724 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700725{
726 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400727 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700728 if(!ctx) {
729 ALOGE("%s: null context ", __FUNCTION__);
730 return -1;
731 }
732
733 private_handle_t *hnd = (private_handle_t *)layer->handle;
734 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700735 if (layer->flags & HWC_COLOR_FILL) { // Color layer
736 return fillColorUsingCopybit(layer, renderBuffer);
737 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700738 ALOGE("%s: invalid handle", __FUNCTION__);
739 return -1;
740 }
741
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800742 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700743 if(!fbHandle) {
744 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700745 return -1;
746 }
747
748 // Set the copybit source:
749 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700750 src.w = getWidth(hnd);
751 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700752 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700753
754 // Handle R/B swap
755 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
756 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
757 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
758 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
759 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
760 }
761 }
762
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700763 src.base = (void *)hnd->base;
764 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700765 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700766 // Initialize vertical padding to zero for now,
767 // this needs to change to accomodate vertical stride
768 // if needed in the future
769 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700770
shuoy7c101f92013-08-26 14:48:57 +0800771 int layerTransform = layer->transform ;
772 // When flip and rotation(90) are present alter the flip,
773 // as GPU is doing the flip and rotation in opposite order
774 // to that of MDP3.0
775 // For 270 degrees, we get 90 + (H+V) which is same as doing
776 // flip first and then rotation (H+V) + 90
777 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
778 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
779 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
780 (layer->transform & HAL_TRANSFORM_ROT_90) &&
781 !(layer->transform == HAL_TRANSFORM_ROT_270)){
782 if(layer->transform & HAL_TRANSFORM_FLIP_H){
783 layerTransform ^= HAL_TRANSFORM_FLIP_H;
784 layerTransform |= HAL_TRANSFORM_FLIP_V;
785 }
786 if(layer->transform & HAL_TRANSFORM_FLIP_V){
787 layerTransform ^= HAL_TRANSFORM_FLIP_V;
788 layerTransform |= HAL_TRANSFORM_FLIP_H;
789 }
790 }
791 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700792 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700793 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700794 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
795 sourceCrop.right,
796 sourceCrop.bottom};
797
798 // Copybit destination rect
799 hwc_rect_t displayFrame = layer->displayFrame;
800 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
801 displayFrame.right,
802 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700803#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530804 //change src and dst with dirtyRect
805 if(mDirtyLayerIndex != -1) {
806 srcRect.l = layer->dirtyRect.left;
807 srcRect.t = layer->dirtyRect.top;
808 srcRect.r = layer->dirtyRect.right;
809 srcRect.b = layer->dirtyRect.bottom;
810 dstRect = srcRect;
811 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700812#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700813 // Copybit dst
814 copybit_image_t dst;
815 dst.w = ALIGN(fbHandle->width,32);
816 dst.h = fbHandle->height;
817 dst.format = fbHandle->format;
818 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800819 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700820
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800821 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700822
823 int32_t screen_w = displayFrame.right - displayFrame.left;
824 int32_t screen_h = displayFrame.bottom - displayFrame.top;
825 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
826 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
827
828 // Copybit dst
829 float copybitsMaxScale =
830 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
831 float copybitsMinScale =
832 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
833
Sushil Chauhandffbf432013-12-09 15:33:57 -0800834 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700835 //swap screen width and height
836 int tmp = screen_w;
837 screen_w = screen_h;
838 screen_h = tmp;
839 }
840 private_handle_t *tmpHnd = NULL;
841
842 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
843 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530844 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
845 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700846 return -1;
847 }
848
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530849 float dsdx = (float)screen_w/(float)src_crop_width;
850 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700851
852 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
853 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
854 if(dsdx > scaleLimitMax ||
855 dtdy > scaleLimitMax ||
856 dsdx < 1/scaleLimitMin ||
857 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400858 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700859 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
860 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700861 return -1;
862 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400863 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700864 if(dsdx > copybitsMaxScale ||
865 dtdy > copybitsMaxScale ||
866 dsdx < 1/copybitsMinScale ||
867 dtdy < 1/copybitsMinScale){
868 // The requested scale is out of the range the hardware
869 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400870 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700871 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
872 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
873 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
874 src_crop_width,src_crop_height);
875
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700876
877 int tmp_w = src_crop_width;
878 int tmp_h = src_crop_height;
879
880 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530881 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
882 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700883 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
Ramakant Singh80f36f22014-02-25 14:11:35 +0530884 // ceil the tmp_w and tmp_h value to maintain proper ratio
885 // b/w src and dst (should not cross the desired scale limit
886 // due to float -> int )
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530887 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
888 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700889 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400890 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700891
Naseer Ahmed64b81212013-02-14 10:29:47 -0500892 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400893 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700894
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400895 // We do not want copybit to generate alpha values from nothing
896 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
897 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
898 format = HAL_PIXEL_FORMAT_RGBX_8888;
899 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800900 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700901 copybit_image_t tmp_dst;
902 copybit_rect_t tmp_rect;
903 tmp_dst.w = tmp_w;
904 tmp_dst.h = tmp_h;
905 tmp_dst.format = tmpHnd->format;
906 tmp_dst.handle = tmpHnd;
907 tmp_dst.horiz_padding = src.horiz_padding;
908 tmp_dst.vert_padding = src.vert_padding;
909 tmp_rect.l = 0;
910 tmp_rect.t = 0;
911 tmp_rect.r = tmp_dst.w;
912 tmp_rect.b = tmp_dst.h;
913 //create one clip region
914 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
915 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
916 region_iterator tmp_it(tmp_hwc_reg);
917 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700918 //TODO: once, we are able to read layer alpha, update this
919 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400920 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700921 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
922 &srcRect, &tmp_it);
923 if(err < 0){
924 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
925 __LINE__);
926 if(tmpHnd)
927 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700928 return err;
929 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400930 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400931 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400932 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400933 close(acquireFd);
934 acquireFd = -1;
935 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700936 // copy new src and src rect crop
937 src = tmp_dst;
938 srcRect = tmp_rect;
939 }
940 }
941 // Copybit region
942 hwc_region_t region = layer->visibleRegionScreen;
943 region_iterator copybitRegion(region);
944
945 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
946 renderBuffer->width);
947 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
948 renderBuffer->height);
949 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800950 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700951 //TODO: once, we are able to read layer alpha, update this
952 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800953 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
954 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700955 copybit->set_parameter(copybit, COPYBIT_DITHER,
956 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
957 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530958 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
959 COPYBIT_ENABLE : COPYBIT_DISABLE);
960
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700961 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
962 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400963 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700964 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
965 &copybitRegion);
966 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
967 COPYBIT_DISABLE);
968
Terence Hampsonadf47302013-05-23 12:21:02 -0400969 if(tmpHnd) {
970 if (ctx->mMDP.version < qdutils::MDP_V4_0){
971 int ret = -1, releaseFd;
972 // we need to wait for the buffer before freeing
973 copybit->flush_get_fence(copybit, &releaseFd);
974 ret = sync_wait(releaseFd, 1000);
975 if(ret < 0) {
976 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
977 __FUNCTION__, errno, strerror(errno));
978 }
979 close(releaseFd);
980 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700981 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400982 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700983
984 if(err < 0)
985 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700986 return err;
987}
988
Sushil Chauhan943797c2013-10-21 17:35:55 -0700989int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
990 private_handle_t *renderBuffer)
991{
992 if (!renderBuffer) {
993 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
994 return -1;
995 }
996
997 // Copybit dst
998 copybit_image_t dst;
999 dst.w = ALIGN(renderBuffer->width, 32);
1000 dst.h = renderBuffer->height;
1001 dst.format = renderBuffer->format;
1002 dst.base = (void *)renderBuffer->base;
1003 dst.handle = (native_handle_t *)renderBuffer;
1004
1005 // Copybit dst rect
1006 hwc_rect_t displayFrame = layer->displayFrame;
1007 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
1008 displayFrame.right, displayFrame.bottom};
1009
1010 uint32_t color = layer->transform;
1011 copybit_device_t *copybit = mEngine;
1012 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1013 renderBuffer->width);
1014 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1015 renderBuffer->height);
1016 copybit->set_parameter(copybit, COPYBIT_DITHER,
1017 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
1018 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -08001019 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -07001020 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
1021 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
1022 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
1023 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
1024 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
1025 return res;
1026}
1027
Naseer Ahmed5b6708a2012-08-02 13:46:08 -07001028void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -07001029 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001030{
1031 hwc_rect_t displayFrame = layer->displayFrame;
1032
1033 width = displayFrame.right - displayFrame.left;
1034 height = displayFrame.bottom - displayFrame.top;
1035}
1036
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001037bool CopyBit::validateParams(hwc_context_t *ctx,
1038 const hwc_display_contents_1_t *list) {
1039 //Validate parameters
1040 if (!ctx) {
1041 ALOGE("%s:Invalid HWC context", __FUNCTION__);
1042 return false;
1043 } else if (!list) {
1044 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
1045 return false;
1046 }
1047 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001048}
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001049
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001050
Naseer Ahmed64b81212013-02-14 10:29:47 -05001051int CopyBit::allocRenderBuffers(int w, int h, int f)
1052{
1053 int ret = 0;
1054 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1055 if (mRenderBuffer[i] == NULL) {
1056 ret = alloc_buffer(&mRenderBuffer[i],
1057 w, h, f,
1058 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
1059 }
1060 if(ret < 0) {
1061 freeRenderBuffers();
1062 break;
1063 }
1064 }
1065 return ret;
1066}
1067
1068void CopyBit::freeRenderBuffers()
1069{
1070 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1071 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301072 //Since we are freeing buffer close the fence if it has a valid one.
1073 if(mRelFd[i] >= 0) {
1074 close(mRelFd[i]);
1075 mRelFd[i] = -1;
1076 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001077 free_buffer(mRenderBuffer[i]);
1078 mRenderBuffer[i] = NULL;
1079 }
1080 }
1081}
1082
1083private_handle_t * CopyBit::getCurrentRenderBuffer() {
1084 return mRenderBuffer[mCurRenderBufferIndex];
1085}
1086
1087void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301088 if(mRelFd[mCurRenderBufferIndex] >=0)
1089 close(mRelFd[mCurRenderBufferIndex]);
1090 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001091}
1092
Sushil Chauhandefd3522014-05-13 18:17:12 -07001093void CopyBit::setReleaseFdSync(int fd) {
1094 if (mRelFd[mCurRenderBufferIndex] >=0) {
1095 int ret = -1;
1096 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1097 if (ret < 0)
1098 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1099 __FUNCTION__, errno, strerror(errno));
1100 close(mRelFd[mCurRenderBufferIndex]);
1101 }
1102 mRelFd[mCurRenderBufferIndex] = dup(fd);
1103}
1104
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001105struct copybit_device_t* CopyBit::getCopyBitDevice() {
1106 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001107}
1108
Shalaj Jaina70b4352014-06-15 13:47:47 -07001109CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mEngine(0),
1110 mIsModeOn(false), mCopyBitDraw(false), mCurRenderBufferIndex(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001111
1112 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1113 ctx->dpyAttr[dpy].yres,
1114 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001115 mAlignedWidth,
1116 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001117
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001118 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301119 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001120 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301121 mRelFd[i] = -1;
1122 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301123
1124 char value[PROPERTY_VALUE_MAX];
1125 property_get("debug.hwc.dynThreshold", value, "2");
1126 mDynThreshold = atof(value);
1127
Ramakant Singh21cec722014-03-07 19:11:45 +05301128 property_get("debug.sf.swaprect", value, "0");
1129 mSwapRectEnable = atoi(value) ? true:false ;
1130 mDirtyLayerIndex = -1;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001131 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001132 if(copybit_open(module, &mEngine) < 0) {
1133 ALOGE("FATAL ERROR: copybit open failed.");
1134 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001135 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001136 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001137 }
1138}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001139
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001140CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001141{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001142 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001143 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001144 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001145 copybit_close(mEngine);
1146 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001147 }
1148}
Ramakant Singh21cec722014-03-07 19:11:45 +05301149CopyBit::LayerCache::LayerCache() {
1150 reset();
1151}
1152void CopyBit::LayerCache::reset() {
1153 memset(&hnd, 0, sizeof(hnd));
1154 layerCount = 0;
1155}
1156void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1157 hwc_display_contents_1_t *list, int dpy)
1158{
1159 layerCount = ctx->listStats[dpy].numAppLayers;
1160 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1161 hnd[i] = list->hwLayers[i].handle;
1162 }
1163}
1164
1165CopyBit::FbCache::FbCache() {
1166 reset();
1167}
1168void CopyBit::FbCache::reset() {
1169 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
1170 FbIndex =0;
1171}
1172
1173void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect) {
1174 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1175 FbdirtyRect[FbIndex] = dirtyRect;
1176 FbIndex++;
1177}
1178
1179int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect){
1180 int sameDirtyCount = 0;
1181 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
1182 if( FbdirtyRect[i] == dirtyRect)
1183 sameDirtyCount++;
1184 }
1185 return sameDirtyCount;
1186}
1187
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001188}; //namespace qhwc