blob: 999fdb2594cde167540aec60a96c525528b154d1 [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 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
Naseer Ahmed45a99602012-07-31 19:15:24 -070021#define DEBUG_COPYBIT 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070022#include <copybit.h>
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080023#include <utils/Timers.h>
Terence Hampson0124cc72013-04-18 23:45:56 -070024#include <mdp_version.h>
Naseer Ahmed45a99602012-07-31 19:15:24 -070025#include "hwc_copybit.h"
26#include "comptype.h"
Naseer Ahmed64b81212013-02-14 10:29:47 -050027#include "gr.h"
Naseer Ahmed74214722013-02-09 08:11:36 -050028
Naseer Ahmed31da0b12012-07-31 18:55:33 -070029namespace qhwc {
30
Naseer Ahmed31da0b12012-07-31 18:55:33 -070031struct range {
32 int current;
33 int end;
34};
35struct region_iterator : public copybit_region_t {
36
37 region_iterator(hwc_region_t region) {
38 mRegion = region;
39 r.end = region.numRects;
40 r.current = 0;
41 this->next = iterate;
42 }
43
44private:
45 static int iterate(copybit_region_t const * self, copybit_rect_t* rect){
46 if (!self || !rect) {
47 ALOGE("iterate invalid parameters");
48 return 0;
49 }
50
51 region_iterator const* me =
52 static_cast<region_iterator const*>(self);
53 if (me->r.current != me->r.end) {
54 rect->l = me->mRegion.rects[me->r.current].left;
55 rect->t = me->mRegion.rects[me->r.current].top;
56 rect->r = me->mRegion.rects[me->r.current].right;
57 rect->b = me->mRegion.rects[me->r.current].bottom;
58 me->r.current++;
59 return 1;
60 }
61 return 0;
62 }
63
64 hwc_region_t mRegion;
65 mutable range r;
66};
67
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080068void CopyBit::reset() {
69 mIsModeOn = false;
70 mCopyBitDraw = false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070071}
72
Naseer Ahmed45a99602012-07-31 19:15:24 -070073bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
74 // return true for non-overlay targets
Terence Hampsond0fd5792013-05-29 11:56:52 -040075 if(ctx->mMDP.hasOverlay && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070076 return false;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070077 }
78 return true;
79}
Naseer Ahmed45a99602012-07-31 19:15:24 -070080
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080081bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
82 hwc_display_contents_1_t *list,
83 int dpy) {
84 int compositionType = qdutils::QCCompositionType::
85 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -070086
Naseer Ahmed45a99602012-07-31 19:15:24 -070087 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
88 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053089 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070090 // this is done based on perf inputs in ICS
91 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080092 int fbWidth = ctx->dpyAttr[dpy].xres;
93 int fbHeight = ctx->dpyAttr[dpy].yres;
94 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -070095 unsigned int renderArea = getRGBRenderingArea(list);
96 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
97 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053098 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070099 return true;
100 }
101 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
102 // MDP composition, use COPYBIT always
103 return true;
104 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
105 // C2D composition, use COPYBIT
106 return true;
107 }
108 return false;
109}
110
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800111unsigned int CopyBit::getRGBRenderingArea
112 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700113 //Calculates total rendering area for RGB layers
114 unsigned int renderArea = 0;
115 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400116 // Skipping last layer since FrameBuffer layer should not affect
117 // which composition to choose
118 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700119 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
120 if (hnd) {
121 if (BUFFER_TYPE_UI == hnd->bufferType) {
122 getLayerResolution(&list->hwLayers[i], w, h);
123 renderArea += (w*h);
124 }
125 }
126 }
127 return renderArea;
128}
129
Pawan Kumar8f45bf62013-08-22 11:45:28 +0530130bool checkNonWormholeRegion(private_handle_t* hnd, hwc_rect_t& rect)
131{
132 unsigned int height = rect.bottom - rect.top;
133 unsigned int width = rect.right - rect.left;
134 copybit_image_t buf;
135 buf.w = ALIGN(hnd->width, 32);
136 buf.h = hnd->height;
137
138 if (buf.h != height || buf.w != width)
139 return false;
140
141 return true;
142}
143
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800144bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
145 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700146
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800147 if(mEngine == NULL) {
148 // No copybit device found - cannot use copybit
149 return false;
150 }
151 int compositionType = qdutils::QCCompositionType::
152 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700153
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800154 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
155 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700156 //GPU/CPU composition, don't change layer composition type
157 return true;
158 }
159
Naseer Ahmed45a99602012-07-31 19:15:24 -0700160 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800161 ALOGE("%s:Invalid Params", __FUNCTION__);
162 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700163 }
164
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800165 if(ctx->listStats[dpy].skipCount) {
166 //GPU will be anyways used
167 return false;
168 }
169
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700170 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700171 // Reached max layers supported by HWC.
172 return false;
173 }
174
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800175 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
176 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500177 LayerProp *layerProp = ctx->layerProp[dpy];
178 size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
179 hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
180 private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;
181
182
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530183 // Following are MDP3 limitations for which we
184 // need to fallback to GPU composition:
Terence Hampsonb1021932013-09-18 11:15:19 -0400185 // 1. Plane alpha is not supported by MDP3.
Terence Hampsonc235bda2013-07-12 12:47:38 -0400186 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
187 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
188 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530189 if (layer->planeAlpha != 0xFF)
190 return true;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400191 }
Pawan Kumar8f45bf62013-08-22 11:45:28 +0530192 /*
193 * Fallback to GPU in MDP3 when NonWormholeRegion is not of frame buffer
194 * size as artifact is seen in WormholeRegion of previous frame.
195 */
196 hwc_rect_t nonWormHoleRegion;
197 getNonWormholeRegion(list, nonWormHoleRegion);
198 if(!checkNonWormholeRegion(fbHnd, nonWormHoleRegion))
199 return true;
200
Terence Hampsonc235bda2013-07-12 12:47:38 -0400201 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500202
203 //Allocate render buffers if they're not allocated
204 if (useCopybitForYUV || useCopybitForRGB) {
205 int ret = allocRenderBuffers(fbHnd->width,
206 fbHnd->height,
207 fbHnd->format);
208 if (ret < 0) {
209 return false;
210 } else {
211 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
212 NUM_RENDER_BUFFERS;
213 }
214 }
215
Terence Hampsond0fd5792013-05-29 11:56:52 -0400216 // We cannot mix copybit layer with layers marked to be drawn on FB
217 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
218 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800219
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530220 mCopyBitDraw = false;
221 if (useCopybitForRGB &&
222 (useCopybitForYUV || !ctx->listStats[dpy].yuvCount)) {
223 mCopyBitDraw = true;
224 // numAppLayers-1, as we iterate till 0th layer index
225 // Mark all layers to be drawn by copybit
226 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500227 layerProp[i].mFlags |= HWC_COPYBIT;
228 list->hwLayers[i].compositionType = HWC_OVERLAY;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700229 }
230 }
Radhika Ranjan Soni14f6c4b2013-08-28 17:04:49 +0530231
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700232 return true;
233}
234
Naseer Ahmed183939d2013-03-14 20:44:17 -0400235int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
236{
237 int ret = 0;
238 copybit_rect_t clear_rect = {rect.left, rect.top,
239 rect.right,
240 rect.bottom};
241
242 copybit_image_t buf;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700243 buf.w = ALIGN(getWidth(hnd),32);
244 buf.h = getHeight(hnd);
Naseer Ahmed183939d2013-03-14 20:44:17 -0400245 buf.format = hnd->format;
246 buf.base = (void *)hnd->base;
247 buf.handle = (native_handle_t *)hnd;
248
249 copybit_device_t *copybit = mEngine;
250 ret = copybit->clear(copybit, &buf, &clear_rect);
251 return ret;
252}
253
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800254bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
255 int dpy, int32_t *fd) {
256 // draw layers marked for COPYBIT
257 int retVal = true;
258 int copybitLayerCount = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500259 LayerProp *layerProp = ctx->layerProp[dpy];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800260
261 if(mCopyBitDraw == false) // there is no layer marked for copybit
Naseer Ahmed64b81212013-02-14 10:29:47 -0500262 return false ;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800263
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800264 //render buffer
Naseer Ahmed64b81212013-02-14 10:29:47 -0500265 private_handle_t *renderBuffer = getCurrentRenderBuffer();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800266 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500267 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800268 return false;
269 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500270
Terence Hampson0124cc72013-04-18 23:45:56 -0700271 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
Terence Hampson65fe4522013-10-17 17:40:03 -0400272 //Wait for the previous frame to complete before rendering onto it
273 if(mRelFd[0] >=0) {
274 sync_wait(mRelFd[0], 1000);
275 close(mRelFd[0]);
276 mRelFd[0] = -1;
277 }
278
Terence Hampson0124cc72013-04-18 23:45:56 -0700279 //Clear the visible region on the render buffer
280 //XXX: Do this only when needed.
281 hwc_rect_t clearRegion;
282 getNonWormholeRegion(list, clearRegion);
283 clear(renderBuffer, clearRegion);
Terence Hampson65fe4522013-10-17 17:40:03 -0400284 } else {
285 if(mRelFd[0] >=0) {
286 copybit_device_t *copybit = getCopyBitDevice();
287 copybit->set_sync(copybit, mRelFd[0]);
288 }
Terence Hampson0124cc72013-04-18 23:45:56 -0700289 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500290 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800291 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
292 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500293 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
294 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800295 continue;
296 }
297 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400298 if (list->hwLayers[i].acquireFenceFd != -1
299 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800300 // Wait for acquire Fence on the App buffers.
301 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
302 if(ret < 0) {
303 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
304 __FUNCTION__, errno, strerror(errno));
305 }
306 close(list->hwLayers[i].acquireFenceFd);
307 list->hwLayers[i].acquireFenceFd = -1;
308 }
309 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530310 renderBuffer, dpy, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800311 copybitLayerCount++;
312 if(retVal < 0) {
313 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
314 }
315 }
316
317 if (copybitLayerCount) {
318 copybit_device_t *copybit = getCopyBitDevice();
319 // Async mode
320 copybit->flush_get_fence(copybit, fd);
Terence Hampson65fe4522013-10-17 17:40:03 -0400321 if(mRelFd[0] >=0 && ctx->mMDP.version == qdutils::MDP_V3_0_4) {
322 close(mRelFd[0]);
323 mRelFd[0] = -1;
324 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800325 }
326 return true;
327}
328
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700329int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530330 private_handle_t *renderBuffer, int dpy, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700331{
332 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400333 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700334 if(!ctx) {
335 ALOGE("%s: null context ", __FUNCTION__);
336 return -1;
337 }
338
339 private_handle_t *hnd = (private_handle_t *)layer->handle;
340 if(!hnd) {
Sushil Chauhan943797c2013-10-21 17:35:55 -0700341 if (layer->flags & HWC_COLOR_FILL) { // Color layer
342 return fillColorUsingCopybit(layer, renderBuffer);
343 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700344 ALOGE("%s: invalid handle", __FUNCTION__);
345 return -1;
346 }
347
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800348 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700349 if(!fbHandle) {
350 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700351 return -1;
352 }
353
354 // Set the copybit source:
355 copybit_image_t src;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700356 src.w = getWidth(hnd);
357 src.h = getHeight(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700358 src.format = hnd->format;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700359
360 // Handle R/B swap
361 if ((layer->flags & HWC_FORMAT_RB_SWAP)) {
362 if (src.format == HAL_PIXEL_FORMAT_RGBA_8888) {
363 src.format = HAL_PIXEL_FORMAT_BGRA_8888;
364 } else if (src.format == HAL_PIXEL_FORMAT_RGBX_8888) {
365 src.format = HAL_PIXEL_FORMAT_BGRX_8888;
366 }
367 }
368
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700369 src.base = (void *)hnd->base;
370 src.handle = (native_handle_t *)layer->handle;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -0700371 src.horiz_padding = src.w - getWidth(hnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700372 // Initialize vertical padding to zero for now,
373 // this needs to change to accomodate vertical stride
374 // if needed in the future
375 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700376
shuoy7c101f92013-08-26 14:48:57 +0800377 int layerTransform = layer->transform ;
378 // When flip and rotation(90) are present alter the flip,
379 // as GPU is doing the flip and rotation in opposite order
380 // to that of MDP3.0
381 // For 270 degrees, we get 90 + (H+V) which is same as doing
382 // flip first and then rotation (H+V) + 90
383 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
384 if (((layer->transform& HAL_TRANSFORM_FLIP_H) ||
385 (layer->transform & HAL_TRANSFORM_FLIP_V)) &&
386 (layer->transform & HAL_TRANSFORM_ROT_90) &&
387 !(layer->transform == HAL_TRANSFORM_ROT_270)){
388 if(layer->transform & HAL_TRANSFORM_FLIP_H){
389 layerTransform ^= HAL_TRANSFORM_FLIP_H;
390 layerTransform |= HAL_TRANSFORM_FLIP_V;
391 }
392 if(layer->transform & HAL_TRANSFORM_FLIP_V){
393 layerTransform ^= HAL_TRANSFORM_FLIP_V;
394 layerTransform |= HAL_TRANSFORM_FLIP_H;
395 }
396 }
397 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700398 // Copybit source rect
Saurabh Shah62e1d732013-09-17 10:44:05 -0700399 hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700400 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
401 sourceCrop.right,
402 sourceCrop.bottom};
403
404 // Copybit destination rect
405 hwc_rect_t displayFrame = layer->displayFrame;
406 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
407 displayFrame.right,
408 displayFrame.bottom};
409
410 // Copybit dst
411 copybit_image_t dst;
412 dst.w = ALIGN(fbHandle->width,32);
413 dst.h = fbHandle->height;
414 dst.format = fbHandle->format;
415 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800416 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700417
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800418 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700419
420 int32_t screen_w = displayFrame.right - displayFrame.left;
421 int32_t screen_h = displayFrame.bottom - displayFrame.top;
422 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
423 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
424
425 // Copybit dst
426 float copybitsMaxScale =
427 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
428 float copybitsMinScale =
429 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
430
431 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
432 (layer->transform == HWC_TRANSFORM_ROT_270)) {
433 //swap screen width and height
434 int tmp = screen_w;
435 screen_w = screen_h;
436 screen_h = tmp;
437 }
438 private_handle_t *tmpHnd = NULL;
439
440 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
441 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
442 screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
443 src_crop_width,screen_w,src_crop_width);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700444 return -1;
445 }
446
447 float dsdx = (float)screen_w/src_crop_width;
448 float dtdy = (float)screen_h/src_crop_height;
449
450 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
451 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
452 if(dsdx > scaleLimitMax ||
453 dtdy > scaleLimitMax ||
454 dsdx < 1/scaleLimitMin ||
455 dtdy < 1/scaleLimitMin) {
Terence Hampson3c560b92013-09-03 16:58:02 -0400456 ALOGW("%s: greater than max supported size dsdx=%f dtdy=%f \
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700457 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
458 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700459 return -1;
460 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400461 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700462 if(dsdx > copybitsMaxScale ||
463 dtdy > copybitsMaxScale ||
464 dsdx < 1/copybitsMinScale ||
465 dtdy < 1/copybitsMinScale){
466 // The requested scale is out of the range the hardware
467 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400468 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700469 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
470 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
471 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
472 src_crop_width,src_crop_height);
473
474 //Driver makes width and height as even
475 //that may cause wrong calculation of the ratio
476 //in display and crop.Hence we make
477 //crop width and height as even.
478 src_crop_width = (src_crop_width/2)*2;
479 src_crop_height = (src_crop_height/2)*2;
480
481 int tmp_w = src_crop_width;
482 int tmp_h = src_crop_height;
483
484 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
485 tmp_w = src_crop_width*copybitsMaxScale;
486 tmp_h = src_crop_height*copybitsMaxScale;
487 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
488 tmp_w = src_crop_width/copybitsMinScale;
489 tmp_h = src_crop_height/copybitsMinScale;
490 tmp_w = (tmp_w/2)*2;
491 tmp_h = (tmp_h/2)*2;
492 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400493 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700494
Naseer Ahmed64b81212013-02-14 10:29:47 -0500495 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400496 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700497
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400498 // We do not want copybit to generate alpha values from nothing
499 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
500 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
501 format = HAL_PIXEL_FORMAT_RGBX_8888;
502 }
503 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage)){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700504 copybit_image_t tmp_dst;
505 copybit_rect_t tmp_rect;
506 tmp_dst.w = tmp_w;
507 tmp_dst.h = tmp_h;
508 tmp_dst.format = tmpHnd->format;
509 tmp_dst.handle = tmpHnd;
510 tmp_dst.horiz_padding = src.horiz_padding;
511 tmp_dst.vert_padding = src.vert_padding;
512 tmp_rect.l = 0;
513 tmp_rect.t = 0;
514 tmp_rect.r = tmp_dst.w;
515 tmp_rect.b = tmp_dst.h;
516 //create one clip region
517 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
518 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
519 region_iterator tmp_it(tmp_hwc_reg);
520 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700521 //TODO: once, we are able to read layer alpha, update this
522 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400523 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700524 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
525 &srcRect, &tmp_it);
526 if(err < 0){
527 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
528 __LINE__);
529 if(tmpHnd)
530 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700531 return err;
532 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400533 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400534 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400535 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400536 close(acquireFd);
537 acquireFd = -1;
538 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700539 // copy new src and src rect crop
540 src = tmp_dst;
541 srcRect = tmp_rect;
542 }
543 }
544 // Copybit region
545 hwc_region_t region = layer->visibleRegionScreen;
546 region_iterator copybitRegion(region);
547
548 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
549 renderBuffer->width);
550 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
551 renderBuffer->height);
552 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
shuoy7c101f92013-08-26 14:48:57 +0800553 layerTransform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700554 //TODO: once, we are able to read layer alpha, update this
555 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800556 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
557 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700558 copybit->set_parameter(copybit, COPYBIT_DITHER,
559 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
560 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530561 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
562 COPYBIT_ENABLE : COPYBIT_DISABLE);
563
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700564 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
565 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400566 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700567 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
568 &copybitRegion);
569 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
570 COPYBIT_DISABLE);
571
Terence Hampsonadf47302013-05-23 12:21:02 -0400572 if(tmpHnd) {
573 if (ctx->mMDP.version < qdutils::MDP_V4_0){
574 int ret = -1, releaseFd;
575 // we need to wait for the buffer before freeing
576 copybit->flush_get_fence(copybit, &releaseFd);
577 ret = sync_wait(releaseFd, 1000);
578 if(ret < 0) {
579 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
580 __FUNCTION__, errno, strerror(errno));
581 }
582 close(releaseFd);
583 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700584 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400585 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700586
587 if(err < 0)
588 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700589 return err;
590}
591
Sushil Chauhan943797c2013-10-21 17:35:55 -0700592int CopyBit::fillColorUsingCopybit(hwc_layer_1_t *layer,
593 private_handle_t *renderBuffer)
594{
595 if (!renderBuffer) {
596 ALOGE("%s: Render Buffer is NULL", __FUNCTION__);
597 return -1;
598 }
599
600 // Copybit dst
601 copybit_image_t dst;
602 dst.w = ALIGN(renderBuffer->width, 32);
603 dst.h = renderBuffer->height;
604 dst.format = renderBuffer->format;
605 dst.base = (void *)renderBuffer->base;
606 dst.handle = (native_handle_t *)renderBuffer;
607
608 // Copybit dst rect
609 hwc_rect_t displayFrame = layer->displayFrame;
610 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
611 displayFrame.right, displayFrame.bottom};
612
613 uint32_t color = layer->transform;
614 copybit_device_t *copybit = mEngine;
615 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
616 renderBuffer->width);
617 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
618 renderBuffer->height);
619 copybit->set_parameter(copybit, COPYBIT_DITHER,
620 (dst.format == HAL_PIXEL_FORMAT_RGB_565) ?
621 COPYBIT_ENABLE : COPYBIT_DISABLE);
622 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE, layer->blending);
623 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, layer->planeAlpha);
624 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_ENABLE);
625 int res = copybit->fill_color(copybit, &dst, &dstRect, color);
626 copybit->set_parameter(copybit,COPYBIT_BLIT_TO_FRAMEBUFFER,COPYBIT_DISABLE);
627 return res;
628}
629
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700630void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700631 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700632{
633 hwc_rect_t displayFrame = layer->displayFrame;
634
635 width = displayFrame.right - displayFrame.left;
636 height = displayFrame.bottom - displayFrame.top;
637}
638
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800639bool CopyBit::validateParams(hwc_context_t *ctx,
640 const hwc_display_contents_1_t *list) {
641 //Validate parameters
642 if (!ctx) {
643 ALOGE("%s:Invalid HWC context", __FUNCTION__);
644 return false;
645 } else if (!list) {
646 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
647 return false;
648 }
649 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700650}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700651
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700652
Naseer Ahmed64b81212013-02-14 10:29:47 -0500653int CopyBit::allocRenderBuffers(int w, int h, int f)
654{
655 int ret = 0;
656 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
657 if (mRenderBuffer[i] == NULL) {
658 ret = alloc_buffer(&mRenderBuffer[i],
659 w, h, f,
660 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
661 }
662 if(ret < 0) {
663 freeRenderBuffers();
664 break;
665 }
666 }
667 return ret;
668}
669
670void CopyBit::freeRenderBuffers()
671{
672 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
673 if(mRenderBuffer[i]) {
674 free_buffer(mRenderBuffer[i]);
675 mRenderBuffer[i] = NULL;
676 }
677 }
678}
679
680private_handle_t * CopyBit::getCurrentRenderBuffer() {
681 return mRenderBuffer[mCurRenderBufferIndex];
682}
683
684void CopyBit::setReleaseFd(int fd) {
685 if(mRelFd[0] >=0)
686 close(mRelFd[0]);
687 mRelFd[0] = mRelFd[1];
688 mRelFd[1] = dup(fd);
689}
690
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800691struct copybit_device_t* CopyBit::getCopyBitDevice() {
692 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700693}
694
Naseer Ahmed64b81212013-02-14 10:29:47 -0500695CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
696 mCurRenderBufferIndex(0){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700697 hw_module_t const *module;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500698 for (int i = 0; i < NUM_RENDER_BUFFERS; i++)
699 mRenderBuffer[i] = NULL;
700 mRelFd[0] = -1;
701 mRelFd[1] = -1;
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530702
703 char value[PROPERTY_VALUE_MAX];
704 property_get("debug.hwc.dynThreshold", value, "2");
705 mDynThreshold = atof(value);
706
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700707 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800708 if(copybit_open(module, &mEngine) < 0) {
709 ALOGE("FATAL ERROR: copybit open failed.");
710 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700711 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800712 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700713 }
714}
Saurabh Shah661a58f2012-08-30 15:30:49 -0700715
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800716CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700717{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500718 freeRenderBuffers();
719 if(mRelFd[0] >=0)
720 close(mRelFd[0]);
721 if(mRelFd[1] >=0)
722 close(mRelFd[1]);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800723 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700724 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800725 copybit_close(mEngine);
726 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700727 }
728}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700729}; //namespace qhwc