blob: 00bd6438a768dff11689fee718a9997aa1cf187c [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
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);
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +053099 unsigned int renderArea = getRGBRenderingArea(ctx, list);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700100 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
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530115unsigned int CopyBit::getRGBRenderingArea (const hwc_context_t *ctx,
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) {
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530125 if (BUFFER_TYPE_UI == hnd->bufferType && !ctx->copybitDrop[i]) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700126 getLayerResolution(&list->hwLayers[i], w, h);
127 renderArea += (w*h);
128 }
129 }
130 }
131 return renderArea;
132}
133
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +0530134bool CopyBit::isLayerChanging(hwc_context_t *ctx,
135 hwc_display_contents_1_t *list, int k) {
Dileep Kumar Reddiaa488882014-12-16 11:19:45 +0530136 if((mLayerCache.hnd[k] != list->hwLayers[k].handle) ||
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +0530137 (mLayerCache.drop[k] != ctx->copybitDrop[k]) ||
Dileep Kumar Reddiaa488882014-12-16 11:19:45 +0530138 (mLayerCache.displayFrame[k].left !=
139 list->hwLayers[k].displayFrame.left) ||
140 (mLayerCache.displayFrame[k].top !=
141 list->hwLayers[k].displayFrame.top) ||
142 (mLayerCache.displayFrame[k].right !=
143 list->hwLayers[k].displayFrame.right) ||
144 (mLayerCache.displayFrame[k].bottom !=
145 list->hwLayers[k].displayFrame.bottom)) {
146 return 1;
147 }
148 return 0;
149}
150
Ramakant Singh21cec722014-03-07 19:11:45 +0530151int CopyBit::getLayersChanging(hwc_context_t *ctx,
152 hwc_display_contents_1_t *list,
153 int dpy){
154
155 int changingLayerIndex = -1;
156 if(mLayerCache.layerCount != ctx->listStats[dpy].numAppLayers) {
157 mLayerCache.reset();
158 mFbCache.reset();
159 mLayerCache.updateCounts(ctx,list,dpy);
160 return -1;
161 }
162
163 int updatingLayerCount = 0;
164 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--){
165 //swap rect will kick in only for single updating layer
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +0530166 if(isLayerChanging(ctx, list, k)) {
Ramakant Singh21cec722014-03-07 19:11:45 +0530167 updatingLayerCount ++;
168 if(updatingLayerCount == 1)
169 changingLayerIndex = k;
170 }
171 }
172 //since we are using more than one framebuffers,we have to
173 //kick in swap rect only if we are getting continuous same
174 //dirty rect for same layer at least equal of number of
175 //framebuffers
176
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530177 if ( updatingLayerCount <= 1 ) {
178 hwc_rect_t dirtyRect;
179 if (updatingLayerCount == 0) {
180 dirtyRect.left = INVALID_DIMENSION;
181 dirtyRect.top = INVALID_DIMENSION;
182 dirtyRect.right = INVALID_DIMENSION;
183 dirtyRect.bottom = INVALID_DIMENSION;
184 changingLayerIndex = NO_UPDATING_LAYER;
185 } else {
186 dirtyRect = list->hwLayers[changingLayerIndex].displayFrame;
Saurabh Shah93c69052014-04-24 10:27:10 -0700187#ifdef QCOM_BSP
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530188 dirtyRect = list->hwLayers[changingLayerIndex].dirtyRect;
Saurabh Shah93c69052014-04-24 10:27:10 -0700189#endif
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530190 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530191
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530192 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--) {
193 //disable swap rect in case of scaling and video .
194 private_handle_t *hnd =(private_handle_t *)list->hwLayers[k].handle;
Dileep Kumar Reddic3545042015-03-11 19:37:06 +0530195 if(needsScaling(&list->hwLayers[k])||( hnd && isYuvBuffer(hnd)) ||
196 (list->hwLayers[k].transform & HAL_TRANSFORM_ROT_90)) {
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530197 mFbCache.reset();
198 return -1;
Ramakant Singh21cec722014-03-07 19:11:45 +0530199 }
200 }
Ramakant Singh19ead272015-03-18 14:11:31 +0530201 hwc_rect_t displayRect = list->hwLayers[changingLayerIndex].displayFrame;
202 if(mFbCache.getUnchangedFbDRCount(dirtyRect, displayRect) <
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530203 NUM_RENDER_BUFFERS) {
Ramakant Singh19ead272015-03-18 14:11:31 +0530204 mFbCache.insertAndUpdateFbCache(dirtyRect, displayRect);
Ramakant Singh21cec722014-03-07 19:11:45 +0530205 changingLayerIndex = -1;
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530206 }
207 } else {
Ramakant Singh21cec722014-03-07 19:11:45 +0530208 mFbCache.reset();
209 changingLayerIndex = -1;
210 }
211 mLayerCache.updateCounts(ctx,list,dpy);
212 return changingLayerIndex;
213}
214
215int CopyBit::checkDirtyRect(hwc_context_t *ctx,
216 hwc_display_contents_1_t *list,
217 int dpy) {
218
219 //dirty rect will enable only if
220 //1.Only single layer is updating.
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530221 //2.No scaling
222 //3.No video layer
Ramakant Singh21cec722014-03-07 19:11:45 +0530223 if(mSwapRectEnable == false)
224 return -1;
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530225 return getLayersChanging(ctx, list, dpy);
Ramakant Singh21cec722014-03-07 19:11:45 +0530226}
227
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700228bool CopyBit::prepareOverlap(hwc_context_t *ctx,
229 hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700230
231 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
232 ALOGE("%s: Invalid request", __FUNCTION__);
233 return false;
234 }
235
Saurabh Shah15455aa2015-01-28 13:54:48 -0800236 if (mEngine == NULL) {
237 ALOGW("%s: Copybit HAL not enabled", __FUNCTION__);
238 return false;
239 }
240
241 if (!(validateParams(ctx, list))) {
242 ALOGE("%s: validateParams() failed", __FUNCTION__);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700243 return false;
244 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700245 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700246
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700247 // Allocate render buffers if they're not allocated
248 int alignW = 0, alignH = 0;
249 int finalW = 0, finalH = 0;
250 for (int i = 0; i < ptorInfo->count; i++) {
251 int ovlapIndex = ptorInfo->layerIndex[i];
252 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
253 // render buffer width will be the max of two layers
254 // Align Widht and height to 32, Mdp would be configured
255 // with Aligned overlap w/h
256 finalW = max(finalW, ALIGN((overlap.right - overlap.left), 32));
257 finalH += ALIGN((overlap.bottom - overlap.top), 32);
258 if(finalH > ALIGN((overlap.bottom - overlap.top), 32)) {
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700259 // Calculate the dest top, left will always be zero
260 ptorInfo->displayFrame[i].top = (finalH -
261 (ALIGN((overlap.bottom - overlap.top), 32)));
262 }
263 // calculate the right and bottom values
264 ptorInfo->displayFrame[i].right = ptorInfo->displayFrame[i].left +
265 (overlap.right - overlap.left);
266 ptorInfo->displayFrame[i].bottom = ptorInfo->displayFrame[i].top +
267 (overlap.bottom - overlap.top);
268 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700269
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700270 getBufferSizeAndDimensions(finalW, finalH, HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -0700271 alignW, alignH);
272
273 if ((mAlignedWidth != alignW) || (mAlignedHeight != alignH)) {
274 // Overlap rect has changed, so free render buffers
275 freeRenderBuffers();
276 }
277
278 int ret = allocRenderBuffers(alignW, alignH, HAL_PIXEL_FORMAT_RGBA_8888);
279
280 if (ret < 0) {
281 ALOGE("%s: Render buffer allocation failed", __FUNCTION__);
282 return false;
283 }
284
285 mAlignedWidth = alignW;
286 mAlignedHeight = alignH;
287 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
288 return true;
289}
290
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800291bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
292 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700293
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800294 if(mEngine == NULL) {
295 // No copybit device found - cannot use copybit
296 return false;
297 }
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +0530298
299 if(ctx->mThermalBurstMode) {
300 ALOGD_IF (DEBUG_COPYBIT, "%s:Copybit failed,"
301 "Running in Thermal Burst mode",__FUNCTION__);
302 return false;
303 }
304
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800305 int compositionType = qdutils::QCCompositionType::
306 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700307
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800308 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
309 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700310 //GPU/CPU composition, don't change layer composition type
311 return true;
312 }
313
Naseer Ahmed45a99602012-07-31 19:15:24 -0700314 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800315 ALOGE("%s:Invalid Params", __FUNCTION__);
316 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700317 }
318
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800319 if(ctx->listStats[dpy].skipCount) {
320 //GPU will be anyways used
321 return false;
322 }
323
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700324 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700325 // Reached max layers supported by HWC.
326 return false;
327 }
328
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800329 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
330 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500331 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500332
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530333 // Following are MDP3 limitations for which we
334 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400335 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500336 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400337 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
338 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500339 int dst_h, dst_w, src_h, src_w;
340 float dx, dy;
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530341 if(ctx->copybitDrop[i]) {
342 continue;
343 }
Terence Hampsonc235bda2013-07-12 12:47:38 -0400344 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530345 if (layer->planeAlpha != 0xFF)
346 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530347 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500348
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700349 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530350 src_h = sourceCrop.right - sourceCrop.left;
351 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500352 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530353 src_h = sourceCrop.bottom - sourceCrop.top;
354 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500355 }
356 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
357 dst_w = layer->displayFrame.right - layer->displayFrame.left;
358
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530359 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
360 ALOGE("%s: wrong params for display screen_w=%d \
361 src_crop_width=%d screen_h=%d src_crop_height=%d",
362 __FUNCTION__, dst_w,src_w,dst_h,src_h);
363 return false;
364 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530365 dx = (float)dst_w/(float)src_w;
366 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530367
Ramakant Singhd5b369f2014-12-09 23:03:43 +0530368 float scale_factor_max = MAX_SCALE_FACTOR;
369 float scale_factor_min = MIN_SCALE_FACTOR;
370
371 if (isAlphaPresent(layer)) {
372 scale_factor_max = MAX_SCALE_FACTOR/4;
373 scale_factor_min = MIN_SCALE_FACTOR*4;
374 }
375
376 if (dx > scale_factor_max || dx < scale_factor_min)
Terence Hampson6b0a4272013-11-06 16:04:51 -0500377 return false;
378
Ramakant Singhd5b369f2014-12-09 23:03:43 +0530379 if (dy > scale_factor_max || dy < scale_factor_min)
Terence Hampson6b0a4272013-11-06 16:04:51 -0500380 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400381 }
382 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500383
384 //Allocate render buffers if they're not allocated
Ramakant Singhb4106a12014-10-07 18:06:56 +0530385 if ((ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
386 ctx->mMDP.version != qdutils::MDP_V3_0_5) &&
Terence Hampsonc67b0372013-10-09 11:32:21 -0400387 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700388 int ret = allocRenderBuffers(mAlignedWidth,
389 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700390 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500391 if (ret < 0) {
392 return false;
393 } else {
394 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
395 NUM_RENDER_BUFFERS;
396 }
397 }
398
Terence Hampsond0fd5792013-05-29 11:56:52 -0400399 // We cannot mix copybit layer with layers marked to be drawn on FB
400 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
401 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800402
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530403 mCopyBitDraw = false;
404 if (useCopybitForRGB &&
405 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
406 mCopyBitDraw = true;
407 // numAppLayers-1, as we iterate till 0th layer index
408 // Mark all layers to be drawn by copybit
409 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500410 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700411#ifdef QCOM_BSP
Ramakant Singhb4106a12014-10-07 18:06:56 +0530412 if (ctx->mMDP.version == qdutils::MDP_V3_0_4 ||
413 ctx->mMDP.version == qdutils::MDP_V3_0_5)
Terence Hampsonc67b0372013-10-09 11:32:21 -0400414 list->hwLayers[i].compositionType = HWC_BLIT;
415 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700416#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400417 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700418 }
419 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530420
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700421 return true;
422}
423
Naseer Ahmed183939d2013-03-14 20:44:17 -0400424int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
425{
426 int ret = 0;
427 copybit_rect_t clear_rect = {rect.left, rect.top,
428 rect.right,
429 rect.bottom};
430
431 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700432 buf.w = ALIGN(getWidth(hnd),32);
433 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400434 buf.format = hnd->format;
435 buf.base = (void *)hnd->base;
436 buf.handle = (native_handle_t *)hnd;
437
438 copybit_device_t *copybit = mEngine;
439 ret = copybit->clear(copybit, &buf, &clear_rect);
440 return ret;
441}
442
Ramakant Singh0def28c2014-03-28 20:43:13 +0530443bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
444 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530445 int dpy, int *copybitFd) {
446 int layerCount = 0;
Shalaj Jaina70b4352014-06-15 13:47:47 -0700447 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530448 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
449 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530450
451 if(ctx->enableABC == false)
452 return false;
453
Ramakant Singh467759f2014-04-16 11:09:40 +0530454 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530455 return false;
456
457 layerCount = ctx->listStats[dpy].numAppLayers;
458 //bottom most layer should
459 //equal to FB
460 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
461 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
462 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
463 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
464 if(tmpLayer->transform ||
Dileep Kumar Reddi2f8a13e2015-03-26 18:01:23 +0530465 (list->flags & HWC_GEOMETRY_CHANGED) ||
Ramakant Singh0def28c2014-03-28 20:43:13 +0530466 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
467 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
468 (needsScaling(tmpLayer) == true)) {
469 return false;
470 }else {
471 ctx->listStats[dpy].renderBufIndexforABC = 0;
472 }
473 }
474
Ramakant Singh467759f2014-04-16 11:09:40 +0530475 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
476 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530477 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530478
Ramakant Singhcc326612014-06-19 14:19:18 +0530479 if(layerCount == MAX_LAYERS_FOR_ABC) {
Ramakant Singh467759f2014-04-16 11:09:40 +0530480 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
Ramakant Singhcc326612014-06-19 14:19:18 +0530481 //enable ABC only for non intersecting layers.
482 hwc_rect_t displayFrame =
483 list->hwLayers[abcRenderBufIdx].displayFrame;
484 for (int i = abcRenderBufIdx + 1; i < layerCount; i++) {
485 hwc_rect_t tmpDisplayFrame = list->hwLayers[i].displayFrame;
486 hwc_rect_t result = getIntersection(displayFrame,tmpDisplayFrame);
487 if (isValidRect(result)) {
488 ctx->listStats[dpy].renderBufIndexforABC = -1;
489 return false;
490 }
491 }
492 // Pass the Acquire Fence FD to driver for base layer
Ramakant Singh467759f2014-04-16 11:09:40 +0530493 private_handle_t *renderBuffer =
494 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
495 copybit_device_t *copybit = getCopyBitDevice();
496 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
497 copybit->set_sync(copybit,
498 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
499 }
Ramakant Singhcc326612014-06-19 14:19:18 +0530500 for(int i = abcRenderBufIdx + 1; i < layerCount; i++){
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530501 mDirtyLayerIndex = -1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530502 int retVal = drawLayerUsingCopybit(ctx,
503 &(list->hwLayers[i]),renderBuffer, 0);
504 if(retVal < 0) {
505 ALOGE("%s : Copybit failed", __FUNCTION__);
506 }
507 }
508 // Get Release Fence FD of copybit for the App layer(s)
509 copybit->flush_get_fence(copybit, copybitFd);
Ramakant Singh52e1c0c2014-12-18 23:35:33 +0530510 close(list->hwLayers[last].acquireFenceFd);
511 list->hwLayers[last].acquireFenceFd = -1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530512 return true;
513 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530514 }
515 return false;
516}
517
518bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
519 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800520 // draw layers marked for COPYBIT
521 int retVal = true;
522 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400523 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500524 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400525 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800526
Ramakant Singh21cec722014-03-07 19:11:45 +0530527 if(mCopyBitDraw == false){
528 mFbCache.reset(); // there is no layer marked for copybit
529 return false ;
530 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530531
Ramakant Singh467759f2014-04-16 11:09:40 +0530532 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530533 return true;
534 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800535 //render buffer
Ramakant Singhb4106a12014-10-07 18:06:56 +0530536 if (ctx->mMDP.version == qdutils::MDP_V3_0_4 ||
537 ctx->mMDP.version == qdutils::MDP_V3_0_5) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530538 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400539 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
540 } else {
541 renderBuffer = getCurrentRenderBuffer();
542 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800543 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500544 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800545 return false;
546 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500547
Terence Hampson0124cc72013-04-18 23:45:56 -0700548 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400549 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530550 if(mRelFd[mCurRenderBufferIndex] >=0) {
551 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
552 close(mRelFd[mCurRenderBufferIndex]);
553 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400554 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400555 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400556 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400557 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400558 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400559 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700560 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530561
Ramakant Singh21cec722014-03-07 19:11:45 +0530562 mDirtyLayerIndex = checkDirtyRect(ctx, list, dpy);
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530563 ALOGD_IF (DEBUG_COPYBIT, "%s:Dirty Layer Index: %d",
564 __FUNCTION__, mDirtyLayerIndex);
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530565
Ramakant Singh75b427c2014-12-08 17:09:31 +0530566 hwc_rect_t clearRegion = {0,0,0,0};
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530567 mDirtyRect = list->hwLayers[last].displayFrame;
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530568
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530569 if (mDirtyLayerIndex != NO_UPDATING_LAYER &&
570 CBUtils::getuiClearRegion(list, clearRegion, layerProp,
571 mDirtyLayerIndex)) {
Ramakant Singh909bc2a2015-02-10 16:29:21 +0530572 int clear_w = clearRegion.right - clearRegion.left;
573 int clear_h = clearRegion.bottom - clearRegion.top;
574 //mdp can't handle solid fill for one line
575 //making solid fill as full in this case
576 //disable swap rect if presents
577 if ((clear_w == 1) || (clear_h ==1)) {
578 clear(renderBuffer, mDirtyRect);
579 mDirtyLayerIndex = -1;
580 }else
581 clear(renderBuffer, clearRegion);
582 }
Dileep Kumar Reddi45da4572015-04-21 08:29:25 +0530583 if (mDirtyLayerIndex != -1) {
584 if (mDirtyLayerIndex == NO_UPDATING_LAYER) {
585 mDirtyRect = clearRegion;
586 } else {
587 mDirtyRect = list->hwLayers[mDirtyLayerIndex].displayFrame;
588 }
589 }
Ramakant Singh909bc2a2015-02-10 16:29:21 +0530590
Naseer Ahmed64b81212013-02-14 10:29:47 -0500591 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800592 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500593 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
594 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800595 continue;
596 }
Dileep Kumar Reddi4070e932014-09-30 09:00:57 +0530597 if(ctx->copybitDrop[i]) {
598 continue;
599 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800600 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400601 if (list->hwLayers[i].acquireFenceFd != -1
602 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800603 // Wait for acquire Fence on the App buffers.
604 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
605 if(ret < 0) {
606 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
607 __FUNCTION__, errno, strerror(errno));
608 }
609 close(list->hwLayers[i].acquireFenceFd);
610 list->hwLayers[i].acquireFenceFd = -1;
611 }
612 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530613 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800614 copybitLayerCount++;
615 if(retVal < 0) {
616 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
617 }
618 }
619
620 if (copybitLayerCount) {
621 copybit_device_t *copybit = getCopyBitDevice();
622 // Async mode
623 copybit->flush_get_fence(copybit, fd);
Ramakant Singhb4106a12014-10-07 18:06:56 +0530624 if((ctx->mMDP.version == qdutils::MDP_V3_0_4 ||
625 ctx->mMDP.version == qdutils::MDP_V3_0_5) &&
Terence Hampsonc67b0372013-10-09 11:32:21 -0400626 list->hwLayers[last].acquireFenceFd >= 0) {
627 close(list->hwLayers[last].acquireFenceFd);
628 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400629 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800630 }
631 return true;
632}
633
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700634int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700635 int fd = -1;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700636 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700637
638 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
639 ALOGE("%s: Invalid request", __FUNCTION__);
640 return fd;
641 }
642
643 private_handle_t *renderBuffer = getCurrentRenderBuffer();
644
645 if (!renderBuffer) {
646 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
647 return fd;
648 }
649
Dileep Kumar Reddid1f08e42014-11-05 21:24:27 +0530650 //Clear the transparent or left out region on the render buffer
651 hwc_rect_t clearRegion = {0,0,0,0};
652 LayerProp *layerProp = ctx->layerProp[0];
653 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
654 clear(renderBuffer, clearRegion);
655
Sushil Chauhandefd3522014-05-13 18:17:12 -0700656 int copybitLayerCount = 0;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700657 for(int j = 0; j < ptorInfo->count; j++) {
658 int ovlapIndex = ptorInfo->layerIndex[j];
659 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
Prabhanjan Kandula9889a202014-09-04 21:50:35 +0530660 if(j) {
661 /**
662 * It's possible that 2 PTOR layers might have overlapping.
663 * In such case, remove the intersection(again if peripheral)
664 * from the lower PTOR layer to avoid overlapping.
665 * If intersection is not on peripheral then compromise
666 * by reducing number of PTOR layers.
667 **/
668 int prevOvlapIndex = ptorInfo->layerIndex[0];
669 hwc_rect_t prevOvlap = list->hwLayers[prevOvlapIndex].displayFrame;
670 hwc_rect_t commonRect = getIntersection(prevOvlap, overlap);
671 if(isValidRect(commonRect)) {
672 overlap = deductRect(overlap, commonRect);
673 }
674 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700675
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700676 // Draw overlapped content of layers on render buffer
677 for (int i = 0; i <= ovlapIndex; i++) {
678 hwc_layer_1_t *layer = &list->hwLayers[i];
679 if(!isValidRect(getIntersection(layer->displayFrame,
680 overlap))) {
681 continue;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700682 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700683 if ((list->hwLayers[i].acquireFenceFd != -1)) {
684 // Wait for acquire fence on the App buffers.
685 if(sync_wait(list->hwLayers[i].acquireFenceFd, 1000) < 0) {
686 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
687 __FUNCTION__, errno, strerror(errno));
688 }
689 close(list->hwLayers[i].acquireFenceFd);
690 list->hwLayers[i].acquireFenceFd = -1;
691 }
Dileep Kumar Reddi408fde52014-11-04 22:53:15 +0530692 /*
693 * Find the intersection of layer display frame with PTOR layer
694 * with respect to screen co-ordinates
695 *
696 * Calculated the destination rect by transforming the overlapping
697 * region of layer display frame with respect to PTOR display frame
698 *
699 * Transform the destination rect on to render buffer
700 * */
701 hwc_rect_t destRect = getIntersection(overlap, layer->displayFrame);
702 destRect.left = destRect.left - overlap.left +
703 ptorInfo->displayFrame[j].left;
704 destRect.right = destRect.right- overlap.left +
705 ptorInfo->displayFrame[j].left;
706 destRect.top = destRect.top - overlap.top +
707 ptorInfo->displayFrame[j].top;
708 destRect.bottom = destRect.bottom - overlap.top +
709 ptorInfo->displayFrame[j].top;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700710
Dileep Kumar Reddi408fde52014-11-04 22:53:15 +0530711 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer,
712 overlap, destRect);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700713 copybitLayerCount++;
714 if(retVal < 0) {
715 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
716 copybitLayerCount = 0;
717 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700718 }
719 }
720
721 if (copybitLayerCount) {
722 copybit_device_t *copybit = getCopyBitDevice();
723 copybit->flush_get_fence(copybit, &fd);
724 }
725
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700726 ALOGD_IF(DEBUG_COPYBIT, "%s: done! copybitLayerCount = %d", __FUNCTION__,
727 copybitLayerCount);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700728 return fd;
729}
730
731int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700732 private_handle_t *renderBuffer, hwc_rect_t overlap,
733 hwc_rect_t destRect)
Sushil Chauhandefd3522014-05-13 18:17:12 -0700734{
735 hwc_context_t* ctx = (hwc_context_t*)(dev);
736 if (!ctx) {
737 ALOGE("%s: null context ", __FUNCTION__);
738 return -1;
739 }
740
741 private_handle_t *hnd = (private_handle_t *)layer->handle;
742 if (!hnd) {
743 ALOGE("%s: invalid handle", __FUNCTION__);
744 return -1;
745 }
746
747 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
748 if (!dstHandle) {
749 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
750 return -1;
751 }
752
753 // Set the Copybit Source
754 copybit_image_t src;
755 src.handle = (native_handle_t *)layer->handle;
756 src.w = hnd->width;
757 src.h = hnd->height;
758 src.base = (void *)hnd->base;
759 src.format = hnd->format;
760 src.horiz_padding = 0;
761 src.vert_padding = 0;
762
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700763
Sushil Chauhandefd3522014-05-13 18:17:12 -0700764 hwc_rect_t dispFrame = layer->displayFrame;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700765 hwc_rect_t iRect = getIntersection(dispFrame, overlap);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700766 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700767 qhwc::calculate_crop_rects(crop, dispFrame, iRect,
768 layer->transform);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700769
770 // Copybit source rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700771 copybit_rect_t srcRect = {crop.left, crop.top, crop.right,
772 crop.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700773
774 // Copybit destination rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700775 copybit_rect_t dstRect = {destRect.left, destRect.top, destRect.right,
776 destRect.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700777
778 // Copybit dst
779 copybit_image_t dst;
780 dst.handle = (native_handle_t *)dstHandle;
781 dst.w = ALIGN(dstHandle->width, 32);
782 dst.h = dstHandle->height;
783 dst.base = (void *)dstHandle->base;
784 dst.format = dstHandle->format;
785
786 copybit_device_t *copybit = mEngine;
787
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700788 // Copybit region is the destRect
789 hwc_rect_t regRect = {dstRect.l,dstRect.t, dstRect.r, dstRect.b};
790 hwc_region_t region;
791 region.numRects = 1;
792 region.rects = &regRect;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700793 region_iterator copybitRegion(region);
794 int acquireFd = layer->acquireFenceFd;
795
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700796 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
797 renderBuffer->width);
798 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
799 renderBuffer->height);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700800 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
801 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
802 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
803 copybit->set_parameter(copybit, COPYBIT_DITHER,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700804 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE :
805 COPYBIT_DISABLE);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700806 copybit->set_sync(copybit, acquireFd);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700807 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
808 &copybitRegion);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700809
810 if (err < 0)
811 ALOGE("%s: copybit stretch failed",__FUNCTION__);
812
813 return err;
814}
815
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700816int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800817 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700818{
819 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400820 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700821 if(!ctx) {
822 ALOGE("%s: null context ", __FUNCTION__);
823 return -1;
824 }
825
826 private_handle_t *hnd = (private_handle_t *)layer->handle;
827 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700828 if (layer->flags & HWC_COLOR_FILL) { // Color layer
829 return fillColorUsingCopybit(layer, renderBuffer);
830 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700831 ALOGE("%s: invalid handle", __FUNCTION__);
832 return -1;
833 }
834
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800835 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700836 if(!fbHandle) {
837 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700838 return -1;
839 }
Ramakant Singh140965d2015-01-08 23:45:04 +0530840 uint32_t dynamic_fps = 0;
841#ifdef DYNAMIC_FPS
842 MetaData_t *mdata = hnd ? (MetaData_t *)hnd->base_metadata : NULL;
843 if (mdata && (mdata->operation & UPDATE_REFRESH_RATE)) {
844 dynamic_fps = roundOff(mdata->refreshrate);
845 }
846#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700847 // Set the copybit source:
848 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700849 src.w = getWidth(hnd);
850 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700851 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700852
853 // Handle R/B swap
854 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
855 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
856 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
857 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
858 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
859 }
860 }
861
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700862 src.base = (void *)hnd->base;
863 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700864 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700865 // Initialize vertical padding to zero for now,
866 // this needs to change to accomodate vertical stride
867 // if needed in the future
868 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700869
shuoy7c101f92013-08-26 14:48:57 +0800870 int layerTransform = layer->transform ;
871 // When flip and rotation(90) are present alter the flip,
872 // as GPU is doing the flip and rotation in opposite order
873 // to that of MDP3.0
874 // For 270 degrees, we get 90 + (H+V) which is same as doing
875 // flip first and then rotation (H+V) + 90
876 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
877 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
878 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
879 (layer->transform & HAL_TRANSFORM_ROT_90) &&
880 !(layer->transform == HAL_TRANSFORM_ROT_270)){
881 if(layer->transform & HAL_TRANSFORM_FLIP_H){
882 layerTransform ^= HAL_TRANSFORM_FLIP_H;
883 layerTransform |= HAL_TRANSFORM_FLIP_V;
884 }
885 if(layer->transform & HAL_TRANSFORM_FLIP_V){
886 layerTransform ^= HAL_TRANSFORM_FLIP_V;
887 layerTransform |= HAL_TRANSFORM_FLIP_H;
888 }
889 }
890 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700891 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700892 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700893 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
894 sourceCrop.right,
895 sourceCrop.bottom};
896
897 // Copybit destination rect
898 hwc_rect_t displayFrame = layer->displayFrame;
899 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
900 displayFrame.right,
901 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700902#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530903 //change src and dst with dirtyRect
904 if(mDirtyLayerIndex != -1) {
Dileep Kumar Reddif7b72052014-12-16 11:25:11 +0530905 hwc_rect_t result = getIntersection(displayFrame, mDirtyRect);
906 if(!isValidRect(result))
907 return true;
908 dstRect.l = result.left;
909 dstRect.t = result.top;
910 dstRect.r = result.right;
911 dstRect.b = result.bottom;
912
913 srcRect.l += (result.left - displayFrame.left);
914 srcRect.t += (result.top - displayFrame.top);
915 srcRect.r -= (displayFrame.right - result.right);
916 srcRect.b -= (displayFrame.bottom - result.bottom);
Ramakant Singh21cec722014-03-07 19:11:45 +0530917 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700918#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700919 // Copybit dst
920 copybit_image_t dst;
921 dst.w = ALIGN(fbHandle->width,32);
922 dst.h = fbHandle->height;
923 dst.format = fbHandle->format;
924 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800925 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700926
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800927 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700928
929 int32_t screen_w = displayFrame.right - displayFrame.left;
930 int32_t screen_h = displayFrame.bottom - displayFrame.top;
931 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
932 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
933
934 // Copybit dst
935 float copybitsMaxScale =
936 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
937 float copybitsMinScale =
938 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
939
Sushil Chauhandffbf432013-12-09 15:33:57 -0800940 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700941 //swap screen width and height
942 int tmp = screen_w;
943 screen_w = screen_h;
944 screen_h = tmp;
945 }
946 private_handle_t *tmpHnd = NULL;
947
948 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
949 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530950 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
951 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700952 return -1;
953 }
954
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530955 float dsdx = (float)screen_w/(float)src_crop_width;
956 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700957
958 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
959 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
960 if(dsdx > scaleLimitMax ||
961 dtdy > scaleLimitMax ||
962 dsdx < 1/scaleLimitMin ||
963 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400964 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700965 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
966 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700967 return -1;
968 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400969 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700970 if(dsdx > copybitsMaxScale ||
971 dtdy > copybitsMaxScale ||
972 dsdx < 1/copybitsMinScale ||
973 dtdy < 1/copybitsMinScale){
974 // The requested scale is out of the range the hardware
975 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400976 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700977 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
978 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
979 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
980 src_crop_width,src_crop_height);
981
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700982
983 int tmp_w = src_crop_width;
984 int tmp_h = src_crop_height;
985
Ramakant Singhd2875e12015-01-19 12:45:41 +0530986 if (dsdx > copybitsMaxScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530987 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +0530988 if (dtdy > copybitsMaxScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530989 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +0530990 // ceil the tmp_w and tmp_h value to maintain proper ratio
991 // b/w src and dst (should not cross the desired scale limit
992 // due to float -> int )
993 if (dsdx < 1/copybitsMinScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530994 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +0530995 if (dtdy < 1/copybitsMinScale)
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530996 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Ramakant Singhd2875e12015-01-19 12:45:41 +0530997
Terence Hampson663dfa72013-08-08 12:35:41 -0400998 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700999
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -05001000 int usage = 0;
Terence Hampsonb6a123a2013-07-18 11:54:16 -04001001 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001002
Terence Hampsonb6a123a2013-07-18 11:54:16 -04001003 // We do not want copybit to generate alpha values from nothing
1004 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
1005 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
1006 format = HAL_PIXEL_FORMAT_RGBX_8888;
1007 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -08001008 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001009 copybit_image_t tmp_dst;
1010 copybit_rect_t tmp_rect;
1011 tmp_dst.w = tmp_w;
1012 tmp_dst.h = tmp_h;
1013 tmp_dst.format = tmpHnd->format;
1014 tmp_dst.handle = tmpHnd;
1015 tmp_dst.horiz_padding = src.horiz_padding;
1016 tmp_dst.vert_padding = src.vert_padding;
1017 tmp_rect.l = 0;
1018 tmp_rect.t = 0;
1019 tmp_rect.r = tmp_dst.w;
1020 tmp_rect.b = tmp_dst.h;
1021 //create one clip region
1022 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
1023 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
1024 region_iterator tmp_it(tmp_hwc_reg);
1025 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -07001026 //TODO: once, we are able to read layer alpha, update this
1027 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -04001028 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001029 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
1030 &srcRect, &tmp_it);
1031 if(err < 0){
1032 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
1033 __LINE__);
1034 if(tmpHnd)
1035 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001036 return err;
1037 }
Terence Hampsonadf47302013-05-23 12:21:02 -04001038 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -04001039 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -04001040 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -04001041 close(acquireFd);
1042 acquireFd = -1;
1043 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001044 // copy new src and src rect crop
1045 src = tmp_dst;
1046 srcRect = tmp_rect;
1047 }
1048 }
1049 // Copybit region
1050 hwc_region_t region = layer->visibleRegionScreen;
Dileep Kumar Reddib588ebd2015-03-18 15:25:22 +05301051 //Do not use visible regions in case of scaling
1052 if (region.numRects > 1) {
1053 if (needsScaling(layer)) {
1054 region.numRects = 1;
1055 region.rects = &layer->displayFrame;
1056 }
1057 }
1058
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001059 region_iterator copybitRegion(region);
1060
1061 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1062 renderBuffer->width);
1063 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1064 renderBuffer->height);
1065 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +08001066 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -07001067 //TODO: once, we are able to read layer alpha, update this
1068 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Ramakant Singh140965d2015-01-08 23:45:04 +05301069 copybit->set_parameter(copybit, COPYBIT_DYNAMIC_FPS, dynamic_fps);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001070 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
1071 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001072 copybit->set_parameter(copybit, COPYBIT_DITHER,
1073 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
1074 COPYBIT_ENABLE : COPYBIT_DISABLE);
Ramakant Singh495f09f2015-04-13 20:06:41 +05301075 copybit->set_parameter(copybit, COPYBIT_FG_LAYER,
1076 (layer->blending == HWC_BLENDING_NONE || isFG ) ?
Shivaraj Shettye86506a2013-08-06 12:56:30 +05301077 COPYBIT_ENABLE : COPYBIT_DISABLE);
1078
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001079 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
1080 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -04001081 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001082 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
1083 &copybitRegion);
1084 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
1085 COPYBIT_DISABLE);
1086
Terence Hampsonadf47302013-05-23 12:21:02 -04001087 if(tmpHnd) {
1088 if (ctx->mMDP.version < qdutils::MDP_V4_0){
1089 int ret = -1, releaseFd;
1090 // we need to wait for the buffer before freeing
1091 copybit->flush_get_fence(copybit, &releaseFd);
1092 ret = sync_wait(releaseFd, 1000);
1093 if(ret < 0) {
1094 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
1095 __FUNCTION__, errno, strerror(errno));
1096 }
1097 close(releaseFd);
1098 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001099 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -04001100 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001101
1102 if(err < 0)
1103 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001104 return err;
1105}
1106
Sushil Chauhan943797c2013-10-21 17:35:55 -07001107int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
1108 private_handle_t *renderBuffer)
1109{
1110 if (!renderBuffer) {
1111 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
1112 return -1;
1113 }
1114
1115 // Copybit dst
1116 copybit_image_t dst;
1117 dst.w = ALIGN(renderBuffer->width, 32);
1118 dst.h = renderBuffer->height;
1119 dst.format = renderBuffer->format;
1120 dst.base = (void *)renderBuffer->base;
1121 dst.handle = (native_handle_t *)renderBuffer;
1122
1123 // Copybit dst rect
1124 hwc_rect_t displayFrame = layer->displayFrame;
1125 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
1126 displayFrame.right, displayFrame.bottom};
1127
1128 uint32_t color = layer->transform;
1129 copybit_device_t *copybit = mEngine;
1130 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
1131 renderBuffer->width);
1132 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1133 renderBuffer->height);
1134 copybit->set_parameter(copybit, COPYBIT_DITHER,
1135 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
1136 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -08001137 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -07001138 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
1139 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
1140 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
1141 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
1142 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
1143 return res;
1144}
1145
Naseer Ahmed5b6708a2012-08-02 13:46:08 -07001146void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -07001147 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001148{
1149 hwc_rect_t displayFrame = layer->displayFrame;
1150
1151 width = displayFrame.right - displayFrame.left;
1152 height = displayFrame.bottom - displayFrame.top;
1153}
1154
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001155bool CopyBit::validateParams(hwc_context_t *ctx,
1156 const hwc_display_contents_1_t *list) {
1157 //Validate parameters
1158 if (!ctx) {
1159 ALOGE("%s:Invalid HWC context", __FUNCTION__);
1160 return false;
1161 } else if (!list) {
1162 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
1163 return false;
1164 }
1165 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001166}
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001167
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001168
Naseer Ahmed64b81212013-02-14 10:29:47 -05001169int CopyBit::allocRenderBuffers(int w, int h, int f)
1170{
1171 int ret = 0;
1172 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1173 if (mRenderBuffer[i] == NULL) {
1174 ret = alloc_buffer(&mRenderBuffer[i],
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -05001175 w, h, f, 0);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001176 }
1177 if(ret < 0) {
1178 freeRenderBuffers();
1179 break;
1180 }
1181 }
1182 return ret;
1183}
1184
1185void CopyBit::freeRenderBuffers()
1186{
1187 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1188 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301189 //Since we are freeing buffer close the fence if it has a valid one.
1190 if(mRelFd[i] >= 0) {
1191 close(mRelFd[i]);
1192 mRelFd[i] = -1;
1193 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001194 free_buffer(mRenderBuffer[i]);
1195 mRenderBuffer[i] = NULL;
1196 }
1197 }
1198}
1199
1200private_handle_t * CopyBit::getCurrentRenderBuffer() {
1201 return mRenderBuffer[mCurRenderBufferIndex];
1202}
1203
1204void CopyBit::setReleaseFd(int fd) {
Sushil Chauhandefd3522014-05-13 18:17:12 -07001205 if (mRelFd[mCurRenderBufferIndex] >=0) {
1206 int ret = -1;
1207 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1208 if (ret < 0)
1209 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1210 __FUNCTION__, errno, strerror(errno));
1211 close(mRelFd[mCurRenderBufferIndex]);
1212 }
1213 mRelFd[mCurRenderBufferIndex] = dup(fd);
1214}
1215
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001216struct copybit_device_t* CopyBit::getCopyBitDevice() {
1217 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001218}
1219
Shalaj Jaina70b4352014-06-15 13:47:47 -07001220CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mEngine(0),
1221 mIsModeOn(false), mCopyBitDraw(false), mCurRenderBufferIndex(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001222
1223 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1224 ctx->dpyAttr[dpy].yres,
1225 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001226 mAlignedWidth,
1227 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001228
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001229 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301230 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001231 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301232 mRelFd[i] = -1;
1233 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301234
1235 char value[PROPERTY_VALUE_MAX];
1236 property_get("debug.hwc.dynThreshold", value, "2");
1237 mDynThreshold = atof(value);
1238
Ramakant Singh21cec722014-03-07 19:11:45 +05301239 property_get("debug.sf.swaprect", value, "0");
1240 mSwapRectEnable = atoi(value) ? true:false ;
1241 mDirtyLayerIndex = -1;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001242 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001243 if(copybit_open(module, &mEngine) < 0) {
1244 ALOGE("FATAL ERROR: copybit open failed.");
1245 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001246 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001247 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001248 }
1249}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001250
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001251CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001252{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001253 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001254 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001255 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001256 copybit_close(mEngine);
1257 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001258 }
1259}
Ramakant Singh21cec722014-03-07 19:11:45 +05301260CopyBit::LayerCache::LayerCache() {
1261 reset();
1262}
1263void CopyBit::LayerCache::reset() {
1264 memset(&hnd, 0, sizeof(hnd));
1265 layerCount = 0;
1266}
1267void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1268 hwc_display_contents_1_t *list, int dpy)
1269{
1270 layerCount = ctx->listStats[dpy].numAppLayers;
1271 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1272 hnd[i] = list->hwLayers[i].handle;
Dileep Kumar Reddiaa488882014-12-16 11:19:45 +05301273 displayFrame[i] = list->hwLayers[i].displayFrame;
Dileep Kumar Reddi9dab73a2015-02-24 16:15:53 +05301274 drop[i] = ctx->copybitDrop[i];
Ramakant Singh21cec722014-03-07 19:11:45 +05301275 }
1276}
1277
1278CopyBit::FbCache::FbCache() {
1279 reset();
1280}
1281void CopyBit::FbCache::reset() {
1282 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
Ramakant Singh19ead272015-03-18 14:11:31 +05301283 memset(&FbdisplayRect, 0, sizeof(FbdisplayRect));
Ramakant Singh21cec722014-03-07 19:11:45 +05301284 FbIndex =0;
1285}
1286
Ramakant Singh19ead272015-03-18 14:11:31 +05301287void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect,
1288 hwc_rect_t displayRect) {
Ramakant Singh21cec722014-03-07 19:11:45 +05301289 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1290 FbdirtyRect[FbIndex] = dirtyRect;
Ramakant Singh19ead272015-03-18 14:11:31 +05301291 FbdisplayRect[FbIndex] = displayRect;
Ramakant Singh21cec722014-03-07 19:11:45 +05301292 FbIndex++;
1293}
1294
Ramakant Singh19ead272015-03-18 14:11:31 +05301295int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect,
1296 hwc_rect_t displayRect){
Ramakant Singh21cec722014-03-07 19:11:45 +05301297 int sameDirtyCount = 0;
1298 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
Ramakant Singh19ead272015-03-18 14:11:31 +05301299 if( FbdirtyRect[i] == dirtyRect &&
1300 FbdisplayRect[i] == displayRect)
Ramakant Singh21cec722014-03-07 19:11:45 +05301301 sameDirtyCount++;
1302 }
1303 return sameDirtyCount;
1304}
1305
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001306}; //namespace qhwc