blob: f703fa0930f5171b1917c62fe553b6cbb08ced94 [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
Sushil Chauhandefd3522014-05-13 18:17:12 -0700211bool CopyBit::prepareOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list,
212 int overlapIndex) {
213
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 }
223
224 //Allocate render buffers if they're not allocated
225 hwc_rect_t overlap = list->hwLayers[overlapIndex].displayFrame;
226 int width = overlap.right - overlap.left;
227 int height = overlap.bottom - overlap.top;
228 int alignW, alignH;
229
230 getBufferSizeAndDimensions(width, height, HAL_PIXEL_FORMAT_RGBA_8888,
231 alignW, alignH);
232
233 if ((mAlignedWidth != alignW) || (mAlignedHeight != alignH)) {
234 // Overlap rect has changed, so free render buffers
235 freeRenderBuffers();
236 }
237
238 int ret = allocRenderBuffers(alignW, alignH, HAL_PIXEL_FORMAT_RGBA_8888);
239
240 if (ret < 0) {
241 ALOGE("%s: Render buffer allocation failed", __FUNCTION__);
242 return false;
243 }
244
245 mAlignedWidth = alignW;
246 mAlignedHeight = alignH;
247 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
248 return true;
249}
250
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800251bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
252 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700253
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800254 if(mEngine == NULL) {
255 // No copybit device found - cannot use copybit
256 return false;
257 }
258 int compositionType = qdutils::QCCompositionType::
259 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700260
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800261 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
262 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700263 //GPU/CPU composition, don't change layer composition type
264 return true;
265 }
266
Naseer Ahmed45a99602012-07-31 19:15:24 -0700267 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800268 ALOGE("%s:Invalid Params", __FUNCTION__);
269 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700270 }
271
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800272 if(ctx->listStats[dpy].skipCount) {
273 //GPU will be anyways used
274 return false;
275 }
276
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700277 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700278 // Reached max layers supported by HWC.
279 return false;
280 }
281
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800282 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
283 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500284 LayerProp *layerProp = ctx->layerProp[dpy];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500285
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530286 // Following are MDP3 limitations for which we
287 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400288 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500289 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400290 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
291 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500292 int dst_h, dst_w, src_h, src_w;
293 float dx, dy;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400294 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530295 if (layer->planeAlpha != 0xFF)
296 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530297 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500298
Sushil Chauhanfda00fc2014-03-20 11:08:41 -0700299 if (has90Transform(layer)) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530300 src_h = sourceCrop.right - sourceCrop.left;
301 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500302 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530303 src_h = sourceCrop.bottom - sourceCrop.top;
304 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500305 }
306 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
307 dst_w = layer->displayFrame.right - layer->displayFrame.left;
308
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530309 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
310 ALOGE("%s: wrong params for display screen_w=%d \
311 src_crop_width=%d screen_h=%d src_crop_height=%d",
312 __FUNCTION__, dst_w,src_w,dst_h,src_h);
313 return false;
314 }
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530315 dx = (float)dst_w/(float)src_w;
316 dy = (float)dst_h/(float)src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530317
Terence Hampson6b0a4272013-11-06 16:04:51 -0500318 if (dx > MAX_SCALE_FACTOR || dx < MIN_SCALE_FACTOR)
319 return false;
320
321 if (dy > MAX_SCALE_FACTOR || dy < MIN_SCALE_FACTOR)
322 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400323 }
324 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500325
326 //Allocate render buffers if they're not allocated
Terence Hampsonc67b0372013-10-09 11:32:21 -0400327 if (ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
328 (useCopybitForYUV || useCopybitForRGB)) {
Sushil Chauhandefd3522014-05-13 18:17:12 -0700329 int ret = allocRenderBuffers(mAlignedWidth,
330 mAlignedHeight,
Saurabh Shah220a30c2013-10-29 16:12:12 -0700331 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500332 if (ret < 0) {
333 return false;
334 } else {
335 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
336 NUM_RENDER_BUFFERS;
337 }
338 }
339
Terence Hampsond0fd5792013-05-29 11:56:52 -0400340 // We cannot mix copybit layer with layers marked to be drawn on FB
341 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
342 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800343
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530344 mCopyBitDraw = false;
345 if (useCopybitForRGB &&
346 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
347 mCopyBitDraw = true;
348 // numAppLayers-1, as we iterate till 0th layer index
349 // Mark all layers to be drawn by copybit
350 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500351 layerProp[i].mFlags |= HWC_COPYBIT;
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700352#ifdef QCOM_BSP
Terence Hampsonc67b0372013-10-09 11:32:21 -0400353 if (ctx->mMDP.version == qdutils::MDP_V3_0_4)
354 list->hwLayers[i].compositionType = HWC_BLIT;
355 else
Saurabh Shah74eff4d2014-03-28 16:33:13 -0700356#endif
Terence Hampsonc67b0372013-10-09 11:32:21 -0400357 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700358 }
359 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530360
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700361 return true;
362}
363
Naseer Ahmed183939d2013-03-14 20:44:17 -0400364int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
365{
366 int ret = 0;
367 copybit_rect_t clear_rect = {rect.left, rect.top,
368 rect.right,
369 rect.bottom};
370
371 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700372 buf.w = ALIGN(getWidth(hnd),32);
373 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400374 buf.format = hnd->format;
375 buf.base = (void *)hnd->base;
376 buf.handle = (native_handle_t *)hnd;
377
378 copybit_device_t *copybit = mEngine;
379 ret = copybit->clear(copybit, &buf, &clear_rect);
380 return ret;
381}
382
Ramakant Singh0def28c2014-03-28 20:43:13 +0530383bool CopyBit::drawUsingAppBufferComposition(hwc_context_t *ctx,
384 hwc_display_contents_1_t *list,
Ramakant Singh467759f2014-04-16 11:09:40 +0530385 int dpy, int *copybitFd) {
386 int layerCount = 0;
387 uint32_t last = list->numHwLayers - 1;
388 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
389 private_handle_t *fbhnd = (private_handle_t *)fbLayer->handle;
Ramakant Singh0def28c2014-03-28 20:43:13 +0530390
391 if(ctx->enableABC == false)
392 return false;
393
Ramakant Singh467759f2014-04-16 11:09:40 +0530394 if(ctx->listStats[dpy].numAppLayers > MAX_LAYERS_FOR_ABC )
Ramakant Singh0def28c2014-03-28 20:43:13 +0530395 return false;
396
397 layerCount = ctx->listStats[dpy].numAppLayers;
398 //bottom most layer should
399 //equal to FB
400 hwc_layer_1_t *tmpLayer = &list->hwLayers[0];
401 private_handle_t *hnd = (private_handle_t *)tmpLayer->handle;
402 if(hnd && fbhnd && (hnd->size == fbhnd->size) &&
403 (hnd->width == fbhnd->width) && (hnd->height == fbhnd->height)){
404 if(tmpLayer->transform ||
405 (!(hnd->format == HAL_PIXEL_FORMAT_RGBA_8888 ||
406 hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) ||
407 (needsScaling(tmpLayer) == true)) {
408 return false;
409 }else {
410 ctx->listStats[dpy].renderBufIndexforABC = 0;
411 }
412 }
413
Ramakant Singh467759f2014-04-16 11:09:40 +0530414 if(ctx->listStats[dpy].renderBufIndexforABC == 0) {
415 if(layerCount == 1)
Ramakant Singh0def28c2014-03-28 20:43:13 +0530416 return true;
Ramakant Singh467759f2014-04-16 11:09:40 +0530417
418 if(layerCount == MAX_LAYERS_FOR_ABC) {
419 // Pass the Acquire Fence FD to driver for base layer
420 int abcRenderBufIdx = ctx->listStats[dpy].renderBufIndexforABC;
421 private_handle_t *renderBuffer =
422 (private_handle_t *)list->hwLayers[abcRenderBufIdx].handle;
423 copybit_device_t *copybit = getCopyBitDevice();
424 if(list->hwLayers[abcRenderBufIdx].acquireFenceFd >=0){
425 copybit->set_sync(copybit,
426 list->hwLayers[abcRenderBufIdx].acquireFenceFd);
427 }
428 for(int i = 1; i < layerCount; i++){
429 int retVal = drawLayerUsingCopybit(ctx,
430 &(list->hwLayers[i]),renderBuffer, 0);
431 if(retVal < 0) {
432 ALOGE("%s : Copybit failed", __FUNCTION__);
433 }
434 }
435 // Get Release Fence FD of copybit for the App layer(s)
436 copybit->flush_get_fence(copybit, copybitFd);
437 close(list->hwLayers[abcRenderBufIdx].acquireFenceFd);
438 list->hwLayers[abcRenderBufIdx].acquireFenceFd = -1;
439 return true;
440 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530441 }
442 return false;
443}
444
445bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
446 int dpy, int32_t *fd) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800447 // draw layers marked for COPYBIT
448 int retVal = true;
449 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400450 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500451 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400452 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800453
Ramakant Singh21cec722014-03-07 19:11:45 +0530454 if(mCopyBitDraw == false){
455 mFbCache.reset(); // there is no layer marked for copybit
456 return false ;
457 }
Ramakant Singh0def28c2014-03-28 20:43:13 +0530458
Ramakant Singh467759f2014-04-16 11:09:40 +0530459 if(drawUsingAppBufferComposition(ctx, list, dpy, fd)) {
Ramakant Singh0def28c2014-03-28 20:43:13 +0530460 return true;
461 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800462 //render buffer
Terence Hampsonc67b0372013-10-09 11:32:21 -0400463 if (ctx->mMDP.version == qdutils::MDP_V3_0_4) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530464 last = (uint32_t)list->numHwLayers - 1;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400465 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
466 } else {
467 renderBuffer = getCurrentRenderBuffer();
468 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800469 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500470 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800471 return false;
472 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500473
Terence Hampson0124cc72013-04-18 23:45:56 -0700474 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400475 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530476 if(mRelFd[mCurRenderBufferIndex] >=0) {
477 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
478 close(mRelFd[mCurRenderBufferIndex]);
479 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400480 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400481 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400482 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400483 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400484 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400485 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700486 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530487
Ramakant Singh21cec722014-03-07 19:11:45 +0530488 mDirtyLayerIndex = checkDirtyRect(ctx, list, dpy);
489 if( mDirtyLayerIndex != -1){
490 hwc_layer_1_t *layer = &list->hwLayers[mDirtyLayerIndex];
Saurabh Shah93c69052014-04-24 10:27:10 -0700491#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530492 clear(renderBuffer,layer->dirtyRect);
Saurabh Shah93c69052014-04-24 10:27:10 -0700493#else
494 clear(renderBuffer,layer->displayFrame);
495#endif
Ramakant Singh21cec722014-03-07 19:11:45 +0530496 } else {
497 hwc_rect_t clearRegion = {0,0,0,0};
498 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
499 clear(renderBuffer, clearRegion);
500 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530501
Naseer Ahmed64b81212013-02-14 10:29:47 -0500502 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800503 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
504 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500505 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
506 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800507 continue;
508 }
Ramakant Singh21cec722014-03-07 19:11:45 +0530509 //skip non updating layers
510 if((mDirtyLayerIndex != -1) && (mDirtyLayerIndex != i) )
Ramakant Singh762ae082013-11-28 18:45:34 +0530511 continue;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800512 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400513 if (list->hwLayers[i].acquireFenceFd != -1
514 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800515 // Wait for acquire Fence on the App buffers.
516 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
517 if(ret < 0) {
518 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
519 __FUNCTION__, errno, strerror(errno));
520 }
521 close(list->hwLayers[i].acquireFenceFd);
522 list->hwLayers[i].acquireFenceFd = -1;
523 }
524 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Ramakant Singh21cec722014-03-07 19:11:45 +0530525 renderBuffer, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800526 copybitLayerCount++;
527 if(retVal < 0) {
528 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
529 }
530 }
531
532 if (copybitLayerCount) {
533 copybit_device_t *copybit = getCopyBitDevice();
534 // Async mode
535 copybit->flush_get_fence(copybit, fd);
Terence Hampsonc67b0372013-10-09 11:32:21 -0400536 if(ctx->mMDP.version == qdutils::MDP_V3_0_4 &&
537 list->hwLayers[last].acquireFenceFd >= 0) {
538 close(list->hwLayers[last].acquireFenceFd);
539 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400540 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800541 }
542 return true;
543}
544
Sushil Chauhandefd3522014-05-13 18:17:12 -0700545int CopyBit::drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list,
546 int overlapIndex) {
547 int fd = -1;
548
549 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
550 ALOGE("%s: Invalid request", __FUNCTION__);
551 return fd;
552 }
553
554 private_handle_t *renderBuffer = getCurrentRenderBuffer();
555
556 if (!renderBuffer) {
557 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
558 return fd;
559 }
560
561 int copybitLayerCount = 0;
562 hwc_rect_t overlap = list->hwLayers[overlapIndex].displayFrame;
563
564 // Draw overlapped content of layers on render buffer
565 for (int i = 0; i <= overlapIndex; i++) {
566
567 int ret = -1;
568 if ((list->hwLayers[i].acquireFenceFd != -1)) {
569 // Wait for acquire fence on the App buffers.
570 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
571 if (ret < 0) {
572 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
573 __FUNCTION__, errno, strerror(errno));
574 }
575 close(list->hwLayers[i].acquireFenceFd);
576 list->hwLayers[i].acquireFenceFd = -1;
577 }
578
579 hwc_layer_1_t *layer = &list->hwLayers[i];
580 int retVal = drawRectUsingCopybit(ctx, layer, renderBuffer, overlap);
581 copybitLayerCount++;
582
583 if(retVal < 0) {
584 ALOGE("%s: drawRectUsingCopybit failed", __FUNCTION__);
585 copybitLayerCount = 0;
586 }
587 }
588
589 if (copybitLayerCount) {
590 copybit_device_t *copybit = getCopyBitDevice();
591 copybit->flush_get_fence(copybit, &fd);
592 }
593
594 ALOGD_IF(DEBUG_COPYBIT, "%s: done!", __FUNCTION__);
595 return fd;
596}
597
598int CopyBit::drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
599 private_handle_t *renderBuffer, hwc_rect_t rect)
600{
601 hwc_context_t* ctx = (hwc_context_t*)(dev);
602 if (!ctx) {
603 ALOGE("%s: null context ", __FUNCTION__);
604 return -1;
605 }
606
607 private_handle_t *hnd = (private_handle_t *)layer->handle;
608 if (!hnd) {
609 ALOGE("%s: invalid handle", __FUNCTION__);
610 return -1;
611 }
612
613 private_handle_t *dstHandle = (private_handle_t *)renderBuffer;
614 if (!dstHandle) {
615 ALOGE("%s: RenderBuffer handle is NULL", __FUNCTION__);
616 return -1;
617 }
618
619 // Set the Copybit Source
620 copybit_image_t src;
621 src.handle = (native_handle_t *)layer->handle;
622 src.w = hnd->width;
623 src.h = hnd->height;
624 src.base = (void *)hnd->base;
625 src.format = hnd->format;
626 src.horiz_padding = 0;
627 src.vert_padding = 0;
628
629 hwc_rect_t dispFrame = layer->displayFrame;
630 hwc_rect_t iRect = getIntersection(dispFrame, rect);
631 hwc_rect_t crop = integerizeSourceCrop(layer->sourceCropf);
632 qhwc::calculate_crop_rects(crop, dispFrame, iRect, layer->transform);
633
634 // Copybit source rect
635 copybit_rect_t srcRect = {crop.left, crop.top, crop.right, crop.bottom};
636
637 // Copybit destination rect
638 copybit_rect_t dstRect = {rect.left, rect.top, rect.right, rect.bottom};
639
640 // Copybit dst
641 copybit_image_t dst;
642 dst.handle = (native_handle_t *)dstHandle;
643 dst.w = ALIGN(dstHandle->width, 32);
644 dst.h = dstHandle->height;
645 dst.base = (void *)dstHandle->base;
646 dst.format = dstHandle->format;
647
648 copybit_device_t *copybit = mEngine;
649
650 // Copybit region
651 hwc_region_t region = layer->visibleRegionScreen;
652 region_iterator copybitRegion(region);
653 int acquireFd = layer->acquireFenceFd;
654
655 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH, renderBuffer->width);
656 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT, renderBuffer->height);
657 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
658 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
659 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
660 copybit->set_parameter(copybit, COPYBIT_DITHER,
661 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ? COPYBIT_ENABLE : COPYBIT_DISABLE);
662 copybit->set_sync(copybit, acquireFd);
663 int err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect, &copybitRegion);
664
665 if (err < 0)
666 ALOGE("%s: copybit stretch failed",__FUNCTION__);
667
668 return err;
669}
670
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700671int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800672 private_handle_t *renderBuffer, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700673{
674 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400675 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700676 if(!ctx) {
677 ALOGE("%s: null context ", __FUNCTION__);
678 return -1;
679 }
680
681 private_handle_t *hnd = (private_handle_t *)layer->handle;
682 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700683 if (layer->flags & HWC_COLOR_FILL) { // Color layer
684 return fillColorUsingCopybit(layer, renderBuffer);
685 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700686 ALOGE("%s: invalid handle", __FUNCTION__);
687 return -1;
688 }
689
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800690 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700691 if(!fbHandle) {
692 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700693 return -1;
694 }
695
696 // Set the copybit source:
697 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700698 src.w = getWidth(hnd);
699 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700700 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700701
702 // Handle R/B swap
703 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
704 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
705 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
706 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
707 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
708 }
709 }
710
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700711 src.base = (void *)hnd->base;
712 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700713 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700714 // Initialize vertical padding to zero for now,
715 // this needs to change to accomodate vertical stride
716 // if needed in the future
717 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700718
shuoy7c101f92013-08-26 14:48:57 +0800719 int layerTransform = layer->transform ;
720 // When flip and rotation(90) are present alter the flip,
721 // as GPU is doing the flip and rotation in opposite order
722 // to that of MDP3.0
723 // For 270 degrees, we get 90 + (H+V) which is same as doing
724 // flip first and then rotation (H+V) + 90
725 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
726 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
727 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
728 (layer->transform & HAL_TRANSFORM_ROT_90) &&
729 !(layer->transform == HAL_TRANSFORM_ROT_270)){
730 if(layer->transform & HAL_TRANSFORM_FLIP_H){
731 layerTransform ^= HAL_TRANSFORM_FLIP_H;
732 layerTransform |= HAL_TRANSFORM_FLIP_V;
733 }
734 if(layer->transform & HAL_TRANSFORM_FLIP_V){
735 layerTransform ^= HAL_TRANSFORM_FLIP_V;
736 layerTransform |= HAL_TRANSFORM_FLIP_H;
737 }
738 }
739 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700740 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700741 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700742 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
743 sourceCrop.right,
744 sourceCrop.bottom};
745
746 // Copybit destination rect
747 hwc_rect_t displayFrame = layer->displayFrame;
748 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
749 displayFrame.right,
750 displayFrame.bottom};
Saurabh Shah93c69052014-04-24 10:27:10 -0700751#ifdef QCOM_BSP
Ramakant Singh21cec722014-03-07 19:11:45 +0530752 //change src and dst with dirtyRect
753 if(mDirtyLayerIndex != -1) {
754 srcRect.l = layer->dirtyRect.left;
755 srcRect.t = layer->dirtyRect.top;
756 srcRect.r = layer->dirtyRect.right;
757 srcRect.b = layer->dirtyRect.bottom;
758 dstRect = srcRect;
759 }
Saurabh Shah93c69052014-04-24 10:27:10 -0700760#endif
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700761 // Copybit dst
762 copybit_image_t dst;
763 dst.w = ALIGN(fbHandle->width,32);
764 dst.h = fbHandle->height;
765 dst.format = fbHandle->format;
766 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800767 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700768
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800769 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700770
771 int32_t screen_w = displayFrame.right - displayFrame.left;
772 int32_t screen_h = displayFrame.bottom - displayFrame.top;
773 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
774 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
775
776 // Copybit dst
777 float copybitsMaxScale =
778 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
779 float copybitsMinScale =
780 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
781
Sushil Chauhandffbf432013-12-09 15:33:57 -0800782 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700783 //swap screen width and height
784 int tmp = screen_w;
785 screen_w = screen_h;
786 screen_h = tmp;
787 }
788 private_handle_t *tmpHnd = NULL;
789
790 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
791 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530792 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
793 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700794 return -1;
795 }
796
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530797 float dsdx = (float)screen_w/(float)src_crop_width;
798 float dtdy = (float)screen_h/(float)src_crop_height;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700799
800 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
801 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
802 if(dsdx > scaleLimitMax ||
803 dtdy > scaleLimitMax ||
804 dsdx < 1/scaleLimitMin ||
805 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400806 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700807 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
808 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700809 return -1;
810 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400811 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700812 if(dsdx > copybitsMaxScale ||
813 dtdy > copybitsMaxScale ||
814 dsdx < 1/copybitsMinScale ||
815 dtdy < 1/copybitsMinScale){
816 // The requested scale is out of the range the hardware
817 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400818 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700819 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
820 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
821 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
822 src_crop_width,src_crop_height);
823
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700824
825 int tmp_w = src_crop_width;
826 int tmp_h = src_crop_height;
827
828 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530829 tmp_w = (int)((float)src_crop_width*copybitsMaxScale);
830 tmp_h = (int)((float)src_crop_height*copybitsMaxScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700831 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
Ramakant Singh80f36f22014-02-25 14:11:35 +0530832 // ceil the tmp_w and tmp_h value to maintain proper ratio
833 // b/w src and dst (should not cross the desired scale limit
834 // due to float -> int )
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530835 tmp_w = (int)ceil((float)src_crop_width/copybitsMinScale);
836 tmp_h = (int)ceil((float)src_crop_height/copybitsMinScale);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700837 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400838 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700839
Naseer Ahmed64b81212013-02-14 10:29:47 -0500840 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400841 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700842
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400843 // We do not want copybit to generate alpha values from nothing
844 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
845 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
846 format = HAL_PIXEL_FORMAT_RGBX_8888;
847 }
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800848 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage) && tmpHnd) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700849 copybit_image_t tmp_dst;
850 copybit_rect_t tmp_rect;
851 tmp_dst.w = tmp_w;
852 tmp_dst.h = tmp_h;
853 tmp_dst.format = tmpHnd->format;
854 tmp_dst.handle = tmpHnd;
855 tmp_dst.horiz_padding = src.horiz_padding;
856 tmp_dst.vert_padding = src.vert_padding;
857 tmp_rect.l = 0;
858 tmp_rect.t = 0;
859 tmp_rect.r = tmp_dst.w;
860 tmp_rect.b = tmp_dst.h;
861 //create one clip region
862 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
863 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
864 region_iterator tmp_it(tmp_hwc_reg);
865 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700866 //TODO: once, we are able to read layer alpha, update this
867 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400868 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700869 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
870 &srcRect, &tmp_it);
871 if(err < 0){
872 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
873 __LINE__);
874 if(tmpHnd)
875 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700876 return err;
877 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400878 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400879 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400880 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400881 close(acquireFd);
882 acquireFd = -1;
883 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700884 // copy new src and src rect crop
885 src = tmp_dst;
886 srcRect = tmp_rect;
887 }
888 }
889 // Copybit region
890 hwc_region_t region = layer->visibleRegionScreen;
891 region_iterator copybitRegion(region);
892
893 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
894 renderBuffer->width);
895 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
896 renderBuffer->height);
897 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800898 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700899 //TODO: once, we are able to read layer alpha, update this
900 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800901 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
902 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700903 copybit->set_parameter(copybit, COPYBIT_DITHER,
904 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
905 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530906 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
907 COPYBIT_ENABLE : COPYBIT_DISABLE);
908
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700909 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
910 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400911 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700912 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
913 &copybitRegion);
914 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
915 COPYBIT_DISABLE);
916
Terence Hampsonadf47302013-05-23 12:21:02 -0400917 if(tmpHnd) {
918 if (ctx->mMDP.version < qdutils::MDP_V4_0){
919 int ret = -1, releaseFd;
920 // we need to wait for the buffer before freeing
921 copybit->flush_get_fence(copybit, &releaseFd);
922 ret = sync_wait(releaseFd, 1000);
923 if(ret < 0) {
924 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
925 __FUNCTION__, errno, strerror(errno));
926 }
927 close(releaseFd);
928 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700929 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400930 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700931
932 if(err < 0)
933 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700934 return err;
935}
936
Sushil Chauhan943797c2013-10-21 17:35:55 -0700937int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
938 private_handle_t *renderBuffer)
939{
940 if (!renderBuffer) {
941 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
942 return -1;
943 }
944
945 // Copybit dst
946 copybit_image_t dst;
947 dst.w = ALIGN(renderBuffer->width, 32);
948 dst.h = renderBuffer->height;
949 dst.format = renderBuffer->format;
950 dst.base = (void *)renderBuffer->base;
951 dst.handle = (native_handle_t *)renderBuffer;
952
953 // Copybit dst rect
954 hwc_rect_t displayFrame = layer->displayFrame;
955 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
956 displayFrame.right, displayFrame.bottom};
957
958 uint32_t color = layer->transform;
959 copybit_device_t *copybit = mEngine;
960 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
961 renderBuffer->width);
962 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
963 renderBuffer->height);
964 copybit->set_parameter(copybit, COPYBIT_DITHER,
965 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
966 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800967 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -0700968 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
969 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
970 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
971 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
972 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
973 return res;
974}
975
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700976void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700977 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700978{
979 hwc_rect_t displayFrame = layer->displayFrame;
980
981 width = displayFrame.right - displayFrame.left;
982 height = displayFrame.bottom - displayFrame.top;
983}
984
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800985bool CopyBit::validateParams(hwc_context_t *ctx,
986 const hwc_display_contents_1_t *list) {
987 //Validate parameters
988 if (!ctx) {
989 ALOGE("%s:Invalid HWC context", __FUNCTION__);
990 return false;
991 } else if (!list) {
992 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
993 return false;
994 }
995 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700996}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700997
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700998
Naseer Ahmed64b81212013-02-14 10:29:47 -0500999int CopyBit::allocRenderBuffers(int w, int h, int f)
1000{
1001 int ret = 0;
1002 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1003 if (mRenderBuffer[i] == NULL) {
1004 ret = alloc_buffer(&mRenderBuffer[i],
1005 w, h, f,
1006 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
1007 }
1008 if(ret < 0) {
1009 freeRenderBuffers();
1010 break;
1011 }
1012 }
1013 return ret;
1014}
1015
1016void CopyBit::freeRenderBuffers()
1017{
1018 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
1019 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301020 //Since we are freeing buffer close the fence if it has a valid one.
1021 if(mRelFd[i] >= 0) {
1022 close(mRelFd[i]);
1023 mRelFd[i] = -1;
1024 }
Naseer Ahmed64b81212013-02-14 10:29:47 -05001025 free_buffer(mRenderBuffer[i]);
1026 mRenderBuffer[i] = NULL;
1027 }
1028 }
1029}
1030
1031private_handle_t * CopyBit::getCurrentRenderBuffer() {
1032 return mRenderBuffer[mCurRenderBufferIndex];
1033}
1034
1035void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301036 if(mRelFd[mCurRenderBufferIndex] >=0)
1037 close(mRelFd[mCurRenderBufferIndex]);
1038 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -05001039}
1040
Sushil Chauhandefd3522014-05-13 18:17:12 -07001041void CopyBit::setReleaseFdSync(int fd) {
1042 if (mRelFd[mCurRenderBufferIndex] >=0) {
1043 int ret = -1;
1044 ret = sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
1045 if (ret < 0)
1046 ALOGE("%s: sync_wait error! errno = %d, err str = %s",
1047 __FUNCTION__, errno, strerror(errno));
1048 close(mRelFd[mCurRenderBufferIndex]);
1049 }
1050 mRelFd[mCurRenderBufferIndex] = dup(fd);
1051}
1052
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001053struct copybit_device_t* CopyBit::getCopyBitDevice() {
1054 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001055}
1056
Saurabh Shah220a30c2013-10-29 16:12:12 -07001057CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mIsModeOn(false),
Praveena Pachipulusu2005e8f2014-05-07 20:01:54 +05301058 mCopyBitDraw(false), mCurRenderBufferIndex(0), mEngine(0) {
Saurabh Shah220a30c2013-10-29 16:12:12 -07001059
1060 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
1061 ctx->dpyAttr[dpy].yres,
1062 HAL_PIXEL_FORMAT_RGBA_8888,
Sushil Chauhandefd3522014-05-13 18:17:12 -07001063 mAlignedWidth,
1064 mAlignedHeight);
Saurabh Shah220a30c2013-10-29 16:12:12 -07001065
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001066 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301067 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -05001068 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +05301069 mRelFd[i] = -1;
1070 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +05301071
1072 char value[PROPERTY_VALUE_MAX];
1073 property_get("debug.hwc.dynThreshold", value, "2");
1074 mDynThreshold = atof(value);
1075
Ramakant Singh21cec722014-03-07 19:11:45 +05301076 property_get("debug.sf.swaprect", value, "0");
1077 mSwapRectEnable = atoi(value) ? true:false ;
1078 mDirtyLayerIndex = -1;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001079 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001080 if(copybit_open(module, &mEngine) < 0) {
1081 ALOGE("FATAL ERROR: copybit open failed.");
1082 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001083 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001084 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001085 }
1086}
Saurabh Shah661a58f2012-08-30 15:30:49 -07001087
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001088CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001089{
Naseer Ahmed64b81212013-02-14 10:29:47 -05001090 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001091 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001092 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08001093 copybit_close(mEngine);
1094 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001095 }
1096}
Ramakant Singh21cec722014-03-07 19:11:45 +05301097CopyBit::LayerCache::LayerCache() {
1098 reset();
1099}
1100void CopyBit::LayerCache::reset() {
1101 memset(&hnd, 0, sizeof(hnd));
1102 layerCount = 0;
1103}
1104void CopyBit::LayerCache::updateCounts(hwc_context_t *ctx,
1105 hwc_display_contents_1_t *list, int dpy)
1106{
1107 layerCount = ctx->listStats[dpy].numAppLayers;
1108 for (int i=0; i<ctx->listStats[dpy].numAppLayers; i++){
1109 hnd[i] = list->hwLayers[i].handle;
1110 }
1111}
1112
1113CopyBit::FbCache::FbCache() {
1114 reset();
1115}
1116void CopyBit::FbCache::reset() {
1117 memset(&FbdirtyRect, 0, sizeof(FbdirtyRect));
1118 FbIndex =0;
1119}
1120
1121void CopyBit::FbCache::insertAndUpdateFbCache(hwc_rect_t dirtyRect) {
1122 FbIndex = FbIndex % NUM_RENDER_BUFFERS;
1123 FbdirtyRect[FbIndex] = dirtyRect;
1124 FbIndex++;
1125}
1126
1127int CopyBit::FbCache::getUnchangedFbDRCount(hwc_rect_t dirtyRect){
1128 int sameDirtyCount = 0;
1129 for (int i = 0 ; i < NUM_RENDER_BUFFERS ; i++ ){
1130 if( FbdirtyRect[i] == dirtyRect)
1131 sameDirtyCount++;
1132 }
1133 return sameDirtyCount;
1134}
1135
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001136}; //namespace qhwc