blob: 38d8d5dca7215159bcae620231f96efb005d79a3 [file] [log] [blame]
Naseer Ahmed31da0b12012-07-31 18:55:33 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08003 * Copyright (C) 2012-2013, 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"
Naseer Ahmed74214722013-02-09 08:11:36 -050028
radhakrishna2f00c8f2013-10-21 16:49:21 +053029using namespace qdutils;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070030namespace qhwc {
31
Naseer Ahmed31da0b12012-07-31 18:55:33 -070032struct range {
33 int current;
34 int end;
35};
36struct region_iterator : public copybit_region_t {
37
38 region_iterator(hwc_region_t region) {
39 mRegion = region;
40 r.end = region.numRects;
41 r.current = 0;
42 this->next = iterate;
43 }
44
45private:
46 static int iterate(copybit_region_t const * self, copybit_rect_t* rect){
47 if (!self || !rect) {
48 ALOGE("iterate invalid parameters");
49 return 0;
50 }
51
52 region_iterator const* me =
53 static_cast<region_iterator const*>(self);
54 if (me->r.current != me->r.end) {
55 rect->l = me->mRegion.rects[me->r.current].left;
56 rect->t = me->mRegion.rects[me->r.current].top;
57 rect->r = me->mRegion.rects[me->r.current].right;
58 rect->b = me->mRegion.rects[me->r.current].bottom;
59 me->r.current++;
60 return 1;
61 }
62 return 0;
63 }
64
65 hwc_region_t mRegion;
66 mutable range r;
67};
68
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080069void CopyBit::reset() {
70 mIsModeOn = false;
71 mCopyBitDraw = false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070072}
73
Naseer Ahmed45a99602012-07-31 19:15:24 -070074bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
75 // return true for non-overlay targets
Terence Hampsond0fd5792013-05-29 11:56:52 -040076 if(ctx->mMDP.hasOverlay && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070077 return false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070078 }
79 return true;
80}
Naseer Ahmed45a99602012-07-31 19:15:24 -070081
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080082bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
83 hwc_display_contents_1_t *list,
84 int dpy) {
85 int compositionType = qdutils::QCCompositionType::
86 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -070087
Naseer Ahmed45a99602012-07-31 19:15:24 -070088 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
89 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053090 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070091 // this is done based on perf inputs in ICS
92 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080093 int fbWidth = ctx->dpyAttr[dpy].xres;
94 int fbHeight = ctx->dpyAttr[dpy].yres;
95 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -070096 unsigned int renderArea = getRGBRenderingArea(list);
97 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
98 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053099 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700100 return true;
101 }
102 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
103 // MDP composition, use COPYBIT always
104 return true;
105 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
106 // C2D composition, use COPYBIT
107 return true;
108 }
109 return false;
110}
111
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800112unsigned int CopyBit::getRGBRenderingArea
113 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700114 //Calculates total rendering area for RGB layers
115 unsigned int renderArea = 0;
116 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400117 // Skipping last layer since FrameBuffer layer should not affect
118 // which composition to choose
119 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700120 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
121 if (hnd) {
122 if (BUFFER_TYPE_UI == hnd->bufferType) {
123 getLayerResolution(&list->hwLayers[i], w, h);
124 renderArea += (w*h);
125 }
126 }
127 }
128 return renderArea;
129}
130
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800131bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
132 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700133
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800134 if(mEngine == NULL) {
135 // No copybit device found - cannot use copybit
136 return false;
137 }
138 int compositionType = qdutils::QCCompositionType::
139 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700140
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800141 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
142 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700143 //GPU/CPU composition, don't change layer composition type
144 return true;
145 }
146
Naseer Ahmed45a99602012-07-31 19:15:24 -0700147 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800148 ALOGE("%s:Invalid Params", __FUNCTION__);
149 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700150 }
151
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800152 if(ctx->listStats[dpy].skipCount) {
153 //GPU will be anyways used
154 return false;
155 }
156
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700157 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700158 // Reached max layers supported by HWC.
159 return false;
160 }
161
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800162 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
163 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500164 LayerProp *layerProp = ctx->layerProp[dpy];
165 size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
166 hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500167
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530168 // Following are MDP3 limitations for which we
169 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400170 // 1. Plane alpha is not supported by MDP3.
Terence Hampson6b0a4272013-11-06 16:04:51 -0500171 // 2. Scaling is within range
Terence Hampsonc235bda2013-07-12 12:47:38 -0400172 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
173 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Terence Hampson6b0a4272013-11-06 16:04:51 -0500174 int dst_h, dst_w, src_h, src_w;
175 float dx, dy;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400176 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530177 if (layer->planeAlpha != 0xFF)
178 return true;
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530179 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Terence Hampson6b0a4272013-11-06 16:04:51 -0500180
181 if (layer->transform & HAL_TRANSFORM_ROT_90) {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530182 src_h = sourceCrop.right - sourceCrop.left;
183 src_w = sourceCrop.bottom - sourceCrop.top;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500184 } else {
Ramakant Singhf2b51e62013-11-28 12:07:51 +0530185 src_h = sourceCrop.bottom - sourceCrop.top;
186 src_w = sourceCrop.right - sourceCrop.left;
Terence Hampson6b0a4272013-11-06 16:04:51 -0500187 }
188 dst_h = layer->displayFrame.bottom - layer->displayFrame.top;
189 dst_w = layer->displayFrame.right - layer->displayFrame.left;
190
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530191 if(src_w <=0 || src_h<=0 ||dst_w<=0 || dst_h<=0 ) {
192 ALOGE("%s: wrong params for display screen_w=%d \
193 src_crop_width=%d screen_h=%d src_crop_height=%d",
194 __FUNCTION__, dst_w,src_w,dst_h,src_h);
195 return false;
196 }
Terence Hampson6b0a4272013-11-06 16:04:51 -0500197 dx = (float)dst_w/src_w;
198 dy = (float)dst_h/src_h;
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530199
Terence Hampson6b0a4272013-11-06 16:04:51 -0500200 if (dx > MAX_SCALE_FACTOR || dx < MIN_SCALE_FACTOR)
201 return false;
202
203 if (dy > MAX_SCALE_FACTOR || dy < MIN_SCALE_FACTOR)
204 return false;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400205 }
206 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500207
208 //Allocate render buffers if they're not allocated
Terence Hampsonc67b0372013-10-09 11:32:21 -0400209 if (ctx->mMDP.version != qdutils::MDP_V3_0_4 &&
210 (useCopybitForYUV || useCopybitForRGB)) {
Saurabh Shah220a30c2013-10-29 16:12:12 -0700211 int ret = allocRenderBuffers(mAlignedFBWidth,
212 mAlignedFBHeight,
213 HAL_PIXEL_FORMAT_RGBA_8888);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500214 if (ret < 0) {
215 return false;
216 } else {
217 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
218 NUM_RENDER_BUFFERS;
219 }
220 }
221
Terence Hampsond0fd5792013-05-29 11:56:52 -0400222 // We cannot mix copybit layer with layers marked to be drawn on FB
223 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
224 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800225
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530226 mCopyBitDraw = false;
227 if (useCopybitForRGB &&
228 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
229 mCopyBitDraw = true;
230 // numAppLayers-1, as we iterate till 0th layer index
231 // Mark all layers to be drawn by copybit
232 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500233 layerProp[i].mFlags |= HWC_COPYBIT;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400234 if (ctx->mMDP.version == qdutils::MDP_V3_0_4)
235 list->hwLayers[i].compositionType = HWC_BLIT;
236 else
237 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700238 }
239 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530240
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700241 return true;
242}
243
Naseer Ahmed183939d2013-03-14 20:44:17 -0400244int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
245{
246 int ret = 0;
247 copybit_rect_t clear_rect = {rect.left, rect.top,
248 rect.right,
249 rect.bottom};
250
251 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700252 buf.w = ALIGN(getWidth(hnd),32);
253 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400254 buf.format = hnd->format;
255 buf.base = (void *)hnd->base;
256 buf.handle = (native_handle_t *)hnd;
257
258 copybit_device_t *copybit = mEngine;
259 ret = copybit->clear(copybit, &buf, &clear_rect);
260 return ret;
261}
262
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800263bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
264 int dpy, int32_t *fd) {
265 // draw layers marked for COPYBIT
266 int retVal = true;
267 int copybitLayerCount = 0;
Terence Hampsonc67b0372013-10-09 11:32:21 -0400268 uint32_t last = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500269 LayerProp *layerProp = ctx->layerProp[dpy];
Terence Hampsonc67b0372013-10-09 11:32:21 -0400270 private_handle_t *renderBuffer;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800271
272 if(mCopyBitDraw == false) // there is no layer marked for copybit
Naseer Ahmed64b81212013-02-14 10:29:47 -0500273 return false ;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800274
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800275 //render buffer
Terence Hampsonc67b0372013-10-09 11:32:21 -0400276 if (ctx->mMDP.version == qdutils::MDP_V3_0_4) {
277 last = list->numHwLayers - 1;
278 renderBuffer = (private_handle_t *)list->hwLayers[last].handle;
279 } else {
280 renderBuffer = getCurrentRenderBuffer();
281 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800282 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500283 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800284 return false;
285 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500286
Terence Hampson0124cc72013-04-18 23:45:56 -0700287 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400288 //Wait for the previous frame to complete before rendering onto it
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530289 if(mRelFd[mCurRenderBufferIndex] >=0) {
290 sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
291 close(mRelFd[mCurRenderBufferIndex]);
292 mRelFd[mCurRenderBufferIndex] = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400293 }
Terence Hampson65fe4522013-10-17 17:40:03 -0400294 } else {
Terence Hampsonc67b0372013-10-09 11:32:21 -0400295 if(list->hwLayers[last].acquireFenceFd >=0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400296 copybit_device_t *copybit = getCopyBitDevice();
Terence Hampsonc67b0372013-10-09 11:32:21 -0400297 copybit->set_sync(copybit, list->hwLayers[last].acquireFenceFd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400298 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700299 }
radhakrishna2f00c8f2013-10-21 16:49:21 +0530300
301 //Clear the transparent or left out region on the render buffer
302 hwc_rect_t clearRegion = {0,0,0,0};
303 if(CBUtils::getuiClearRegion(list, clearRegion, layerProp))
304 clear(renderBuffer, clearRegion);
305
Naseer Ahmed64b81212013-02-14 10:29:47 -0500306 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800307 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
308 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500309 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
310 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800311 continue;
312 }
313 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400314 if (list->hwLayers[i].acquireFenceFd != -1
315 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800316 // Wait for acquire Fence on the App buffers.
317 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
318 if(ret < 0) {
319 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
320 __FUNCTION__, errno, strerror(errno));
321 }
322 close(list->hwLayers[i].acquireFenceFd);
323 list->hwLayers[i].acquireFenceFd = -1;
324 }
325 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530326 renderBuffer, dpy, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800327 copybitLayerCount++;
328 if(retVal < 0) {
329 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
330 }
331 }
332
333 if (copybitLayerCount) {
334 copybit_device_t *copybit = getCopyBitDevice();
335 // Async mode
336 copybit->flush_get_fence(copybit, fd);
Terence Hampsonc67b0372013-10-09 11:32:21 -0400337 if(ctx->mMDP.version == qdutils::MDP_V3_0_4 &&
338 list->hwLayers[last].acquireFenceFd >= 0) {
339 close(list->hwLayers[last].acquireFenceFd);
340 list->hwLayers[last].acquireFenceFd = -1;
Terence Hampson65fe4522013-10-17 17:40:03 -0400341 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800342 }
343 return true;
344}
345
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700346int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530347 private_handle_t *renderBuffer, int dpy, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700348{
349 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400350 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700351 if(!ctx) {
352 ALOGE("%s: null context ", __FUNCTION__);
353 return -1;
354 }
355
356 private_handle_t *hnd = (private_handle_t *)layer->handle;
357 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700358 if (layer->flags & HWC_COLOR_FILL) { // Color layer
359 return fillColorUsingCopybit(layer, renderBuffer);
360 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700361 ALOGE("%s: invalid handle", __FUNCTION__);
362 return -1;
363 }
364
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800365 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700366 if(!fbHandle) {
367 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700368 return -1;
369 }
370
371 // Set the copybit source:
372 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700373 src.w = getWidth(hnd);
374 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700375 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700376
377 // Handle R/B swap
378 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
379 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
380 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
381 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
382 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
383 }
384 }
385
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700386 src.base = (void *)hnd->base;
387 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700388 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700389 // Initialize vertical padding to zero for now,
390 // this needs to change to accomodate vertical stride
391 // if needed in the future
392 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700393
shuoy7c101f92013-08-26 14:48:57 +0800394 int layerTransform = layer->transform ;
395 // When flip and rotation(90) are present alter the flip,
396 // as GPU is doing the flip and rotation in opposite order
397 // to that of MDP3.0
398 // For 270 degrees, we get 90 + (H+V) which is same as doing
399 // flip first and then rotation (H+V) + 90
400 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
401 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
402 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
403 (layer->transform & HAL_TRANSFORM_ROT_90) &&
404 !(layer->transform == HAL_TRANSFORM_ROT_270)){
405 if(layer->transform & HAL_TRANSFORM_FLIP_H){
406 layerTransform ^= HAL_TRANSFORM_FLIP_H;
407 layerTransform |= HAL_TRANSFORM_FLIP_V;
408 }
409 if(layer->transform & HAL_TRANSFORM_FLIP_V){
410 layerTransform ^= HAL_TRANSFORM_FLIP_V;
411 layerTransform |= HAL_TRANSFORM_FLIP_H;
412 }
413 }
414 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700415 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700416 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700417 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
418 sourceCrop.right,
419 sourceCrop.bottom};
420
421 // Copybit destination rect
422 hwc_rect_t displayFrame = layer->displayFrame;
423 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
424 displayFrame.right,
425 displayFrame.bottom};
426
427 // Copybit dst
428 copybit_image_t dst;
429 dst.w = ALIGN(fbHandle->width,32);
430 dst.h = fbHandle->height;
431 dst.format = fbHandle->format;
432 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800433 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700434
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800435 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700436
437 int32_t screen_w = displayFrame.right - displayFrame.left;
438 int32_t screen_h = displayFrame.bottom - displayFrame.top;
439 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
440 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
441
442 // Copybit dst
443 float copybitsMaxScale =
444 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
445 float copybitsMinScale =
446 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
447
Sushil Chauhandffbf432013-12-09 15:33:57 -0800448 if (layer->transform & HWC_TRANSFORM_ROT_90) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700449 //swap screen width and height
450 int tmp = screen_w;
451 screen_w = screen_h;
452 screen_h = tmp;
453 }
454 private_handle_t *tmpHnd = NULL;
455
456 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
457 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
Ramakant Singh0b35f2c2014-01-07 11:25:48 +0530458 screen_h=%d src_crop_height=%d", __FUNCTION__, screen_w,
459 src_crop_width,screen_h,src_crop_height);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700460 return -1;
461 }
462
463 float dsdx = (float)screen_w/src_crop_width;
464 float dtdy = (float)screen_h/src_crop_height;
465
466 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
467 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
468 if(dsdx > scaleLimitMax ||
469 dtdy > scaleLimitMax ||
470 dsdx < 1/scaleLimitMin ||
471 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400472 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700473 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
474 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700475 return -1;
476 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400477 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700478 if(dsdx > copybitsMaxScale ||
479 dtdy > copybitsMaxScale ||
480 dsdx < 1/copybitsMinScale ||
481 dtdy < 1/copybitsMinScale){
482 // The requested scale is out of the range the hardware
483 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400484 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700485 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
486 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
487 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
488 src_crop_width,src_crop_height);
489
490 //Driver makes width and height as even
491 //that may cause wrong calculation of the ratio
492 //in display and crop.Hence we make
493 //crop width and height as even.
494 src_crop_width = (src_crop_width/2)*2;
495 src_crop_height = (src_crop_height/2)*2;
496
497 int tmp_w = src_crop_width;
498 int tmp_h = src_crop_height;
499
500 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
501 tmp_w = src_crop_width*copybitsMaxScale;
502 tmp_h = src_crop_height*copybitsMaxScale;
503 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
504 tmp_w = src_crop_width/copybitsMinScale;
505 tmp_h = src_crop_height/copybitsMinScale;
506 tmp_w = (tmp_w/2)*2;
507 tmp_h = (tmp_h/2)*2;
508 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400509 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700510
Naseer Ahmed64b81212013-02-14 10:29:47 -0500511 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400512 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700513
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400514 // We do not want copybit to generate alpha values from nothing
515 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
516 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
517 format = HAL_PIXEL_FORMAT_RGBX_8888;
518 }
519 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage)){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700520 copybit_image_t tmp_dst;
521 copybit_rect_t tmp_rect;
522 tmp_dst.w = tmp_w;
523 tmp_dst.h = tmp_h;
524 tmp_dst.format = tmpHnd->format;
525 tmp_dst.handle = tmpHnd;
526 tmp_dst.horiz_padding = src.horiz_padding;
527 tmp_dst.vert_padding = src.vert_padding;
528 tmp_rect.l = 0;
529 tmp_rect.t = 0;
530 tmp_rect.r = tmp_dst.w;
531 tmp_rect.b = tmp_dst.h;
532 //create one clip region
533 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
534 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
535 region_iterator tmp_it(tmp_hwc_reg);
536 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700537 //TODO: once, we are able to read layer alpha, update this
538 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400539 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700540 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
541 &srcRect, &tmp_it);
542 if(err < 0){
543 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
544 __LINE__);
545 if(tmpHnd)
546 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700547 return err;
548 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400549 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400550 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400551 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400552 close(acquireFd);
553 acquireFd = -1;
554 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700555 // copy new src and src rect crop
556 src = tmp_dst;
557 srcRect = tmp_rect;
558 }
559 }
560 // Copybit region
561 hwc_region_t region = layer->visibleRegionScreen;
562 region_iterator copybitRegion(region);
563
564 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
565 renderBuffer->width);
566 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
567 renderBuffer->height);
568 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800569 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700570 //TODO: once, we are able to read layer alpha, update this
571 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800572 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
573 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700574 copybit->set_parameter(copybit, COPYBIT_DITHER,
575 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
576 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530577 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
578 COPYBIT_ENABLE : COPYBIT_DISABLE);
579
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700580 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
581 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400582 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700583 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
584 &copybitRegion);
585 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
586 COPYBIT_DISABLE);
587
Terence Hampsonadf47302013-05-23 12:21:02 -0400588 if(tmpHnd) {
589 if (ctx->mMDP.version < qdutils::MDP_V4_0){
590 int ret = -1, releaseFd;
591 // we need to wait for the buffer before freeing
592 copybit->flush_get_fence(copybit, &releaseFd);
593 ret = sync_wait(releaseFd, 1000);
594 if(ret < 0) {
595 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
596 __FUNCTION__, errno, strerror(errno));
597 }
598 close(releaseFd);
599 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700600 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400601 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700602
603 if(err < 0)
604 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700605 return err;
606}
607
Sushil Chauhan943797c2013-10-21 17:35:55 -0700608int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
609 private_handle_t *renderBuffer)
610{
611 if (!renderBuffer) {
612 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
613 return -1;
614 }
615
616 // Copybit dst
617 copybit_image_t dst;
618 dst.w = ALIGN(renderBuffer->width, 32);
619 dst.h = renderBuffer->height;
620 dst.format = renderBuffer->format;
621 dst.base = (void *)renderBuffer->base;
622 dst.handle = (native_handle_t *)renderBuffer;
623
624 // Copybit dst rect
625 hwc_rect_t displayFrame = layer->displayFrame;
626 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
627 displayFrame.right, displayFrame.bottom};
628
629 uint32_t color = layer->transform;
630 copybit_device_t *copybit = mEngine;
631 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
632 renderBuffer->width);
633 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
634 renderBuffer->height);
635 copybit->set_parameter(copybit, COPYBIT_DITHER,
636 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
637 COPYBIT_ENABLE : COPYBIT_DISABLE);
Sushil Chauhan2babecc2013-12-04 17:29:48 -0800638 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
Sushil Chauhan943797c2013-10-21 17:35:55 -0700639 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
640 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
641 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
642 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
643 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
644 return res;
645}
646
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700647void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700648 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700649{
650 hwc_rect_t displayFrame = layer->displayFrame;
651
652 width = displayFrame.right - displayFrame.left;
653 height = displayFrame.bottom - displayFrame.top;
654}
655
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800656bool CopyBit::validateParams(hwc_context_t *ctx,
657 const hwc_display_contents_1_t *list) {
658 //Validate parameters
659 if (!ctx) {
660 ALOGE("%s:Invalid HWC context", __FUNCTION__);
661 return false;
662 } else if (!list) {
663 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
664 return false;
665 }
666 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700667}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700668
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700669
Naseer Ahmed64b81212013-02-14 10:29:47 -0500670int CopyBit::allocRenderBuffers(int w, int h, int f)
671{
672 int ret = 0;
673 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
674 if (mRenderBuffer[i] == NULL) {
675 ret = alloc_buffer(&mRenderBuffer[i],
676 w, h, f,
677 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
678 }
679 if(ret < 0) {
680 freeRenderBuffers();
681 break;
682 }
683 }
684 return ret;
685}
686
687void CopyBit::freeRenderBuffers()
688{
689 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
690 if(mRenderBuffer[i]) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530691 //Since we are freeing buffer close the fence if it has a valid one.
692 if(mRelFd[i] >= 0) {
693 close(mRelFd[i]);
694 mRelFd[i] = -1;
695 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500696 free_buffer(mRenderBuffer[i]);
697 mRenderBuffer[i] = NULL;
698 }
699 }
700}
701
702private_handle_t * CopyBit::getCurrentRenderBuffer() {
703 return mRenderBuffer[mCurRenderBufferIndex];
704}
705
706void CopyBit::setReleaseFd(int fd) {
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530707 if(mRelFd[mCurRenderBufferIndex] >=0)
708 close(mRelFd[mCurRenderBufferIndex]);
709 mRelFd[mCurRenderBufferIndex] = dup(fd);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500710}
711
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800712struct copybit_device_t* CopyBit::getCopyBitDevice() {
713 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700714}
715
Saurabh Shah220a30c2013-10-29 16:12:12 -0700716CopyBit::CopyBit(hwc_context_t *ctx, const int& dpy) : mIsModeOn(false),
717 mCopyBitDraw(false), mCurRenderBufferIndex(0) {
718
719 getBufferSizeAndDimensions(ctx->dpyAttr[dpy].xres,
720 ctx->dpyAttr[dpy].yres,
721 HAL_PIXEL_FORMAT_RGBA_8888,
722 mAlignedFBWidth,
723 mAlignedFBHeight);
724
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700725 hw_module_t const *module;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530726 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500727 mRenderBuffer[i] = NULL;
Prabhanjan Kandula5719da42013-11-01 00:14:04 +0530728 mRelFd[i] = -1;
729 }
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530730
731 char value[PROPERTY_VALUE_MAX];
732 property_get("debug.hwc.dynThreshold", value, "2");
733 mDynThreshold = atof(value);
734
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700735 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800736 if(copybit_open(module, &mEngine) < 0) {
737 ALOGE("FATAL ERROR: copybit open failed.");
738 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700739 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800740 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700741 }
742}
Saurabh Shah661a58f2012-08-30 15:30:49 -0700743
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800744CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700745{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500746 freeRenderBuffers();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800747 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700748 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800749 copybit_close(mEngine);
750 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700751 }
752}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700753}; //namespace qhwc