blob: 2799d51ddcb0d7c10d658eab8a8c1af39c71795b [file] [log] [blame]
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -08003 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07004 *
radhakrishna2f00c8f2013-10-21 16:49:21 +05305 * Not a Contribution.
Naseer Ahmed31da0b12012-07-31 18:55:33 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Naseer Ahmed45a99602012-07-31 19:15:24 -070020#define DEBUG_COPYBIT 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070021#include <copybit.h>
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080022#include <utils/Timers.h>
Terence Hampson0124cc72013-04-18 23:45:56 -070023#include <mdp_version.h>
Naseer Ahmed45a99602012-07-31 19:15:24 -070024#include "hwc_copybit.h"
25#include "comptype.h"
Naseer Ahmed64b81212013-02-14 10:29:47 -050026#include "gr.h"
radhakrishna2f00c8f2013-10-21 16:49:21 +053027#include "cb_utils.h"
Ramakant Singh762ae082013-11-28 18:45:34 +053028#include "cb_swap_rect.h"
Ramakant Singh80f36f22014-02-25 14:11:35 +053029#include "math.h"
Saurabh Shah74eff4d2014-03-28 16:33:13 -070030#include "sync/sync.h"
31
radhakrishna2f00c8f2013-10-21 16:49:21 +053032using namespace qdutils;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070033namespace qhwc {
34
Naseer Ahmed31da0b12012-07-31 18:55:33 -070035struct range {
36 int current;
37 int end;
38};
39struct region_iterator : public copybit_region_t {
40
41 region_iterator(hwc_region_t region) {
42 mRegion = region;
Praveena Pachipulusud9443c72014-02-17 10:42:28 +053043 r.end = (int)region.numRects;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070044 r.current = 0;
45 this->next = iterate;
46 }
47
48private:
49 static int iterate(copybit_region_t const * self, copybit_rect_t* rect){
50 if (!self || !rect) {
51 ALOGE("iterate invalid parameters");
52 return 0;
53 }
54
55 region_iterator const* me =
56 static_cast<region_iterator const*>(self);
57 if (me->r.current != me->r.end) {
58 rect->l = me->mRegion.rects[me->r.current].left;
59 rect->t = me->mRegion.rects[me->r.current].top;
60 rect->r = me->mRegion.rects[me->r.current].right;
61 rect->b = me->mRegion.rects[me->r.current].bottom;
62 me->r.current++;
63 return 1;
64 }
65 return 0;
66 }
67
68 hwc_region_t mRegion;
69 mutable range r;
70};
71
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080072void CopyBit::reset() {
73 mIsModeOn = false;
74 mCopyBitDraw = false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070075}
76
Naseer Ahmed45a99602012-07-31 19:15:24 -070077bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
78 // return true for non-overlay targets
Terence Hampsond0fd5792013-05-29 11:56:52 -040079 if(ctx->mMDP.hasOverlay && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070080 return false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070081 }
82 return true;
83}
Naseer Ahmed45a99602012-07-31 19:15:24 -070084
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080085bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
86 hwc_display_contents_1_t *list,
87 int dpy) {
88 int compositionType = qdutils::QCCompositionType::
89 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -070090
Naseer Ahmed45a99602012-07-31 19:15:24 -070091 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
92 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053093 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070094 // this is done based on perf inputs in ICS
95 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080096 int fbWidth = ctx->dpyAttr[dpy].xres;
97 int fbHeight = ctx->dpyAttr[dpy].yres;
98 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -070099 unsigned int renderArea = getRGBRenderingArea(list);
100 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
101 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530102 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700103 return true;
104 }
105 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
106 // MDP composition, use COPYBIT always
107 return true;
108 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
109 // C2D composition, use COPYBIT
110 return true;
111 }
112 return false;
113}
114
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800115unsigned int CopyBit::getRGBRenderingArea
116 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700117 //Calculates total rendering area for RGB layers
118 unsigned int renderArea = 0;
119 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400120 // Skipping last layer since FrameBuffer layer should not affect
121 // which composition to choose
122 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700123 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
124 if (hnd) {
125 if (BUFFER_TYPE_UI == hnd->bufferType) {
126 getLayerResolution(&list->hwLayers[i], w, h);
127 renderArea += (w*h);
128 }
129 }
130 }
131 return renderArea;
132}
133
Ramakant Singh21cec722014-03-07 19:11:45 +0530134int CopyBit::getLayersChanging(hwc_context_t *ctx,
135 hwc_display_contents_1_t *list,
136 int dpy){
137
138 int changingLayerIndex = -1;
139 if(mLayerCache.layerCount != ctx->listStats[dpy].numAppLayers) {
140 mLayerCache.reset();
141 mFbCache.reset();
142 mLayerCache.updateCounts(ctx,list,dpy);
143 return -1;
144 }
145
146 int updatingLayerCount = 0;
147 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--){
148 //swap rect will kick in only for single updating layer
149 if(mLayerCache.hnd[k] != list->hwLayers[k].handle){
150 updatingLayerCount ++;
151 if(updatingLayerCount == 1)
152 changingLayerIndex = k;
153 }
154 }
155 //since we are using more than one framebuffers,we have to
156 //kick in swap rect only if we are getting continuous same
157 //dirty rect for same layer at least equal of number of
158 //framebuffers
159
160 if ( updatingLayerCount == 1 ) {
Saurabh Shah93c69052014-04-24 10:27:10 -0700161 hwc_rect_t dirtyRect = list->hwLayers[changingLayerIndex].displayFrame;
162#ifdef QCOM_BSP
163 dirtyRect = list->hwLayers[changingLayerIndex].dirtyRect;
164#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530165
166 for (int k = ctx->listStats[dpy].numAppLayers-1; k >= 0 ; k--){
167 //disable swap rect for overlapping visible layer(s)
168 hwc_rect_t displayFrame = list->hwLayers[k].displayFrame;
169 hwc_rect_t result = getIntersection(displayFrame,dirtyRect);
170 if((k != changingLayerIndex) && isValidRect(result)){
171 return -1;
172 }
173 }
174 mFbCache.insertAndUpdateFbCache(dirtyRect);
175 if(mFbCache.getUnchangedFbDRCount(dirtyRect) <
176 NUM_RENDER_BUFFERS)
177 changingLayerIndex = -1;
178 }else {
179 mFbCache.reset();
180 changingLayerIndex = -1;
181 }
182 mLayerCache.updateCounts(ctx,list,dpy);
183 return changingLayerIndex;
184}
185
186int CopyBit::checkDirtyRect(hwc_context_t *ctx,
187 hwc_display_contents_1_t *list,
188 int dpy) {
189
190 //dirty rect will enable only if
191 //1.Only single layer is updating.
192 //2.No overlapping
193 //3.No scaling
194 //4.No video layer
195 if(mSwapRectEnable == false)
196 return -1;
197 int changingLayerIndex = getLayersChanging(ctx, list, dpy);
198 //swap rect will kick in only for single updating layer
199 if(changingLayerIndex == -1){
200 return -1;
201 }
202 if(!needsScaling(&list->hwLayers[changingLayerIndex])){
203 private_handle_t *hnd =
204 (private_handle_t *)list->hwLayers[changingLayerIndex].handle;
205 if( hnd && !isYuvBuffer(hnd))
206 return changingLayerIndex;
207 }
208 return -1;
209}
210
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700211bool CopyBit::prepareOverlap(hwc_context_t *ctx,
212 hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700213
214 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
215 ALOGE("%s: Invalid request", __FUNCTION__);
216 return false;
217 }
218
219 if (mEngine == NULL || !(validateParams(ctx, list))) {
220 ALOGE("%s: Invalid Params", __FUNCTION__);
221 return false;
222 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700223 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700224
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700225 // Allocate render buffers if they're not allocated
226 int alignW = 0, alignH = 0;
227 int finalW = 0, finalH = 0;
228 for (int i = 0; i < ptorInfo->count; i++) {
229 int ovlapIndex = ptorInfo->layerIndex[i];
230 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
231 // render buffer width will be the max of two layers
232 // Align Widht and height to 32, Mdp would be configured
233 // with Aligned overlap w/h
234 finalW = max(finalW, ALIGN((overlap.right - overlap.left), 32));
235 finalH += ALIGN((overlap.bottom - overlap.top), 32);
236 if(finalH > ALIGN((overlap.bottom - overlap.top), 32)) {
237 // Calculate the offset for RGBA(4BPP)
238 ptorInfo->mRenderBuffOffset[i] = finalW *
239 (finalH - ALIGN((overlap.bottom - overlap.top), 32)) * 4;
240 // Calculate the dest top, left will always be zero
241 ptorInfo->displayFrame[i].top = (finalH -
242 (ALIGN((overlap.bottom - overlap.top), 32)));
243 }
244 // calculate the right and bottom values
245 ptorInfo->displayFrame[i].right = ptorInfo->displayFrame[i].left +
246 (overlap.right - overlap.left);
247 ptorInfo->displayFrame[i].bottom = ptorInfo->displayFrame[i].top +
248 (overlap.bottom - overlap.top);
249 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700250
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700251 getBufferSizeAndDimensions(finalW, finalH, HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -0700252 alignW, alignH);
253
254 if ((mAlignedWidth != alignW) || (mAlignedHeight != alignH)) {
255 // Overlap rect has changed, so free render buffers
256 freeRenderBuffers();
257 }
258
259 int ret = allocRenderBuffers(alignW, alignH, HAL_PIXEL_FORMAT_RGBA_8888);
260
261 if (ret < 0) {
262 ALOGE("%s: Render buffer allocation failed", __FUNCTION__);
263 return false;
264 }
265
266 mAlignedWidth = alignW;
267 mAlignedHeight = alignH;
268 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
269 return true;
270}
271
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800272bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
273 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700274
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800275 if(mEngine == NULL) {
276 // No copybit device found - cannot use copybit
277 return false;
278 }
279 int compositionType = qdutils::QCCompositionType::
280 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700281
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800282 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
283 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700284 //GPU/CPU composition, don't change layer composition type
285 return true;
286 }
287
Naseer Ahmed45a99602012-07-31 19:15:24 -0700288 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800289 ALOGE("%s:Invalid Params", __FUNCTION__);
290 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700291 }
292
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800293 if(ctx->listStats[dpy].skipCount) {
294 //GPU will be anyways used
295 return false;
296 }
297
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700298 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700299 // Reached max layers supported by HWC.
300 return false;
301 }
302
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800303 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
304 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500305 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500306
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530307 // Following are MDP3 limitations for which we
308 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400309 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500310 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400311 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
312 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500313 int dst_h, dst_w, src_h, src_w;
314 float dx, dy;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400315 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530316 if (layer->planeAlpha != 0xFF)
317 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530318 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500319
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700320 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530321 src_h = sourceCrop.right - sourceCrop.left;
322 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500323 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530324 src_h = sourceCrop.bottom - sourceCrop.top;
325 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500326 }
327 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
328 dst_w = layer->displayFrame.right - layer->displayFrame.left;
329
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530330 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
331 ALOGE("%s: wrong params for display screen_w=%d \
332 src_crop_width=%d screen_h=%d src_crop_height=%d",
333 __FUNCTION__, dst_w,src_w,dst_h,src_h);
334 return false;
335 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530336 dx = (float)dst_w/(float)src_w;
337 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530338
Terence Hampson6b0a4272013-11-06 16:04:51 -0500339 if (dx > MAX_SCALE_FACTOR || dx < MIN_SCALE_FACTOR)
340 return false;
341
342 if (dy > MAX_SCALE_FACTOR || dy < MIN_SCALE_FACTOR)
343 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400344 }
345 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500346
347 //Allocate render buffers if they're not allocated
Terence Hampsonc67b0372013-10-09 11:32:21 -0400348 if (ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
349 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700350 int ret = allocRenderBuffers(mAlignedWidth,
351 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700352 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500353 if (ret < 0) {
354 return false;
355 } else {
356 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
357 NUM_RENDER_BUFFERS;
358 }
359 }
360
Terence Hampsond0fd5792013-05-29 11:56:52 -0400361 // We cannot mix copybit layer with layers marked to be drawn on FB
362 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
363 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800364
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530365 mCopyBitDraw = false;
366 if (useCopybitForRGB &&
367 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
368 mCopyBitDraw = true;
369 // numAppLayers-1, as we iterate till 0th layer index
370 // Mark all layers to be drawn by copybit
371 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500372 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700373#ifdef QCOM_BSP
Terence Hampsonc67b0372013-10-09 11:32:21 -0400374 if (ctx->mMDP.version == qdutils::MDP_V3_0_4)
375 list->hwLayers[i].compositionType = HWC_BLIT;
376 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700377#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400378 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700379 }
380 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530381
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700382 return true;
383}
384
Naseer Ahmed183939d2013-03-14 20:44:17 -0400385int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
386{
387 int ret = 0;
388 copybit_rect_t clear_rect = {rect.left, rect.top,
389 rect.right,
390 rect.bottom};
391
392 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700393 buf.w = ALIGN(getWidth(hnd),32);
394 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400395 buf.format = hnd->format;
396 buf.base = (void *)hnd->base;
397 buf.handle = (native_handle_t *)hnd;
398
399 copybit_device_t *copybit = mEngine;
400 ret = copybit->clear(copybit, &buf, &clear_rect);
401 return ret;
402}
403
Ramakant Singh0def28c2014-03-28 20:43:13 +0530404bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
405 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530406 int dpy, int *copybitFd) {
407 int layerCount = 0;
Shalaj Jaina70b4352014-06-15 13:47:47 -0700408 uint32_t last = (uint32_t)list->numHwLayers - 1;
Ramakant Singh467759f2014-04-16 11:09:40 +0530409 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
410 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530411
412 if(ctx->enableABC == false)
413 return false;
414
Ramakant Singh467759f2014-04-16 11:09:40 +0530415 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530416 return false;
417
418 layerCount = ctx->listStats[dpy].numAppLayers;
419 //bottom most layer should
420 //equal to FB
421 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
422 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
423 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
424 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
425 if(tmpLayer->transform ||
426 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
427 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
428 (needsScaling(tmpLayer) == true)) {
429 return false;
430 }else {
431 ctx->listStats[dpy].renderBufIndexforABC = 0;
432 }
433 }
434
Ramakant Singh467759f2014-04-16 11:09:40 +0530435 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
436 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530437 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530438
439 if(layerCount == MAX_LAYERS_FOR_ABC) {
440 // Pass the Acquire Fence FD to driver for base layer
441 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
442 private_handle_t *renderBuffer =
443 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
444 copybit_device_t *copybit = getCopyBitDevice();
445 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
446 copybit->set_sync(copybit,
447 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
448 }
449 for(int i = 1; i < layerCount; i++){
450 int retVal = drawLayerUsingCopybit(ctx,
451 &(list->hwLayers[i]),renderBuffer, 0);
452 if(retVal < 0) {
453 ALOGE("%s : Copybit failed", __FUNCTION__);
454 }
455 }
456 // Get Release Fence FD of copybit for the App layer(s)
457 copybit->flush_get_fence(copybit, copybitFd);
458 close(list->hwLayers[abcRenderBufIdx].acquireFenceFd);
459 list->hwLayers[abcRenderBufIdx].acquireFenceFd = -1;
460 return true;
461 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530462 }
463 return false;
464}
465
466bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
467 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800468 // draw layers marked for COPYBIT
469 int retVal = true;
470 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400471 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500472 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400473 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800474
Ramakant Singh21cec722014-03-07 19:11:45 +0530475 if(mCopyBitDraw == false){
476 mFbCache.reset(); // there is no layer marked for copybit
477 return false ;
478 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530479
Ramakant Singh467759f2014-04-16 11:09:40 +0530480 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530481 return true;
482 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800483 //render buffer
Terence Hampsonc67b0372013-10-09 11:32:21 -0400484 if (ctx->mMDP.version == qdutils::MDP_V3_0_4) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530485 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400486 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
487 } else {
488 renderBuffer = getCurrentRenderBuffer();
489 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800490 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500491 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800492 return false;
493 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500494
Terence Hampson0124cc72013-04-18 23:45:56 -0700495 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400496 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530497 if(mRelFd[mCurRenderBufferIndex] >=0) {
498 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
499 close(mRelFd[mCurRenderBufferIndex]);
500 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400501 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400502 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400503 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400504 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400505 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400506 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700507 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530508
Ramakant Singh21cec722014-03-07 19:11:45 +0530509 mDirtyLayerIndex = checkDirtyRect(ctx, list, dpy);
510 if( mDirtyLayerIndex != -1){
511 hwc_layer_1_t *layer = &list->hwLayers[mDirtyLayerIndex];
Saurabh Shah93c69052014-04-24 10:27:10 -0700512#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530513 clear(renderBuffer,layer->dirtyRect);
Saurabh Shah93c69052014-04-24 10:27:10 -0700514#else
515 clear(renderBuffer,layer->displayFrame);
516#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530517 } else {
518 hwc_rect_t clearRegion = {0,0,0,0};
519 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
520 clear(renderBuffer, clearRegion);
521 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530522
Naseer Ahmed64b81212013-02-14 10:29:47 -0500523 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800524 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500525 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
526 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800527 continue;
528 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530529 //skip non updating layers
530 if((mDirtyLayerIndex != -1) && (mDirtyLayerIndex != i) )
Ramakant Singh762ae082013-11-28 18:45:34 +0530531 continue;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800532 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400533 if (list->hwLayers[i].acquireFenceFd != -1
534 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800535 // Wait for acquire Fence on the App buffers.
536 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
537 if(ret < 0) {
538 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
539 __FUNCTION__, errno, strerror(errno));
540 }
541 close(list->hwLayers[i].acquireFenceFd);
542 list->hwLayers[i].acquireFenceFd = -1;
543 }
544 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530545 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800546 copybitLayerCount++;
547 if(retVal < 0) {
548 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
549 }
550 }
551
552 if (copybitLayerCount) {
553 copybit_device_t *copybit = getCopyBitDevice();
554 // Async mode
555 copybit->flush_get_fence(copybit, fd);
Terence Hampsonc67b0372013-10-09 11:32:21 -0400556 if(ctx->mMDP.version == qdutils::MDP_V3_0_4 &&
557 list->hwLayers[last].acquireFenceFd >= 0) {
558 close(list->hwLayers[last].acquireFenceFd);
559 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400560 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800561 }
562 return true;
563}
564
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700565int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700566 int fd = -1;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700567 PtorInfo* ptorInfo = &(ctx->mPtorInfo);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700568
569 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
570 ALOGE("%s: Invalid request", __FUNCTION__);
571 return fd;
572 }
573
574 private_handle_t *renderBuffer = getCurrentRenderBuffer();
575
576 if (!renderBuffer) {
577 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
578 return fd;
579 }
580
581 int copybitLayerCount = 0;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700582 for(int j = 0; j < ptorInfo->count; j++) {
583 int ovlapIndex = ptorInfo->layerIndex[j];
584 hwc_rect_t overlap = list->hwLayers[ovlapIndex].displayFrame;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700585
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700586 // Draw overlapped content of layers on render buffer
587 for (int i = 0; i <= ovlapIndex; i++) {
588 hwc_layer_1_t *layer = &list->hwLayers[i];
589 if(!isValidRect(getIntersection(layer->displayFrame,
590 overlap))) {
591 continue;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700592 }
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700593 if ((list->hwLayers[i].acquireFenceFd != -1)) {
594 // Wait for acquire fence on the App buffers.
595 if(sync_wait(list->hwLayers[i].acquireFenceFd, 1000) < 0) {
596 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
597 __FUNCTION__, errno, strerror(errno));
598 }
599 close(list->hwLayers[i].acquireFenceFd);
600 list->hwLayers[i].acquireFenceFd = -1;
601 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700602
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700603 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer, overlap,
604 ptorInfo->displayFrame[j]);
605 copybitLayerCount++;
606 if(retVal < 0) {
607 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
608 copybitLayerCount = 0;
609 }
Sushil Chauhandefd3522014-05-13 18:17:12 -0700610 }
611 }
612
613 if (copybitLayerCount) {
614 copybit_device_t *copybit = getCopyBitDevice();
615 copybit->flush_get_fence(copybit, &fd);
616 }
617
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700618 ALOGD_IF(DEBUG_COPYBIT, "%s: done! copybitLayerCount = %d", __FUNCTION__,
619 copybitLayerCount);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700620 return fd;
621}
622
623int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700624 private_handle_t *renderBuffer, hwc_rect_t overlap,
625 hwc_rect_t destRect)
Sushil Chauhandefd3522014-05-13 18:17:12 -0700626{
627 hwc_context_t* ctx = (hwc_context_t*)(dev);
628 if (!ctx) {
629 ALOGE("%s: null context ", __FUNCTION__);
630 return -1;
631 }
632
633 private_handle_t *hnd = (private_handle_t *)layer->handle;
634 if (!hnd) {
635 ALOGE("%s: invalid handle", __FUNCTION__);
636 return -1;
637 }
638
639 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
640 if (!dstHandle) {
641 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
642 return -1;
643 }
644
645 // Set the Copybit Source
646 copybit_image_t src;
647 src.handle = (native_handle_t *)layer->handle;
648 src.w = hnd->width;
649 src.h = hnd->height;
650 src.base = (void *)hnd->base;
651 src.format = hnd->format;
652 src.horiz_padding = 0;
653 src.vert_padding = 0;
654
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700655
Sushil Chauhandefd3522014-05-13 18:17:12 -0700656 hwc_rect_t dispFrame = layer->displayFrame;
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700657 hwc_rect_t iRect = getIntersection(dispFrame, overlap);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700658 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700659 qhwc::calculate_crop_rects(crop, dispFrame, iRect,
660 layer->transform);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700661
662 // Copybit source rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700663 copybit_rect_t srcRect = {crop.left, crop.top, crop.right,
664 crop.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700665
666 // Copybit destination rect
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700667 copybit_rect_t dstRect = {destRect.left, destRect.top, destRect.right,
668 destRect.bottom};
Sushil Chauhandefd3522014-05-13 18:17:12 -0700669
670 // Copybit dst
671 copybit_image_t dst;
672 dst.handle = (native_handle_t *)dstHandle;
673 dst.w = ALIGN(dstHandle->width, 32);
674 dst.h = dstHandle->height;
675 dst.base = (void *)dstHandle->base;
676 dst.format = dstHandle->format;
677
678 copybit_device_t *copybit = mEngine;
679
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700680 // Copybit region is the destRect
681 hwc_rect_t regRect = {dstRect.l,dstRect.t, dstRect.r, dstRect.b};
682 hwc_region_t region;
683 region.numRects = 1;
684 region.rects = &regRect;
Sushil Chauhandefd3522014-05-13 18:17:12 -0700685 region_iterator copybitRegion(region);
686 int acquireFd = layer->acquireFenceFd;
687
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700688 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
689 renderBuffer->width);
690 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
691 renderBuffer->height);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700692 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
693 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
694 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
695 copybit->set_parameter(copybit, COPYBIT_DITHER,
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700696 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE :
697 COPYBIT_DISABLE);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700698 copybit->set_sync(copybit, acquireFd);
Arun Kumar K.Rb2a03b12014-06-03 11:54:10 -0700699 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
700 &copybitRegion);
Sushil Chauhandefd3522014-05-13 18:17:12 -0700701
702 if (err < 0)
703 ALOGE("%s: copybit stretch failed",__FUNCTION__);
704
705 return err;
706}
707
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700708int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800709 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700710{
711 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400712 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700713 if(!ctx) {
714 ALOGE("%s: null context ", __FUNCTION__);
715 return -1;
716 }
717
718 private_handle_t *hnd = (private_handle_t *)layer->handle;
719 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700720 if (layer->flags & HWC_COLOR_FILL) { // Color layer
721 return fillColorUsingCopybit(layer, renderBuffer);
722 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700723 ALOGE("%s: invalid handle", __FUNCTION__);
724 return -1;
725 }
726
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800727 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700728 if(!fbHandle) {
729 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700730 return -1;
731 }
732
733 // Set the copybit source:
734 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700735 src.w = getWidth(hnd);
736 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700737 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700738
739 // Handle R/B swap
740 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
741 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
742 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
743 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
744 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
745 }
746 }
747
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700748 src.base = (void *)hnd->base;
749 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700750 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700751 // Initialize vertical padding to zero for now,
752 // this needs to change to accomodate vertical stride
753 // if needed in the future
754 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700755
shuoy7c101f92013-08-26 14:48:57 +0800756 int layerTransform = layer->transform ;
757 // When flip and rotation(90) are present alter the flip,
758 // as GPU is doing the flip and rotation in opposite order
759 // to that of MDP3.0
760 // For 270 degrees, we get 90 + (H+V) which is same as doing
761 // flip first and then rotation (H+V) + 90
762 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
763 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
764 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
765 (layer->transform & HAL_TRANSFORM_ROT_90) &&
766 !(layer->transform == HAL_TRANSFORM_ROT_270)){
767 if(layer->transform & HAL_TRANSFORM_FLIP_H){
768 layerTransform ^= HAL_TRANSFORM_FLIP_H;
769 layerTransform |= HAL_TRANSFORM_FLIP_V;
770 }
771 if(layer->transform & HAL_TRANSFORM_FLIP_V){
772 layerTransform ^= HAL_TRANSFORM_FLIP_V;
773 layerTransform |= HAL_TRANSFORM_FLIP_H;
774 }
775 }
776 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700777 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700778 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700779 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
780 sourceCrop.right,
781 sourceCrop.bottom};
782
783 // Copybit destination rect
784 hwc_rect_t displayFrame = layer->displayFrame;
785 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
786 displayFrame.right,
787 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700788#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530789 //change src and dst with dirtyRect
790 if(mDirtyLayerIndex != -1) {
791 srcRect.l = layer->dirtyRect.left;
792 srcRect.t = layer->dirtyRect.top;
793 srcRect.r = layer->dirtyRect.right;
794 srcRect.b = layer->dirtyRect.bottom;
795 dstRect = srcRect;
796 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700797#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700798 // Copybit dst
799 copybit_image_t dst;
800 dst.w = ALIGN(fbHandle->width,32);
801 dst.h = fbHandle->height;
802 dst.format = fbHandle->format;
803 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800804 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700805
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800806 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700807
808 int32_t screen_w = displayFrame.right - displayFrame.left;
809 int32_t screen_h = displayFrame.bottom - displayFrame.top;
810 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
811 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
812
813 // Copybit dst
814 float copybitsMaxScale =
815 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
816 float copybitsMinScale =
817 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
818
Sushil Chauhandffbf432013-12-09 15:33:57 -0800819 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700820 //swap screen width and height
821 int tmp = screen_w;
822 screen_w = screen_h;
823 screen_h = tmp;
824 }
825 private_handle_t *tmpHnd = NULL;
826
827 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
828 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530829 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
830 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700831 return -1;
832 }
833
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530834 float dsdx = (float)screen_w/(float)src_crop_width;
835 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700836
837 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
838 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
839 if(dsdx > scaleLimitMax ||
840 dtdy > scaleLimitMax ||
841 dsdx < 1/scaleLimitMin ||
842 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400843 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700844 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
845 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700846 return -1;
847 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400848 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700849 if(dsdx > copybitsMaxScale ||
850 dtdy > copybitsMaxScale ||
851 dsdx < 1/copybitsMinScale ||
852 dtdy < 1/copybitsMinScale){
853 // The requested scale is out of the range the hardware
854 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400855 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700856 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
857 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
858 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
859 src_crop_width,src_crop_height);
860
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700861
862 int tmp_w = src_crop_width;
863 int tmp_h = src_crop_height;
864
865 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530866 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
867 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700868 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
Ramakant Singh80f36f22014-02-25 14:11:35 +0530869 // ceil the tmp_w and tmp_h value to maintain proper ratio
870 // b/w src and dst (should not cross the desired scale limit
871 // due to float -> int )
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530872 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
873 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700874 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400875 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700876
Naseer Ahmed64b81212013-02-14 10:29:47 -0500877 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400878 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700879
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400880 // We do not want copybit to generate alpha values from nothing
881 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
882 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
883 format = HAL_PIXEL_FORMAT_RGBX_8888;
884 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800885 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700886 copybit_image_t tmp_dst;
887 copybit_rect_t tmp_rect;
888 tmp_dst.w = tmp_w;
889 tmp_dst.h = tmp_h;
890 tmp_dst.format = tmpHnd->format;
891 tmp_dst.handle = tmpHnd;
892 tmp_dst.horiz_padding = src.horiz_padding;
893 tmp_dst.vert_padding = src.vert_padding;
894 tmp_rect.l = 0;
895 tmp_rect.t = 0;
896 tmp_rect.r = tmp_dst.w;
897 tmp_rect.b = tmp_dst.h;
898 //create one clip region
899 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
900 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
901 region_iterator tmp_it(tmp_hwc_reg);
902 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700903 //TODO: once, we are able to read layer alpha, update this
904 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400905 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700906 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
907 &srcRect, &tmp_it);
908 if(err < 0){
909 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
910 __LINE__);
911 if(tmpHnd)
912 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700913 return err;
914 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400915 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400916 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400917 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400918 close(acquireFd);
919 acquireFd = -1;
920 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700921 // copy new src and src rect crop
922 src = tmp_dst;
923 srcRect = tmp_rect;
924 }
925 }
926 // Copybit region
927 hwc_region_t region = layer->visibleRegionScreen;
928 region_iterator copybitRegion(region);
929
930 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
931 renderBuffer->width);
932 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
933 renderBuffer->height);
934 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800935 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700936 //TODO: once, we are able to read layer alpha, update this
937 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800938 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
939 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700940 copybit->set_parameter(copybit, COPYBIT_DITHER,
941 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
942 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530943 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
944 COPYBIT_ENABLE : COPYBIT_DISABLE);
945
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700946 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
947 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400948 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700949 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
950 &copybitRegion);
951 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
952 COPYBIT_DISABLE);
953
Terence Hampsonadf47302013-05-23 12:21:02 -0400954 if(tmpHnd) {
955 if (ctx->mMDP.version < qdutils::MDP_V4_0){
956 int ret = -1, releaseFd;
957 // we need to wait for the buffer before freeing
958 copybit->flush_get_fence(copybit, &releaseFd);
959 ret = sync_wait(releaseFd, 1000);
960 if(ret < 0) {
961 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
962 __FUNCTION__, errno, strerror(errno));
963 }
964 close(releaseFd);
965 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700966 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400967 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700968
969 if(err < 0)
970 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700971 return err;
972}
973
Sushil Chauhan943797c2013-10-21 17:35:55 -0700974int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
975 private_handle_t *renderBuffer)
976{
977 if (!renderBuffer) {
978 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
979 return -1;
980 }
981
982 // Copybit dst
983 copybit_image_t dst;
984 dst.w = ALIGN(renderBuffer->width, 32);
985 dst.h = renderBuffer->height;
986 dst.format = renderBuffer->format;
987 dst.base = (void *)renderBuffer->base;
988 dst.handle = (native_handle_t *)renderBuffer;
989
990 // Copybit dst rect
991 hwc_rect_t displayFrame = layer->displayFrame;
992 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
993 displayFrame.right, displayFrame.bottom};
994
995 uint32_t color = layer->transform;
996 copybit_device_t *copybit = mEngine;
997 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
998 renderBuffer->width);
999 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
1000 renderBuffer->height);
1001 copybit->set_parameter(copybit, COPYBIT_DITHER,
1002 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
1003 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -08001004 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -07001005 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
1006 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
1007 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
1008 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
1009 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
1010 return res;
1011}
1012
Naseer Ahmed5b6708a2012-08-02 13:46:08 -07001013void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -07001014 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001015{
1016 hwc_rect_t displayFrame = layer->displayFrame;
1017
1018 width = displayFrame.right - displayFrame.left;
1019 height = displayFrame.bottom - displayFrame.top;
1020}
1021
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001022bool CopyBit::validateParams(hwc_context_t *ctx,
1023 const hwc_display_contents_1_t *list) {
1024 //Validate parameters
1025 if (!ctx) {
1026 ALOGE("%s:Invalid HWC context", __FUNCTION__);
1027 return false;
1028 } else if (!list) {
1029 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
1030 return false;
1031 }
1032 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001033}
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001034
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001035
Naseer Ahmed64b81212013-02-14 10:29:47 -05001036int CopyBit::allocRenderBuffers(int w, int h, int f)
1037{
1038 int ret = 0;
1039 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1040 if (mRenderBuffer[i] == NULL) {
1041 ret = alloc_buffer(&mRenderBuffer[i],
1042 w, h, f,
1043 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
1044 }
1045 if(ret < 0) {
1046 freeRenderBuffers();
1047 break;
1048 }
1049 }
1050 return ret;
1051}
1052
1053void CopyBit::freeRenderBuffers()
1054{
1055 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1056 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301057 //Since we are freeing buffer close the fence if it has a valid one.
1058 if(mRelFd[i] >= 0) {
1059 close(mRelFd[i]);
1060 mRelFd[i] = -1;
1061 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001062 free_buffer(mRenderBuffer[i]);
1063 mRenderBuffer[i] = NULL;
1064 }
1065 }
1066}
1067
1068private_handle_t * CopyBit::getCurrentRenderBuffer() {
1069 return mRenderBuffer[mCurRenderBufferIndex];
1070}
1071
1072void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301073 if(mRelFd[mCurRenderBufferIndex] >=0)
1074 close(mRelFd[mCurRenderBufferIndex]);
1075 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001076}
1077
Sushil Chauhandefd3522014-05-13 18:17:12 -07001078void CopyBit::setReleaseFdSync(int fd) {
1079 if (mRelFd[mCurRenderBufferIndex] >=0) {
1080 int ret = -1;
1081 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1082 if (ret < 0)
1083 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1084 __FUNCTION__, errno, strerror(errno));
1085 close(mRelFd[mCurRenderBufferIndex]);
1086 }
1087 mRelFd[mCurRenderBufferIndex] = dup(fd);
1088}
1089
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001090struct copybit_device_t* CopyBit::getCopyBitDevice() {
1091 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001092}
1093
Shalaj Jaina70b4352014-06-15 13:47:47 -07001094CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mEngine(0),
1095 mIsModeOn(false), mCopyBitDraw(false), mCurRenderBufferIndex(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001096
1097 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1098 ctx->dpyAttr[dpy].yres,
1099 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001100 mAlignedWidth,
1101 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001102
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001103 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301104 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001105 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301106 mRelFd[i] = -1;
1107 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301108
1109 char value[PROPERTY_VALUE_MAX];
1110 property_get("debug.hwc.dynThreshold", value, "2");
1111 mDynThreshold = atof(value);
1112
Ramakant Singh21cec722014-03-07 19:11:45 +05301113 property_get("debug.sf.swaprect", value, "0");
1114 mSwapRectEnable = atoi(value) ? true:false ;
1115 mDirtyLayerIndex = -1;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001116 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001117 if(copybit_open(module, &mEngine) < 0) {
1118 ALOGE("FATAL ERROR: copybit open failed.");
1119 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001120 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001121 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001122 }
1123}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001124
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001125CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001126{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001127 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001128 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001129 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001130 copybit_close(mEngine);
1131 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001132 }
1133}
Ramakant Singh21cec722014-03-07 19:11:45 +05301134CopyBit::LayerCache::LayerCache() {
1135 reset();
1136}
1137void CopyBit::LayerCache::reset() {
1138 memset(&hnd, 0, sizeof(hnd));
1139 layerCount = 0;
1140}
1141void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1142 hwc_display_contents_1_t *list, int dpy)
1143{
1144 layerCount = ctx->listStats[dpy].numAppLayers;
1145 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1146 hnd[i] = list->hwLayers[i].handle;
1147 }
1148}
1149
1150CopyBit::FbCache::FbCache() {
1151 reset();
1152}
1153void CopyBit::FbCache::reset() {
1154 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
1155 FbIndex =0;
1156}
1157
1158void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect) {
1159 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1160 FbdirtyRect[FbIndex] = dirtyRect;
1161 FbIndex++;
1162}
1163
1164int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect){
1165 int sameDirtyCount = 0;
1166 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
1167 if( FbdirtyRect[i] == dirtyRect)
1168 sameDirtyCount++;
1169 }
1170 return sameDirtyCount;
1171}
1172
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001173}; //namespace qhwc