blob: 068e28a6e56bbeeb2505bc029d283b988985f199 [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
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800130bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
131 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700132
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800133 if(mEngine == NULL) {
134 // No copybit device found - cannot use copybit
135 return false;
136 }
137 int compositionType = qdutils::QCCompositionType::
138 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700139
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800140 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
141 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700142 //GPU/CPU composition, don't change layer composition type
143 return true;
144 }
145
Naseer Ahmed45a99602012-07-31 19:15:24 -0700146 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800147 ALOGE("%s:Invalid Params", __FUNCTION__);
148 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700149 }
150
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800151 if(ctx->listStats[dpy].skipCount) {
152 //GPU will be anyways used
153 return false;
154 }
155
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700156 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700157 // Reached max layers supported by HWC.
158 return false;
159 }
160
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800161 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
162 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500163 LayerProp *layerProp = ctx->layerProp[dpy];
164 size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
165 hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
166 private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;
167
168
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530169 // Following are MDP3 limitations for which we
170 // need to fallback to GPU composition:
171 // 1. HW issues with mdp3 and rotation.
172 // 2. Plane alpha is not supported by MDP3.
Terence Hampsonc235bda2013-07-12 12:47:38 -0400173 if (qdutils::MDPVersion::getInstance().getMDPVersion() < 400) {
174 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
175 hwc_layer_1_t *layer = (hwc_layer_1_t *) &list->hwLayers[i];
176 if ((layer->transform & (HAL_TRANSFORM_FLIP_H |
177 HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)) &&
178 ((layer->displayFrame.bottom - layer->displayFrame.top) % 16 ||
179 (layer->displayFrame.right - layer->displayFrame.left) % 16))
180 return true;
Radhika Ranjan Soni46cf4e92013-08-06 16:40:05 +0530181 if (layer->planeAlpha != 0xFF)
182 return true;
Terence Hampsonc235bda2013-07-12 12:47:38 -0400183 }
184 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500185
186 //Allocate render buffers if they're not allocated
187 if (useCopybitForYUV || useCopybitForRGB) {
188 int ret = allocRenderBuffers(fbHnd->width,
189 fbHnd->height,
190 fbHnd->format);
191 if (ret < 0) {
192 return false;
193 } else {
194 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
195 NUM_RENDER_BUFFERS;
196 }
197 }
198
Terence Hampsond0fd5792013-05-29 11:56:52 -0400199 // We cannot mix copybit layer with layers marked to be drawn on FB
200 if (!useCopybitForYUV && ctx->listStats[dpy].yuvCount)
201 return true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800202
203 // numAppLayers-1, as we iterate till 0th layer index
204 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700205 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
206
Naseer Ahmed64b81212013-02-14 10:29:47 -0500207 if ((hnd->bufferType == BUFFER_TYPE_VIDEO && useCopybitForYUV) ||
208 (hnd->bufferType == BUFFER_TYPE_UI && useCopybitForRGB)) {
209 layerProp[i].mFlags |= HWC_COPYBIT;
210 list->hwLayers[i].compositionType = HWC_OVERLAY;
211 mCopyBitDraw = true;
212 } else {
213 // We currently cannot mix copybit layers with layers marked to
214 // be drawn on the framebuffer or that are on the layer cache.
215 mCopyBitDraw = false;
216 //There is no need to reset layer properties here as we return in
217 //draw if mCopyBitDraw is false
Terence Hampsond0fd5792013-05-29 11:56:52 -0400218 break;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700219 }
220 }
221 return true;
222}
223
Naseer Ahmed183939d2013-03-14 20:44:17 -0400224int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
225{
226 int ret = 0;
227 copybit_rect_t clear_rect = {rect.left, rect.top,
228 rect.right,
229 rect.bottom};
230
231 copybit_image_t buf;
232 buf.w = ALIGN(hnd->width,32);
233 buf.h = hnd->height;
234 buf.format = hnd->format;
235 buf.base = (void *)hnd->base;
236 buf.handle = (native_handle_t *)hnd;
237
238 copybit_device_t *copybit = mEngine;
239 ret = copybit->clear(copybit, &buf, &clear_rect);
240 return ret;
241}
242
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800243bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
244 int dpy, int32_t *fd) {
245 // draw layers marked for COPYBIT
246 int retVal = true;
247 int copybitLayerCount = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500248 LayerProp *layerProp = ctx->layerProp[dpy];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800249
250 if(mCopyBitDraw == false) // there is no layer marked for copybit
Naseer Ahmed64b81212013-02-14 10:29:47 -0500251 return false ;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800252
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800253 //render buffer
Naseer Ahmed64b81212013-02-14 10:29:47 -0500254 private_handle_t *renderBuffer = getCurrentRenderBuffer();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800255 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500256 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800257 return false;
258 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500259
260 //Wait for the previous frame to complete before rendering onto it
261 if(mRelFd[0] >=0) {
262 sync_wait(mRelFd[0], 1000);
263 close(mRelFd[0]);
264 mRelFd[0] = -1;
265 }
Naseer Ahmed183939d2013-03-14 20:44:17 -0400266
Terence Hampson0124cc72013-04-18 23:45:56 -0700267 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
268 //Clear the visible region on the render buffer
269 //XXX: Do this only when needed.
270 hwc_rect_t clearRegion;
271 getNonWormholeRegion(list, clearRegion);
272 clear(renderBuffer, clearRegion);
273 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500274 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800275 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
276 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500277 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
278 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800279 continue;
280 }
281 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400282 if (list->hwLayers[i].acquireFenceFd != -1
283 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800284 // Wait for acquire Fence on the App buffers.
285 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
286 if(ret < 0) {
287 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
288 __FUNCTION__, errno, strerror(errno));
289 }
290 close(list->hwLayers[i].acquireFenceFd);
291 list->hwLayers[i].acquireFenceFd = -1;
292 }
293 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530294 renderBuffer, dpy, !i);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800295 copybitLayerCount++;
296 if(retVal < 0) {
297 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
298 }
299 }
300
301 if (copybitLayerCount) {
302 copybit_device_t *copybit = getCopyBitDevice();
303 // Async mode
304 copybit->flush_get_fence(copybit, fd);
305 }
306 return true;
307}
308
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700309int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530310 private_handle_t *renderBuffer, int dpy, bool isFG)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700311{
312 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400313 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700314 if(!ctx) {
315 ALOGE("%s: null context ", __FUNCTION__);
316 return -1;
317 }
318
319 private_handle_t *hnd = (private_handle_t *)layer->handle;
320 if(!hnd) {
321 ALOGE("%s: invalid handle", __FUNCTION__);
322 return -1;
323 }
324
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800325 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700326 if(!fbHandle) {
327 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700328 return -1;
329 }
330
331 // Set the copybit source:
332 copybit_image_t src;
333 src.w = hnd->width;
334 src.h = hnd->height;
335 src.format = hnd->format;
336 src.base = (void *)hnd->base;
337 src.handle = (native_handle_t *)layer->handle;
338 src.horiz_padding = src.w - hnd->width;
339 // Initialize vertical padding to zero for now,
340 // this needs to change to accomodate vertical stride
341 // if needed in the future
342 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700343
344 // Copybit source rect
345 hwc_rect_t sourceCrop = layer->sourceCrop;
346 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
347 sourceCrop.right,
348 sourceCrop.bottom};
349
350 // Copybit destination rect
351 hwc_rect_t displayFrame = layer->displayFrame;
352 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
353 displayFrame.right,
354 displayFrame.bottom};
355
356 // Copybit dst
357 copybit_image_t dst;
358 dst.w = ALIGN(fbHandle->width,32);
359 dst.h = fbHandle->height;
360 dst.format = fbHandle->format;
361 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800362 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700363
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800364 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700365
366 int32_t screen_w = displayFrame.right - displayFrame.left;
367 int32_t screen_h = displayFrame.bottom - displayFrame.top;
368 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
369 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
370
371 // Copybit dst
372 float copybitsMaxScale =
373 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
374 float copybitsMinScale =
375 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
376
377 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
378 (layer->transform == HWC_TRANSFORM_ROT_270)) {
379 //swap screen width and height
380 int tmp = screen_w;
381 screen_w = screen_h;
382 screen_h = tmp;
383 }
384 private_handle_t *tmpHnd = NULL;
385
386 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
387 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
388 screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
389 src_crop_width,screen_w,src_crop_width);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700390 return -1;
391 }
392
393 float dsdx = (float)screen_w/src_crop_width;
394 float dtdy = (float)screen_h/src_crop_height;
395
396 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
397 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
398 if(dsdx > scaleLimitMax ||
399 dtdy > scaleLimitMax ||
400 dsdx < 1/scaleLimitMin ||
401 dtdy < 1/scaleLimitMin) {
402 ALOGE("%s: greater than max supported size dsdx=%f dtdy=%f \
403 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
404 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700405 return -1;
406 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400407 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700408 if(dsdx > copybitsMaxScale ||
409 dtdy > copybitsMaxScale ||
410 dsdx < 1/copybitsMinScale ||
411 dtdy < 1/copybitsMinScale){
412 // The requested scale is out of the range the hardware
413 // can support.
Terence Hampson663dfa72013-08-08 12:35:41 -0400414 ALOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700415 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
416 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
417 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
418 src_crop_width,src_crop_height);
419
420 //Driver makes width and height as even
421 //that may cause wrong calculation of the ratio
422 //in display and crop.Hence we make
423 //crop width and height as even.
424 src_crop_width = (src_crop_width/2)*2;
425 src_crop_height = (src_crop_height/2)*2;
426
427 int tmp_w = src_crop_width;
428 int tmp_h = src_crop_height;
429
430 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
431 tmp_w = src_crop_width*copybitsMaxScale;
432 tmp_h = src_crop_height*copybitsMaxScale;
433 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
434 tmp_w = src_crop_width/copybitsMinScale;
435 tmp_h = src_crop_height/copybitsMinScale;
436 tmp_w = (tmp_w/2)*2;
437 tmp_h = (tmp_h/2)*2;
438 }
Terence Hampson663dfa72013-08-08 12:35:41 -0400439 ALOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700440
Naseer Ahmed64b81212013-02-14 10:29:47 -0500441 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400442 int format = fbHandle->format;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700443
Terence Hampsonb6a123a2013-07-18 11:54:16 -0400444 // We do not want copybit to generate alpha values from nothing
445 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
446 src.format != HAL_PIXEL_FORMAT_RGBA_8888) {
447 format = HAL_PIXEL_FORMAT_RGBX_8888;
448 }
449 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, format, usage)){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700450 copybit_image_t tmp_dst;
451 copybit_rect_t tmp_rect;
452 tmp_dst.w = tmp_w;
453 tmp_dst.h = tmp_h;
454 tmp_dst.format = tmpHnd->format;
455 tmp_dst.handle = tmpHnd;
456 tmp_dst.horiz_padding = src.horiz_padding;
457 tmp_dst.vert_padding = src.vert_padding;
458 tmp_rect.l = 0;
459 tmp_rect.t = 0;
460 tmp_rect.r = tmp_dst.w;
461 tmp_rect.b = tmp_dst.h;
462 //create one clip region
463 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
464 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
465 region_iterator tmp_it(tmp_hwc_reg);
466 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700467 //TODO: once, we are able to read layer alpha, update this
468 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400469 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700470 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
471 &srcRect, &tmp_it);
472 if(err < 0){
473 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
474 __LINE__);
475 if(tmpHnd)
476 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700477 return err;
478 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400479 // use release fence as aquire fd for next stretch
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400480 if (ctx->mMDP.version < qdutils::MDP_V4_0) {
Terence Hampsonadf47302013-05-23 12:21:02 -0400481 copybit->flush_get_fence(copybit, &acquireFd);
Terence Hampson1d8db6f2013-07-17 11:05:36 -0400482 close(acquireFd);
483 acquireFd = -1;
484 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700485 // copy new src and src rect crop
486 src = tmp_dst;
487 srcRect = tmp_rect;
488 }
489 }
490 // Copybit region
491 hwc_region_t region = layer->visibleRegionScreen;
492 region_iterator copybitRegion(region);
493
494 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
495 renderBuffer->width);
496 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
497 renderBuffer->height);
498 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
499 layer->transform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700500 //TODO: once, we are able to read layer alpha, update this
501 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800502 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
503 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700504 copybit->set_parameter(copybit, COPYBIT_DITHER,
505 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
506 COPYBIT_ENABLE : COPYBIT_DISABLE);
Shivaraj Shettye86506a2013-08-06 12:56:30 +0530507 copybit->set_parameter(copybit, COPYBIT_FG_LAYER, isFG ?
508 COPYBIT_ENABLE : COPYBIT_DISABLE);
509
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700510 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
511 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400512 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700513 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
514 &copybitRegion);
515 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
516 COPYBIT_DISABLE);
517
Terence Hampsonadf47302013-05-23 12:21:02 -0400518 if(tmpHnd) {
519 if (ctx->mMDP.version < qdutils::MDP_V4_0){
520 int ret = -1, releaseFd;
521 // we need to wait for the buffer before freeing
522 copybit->flush_get_fence(copybit, &releaseFd);
523 ret = sync_wait(releaseFd, 1000);
524 if(ret < 0) {
525 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
526 __FUNCTION__, errno, strerror(errno));
527 }
528 close(releaseFd);
529 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700530 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400531 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700532
533 if(err < 0)
534 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700535 return err;
536}
537
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700538void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700539 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700540{
541 hwc_rect_t displayFrame = layer->displayFrame;
542
543 width = displayFrame.right - displayFrame.left;
544 height = displayFrame.bottom - displayFrame.top;
545}
546
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800547bool CopyBit::validateParams(hwc_context_t *ctx,
548 const hwc_display_contents_1_t *list) {
549 //Validate parameters
550 if (!ctx) {
551 ALOGE("%s:Invalid HWC context", __FUNCTION__);
552 return false;
553 } else if (!list) {
554 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
555 return false;
556 }
557 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700558}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700559
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700560
Naseer Ahmed64b81212013-02-14 10:29:47 -0500561int CopyBit::allocRenderBuffers(int w, int h, int f)
562{
563 int ret = 0;
564 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
565 if (mRenderBuffer[i] == NULL) {
566 ret = alloc_buffer(&mRenderBuffer[i],
567 w, h, f,
568 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
569 }
570 if(ret < 0) {
571 freeRenderBuffers();
572 break;
573 }
574 }
575 return ret;
576}
577
578void CopyBit::freeRenderBuffers()
579{
580 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
581 if(mRenderBuffer[i]) {
582 free_buffer(mRenderBuffer[i]);
583 mRenderBuffer[i] = NULL;
584 }
585 }
586}
587
588private_handle_t * CopyBit::getCurrentRenderBuffer() {
589 return mRenderBuffer[mCurRenderBufferIndex];
590}
591
592void CopyBit::setReleaseFd(int fd) {
593 if(mRelFd[0] >=0)
594 close(mRelFd[0]);
595 mRelFd[0] = mRelFd[1];
596 mRelFd[1] = dup(fd);
597}
598
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800599struct copybit_device_t* CopyBit::getCopyBitDevice() {
600 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700601}
602
Naseer Ahmed64b81212013-02-14 10:29:47 -0500603CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
604 mCurRenderBufferIndex(0){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700605 hw_module_t const *module;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500606 for (int i = 0; i < NUM_RENDER_BUFFERS; i++)
607 mRenderBuffer[i] = NULL;
608 mRelFd[0] = -1;
609 mRelFd[1] = -1;
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530610
611 char value[PROPERTY_VALUE_MAX];
612 property_get("debug.hwc.dynThreshold", value, "2");
613 mDynThreshold = atof(value);
614
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700615 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800616 if(copybit_open(module, &mEngine) < 0) {
617 ALOGE("FATAL ERROR: copybit open failed.");
618 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700619 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800620 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700621 }
622}
Saurabh Shah661a58f2012-08-30 15:30:49 -0700623
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800624CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700625{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500626 freeRenderBuffers();
627 if(mRelFd[0] >=0)
628 close(mRelFd[0]);
629 if(mRelFd[1] >=0)
630 close(mRelFd[1]);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800631 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700632 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800633 copybit_close(mEngine);
634 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700635 }
636}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700637}; //namespace qhwc