blob: d402e0c0d896d3ffe1c201d9b137c55090a992df [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
75 if(ctx->mMDP.hasOverlay) {
76 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
87 if ((compositionType & qdutils::COMPOSITION_TYPE_C2D) ||
88 (compositionType & qdutils::COMPOSITION_TYPE_DYN)) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080089 if(ctx->listStats[dpy].yuvCount) {
Naseer Ahmed45a99602012-07-31 19:15:24 -070090 //Overlay up & running. Dont use COPYBIT for RGB layers.
Naseer Ahmed45a99602012-07-31 19:15:24 -070091 return false;
92 }
93 }
94
95 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
96 // DYN Composition:
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +053097 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
Naseer Ahmed45a99602012-07-31 19:15:24 -070098 // this is done based on perf inputs in ICS
99 // TODO: Above condition needs to be re-evaluated in JB
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800100 int fbWidth = ctx->dpyAttr[dpy].xres;
101 int fbHeight = ctx->dpyAttr[dpy].yres;
102 unsigned int fbArea = (fbWidth * fbHeight);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700103 unsigned int renderArea = getRGBRenderingArea(list);
104 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
105 __FUNCTION__, renderArea, fbArea);
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530106 if (renderArea < (mDynThreshold * fbArea)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700107 return true;
108 }
109 } else if ((compositionType & qdutils::COMPOSITION_TYPE_MDP)) {
110 // MDP composition, use COPYBIT always
111 return true;
112 } else if ((compositionType & qdutils::COMPOSITION_TYPE_C2D)) {
113 // C2D composition, use COPYBIT
114 return true;
115 }
116 return false;
117}
118
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800119unsigned int CopyBit::getRGBRenderingArea
120 (const hwc_display_contents_1_t *list) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700121 //Calculates total rendering area for RGB layers
122 unsigned int renderArea = 0;
123 unsigned int w=0, h=0;
Terence Hampsonef379d42013-05-21 10:38:01 -0400124 // Skipping last layer since FrameBuffer layer should not affect
125 // which composition to choose
126 for (unsigned int i=0; i<list->numHwLayers -1; i++) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700127 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
128 if (hnd) {
129 if (BUFFER_TYPE_UI == hnd->bufferType) {
130 getLayerResolution(&list->hwLayers[i], w, h);
131 renderArea += (w*h);
132 }
133 }
134 }
135 return renderArea;
136}
137
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800138bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
139 int dpy) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700140
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800141 if(mEngine == NULL) {
142 // No copybit device found - cannot use copybit
143 return false;
144 }
145 int compositionType = qdutils::QCCompositionType::
146 getInstance().getCompositionType();
Naseer Ahmed45a99602012-07-31 19:15:24 -0700147
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800148 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
149 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700150 //GPU/CPU composition, don't change layer composition type
151 return true;
152 }
153
Naseer Ahmed45a99602012-07-31 19:15:24 -0700154 if(!(validateParams(ctx, list))) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800155 ALOGE("%s:Invalid Params", __FUNCTION__);
156 return false;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700157 }
158
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800159 if(ctx->listStats[dpy].skipCount) {
160 //GPU will be anyways used
161 return false;
162 }
163
Sushil Chauhanb00f59d2013-04-29 18:35:54 -0700164 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_LAYERS) {
165 // Reached max layers supported by HWC.
166 return false;
167 }
168
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800169 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
170 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500171 LayerProp *layerProp = ctx->layerProp[dpy];
172 size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
173 hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
174 private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;
175
176
177
178 //Allocate render buffers if they're not allocated
179 if (useCopybitForYUV || useCopybitForRGB) {
180 int ret = allocRenderBuffers(fbHnd->width,
181 fbHnd->height,
182 fbHnd->format);
183 if (ret < 0) {
184 return false;
185 } else {
186 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
187 NUM_RENDER_BUFFERS;
188 }
189 }
190
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800191
192 // numAppLayers-1, as we iterate till 0th layer index
193 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
Naseer Ahmed45a99602012-07-31 19:15:24 -0700194 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
195
Naseer Ahmed64b81212013-02-14 10:29:47 -0500196 if ((hnd->bufferType == BUFFER_TYPE_VIDEO && useCopybitForYUV) ||
197 (hnd->bufferType == BUFFER_TYPE_UI && useCopybitForRGB)) {
198 layerProp[i].mFlags |= HWC_COPYBIT;
199 list->hwLayers[i].compositionType = HWC_OVERLAY;
200 mCopyBitDraw = true;
201 } else {
202 // We currently cannot mix copybit layers with layers marked to
203 // be drawn on the framebuffer or that are on the layer cache.
204 mCopyBitDraw = false;
205 //There is no need to reset layer properties here as we return in
206 //draw if mCopyBitDraw is false
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700207 }
208 }
209 return true;
210}
211
Naseer Ahmed183939d2013-03-14 20:44:17 -0400212int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
213{
214 int ret = 0;
215 copybit_rect_t clear_rect = {rect.left, rect.top,
216 rect.right,
217 rect.bottom};
218
219 copybit_image_t buf;
220 buf.w = ALIGN(hnd->width,32);
221 buf.h = hnd->height;
222 buf.format = hnd->format;
223 buf.base = (void *)hnd->base;
224 buf.handle = (native_handle_t *)hnd;
225
226 copybit_device_t *copybit = mEngine;
227 ret = copybit->clear(copybit, &buf, &clear_rect);
228 return ret;
229}
230
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800231bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
232 int dpy, int32_t *fd) {
233 // draw layers marked for COPYBIT
234 int retVal = true;
235 int copybitLayerCount = 0;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500236 LayerProp *layerProp = ctx->layerProp[dpy];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800237
238 if(mCopyBitDraw == false) // there is no layer marked for copybit
Naseer Ahmed64b81212013-02-14 10:29:47 -0500239 return false ;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800240
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800241 //render buffer
Naseer Ahmed64b81212013-02-14 10:29:47 -0500242 private_handle_t *renderBuffer = getCurrentRenderBuffer();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800243 if (!renderBuffer) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500244 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800245 return false;
246 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500247
248 //Wait for the previous frame to complete before rendering onto it
249 if(mRelFd[0] >=0) {
250 sync_wait(mRelFd[0], 1000);
251 close(mRelFd[0]);
252 mRelFd[0] = -1;
253 }
Naseer Ahmed183939d2013-03-14 20:44:17 -0400254
Terence Hampson0124cc72013-04-18 23:45:56 -0700255 if (ctx->mMDP.version >= qdutils::MDP_V4_0) {
256 //Clear the visible region on the render buffer
257 //XXX: Do this only when needed.
258 hwc_rect_t clearRegion;
259 getNonWormholeRegion(list, clearRegion);
260 clear(renderBuffer, clearRegion);
261 }
Naseer Ahmed64b81212013-02-14 10:29:47 -0500262 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800263 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
264 hwc_layer_1_t *layer = &list->hwLayers[i];
Naseer Ahmed64b81212013-02-14 10:29:47 -0500265 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
266 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800267 continue;
268 }
269 int ret = -1;
Terence Hampsonadf47302013-05-23 12:21:02 -0400270 if (list->hwLayers[i].acquireFenceFd != -1
271 && ctx->mMDP.version >= qdutils::MDP_V4_0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800272 // Wait for acquire Fence on the App buffers.
273 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
274 if(ret < 0) {
275 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
276 __FUNCTION__, errno, strerror(errno));
277 }
278 close(list->hwLayers[i].acquireFenceFd);
279 list->hwLayers[i].acquireFenceFd = -1;
280 }
281 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
282 renderBuffer, dpy);
283 copybitLayerCount++;
284 if(retVal < 0) {
285 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
286 }
287 }
288
289 if (copybitLayerCount) {
290 copybit_device_t *copybit = getCopyBitDevice();
291 // Async mode
292 copybit->flush_get_fence(copybit, fd);
293 }
294 return true;
295}
296
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700297int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800298 private_handle_t *renderBuffer, int dpy)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700299{
300 hwc_context_t* ctx = (hwc_context_t*)(dev);
Terence Hampsonadf47302013-05-23 12:21:02 -0400301 int err = 0, acquireFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700302 if(!ctx) {
303 ALOGE("%s: null context ", __FUNCTION__);
304 return -1;
305 }
306
307 private_handle_t *hnd = (private_handle_t *)layer->handle;
308 if(!hnd) {
309 ALOGE("%s: invalid handle", __FUNCTION__);
310 return -1;
311 }
312
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800313 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700314 if(!fbHandle) {
315 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700316 return -1;
317 }
318
319 // Set the copybit source:
320 copybit_image_t src;
321 src.w = hnd->width;
322 src.h = hnd->height;
323 src.format = hnd->format;
324 src.base = (void *)hnd->base;
325 src.handle = (native_handle_t *)layer->handle;
326 src.horiz_padding = src.w - hnd->width;
327 // Initialize vertical padding to zero for now,
328 // this needs to change to accomodate vertical stride
329 // if needed in the future
330 src.vert_padding = 0;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700331
332 // Copybit source rect
333 hwc_rect_t sourceCrop = layer->sourceCrop;
334 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
335 sourceCrop.right,
336 sourceCrop.bottom};
337
338 // Copybit destination rect
339 hwc_rect_t displayFrame = layer->displayFrame;
340 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
341 displayFrame.right,
342 displayFrame.bottom};
343
344 // Copybit dst
345 copybit_image_t dst;
346 dst.w = ALIGN(fbHandle->width,32);
347 dst.h = fbHandle->height;
348 dst.format = fbHandle->format;
349 dst.base = (void *)fbHandle->base;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800350 dst.handle = (native_handle_t *)fbHandle;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700351
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800352 copybit_device_t *copybit = mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700353
354 int32_t screen_w = displayFrame.right - displayFrame.left;
355 int32_t screen_h = displayFrame.bottom - displayFrame.top;
356 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
357 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
358
359 // Copybit dst
360 float copybitsMaxScale =
361 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
362 float copybitsMinScale =
363 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
364
365 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
366 (layer->transform == HWC_TRANSFORM_ROT_270)) {
367 //swap screen width and height
368 int tmp = screen_w;
369 screen_w = screen_h;
370 screen_h = tmp;
371 }
372 private_handle_t *tmpHnd = NULL;
373
374 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
375 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
376 screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
377 src_crop_width,screen_w,src_crop_width);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700378 return -1;
379 }
380
381 float dsdx = (float)screen_w/src_crop_width;
382 float dtdy = (float)screen_h/src_crop_height;
383
384 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
385 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
386 if(dsdx > scaleLimitMax ||
387 dtdy > scaleLimitMax ||
388 dsdx < 1/scaleLimitMin ||
389 dtdy < 1/scaleLimitMin) {
390 ALOGE("%s: greater than max supported size dsdx=%f dtdy=%f \
391 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
392 scaleLimitMax,1/scaleLimitMin);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700393 return -1;
394 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400395 acquireFd = layer->acquireFenceFd;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700396 if(dsdx > copybitsMaxScale ||
397 dtdy > copybitsMaxScale ||
398 dsdx < 1/copybitsMinScale ||
399 dtdy < 1/copybitsMinScale){
400 // The requested scale is out of the range the hardware
401 // can support.
402 ALOGE("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
403 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
404 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
405 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
406 src_crop_width,src_crop_height);
407
408 //Driver makes width and height as even
409 //that may cause wrong calculation of the ratio
410 //in display and crop.Hence we make
411 //crop width and height as even.
412 src_crop_width = (src_crop_width/2)*2;
413 src_crop_height = (src_crop_height/2)*2;
414
415 int tmp_w = src_crop_width;
416 int tmp_h = src_crop_height;
417
418 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
419 tmp_w = src_crop_width*copybitsMaxScale;
420 tmp_h = src_crop_height*copybitsMaxScale;
421 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
422 tmp_w = src_crop_width/copybitsMinScale;
423 tmp_h = src_crop_height/copybitsMinScale;
424 tmp_w = (tmp_w/2)*2;
425 tmp_h = (tmp_h/2)*2;
426 }
427 ALOGE("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
428
Naseer Ahmed64b81212013-02-14 10:29:47 -0500429 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700430
431 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, fbHandle->format, usage)){
432 copybit_image_t tmp_dst;
433 copybit_rect_t tmp_rect;
434 tmp_dst.w = tmp_w;
435 tmp_dst.h = tmp_h;
436 tmp_dst.format = tmpHnd->format;
437 tmp_dst.handle = tmpHnd;
438 tmp_dst.horiz_padding = src.horiz_padding;
439 tmp_dst.vert_padding = src.vert_padding;
440 tmp_rect.l = 0;
441 tmp_rect.t = 0;
442 tmp_rect.r = tmp_dst.w;
443 tmp_rect.b = tmp_dst.h;
444 //create one clip region
445 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
446 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
447 region_iterator tmp_it(tmp_hwc_reg);
448 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700449 //TODO: once, we are able to read layer alpha, update this
450 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Terence Hampsonadf47302013-05-23 12:21:02 -0400451 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700452 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
453 &srcRect, &tmp_it);
454 if(err < 0){
455 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
456 __LINE__);
457 if(tmpHnd)
458 free_buffer(tmpHnd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700459 return err;
460 }
Terence Hampsonadf47302013-05-23 12:21:02 -0400461 // use release fence as aquire fd for next stretch
462 if (ctx->mMDP.version < qdutils::MDP_V4_0)
463 copybit->flush_get_fence(copybit, &acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700464 // copy new src and src rect crop
465 src = tmp_dst;
466 srcRect = tmp_rect;
467 }
468 }
469 // Copybit region
470 hwc_region_t region = layer->visibleRegionScreen;
471 region_iterator copybitRegion(region);
472
473 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
474 renderBuffer->width);
475 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
476 renderBuffer->height);
477 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
478 layer->transform);
Naseer Ahmed45a99602012-07-31 19:15:24 -0700479 //TODO: once, we are able to read layer alpha, update this
480 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800481 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
482 layer->blending);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700483 copybit->set_parameter(copybit, COPYBIT_DITHER,
484 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
485 COPYBIT_ENABLE : COPYBIT_DISABLE);
486 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
487 COPYBIT_ENABLE);
Terence Hampsonadf47302013-05-23 12:21:02 -0400488 copybit->set_sync(copybit, acquireFd);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700489 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
490 &copybitRegion);
491 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
492 COPYBIT_DISABLE);
493
Terence Hampsonadf47302013-05-23 12:21:02 -0400494 if(tmpHnd) {
495 if (ctx->mMDP.version < qdutils::MDP_V4_0){
496 int ret = -1, releaseFd;
497 // we need to wait for the buffer before freeing
498 copybit->flush_get_fence(copybit, &releaseFd);
499 ret = sync_wait(releaseFd, 1000);
500 if(ret < 0) {
501 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
502 __FUNCTION__, errno, strerror(errno));
503 }
504 close(releaseFd);
505 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700506 free_buffer(tmpHnd);
Terence Hampsonadf47302013-05-23 12:21:02 -0400507 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700508
509 if(err < 0)
510 ALOGE("%s: copybit stretch failed",__FUNCTION__);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700511 return err;
512}
513
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700514void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
Naseer Ahmed45a99602012-07-31 19:15:24 -0700515 unsigned int& width, unsigned int& height)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700516{
517 hwc_rect_t displayFrame = layer->displayFrame;
518
519 width = displayFrame.right - displayFrame.left;
520 height = displayFrame.bottom - displayFrame.top;
521}
522
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800523bool CopyBit::validateParams(hwc_context_t *ctx,
524 const hwc_display_contents_1_t *list) {
525 //Validate parameters
526 if (!ctx) {
527 ALOGE("%s:Invalid HWC context", __FUNCTION__);
528 return false;
529 } else if (!list) {
530 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
531 return false;
532 }
533 return true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700534}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700535
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700536
Naseer Ahmed64b81212013-02-14 10:29:47 -0500537int CopyBit::allocRenderBuffers(int w, int h, int f)
538{
539 int ret = 0;
540 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
541 if (mRenderBuffer[i] == NULL) {
542 ret = alloc_buffer(&mRenderBuffer[i],
543 w, h, f,
544 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
545 }
546 if(ret < 0) {
547 freeRenderBuffers();
548 break;
549 }
550 }
551 return ret;
552}
553
554void CopyBit::freeRenderBuffers()
555{
556 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
557 if(mRenderBuffer[i]) {
558 free_buffer(mRenderBuffer[i]);
559 mRenderBuffer[i] = NULL;
560 }
561 }
562}
563
564private_handle_t * CopyBit::getCurrentRenderBuffer() {
565 return mRenderBuffer[mCurRenderBufferIndex];
566}
567
568void CopyBit::setReleaseFd(int fd) {
569 if(mRelFd[0] >=0)
570 close(mRelFd[0]);
571 mRelFd[0] = mRelFd[1];
572 mRelFd[1] = dup(fd);
573}
574
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800575struct copybit_device_t* CopyBit::getCopyBitDevice() {
576 return mEngine;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700577}
578
Naseer Ahmed64b81212013-02-14 10:29:47 -0500579CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
580 mCurRenderBufferIndex(0){
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700581 hw_module_t const *module;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500582 for (int i = 0; i < NUM_RENDER_BUFFERS; i++)
583 mRenderBuffer[i] = NULL;
584 mRelFd[0] = -1;
585 mRelFd[1] = -1;
Prabhanjan Kandula73030ba2013-03-15 22:33:39 +0530586
587 char value[PROPERTY_VALUE_MAX];
588 property_get("debug.hwc.dynThreshold", value, "2");
589 mDynThreshold = atof(value);
590
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700591 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800592 if(copybit_open(module, &mEngine) < 0) {
593 ALOGE("FATAL ERROR: copybit open failed.");
594 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700595 } else {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800596 ALOGE("FATAL ERROR: copybit hw module not found");
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700597 }
598}
Saurabh Shah661a58f2012-08-30 15:30:49 -0700599
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800600CopyBit::~CopyBit()
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700601{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500602 freeRenderBuffers();
603 if(mRelFd[0] >=0)
604 close(mRelFd[0]);
605 if(mRelFd[1] >=0)
606 close(mRelFd[1]);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800607 if(mEngine)
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700608 {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800609 copybit_close(mEngine);
610 mEngine = NULL;
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700611 }
612}
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700613}; //namespace qhwc