blob: e7c2928b8d435238c038f2197fee0675274626a6 [file] [log] [blame]
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Ramakant Singhd2875e12015-01-19 12:45:41 +05303 * Copyright (C) 2012-2015, 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
Dileep Kumar Reddiad376f12015-05-08 10:00:04 +053085bool CopyBit::isSmartBlitPossible(const hwc_display_contents_1_t *list){
86 if(list->numHwLayers > 2) {
87 hwc_rect_t displayFrame0 = {0, 0, 0, 0};
88 hwc_rect_t displayFrame1 = {0, 0, 0, 0};
89 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
90 hwc_rect_t displayFrame = getIntersection(mDirtyRect,
91 list->hwLayers[i].displayFrame);
92 if (isValidRect(displayFrame) && !isValidRect(displayFrame0)) {
93 displayFrame0 = displayFrame;
94 } else if(isValidRect(displayFrame)) {
95 displayFrame1 = displayFrame;
96 break;
97 }
98 }
99 if((displayFrame0 == displayFrame1) &&
100 not (list->flags & (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR))) {
101 ALOGD_IF (DEBUG_COPYBIT, "%s:Smart Bilt Possible",__FUNCTION__);
102 return true;
103 }
104 }
105 return false;
106}
107
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800108bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
109 hwc_display_contents_1_t *list,
110 int dpy) {
111 int compositionType = qdutils::QCCompositionType::
112 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700113
Naseer Ahmed45a99602012-07-31 19:15:24 -0700114 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
115 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530116 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -0700117 // this is done based on perf inputs in ICS
118 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800119 int fbWidth = ctx->dpyAttr[dpy].xres;
120 int fbHeight = ctx->dpyAttr[dpy].yres;
121 unsigned int fbArea = (fbWidth * fbHeight);
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530122 unsigned int renderArea = getRGBRenderingArea(ctx, list);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700123 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
124 __FUNCTION__, renderArea, fbArea);
Dileep Kumar Reddiad376f12015-05-08 10:00:04 +0530125 double dynThreshold = mDynThreshold;
126 if(not isSmartBlitPossible(list))
127 dynThreshold -= 1;
128
129 if (renderArea < (dynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700130 return true;
131 }
132 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
133 // MDP composition, use COPYBIT always
134 return true;
135 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
136 // C2D composition, use COPYBIT
137 return true;
138 }
139 return false;
140}
141
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530142unsigned int CopyBit::getRGBRenderingArea (const hwc_context_t *ctx,
143 const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700144 //Calculates total rendering area for RGB layers
145 unsigned int renderArea = 0;
146 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400147 // Skipping last layer since FrameBuffer layer should not affect
148 // which composition to choose
149 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700150 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
151 if (hnd) {
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530152 if (BUFFER_TYPE_UI == hnd->bufferType && !ctx->copybitDrop[i]) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700153 getLayerResolution(&list->hwLayers[i], w, h);
154 renderArea += (w*h);
155 }
156 }
157 }
158 return renderArea;
159}
160
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +0530161bool CopyBit::isLayerChanging(hwc_context_t *ctx,
162 hwc_display_contents_1_t *list, int k) {
Dileep Kumar Reddiaa488882014-12-16 11:19:45 +0530163 if((mLayerCache.hnd[k] != list->hwLayers[k].handle) ||
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +0530164 (mLayerCache.drop[k] != ctx->copybitDrop[k]) ||
Dileep Kumar Reddiaa488882014-12-16 11:19:45 +0530165 (mLayerCache.displayFrame[k].left !=
166 list->hwLayers[k].displayFrame.left) ||
167 (mLayerCache.displayFrame[k].top !=
168 list->hwLayers[k].displayFrame.top) ||
169 (mLayerCache.displayFrame[k].right !=
170 list->hwLayers[k].displayFrame.right) ||
171 (mLayerCache.displayFrame[k].bottom !=
172 list->hwLayers[k].displayFrame.bottom)) {
173 return 1;
174 }
175 return 0;
176}
177
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530178bool CopyBit::prepareSwapRect(hwc_context_t *ctx,
179 hwc_display_contents_1_t *list,
180 int dpy) {
181 bool canUseSwapRect = 0;
182 hwc_rect_t dirtyRect = {0, 0, 0, 0};
183 hwc_rect_t displayRect = {0, 0, 0, 0};
184 if((mLayerCache.layerCount != ctx->listStats[dpy].numAppLayers) ||
185 not mSwapRectEnable) {
Ramakant Singh21cec722014-03-07 19:11:45 +0530186 mLayerCache.reset();
187 mFbCache.reset();
188 mLayerCache.updateCounts(ctx,list,dpy);
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530189 mDirtyRect = displayRect;
190 return 0;
Ramakant Singh21cec722014-03-07 19:11:45 +0530191 }
192
193 int updatingLayerCount = 0;
194 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--){
195 //swap rect will kick in only for single updating layer
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +0530196 if(isLayerChanging(ctx, list, k)) {
Ramakant Singh21cec722014-03-07 19:11:45 +0530197 updatingLayerCount ++;
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530198 hwc_layer_1_t layer = list->hwLayers[k];
199 canUseSwapRect = 1;
200 dirtyRect = getUnion(dirtyRect, layer.dirtyRect);
201 displayRect = getUnion(displayRect, layer.displayFrame);
Ramakant Singh21cec722014-03-07 19:11:45 +0530202 }
203 }
204 //since we are using more than one framebuffers,we have to
205 //kick in swap rect only if we are getting continuous same
206 //dirty rect for same layer at least equal of number of
207 //framebuffers
208
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530209 if (canUseSwapRect || updatingLayerCount == 0) {
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530210 if (updatingLayerCount == 0) {
211 dirtyRect.left = INVALID_DIMENSION;
212 dirtyRect.top = INVALID_DIMENSION;
213 dirtyRect.right = INVALID_DIMENSION;
214 dirtyRect.bottom = INVALID_DIMENSION;
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530215 canUseSwapRect = 1;
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530216 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530217
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530218 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--) {
219 //disable swap rect in case of scaling and video .
220 private_handle_t *hnd =(private_handle_t *)list->hwLayers[k].handle;
Dileep Kumar Reddic3545042015-03-11 19:37:06 +0530221 if(needsScaling(&list->hwLayers[k])||( hnd && isYuvBuffer(hnd)) ||
222 (list->hwLayers[k].transform & HAL_TRANSFORM_ROT_90)) {
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530223 mFbCache.reset();
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530224 displayRect.bottom = 0;
225 displayRect.top = 0;
226 displayRect.right = 0;
227 displayRect.bottom = 0;
228 mDirtyRect = displayRect;
229 return 0;
Ramakant Singh21cec722014-03-07 19:11:45 +0530230 }
231 }
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530232
Ramakant Singh19ead272015-03-18 14:11:31 +0530233 if(mFbCache.getUnchangedFbDRCount(dirtyRect, displayRect) <
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530234 NUM_RENDER_BUFFERS) {
Ramakant Singh19ead272015-03-18 14:11:31 +0530235 mFbCache.insertAndUpdateFbCache(dirtyRect, displayRect);
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530236 canUseSwapRect = 0;
237 displayRect.bottom = 0;
238 displayRect.top = 0;
239 displayRect.right = 0;
240 displayRect.bottom = 0;
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530241 }
242 } else {
Ramakant Singh21cec722014-03-07 19:11:45 +0530243 mFbCache.reset();
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530244 canUseSwapRect = 0;
245 displayRect.bottom = 0;
246 displayRect.top = 0;
247 displayRect.right = 0;
248 displayRect.bottom = 0;
249
Ramakant Singh21cec722014-03-07 19:11:45 +0530250 }
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530251 mDirtyRect = displayRect;
Ramakant Singh21cec722014-03-07 19:11:45 +0530252 mLayerCache.updateCounts(ctx,list,dpy);
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530253 return canUseSwapRect;
Ramakant Singh21cec722014-03-07 19:11:45 +0530254}
255
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700256bool CopyBit::prepareOverlap(hwc_context_t *ctx,
257 hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700258
259 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
260 ALOGE("%s: Invalid request", __FUNCTION__);
261 return false;
262 }
263
Saurabh Shah15455aa2015-01-28 13:54:48 -0800264 if (mEngine == NULL) {
265 ALOGW("%s: Copybit HAL not enabled", __FUNCTION__);
266 return false;
267 }
268
269 if (!(validateParams(ctx, list))) {
270 ALOGE("%s: validateParams() failed", __FUNCTION__);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700271 return false;
272 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700273 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700274
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700275 // Allocate render buffers if they're not allocated
276 int alignW = 0, alignH = 0;
277 int finalW = 0, finalH = 0;
278 for (int i = 0; i < ptorInfo->count; i++) {
279 int ovlapIndex = ptorInfo->layerIndex[i];
280 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
281 // render buffer width will be the max of two layers
282 // Align Widht and height to 32, Mdp would be configured
283 // with Aligned overlap w/h
284 finalW = max(finalW, ALIGN((overlap.right - overlap.left), 32));
285 finalH += ALIGN((overlap.bottom - overlap.top), 32);
286 if(finalH > ALIGN((overlap.bottom - overlap.top), 32)) {
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700287 // Calculate the dest top, left will always be zero
288 ptorInfo->displayFrame[i].top = (finalH -
289 (ALIGN((overlap.bottom - overlap.top), 32)));
290 }
291 // calculate the right and bottom values
292 ptorInfo->displayFrame[i].right = ptorInfo->displayFrame[i].left +
293 (overlap.right - overlap.left);
294 ptorInfo->displayFrame[i].bottom = ptorInfo->displayFrame[i].top +
295 (overlap.bottom - overlap.top);
296 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700297
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700298 getBufferSizeAndDimensions(finalW, finalH, HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -0700299 alignW, alignH);
300
301 if ((mAlignedWidth != alignW) || (mAlignedHeight != alignH)) {
302 // Overlap rect has changed, so free render buffers
303 freeRenderBuffers();
304 }
305
306 int ret = allocRenderBuffers(alignW, alignH, HAL_PIXEL_FORMAT_RGBA_8888);
307
308 if (ret < 0) {
309 ALOGE("%s: Render buffer allocation failed", __FUNCTION__);
310 return false;
311 }
312
313 mAlignedWidth = alignW;
314 mAlignedHeight = alignH;
315 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
316 return true;
317}
318
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800319bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
320 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700321
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800322 if(mEngine == NULL) {
323 // No copybit device found - cannot use copybit
324 return false;
325 }
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +0530326
327 if(ctx->mThermalBurstMode) {
328 ALOGD_IF (DEBUG_COPYBIT, "%s:Copybit failed,"
329 "Running in Thermal Burst mode",__FUNCTION__);
330 return false;
331 }
332
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800333 int compositionType = qdutils::QCCompositionType::
334 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700335
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800336 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
337 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700338 //GPU/CPU composition, don't change layer composition type
339 return true;
340 }
341
Naseer Ahmed45a99602012-07-31 19:15:24 -0700342 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800343 ALOGE("%s:Invalid Params", __FUNCTION__);
344 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700345 }
346
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800347 if(ctx->listStats[dpy].skipCount) {
348 //GPU will be anyways used
349 return false;
350 }
351
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700352 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700353 // Reached max layers supported by HWC.
354 return false;
355 }
356
Dileep Kumar Reddiad376f12015-05-08 10:00:04 +0530357 int last = (uint32_t)list->numHwLayers - 1;
358 mDirtyRect = list->hwLayers[last].displayFrame;
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530359 mSwapRect = prepareSwapRect(ctx, list, dpy);
360 ALOGD_IF (DEBUG_COPYBIT, "%s: mSwapRect: %d mDirtyRect: [%d, %d, %d, %d]",
361 __FUNCTION__, mSwapRect, mDirtyRect.left,
362 mDirtyRect.top, mDirtyRect.right, mDirtyRect.bottom);
Dileep Kumar Reddiad376f12015-05-08 10:00:04 +0530363
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800364 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
365 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500366 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500367
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530368 // Following are MDP3 limitations for which we
369 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400370 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500371 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400372 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
373 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500374 int dst_h, dst_w, src_h, src_w;
375 float dx, dy;
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530376 if(ctx->copybitDrop[i]) {
377 continue;
378 }
Terence Hampsonc235bda2013-07-12 12:47:38 -0400379 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530380 if (layer->planeAlpha != 0xFF)
381 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530382 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500383
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700384 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530385 src_h = sourceCrop.right - sourceCrop.left;
386 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500387 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530388 src_h = sourceCrop.bottom - sourceCrop.top;
389 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500390 }
391 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
392 dst_w = layer->displayFrame.right - layer->displayFrame.left;
393
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530394 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
395 ALOGE("%s: wrong params for display screen_w=%d \
396 src_crop_width=%d screen_h=%d src_crop_height=%d",
397 __FUNCTION__, dst_w,src_w,dst_h,src_h);
398 return false;
399 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530400 dx = (float)dst_w/(float)src_w;
401 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530402
Ramakant Singhd5b369f2014-12-09 23:03:43 +0530403 float scale_factor_max = MAX_SCALE_FACTOR;
404 float scale_factor_min = MIN_SCALE_FACTOR;
405
406 if (isAlphaPresent(layer)) {
407 scale_factor_max = MAX_SCALE_FACTOR/4;
408 scale_factor_min = MIN_SCALE_FACTOR*4;
409 }
410
411 if (dx > scale_factor_max || dx < scale_factor_min)
Terence Hampson6b0a4272013-11-06 16:04:51 -0500412 return false;
413
Ramakant Singhd5b369f2014-12-09 23:03:43 +0530414 if (dy > scale_factor_max || dy < scale_factor_min)
Terence Hampson6b0a4272013-11-06 16:04:51 -0500415 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400416 }
417 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500418
419 //Allocate render buffers if they're not allocated
Ramakant Singhb4106a12014-10-07 18:06:56 +0530420 if ((ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
421 ctx->mMDP.version != qdutils::MDP_V3_0_5) &&
Terence Hampsonc67b0372013-10-09 11:32:21 -0400422 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700423 int ret = allocRenderBuffers(mAlignedWidth,
424 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700425 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500426 if (ret < 0) {
427 return false;
428 } else {
429 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
430 NUM_RENDER_BUFFERS;
431 }
432 }
433
Terence Hampsond0fd5792013-05-29 11:56:52 -0400434 // We cannot mix copybit layer with layers marked to be drawn on FB
435 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
436 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800437
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530438 mCopyBitDraw = false;
439 if (useCopybitForRGB &&
440 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
441 mCopyBitDraw = true;
442 // numAppLayers-1, as we iterate till 0th layer index
443 // Mark all layers to be drawn by copybit
444 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500445 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700446#ifdef QCOM_BSP
Ramakant Singhb4106a12014-10-07 18:06:56 +0530447 if (ctx->mMDP.version == qdutils::MDP_V3_0_4 ||
448 ctx->mMDP.version == qdutils::MDP_V3_0_5)
Terence Hampsonc67b0372013-10-09 11:32:21 -0400449 list->hwLayers[i].compositionType = HWC_BLIT;
450 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700451#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400452 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700453 }
454 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530455
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700456 return true;
457}
458
Naseer Ahmed183939d2013-03-14 20:44:17 -0400459int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
460{
461 int ret = 0;
462 copybit_rect_t clear_rect = {rect.left, rect.top,
463 rect.right,
464 rect.bottom};
465
466 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700467 buf.w = ALIGN(getWidth(hnd),32);
468 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400469 buf.format = hnd->format;
470 buf.base = (void *)hnd->base;
471 buf.handle = (native_handle_t *)hnd;
472
473 copybit_device_t *copybit = mEngine;
474 ret = copybit->clear(copybit, &buf, &clear_rect);
475 return ret;
476}
477
Ramakant Singh0def28c2014-03-28 20:43:13 +0530478bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
479 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530480 int dpy, int *copybitFd) {
481 int layerCount = 0;
Shalaj Jaina70b4352014-06-15 13:47:47 -0700482 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530483 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
484 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530485
486 if(ctx->enableABC == false)
487 return false;
488
Ramakant Singh467759f2014-04-16 11:09:40 +0530489 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530490 return false;
491
492 layerCount = ctx->listStats[dpy].numAppLayers;
493 //bottom most layer should
494 //equal to FB
495 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
496 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
497 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
498 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
499 if(tmpLayer->transform ||
Dileep Kumar Reddi2f8a13e2015-03-26 18:01:23 +0530500 (list->flags & HWC_GEOMETRY_CHANGED) ||
Ramakant Singh0def28c2014-03-28 20:43:13 +0530501 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
502 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
503 (needsScaling(tmpLayer) == true)) {
504 return false;
505 }else {
506 ctx->listStats[dpy].renderBufIndexforABC = 0;
507 }
508 }
509
Ramakant Singh467759f2014-04-16 11:09:40 +0530510 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
511 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530512 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530513
Ramakant Singhcc326612014-06-19 14:19:18 +0530514 if(layerCount == MAX_LAYERS_FOR_ABC) {
Ramakant Singh467759f2014-04-16 11:09:40 +0530515 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
Ramakant Singhcc326612014-06-19 14:19:18 +0530516 //enable ABC only for non intersecting layers.
517 hwc_rect_t displayFrame =
518 list->hwLayers[abcRenderBufIdx].displayFrame;
519 for (int i = abcRenderBufIdx + 1; i < layerCount; i++) {
520 hwc_rect_t tmpDisplayFrame = list->hwLayers[i].displayFrame;
521 hwc_rect_t result = getIntersection(displayFrame,tmpDisplayFrame);
522 if (isValidRect(result)) {
523 ctx->listStats[dpy].renderBufIndexforABC = -1;
524 return false;
525 }
526 }
527 // Pass the Acquire Fence FD to driver for base layer
Ramakant Singh467759f2014-04-16 11:09:40 +0530528 private_handle_t *renderBuffer =
529 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
530 copybit_device_t *copybit = getCopyBitDevice();
531 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
532 copybit->set_sync(copybit,
533 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
534 }
Ramakant Singhcc326612014-06-19 14:19:18 +0530535 for(int i = abcRenderBufIdx + 1; i < layerCount; i++){
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530536 mSwapRect = 0;
Ramakant Singh467759f2014-04-16 11:09:40 +0530537 int retVal = drawLayerUsingCopybit(ctx,
538 &(list->hwLayers[i]),renderBuffer, 0);
539 if(retVal < 0) {
540 ALOGE("%s : Copybit failed", __FUNCTION__);
541 }
542 }
543 // Get Release Fence FD of copybit for the App layer(s)
544 copybit->flush_get_fence(copybit, copybitFd);
Ramakant Singh52e1c0c2014-12-18 23:35:33 +0530545 close(list->hwLayers[last].acquireFenceFd);
546 list->hwLayers[last].acquireFenceFd = -1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530547 return true;
548 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530549 }
550 return false;
551}
552
553bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
554 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800555 // draw layers marked for COPYBIT
556 int retVal = true;
557 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400558 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500559 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400560 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800561
Ramakant Singh21cec722014-03-07 19:11:45 +0530562 if(mCopyBitDraw == false){
563 mFbCache.reset(); // there is no layer marked for copybit
564 return false ;
565 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530566
Ramakant Singh467759f2014-04-16 11:09:40 +0530567 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530568 return true;
569 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800570 //render buffer
Ramakant Singhb4106a12014-10-07 18:06:56 +0530571 if (ctx->mMDP.version == qdutils::MDP_V3_0_4 ||
572 ctx->mMDP.version == qdutils::MDP_V3_0_5) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530573 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400574 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
575 } else {
576 renderBuffer = getCurrentRenderBuffer();
577 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800578 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500579 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800580 return false;
581 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500582
Terence Hampson0124cc72013-04-18 23:45:56 -0700583 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400584 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530585 if(mRelFd[mCurRenderBufferIndex] >=0) {
586 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
587 close(mRelFd[mCurRenderBufferIndex]);
588 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400589 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400590 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400591 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400592 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400593 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400594 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700595 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530596
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530597 if (not CBUtils::uiClearRegion(list, ctx->mMDP.version, layerProp,
598 mDirtyRect, mEngine, renderBuffer)){
599 mSwapRect = 0;
Ramakant Singh909bc2a2015-02-10 16:29:21 +0530600 }
Dileep Kumar Reddi2d0c0ef2015-05-07 17:24:23 +0530601
Naseer Ahmed64b81212013-02-14 10:29:47 -0500602 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800603 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500604 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
605 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800606 continue;
607 }
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530608 if(ctx->copybitDrop[i]) {
609 continue;
610 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800611 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400612 if (list->hwLayers[i].acquireFenceFd != -1
613 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800614 // Wait for acquire Fence on the App buffers.
615 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
616 if(ret < 0) {
617 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
618 __FUNCTION__, errno, strerror(errno));
619 }
620 close(list->hwLayers[i].acquireFenceFd);
621 list->hwLayers[i].acquireFenceFd = -1;
622 }
623 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530624 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800625 copybitLayerCount++;
626 if(retVal < 0) {
627 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
628 }
629 }
630
631 if (copybitLayerCount) {
632 copybit_device_t *copybit = getCopyBitDevice();
633 // Async mode
634 copybit->flush_get_fence(copybit, fd);
Ramakant Singhb4106a12014-10-07 18:06:56 +0530635 if((ctx->mMDP.version == qdutils::MDP_V3_0_4 ||
636 ctx->mMDP.version == qdutils::MDP_V3_0_5) &&
Terence Hampsonc67b0372013-10-09 11:32:21 -0400637 list->hwLayers[last].acquireFenceFd >= 0) {
638 close(list->hwLayers[last].acquireFenceFd);
639 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400640 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800641 }
642 return true;
643}
644
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700645int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700646 int fd = -1;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700647 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700648
649 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
650 ALOGE("%s: Invalid request", __FUNCTION__);
651 return fd;
652 }
653
654 private_handle_t *renderBuffer = getCurrentRenderBuffer();
655
656 if (!renderBuffer) {
657 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
658 return fd;
659 }
660
Dileep Kumar Reddid1f08e42014-11-05 21:24:27 +0530661 //Clear the transparent or left out region on the render buffer
Dileep Kumar Reddid1f08e42014-11-05 21:24:27 +0530662 LayerProp *layerProp = ctx->layerProp[0];
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530663 hwc_rect_t clearRegion = {0, 0, 0, 0};
664 CBUtils::uiClearRegion(list, ctx->mMDP.version, layerProp, clearRegion,
Dileep Kumar Reddi2d0c0ef2015-05-07 17:24:23 +0530665 mEngine, renderBuffer);
Dileep Kumar Reddid1f08e42014-11-05 21:24:27 +0530666
Sushil Chauhandefd3522014-05-13 18:17:12 -0700667 int copybitLayerCount = 0;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700668 for(int j = 0; j < ptorInfo->count; j++) {
669 int ovlapIndex = ptorInfo->layerIndex[j];
670 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
Prabhanjan Kandula9889a202014-09-04 21:50:35 +0530671 if(j) {
672 /**
673 * It's possible that 2 PTOR layers might have overlapping.
674 * In such case, remove the intersection(again if peripheral)
675 * from the lower PTOR layer to avoid overlapping.
676 * If intersection is not on peripheral then compromise
677 * by reducing number of PTOR layers.
678 **/
679 int prevOvlapIndex = ptorInfo->layerIndex[0];
680 hwc_rect_t prevOvlap = list->hwLayers[prevOvlapIndex].displayFrame;
681 hwc_rect_t commonRect = getIntersection(prevOvlap, overlap);
682 if(isValidRect(commonRect)) {
683 overlap = deductRect(overlap, commonRect);
684 }
685 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700686
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700687 // Draw overlapped content of layers on render buffer
688 for (int i = 0; i <= ovlapIndex; i++) {
689 hwc_layer_1_t *layer = &list->hwLayers[i];
690 if(!isValidRect(getIntersection(layer->displayFrame,
691 overlap))) {
692 continue;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700693 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700694 if ((list->hwLayers[i].acquireFenceFd != -1)) {
695 // Wait for acquire fence on the App buffers.
696 if(sync_wait(list->hwLayers[i].acquireFenceFd, 1000) < 0) {
697 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
698 __FUNCTION__, errno, strerror(errno));
699 }
700 close(list->hwLayers[i].acquireFenceFd);
701 list->hwLayers[i].acquireFenceFd = -1;
702 }
Dileep Kumar Reddi408fde52014-11-04 22:53:15 +0530703 /*
704 * Find the intersection of layer display frame with PTOR layer
705 * with respect to screen co-ordinates
706 *
707 * Calculated the destination rect by transforming the overlapping
708 * region of layer display frame with respect to PTOR display frame
709 *
710 * Transform the destination rect on to render buffer
711 * */
712 hwc_rect_t destRect = getIntersection(overlap, layer->displayFrame);
713 destRect.left = destRect.left - overlap.left +
714 ptorInfo->displayFrame[j].left;
715 destRect.right = destRect.right- overlap.left +
716 ptorInfo->displayFrame[j].left;
717 destRect.top = destRect.top - overlap.top +
718 ptorInfo->displayFrame[j].top;
719 destRect.bottom = destRect.bottom - overlap.top +
720 ptorInfo->displayFrame[j].top;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700721
Dileep Kumar Reddi408fde52014-11-04 22:53:15 +0530722 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer,
723 overlap, destRect);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700724 copybitLayerCount++;
725 if(retVal < 0) {
726 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
727 copybitLayerCount = 0;
728 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700729 }
730 }
731
732 if (copybitLayerCount) {
733 copybit_device_t *copybit = getCopyBitDevice();
734 copybit->flush_get_fence(copybit, &fd);
735 }
736
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700737 ALOGD_IF(DEBUG_COPYBIT, "%s: done! copybitLayerCount = %d", __FUNCTION__,
738 copybitLayerCount);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700739 return fd;
740}
741
742int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700743 private_handle_t *renderBuffer, hwc_rect_t overlap,
744 hwc_rect_t destRect)
Sushil Chauhandefd3522014-05-13 18:17:12 -0700745{
746 hwc_context_t* ctx = (hwc_context_t*)(dev);
747 if (!ctx) {
748 ALOGE("%s: null context ", __FUNCTION__);
749 return -1;
750 }
751
752 private_handle_t *hnd = (private_handle_t *)layer->handle;
753 if (!hnd) {
754 ALOGE("%s: invalid handle", __FUNCTION__);
755 return -1;
756 }
757
758 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
759 if (!dstHandle) {
760 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
761 return -1;
762 }
763
764 // Set the Copybit Source
765 copybit_image_t src;
766 src.handle = (native_handle_t *)layer->handle;
767 src.w = hnd->width;
768 src.h = hnd->height;
769 src.base = (void *)hnd->base;
770 src.format = hnd->format;
771 src.horiz_padding = 0;
772 src.vert_padding = 0;
773
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700774
Sushil Chauhandefd3522014-05-13 18:17:12 -0700775 hwc_rect_t dispFrame = layer->displayFrame;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700776 hwc_rect_t iRect = getIntersection(dispFrame, overlap);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700777 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700778 qhwc::calculate_crop_rects(crop, dispFrame, iRect,
779 layer->transform);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700780
781 // Copybit source rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700782 copybit_rect_t srcRect = {crop.left, crop.top, crop.right,
783 crop.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700784
785 // Copybit destination rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700786 copybit_rect_t dstRect = {destRect.left, destRect.top, destRect.right,
787 destRect.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700788
789 // Copybit dst
790 copybit_image_t dst;
791 dst.handle = (native_handle_t *)dstHandle;
792 dst.w = ALIGN(dstHandle->width, 32);
793 dst.h = dstHandle->height;
794 dst.base = (void *)dstHandle->base;
795 dst.format = dstHandle->format;
796
797 copybit_device_t *copybit = mEngine;
798
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700799 // Copybit region is the destRect
800 hwc_rect_t regRect = {dstRect.l,dstRect.t, dstRect.r, dstRect.b};
801 hwc_region_t region;
802 region.numRects = 1;
803 region.rects = &regRect;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700804 region_iterator copybitRegion(region);
805 int acquireFd = layer->acquireFenceFd;
806
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700807 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
808 renderBuffer->width);
809 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
810 renderBuffer->height);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700811 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
812 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
813 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
814 copybit->set_parameter(copybit, COPYBIT_DITHER,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700815 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE :
816 COPYBIT_DISABLE);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700817 copybit->set_sync(copybit, acquireFd);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700818 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
819 &copybitRegion);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700820
821 if (err < 0)
822 ALOGE("%s: copybit stretch failed",__FUNCTION__);
823
824 return err;
825}
826
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700827int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800828 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700829{
830 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400831 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700832 if(!ctx) {
833 ALOGE("%s: null context ", __FUNCTION__);
834 return -1;
835 }
836
837 private_handle_t *hnd = (private_handle_t *)layer->handle;
838 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700839 if (layer->flags & HWC_COLOR_FILL) { // Color layer
840 return fillColorUsingCopybit(layer, renderBuffer);
841 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700842 ALOGE("%s: invalid handle", __FUNCTION__);
843 return -1;
844 }
845
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800846 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700847 if(!fbHandle) {
848 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700849 return -1;
850 }
Ramakant Singh140965d2015-01-08 23:45:04 +0530851 uint32_t dynamic_fps = 0;
852#ifdef DYNAMIC_FPS
853 MetaData_t *mdata = hnd ? (MetaData_t *)hnd->base_metadata : NULL;
854 if (mdata && (mdata->operation & UPDATE_REFRESH_RATE)) {
855 dynamic_fps = roundOff(mdata->refreshrate);
856 }
857#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700858 // Set the copybit source:
859 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700860 src.w = getWidth(hnd);
861 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700862 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700863
864 // Handle R/B swap
865 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
866 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
867 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
868 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
869 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
870 }
871 }
872
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700873 src.base = (void *)hnd->base;
874 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700875 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700876 // Initialize vertical padding to zero for now,
877 // this needs to change to accomodate vertical stride
878 // if needed in the future
879 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700880
shuoy7c101f92013-08-26 14:48:57 +0800881 int layerTransform = layer->transform ;
882 // When flip and rotation(90) are present alter the flip,
883 // as GPU is doing the flip and rotation in opposite order
884 // to that of MDP3.0
885 // For 270 degrees, we get 90 + (H+V) which is same as doing
886 // flip first and then rotation (H+V) + 90
887 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
888 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
889 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
890 (layer->transform & HAL_TRANSFORM_ROT_90) &&
891 !(layer->transform == HAL_TRANSFORM_ROT_270)){
892 if(layer->transform & HAL_TRANSFORM_FLIP_H){
893 layerTransform ^= HAL_TRANSFORM_FLIP_H;
894 layerTransform |= HAL_TRANSFORM_FLIP_V;
895 }
896 if(layer->transform & HAL_TRANSFORM_FLIP_V){
897 layerTransform ^= HAL_TRANSFORM_FLIP_V;
898 layerTransform |= HAL_TRANSFORM_FLIP_H;
899 }
900 }
901 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700902 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700903 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700904 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
905 sourceCrop.right,
906 sourceCrop.bottom};
907
908 // Copybit destination rect
909 hwc_rect_t displayFrame = layer->displayFrame;
910 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
911 displayFrame.right,
912 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700913#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530914 //change src and dst with dirtyRect
Dileep Kumar Reddid1590482015-05-14 15:16:05 +0530915 if(mSwapRect) {
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530916 hwc_rect_t result = getIntersection(displayFrame, mDirtyRect);
917 if(!isValidRect(result))
918 return true;
919 dstRect.l = result.left;
920 dstRect.t = result.top;
921 dstRect.r = result.right;
922 dstRect.b = result.bottom;
923
924 srcRect.l += (result.left - displayFrame.left);
925 srcRect.t += (result.top - displayFrame.top);
926 srcRect.r -= (displayFrame.right - result.right);
927 srcRect.b -= (displayFrame.bottom - result.bottom);
Ramakant Singh21cec722014-03-07 19:11:45 +0530928 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700929#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700930 // Copybit dst
931 copybit_image_t dst;
932 dst.w = ALIGN(fbHandle->width,32);
933 dst.h = fbHandle->height;
934 dst.format = fbHandle->format;
935 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800936 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700937
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800938 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700939
940 int32_t screen_w = displayFrame.right - displayFrame.left;
941 int32_t screen_h = displayFrame.bottom - displayFrame.top;
942 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
943 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
944
945 // Copybit dst
946 float copybitsMaxScale =
947 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
948 float copybitsMinScale =
949 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
950
Sushil Chauhandffbf432013-12-09 15:33:57 -0800951 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700952 //swap screen width and height
953 int tmp = screen_w;
954 screen_w = screen_h;
955 screen_h = tmp;
956 }
957 private_handle_t *tmpHnd = NULL;
958
959 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
960 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530961 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
962 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700963 return -1;
964 }
965
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530966 float dsdx = (float)screen_w/(float)src_crop_width;
967 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700968
969 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
970 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
971 if(dsdx > scaleLimitMax ||
972 dtdy > scaleLimitMax ||
973 dsdx < 1/scaleLimitMin ||
974 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400975 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700976 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
977 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700978 return -1;
979 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400980 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700981 if(dsdx > copybitsMaxScale ||
982 dtdy > copybitsMaxScale ||
983 dsdx < 1/copybitsMinScale ||
984 dtdy < 1/copybitsMinScale){
985 // The requested scale is out of the range the hardware
986 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400987 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700988 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
989 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
990 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
991 src_crop_width,src_crop_height);
992
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700993
994 int tmp_w = src_crop_width;
995 int tmp_h = src_crop_height;
996
Ramakant Singhd2875e12015-01-19 12:45:41 +0530997 if (dsdx > copybitsMaxScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530998 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +0530999 if (dtdy > copybitsMaxScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +05301000 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +05301001 // ceil the tmp_w and tmp_h value to maintain proper ratio
1002 // b/w src and dst (should not cross the desired scale limit
1003 // due to float -> int )
1004 if (dsdx < 1/copybitsMinScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +05301005 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +05301006 if (dtdy < 1/copybitsMinScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +05301007 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +05301008
Terence Hampson663dfa72013-08-08 12:35:41 -04001009 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001010
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -05001011 int usage = 0;
Terence Hampsonb6a123a2013-07-18 11:54:16 -04001012 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001013
Terence Hampsonb6a123a2013-07-18 11:54:16 -04001014 // We do not want copybit to generate alpha values from nothing
1015 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
1016 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
1017 format = HAL_PIXEL_FORMAT_RGBX_8888;
1018 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -08001019 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001020 copybit_image_t tmp_dst;
1021 copybit_rect_t tmp_rect;
1022 tmp_dst.w = tmp_w;
1023 tmp_dst.h = tmp_h;
1024 tmp_dst.format = tmpHnd->format;
1025 tmp_dst.handle = tmpHnd;
1026 tmp_dst.horiz_padding = src.horiz_padding;
1027 tmp_dst.vert_padding = src.vert_padding;
1028 tmp_rect.l = 0;
1029 tmp_rect.t = 0;
1030 tmp_rect.r = tmp_dst.w;
1031 tmp_rect.b = tmp_dst.h;
1032 //create one clip region
1033 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
1034 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
1035 region_iterator tmp_it(tmp_hwc_reg);
1036 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -07001037 //TODO: once, we are able to read layer alpha, update this
1038 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -04001039 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001040 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
1041 &srcRect, &tmp_it);
1042 if(err < 0){
1043 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
1044 __LINE__);
1045 if(tmpHnd)
1046 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001047 return err;
1048 }
Terence Hampsonadf47302013-05-23 12:21:02 -04001049 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -04001050 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -04001051 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -04001052 close(acquireFd);
1053 acquireFd = -1;
1054 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001055 // copy new src and src rect crop
1056 src = tmp_dst;
1057 srcRect = tmp_rect;
1058 }
1059 }
1060 // Copybit region
1061 hwc_region_t region = layer->visibleRegionScreen;
Dileep Kumar Reddib588ebd2015-03-18 15:25:22 +05301062 //Do not use visible regions in case of scaling
1063 if (region.numRects > 1) {
1064 if (needsScaling(layer)) {
1065 region.numRects = 1;
1066 region.rects = &layer->displayFrame;
1067 }
1068 }
1069
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001070 region_iterator copybitRegion(region);
1071
1072 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1073 renderBuffer->width);
1074 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1075 renderBuffer->height);
1076 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +08001077 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -07001078 //TODO: once, we are able to read layer alpha, update this
1079 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Ramakant Singh140965d2015-01-08 23:45:04 +05301080 copybit->set_parameter(copybit, COPYBIT_DYNAMIC_FPS, dynamic_fps);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001081 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
1082 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001083 copybit->set_parameter(copybit, COPYBIT_DITHER,
1084 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
1085 COPYBIT_ENABLE : COPYBIT_DISABLE);
Ramakant Singh495f09f2015-04-13 20:06:41 +05301086 copybit->set_parameter(copybit, COPYBIT_FG_LAYER,
1087 (layer->blending == HWC_BLENDING_NONE || isFG ) ?
Shivaraj Shettye86506a2013-08-06 12:56:30 +05301088 COPYBIT_ENABLE : COPYBIT_DISABLE);
1089
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001090 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
1091 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -04001092 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001093 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
1094 &copybitRegion);
1095 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
1096 COPYBIT_DISABLE);
1097
Terence Hampsonadf47302013-05-23 12:21:02 -04001098 if(tmpHnd) {
1099 if (ctx->mMDP.version < qdutils::MDP_V4_0){
1100 int ret = -1, releaseFd;
1101 // we need to wait for the buffer before freeing
1102 copybit->flush_get_fence(copybit, &releaseFd);
1103 ret = sync_wait(releaseFd, 1000);
1104 if(ret < 0) {
1105 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
1106 __FUNCTION__, errno, strerror(errno));
1107 }
1108 close(releaseFd);
1109 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001110 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -04001111 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001112
1113 if(err < 0)
1114 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001115 return err;
1116}
1117
Sushil Chauhan943797c2013-10-21 17:35:55 -07001118int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
1119 private_handle_t *renderBuffer)
1120{
1121 if (!renderBuffer) {
1122 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
1123 return -1;
1124 }
1125
1126 // Copybit dst
1127 copybit_image_t dst;
1128 dst.w = ALIGN(renderBuffer->width, 32);
1129 dst.h = renderBuffer->height;
1130 dst.format = renderBuffer->format;
1131 dst.base = (void *)renderBuffer->base;
1132 dst.handle = (native_handle_t *)renderBuffer;
1133
1134 // Copybit dst rect
1135 hwc_rect_t displayFrame = layer->displayFrame;
1136 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
1137 displayFrame.right, displayFrame.bottom};
1138
1139 uint32_t color = layer->transform;
1140 copybit_device_t *copybit = mEngine;
1141 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1142 renderBuffer->width);
1143 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1144 renderBuffer->height);
1145 copybit->set_parameter(copybit, COPYBIT_DITHER,
1146 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
1147 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -08001148 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -07001149 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
1150 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
1151 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
1152 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
1153 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
1154 return res;
1155}
1156
Naseer Ahmed5b6708a2012-08-02 13:46:08 -07001157void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -07001158 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001159{
1160 hwc_rect_t displayFrame = layer->displayFrame;
Dileep Kumar Reddiad376f12015-05-08 10:00:04 +05301161 hwc_rect_t result = getIntersection(mDirtyRect, displayFrame);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001162
Dileep Kumar Reddiad376f12015-05-08 10:00:04 +05301163 width = result.right - result.left;
1164 height = result.bottom - result.top;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001165}
1166
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001167bool CopyBit::validateParams(hwc_context_t *ctx,
1168 const hwc_display_contents_1_t *list) {
1169 //Validate parameters
1170 if (!ctx) {
1171 ALOGE("%s:Invalid HWC context", __FUNCTION__);
1172 return false;
1173 } else if (!list) {
1174 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
1175 return false;
1176 }
1177 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001178}
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001179
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001180
Naseer Ahmed64b81212013-02-14 10:29:47 -05001181int CopyBit::allocRenderBuffers(int w, int h, int f)
1182{
1183 int ret = 0;
1184 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1185 if (mRenderBuffer[i] == NULL) {
1186 ret = alloc_buffer(&mRenderBuffer[i],
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -05001187 w, h, f, 0);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001188 }
1189 if(ret < 0) {
1190 freeRenderBuffers();
1191 break;
1192 }
1193 }
1194 return ret;
1195}
1196
1197void CopyBit::freeRenderBuffers()
1198{
1199 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1200 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301201 //Since we are freeing buffer close the fence if it has a valid one.
1202 if(mRelFd[i] >= 0) {
1203 close(mRelFd[i]);
1204 mRelFd[i] = -1;
1205 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001206 free_buffer(mRenderBuffer[i]);
1207 mRenderBuffer[i] = NULL;
1208 }
1209 }
1210}
1211
1212private_handle_t * CopyBit::getCurrentRenderBuffer() {
1213 return mRenderBuffer[mCurRenderBufferIndex];
1214}
1215
1216void CopyBit::setReleaseFd(int fd) {
Sushil Chauhandefd3522014-05-13 18:17:12 -07001217 if (mRelFd[mCurRenderBufferIndex] >=0) {
1218 int ret = -1;
1219 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1220 if (ret < 0)
1221 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1222 __FUNCTION__, errno, strerror(errno));
1223 close(mRelFd[mCurRenderBufferIndex]);
1224 }
1225 mRelFd[mCurRenderBufferIndex] = dup(fd);
1226}
1227
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001228struct copybit_device_t* CopyBit::getCopyBitDevice() {
1229 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001230}
1231
Shalaj Jaina70b4352014-06-15 13:47:47 -07001232CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mEngine(0),
1233 mIsModeOn(false), mCopyBitDraw(false), mCurRenderBufferIndex(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001234
1235 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1236 ctx->dpyAttr[dpy].yres,
1237 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001238 mAlignedWidth,
1239 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001240
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001241 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301242 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001243 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301244 mRelFd[i] = -1;
1245 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301246
1247 char value[PROPERTY_VALUE_MAX];
1248 property_get("debug.hwc.dynThreshold", value, "2");
1249 mDynThreshold = atof(value);
1250
Ramakant Singh21cec722014-03-07 19:11:45 +05301251 property_get("debug.sf.swaprect", value, "0");
1252 mSwapRectEnable = atoi(value) ? true:false ;
Dileep Kumar Reddid1590482015-05-14 15:16:05 +05301253 mSwapRect = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001254 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001255 if(copybit_open(module, &mEngine) < 0) {
1256 ALOGE("FATAL ERROR: copybit open failed.");
1257 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001258 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001259 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001260 }
1261}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001262
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001263CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001264{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001265 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001266 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001267 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001268 copybit_close(mEngine);
1269 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001270 }
1271}
Ramakant Singh21cec722014-03-07 19:11:45 +05301272CopyBit::LayerCache::LayerCache() {
1273 reset();
1274}
1275void CopyBit::LayerCache::reset() {
1276 memset(&hnd, 0, sizeof(hnd));
1277 layerCount = 0;
1278}
1279void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1280 hwc_display_contents_1_t *list, int dpy)
1281{
1282 layerCount = ctx->listStats[dpy].numAppLayers;
1283 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1284 hnd[i] = list->hwLayers[i].handle;
Dileep Kumar Reddiaa488882014-12-16 11:19:45 +05301285 displayFrame[i] = list->hwLayers[i].displayFrame;
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +05301286 drop[i] = ctx->copybitDrop[i];
Ramakant Singh21cec722014-03-07 19:11:45 +05301287 }
1288}
1289
1290CopyBit::FbCache::FbCache() {
1291 reset();
1292}
1293void CopyBit::FbCache::reset() {
1294 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
Ramakant Singh19ead272015-03-18 14:11:31 +05301295 memset(&FbdisplayRect, 0, sizeof(FbdisplayRect));
Ramakant Singh21cec722014-03-07 19:11:45 +05301296 FbIndex =0;
1297}
1298
Ramakant Singh19ead272015-03-18 14:11:31 +05301299void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect,
1300 hwc_rect_t displayRect) {
Ramakant Singh21cec722014-03-07 19:11:45 +05301301 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1302 FbdirtyRect[FbIndex] = dirtyRect;
Ramakant Singh19ead272015-03-18 14:11:31 +05301303 FbdisplayRect[FbIndex] = displayRect;
Ramakant Singh21cec722014-03-07 19:11:45 +05301304 FbIndex++;
1305}
1306
Ramakant Singh19ead272015-03-18 14:11:31 +05301307int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect,
1308 hwc_rect_t displayRect){
Ramakant Singh21cec722014-03-07 19:11:45 +05301309 int sameDirtyCount = 0;
1310 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
Ramakant Singh19ead272015-03-18 14:11:31 +05301311 if( FbdirtyRect[i] == dirtyRect &&
1312 FbdisplayRect[i] == displayRect)
Ramakant Singh21cec722014-03-07 19:11:45 +05301313 sameDirtyCount++;
1314 }
1315 return sameDirtyCount;
1316}
1317
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001318}; //namespace qhwc