1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
4 *
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
21 #define DEBUG_COPYBIT 0
22 #include <sync/sync.h>
23 #include <copybit.h>
24 #include <utils/Timers.h>
25 #include "hwc_copybit.h"
26 #include "comptype.h"
27 #include "gr.h"
28
29 namespace qhwc {
30
31 struct range {
32 int current;
33 int end;
34 };
35 struct region_iterator : public copybit_region_t {
36
region_iteratorqhwc::region_iterator37 region_iterator(hwc_region_t region) {
38 mRegion = region;
39 r.end = region.numRects;
40 r.current = 0;
41 this->next = iterate;
42 }
43
44 private:
iterateqhwc::region_iterator45 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
reset()68 void CopyBit::reset() {
69 mIsModeOn = false;
70 mCopyBitDraw = false;
71 }
72
canUseCopybitForYUV(hwc_context_t * ctx)73 bool CopyBit::canUseCopybitForYUV(hwc_context_t *ctx) {
74 // return true for non-overlay targets
75 if(ctx->mMDP.hasOverlay) {
76 return false;
77 }
78 return true;
79 }
80
canUseCopybitForRGB(hwc_context_t * ctx,hwc_display_contents_1_t * list,int dpy)81 bool CopyBit::canUseCopybitForRGB(hwc_context_t *ctx,
82 hwc_display_contents_1_t *list,
83 int dpy) {
84 int compositionType = qdutils::QCCompositionType::
85 getInstance().getCompositionType();
86
87 if ((compositionType & qdutils::COMPOSITION_TYPE_C2D) ||
88 (compositionType & qdutils::COMPOSITION_TYPE_DYN)) {
89 if(ctx->listStats[dpy].yuvCount) {
90 //Overlay up & running. Dont use COPYBIT for RGB layers.
91 return false;
92 }
93 }
94
95 if (compositionType & qdutils::COMPOSITION_TYPE_DYN) {
96 // DYN Composition:
97 // use copybit, if (TotalRGBRenderArea < threashold * FB Area)
98 // this is done based on perf inputs in ICS
99 // TODO: Above condition needs to be re-evaluated in JB
100 int fbWidth = ctx->dpyAttr[dpy].xres;
101 int fbHeight = ctx->dpyAttr[dpy].yres;
102 unsigned int fbArea = (fbWidth * fbHeight);
103 unsigned int renderArea = getRGBRenderingArea(list);
104 ALOGD_IF (DEBUG_COPYBIT, "%s:renderArea %u, fbArea %u",
105 __FUNCTION__, renderArea, fbArea);
106 if (renderArea < (mDynThreshold * fbArea)) {
107 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
getRGBRenderingArea(const hwc_display_contents_1_t * list)119 unsigned int CopyBit::getRGBRenderingArea
120 (const hwc_display_contents_1_t *list) {
121 //Calculates total rendering area for RGB layers
122 unsigned int renderArea = 0;
123 unsigned int w=0, h=0;
124 for (unsigned int i=0; i<list->numHwLayers; i++) {
125 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
126 if (hnd) {
127 if (BUFFER_TYPE_UI == hnd->bufferType) {
128 getLayerResolution(&list->hwLayers[i], w, h);
129 renderArea += (w*h);
130 }
131 }
132 }
133 return renderArea;
134 }
135
prepare(hwc_context_t * ctx,hwc_display_contents_1_t * list,int dpy)136 bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
137 int dpy) {
138
139 if(mEngine == NULL) {
140 // No copybit device found - cannot use copybit
141 return false;
142 }
143 int compositionType = qdutils::QCCompositionType::
144 getInstance().getCompositionType();
145
146 if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
147 (compositionType == qdutils::COMPOSITION_TYPE_CPU)) {
148 //GPU/CPU composition, don't change layer composition type
149 return true;
150 }
151
152 if(!(validateParams(ctx, list))) {
153 ALOGE("%s:Invalid Params", __FUNCTION__);
154 return false;
155 }
156
157 if(ctx->listStats[dpy].skipCount) {
158 //GPU will be anyways used
159 return false;
160 }
161
162 if (ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS) {
163 // Reached max layers supported by HWC.
164 return false;
165 }
166
167 bool useCopybitForYUV = canUseCopybitForYUV(ctx);
168 bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
169 LayerProp *layerProp = ctx->layerProp[dpy];
170 size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
171 hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
172 private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;
173
174
175
176 //Allocate render buffers if they're not allocated
177 if (useCopybitForYUV || useCopybitForRGB) {
178 int ret = allocRenderBuffers(fbHnd->width,
179 fbHnd->height,
180 fbHnd->format);
181 if (ret < 0) {
182 return false;
183 } else {
184 mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
185 NUM_RENDER_BUFFERS;
186 }
187 }
188
189
190 // numAppLayers-1, as we iterate till 0th layer index
191 for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
192 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
193
194 if ((hnd->bufferType == BUFFER_TYPE_VIDEO && useCopybitForYUV) ||
195 (hnd->bufferType == BUFFER_TYPE_UI && useCopybitForRGB)) {
196 layerProp[i].mFlags |= HWC_COPYBIT;
197 list->hwLayers[i].compositionType = HWC_OVERLAY;
198 mCopyBitDraw = true;
199 } else {
200 // We currently cannot mix copybit layers with layers marked to
201 // be drawn on the framebuffer or that are on the layer cache.
202 mCopyBitDraw = false;
203 //There is no need to reset layer properties here as we return in
204 //draw if mCopyBitDraw is false
205 }
206 }
207 return true;
208 }
209
clear(private_handle_t * hnd,hwc_rect_t & rect)210 int CopyBit::clear (private_handle_t* hnd, hwc_rect_t& rect)
211 {
212 int ret = 0;
213 copybit_rect_t clear_rect = {rect.left, rect.top,
214 rect.right,
215 rect.bottom};
216
217 copybit_image_t buf;
218 buf.w = ALIGN(getWidth(hnd),32);
219 buf.h = getHeight(hnd);
220 buf.format = hnd->format;
221 buf.base = (void *)hnd->base;
222 buf.handle = (native_handle_t *)hnd;
223
224 copybit_device_t *copybit = mEngine;
225 ret = copybit->clear(copybit, &buf, &clear_rect);
226 return ret;
227 }
228
draw(hwc_context_t * ctx,hwc_display_contents_1_t * list,int dpy,int32_t * fd)229 bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
230 int dpy, int32_t *fd) {
231 // draw layers marked for COPYBIT
232 int retVal = true;
233 int copybitLayerCount = 0;
234 LayerProp *layerProp = ctx->layerProp[dpy];
235
236 if(mCopyBitDraw == false) // there is no layer marked for copybit
237 return false ;
238
239 //render buffer
240 private_handle_t *renderBuffer = getCurrentRenderBuffer();
241 if (!renderBuffer) {
242 ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
243 return false;
244 }
245
246 //Wait for the previous frame to complete before rendering onto it
247 if(mRelFd[0] >=0) {
248 sync_wait(mRelFd[0], 1000);
249 close(mRelFd[0]);
250 mRelFd[0] = -1;
251 }
252
253 //Clear the visible region on the render buffer
254 //XXX: Do this only when needed.
255 hwc_rect_t clearRegion;
256 getNonWormholeRegion(list, clearRegion);
257 clear(renderBuffer, clearRegion);
258 // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
259 for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
260 if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
261 ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
262 continue;
263 }
264 int ret = -1;
265 if (list->hwLayers[i].acquireFenceFd != -1 ) {
266 // Wait for acquire Fence on the App buffers.
267 ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
268 if(ret < 0) {
269 ALOGE("%s: sync_wait error!! error no = %d err str = %s",
270 __FUNCTION__, errno, strerror(errno));
271 }
272 close(list->hwLayers[i].acquireFenceFd);
273 list->hwLayers[i].acquireFenceFd = -1;
274 }
275 retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
276 renderBuffer, dpy);
277 copybitLayerCount++;
278 if(retVal < 0) {
279 ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
280 }
281 }
282
283 if (copybitLayerCount) {
284 copybit_device_t *copybit = getCopyBitDevice();
285 // Async mode
286 copybit->flush_get_fence(copybit, fd);
287 }
288 return true;
289 }
290
drawLayerUsingCopybit(hwc_context_t * dev,hwc_layer_1_t * layer,private_handle_t * renderBuffer,int dpy)291 int CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
292 private_handle_t *renderBuffer, int dpy)
293 {
294 hwc_context_t* ctx = (hwc_context_t*)(dev);
295 int err = 0;
296 if(!ctx) {
297 ALOGE("%s: null context ", __FUNCTION__);
298 return -1;
299 }
300
301 private_handle_t *hnd = (private_handle_t *)layer->handle;
302 if(!hnd) {
303 ALOGE("%s: invalid handle", __FUNCTION__);
304 return -1;
305 }
306
307 private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
308 if(!fbHandle) {
309 ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
310 return -1;
311 }
312
313 // Set the copybit source:
314 copybit_image_t src;
315 src.w = getWidth(hnd);
316 src.h = getHeight(hnd);
317 src.format = hnd->format;
318 src.base = (void *)hnd->base;
319 src.handle = (native_handle_t *)layer->handle;
320 src.horiz_padding = src.w - getWidth(hnd);
321 // Initialize vertical padding to zero for now,
322 // this needs to change to accomodate vertical stride
323 // if needed in the future
324 src.vert_padding = 0;
325
326 // Copybit source rect
327 hwc_rect_t sourceCrop = layer->sourceCrop;
328 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
329 sourceCrop.right,
330 sourceCrop.bottom};
331
332 // Copybit destination rect
333 hwc_rect_t displayFrame = layer->displayFrame;
334 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
335 displayFrame.right,
336 displayFrame.bottom};
337
338 // Copybit dst
339 copybit_image_t dst;
340 dst.w = ALIGN(fbHandle->width,32);
341 dst.h = fbHandle->height;
342 dst.format = fbHandle->format;
343 dst.base = (void *)fbHandle->base;
344 dst.handle = (native_handle_t *)fbHandle;
345
346 copybit_device_t *copybit = mEngine;
347
348 int32_t screen_w = displayFrame.right - displayFrame.left;
349 int32_t screen_h = displayFrame.bottom - displayFrame.top;
350 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
351 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
352
353 // Copybit dst
354 float copybitsMaxScale =
355 (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
356 float copybitsMinScale =
357 (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
358
359 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
360 (layer->transform == HWC_TRANSFORM_ROT_270)) {
361 //swap screen width and height
362 int tmp = screen_w;
363 screen_w = screen_h;
364 screen_h = tmp;
365 }
366 private_handle_t *tmpHnd = NULL;
367
368 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
369 ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
370 screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
371 src_crop_width,screen_w,src_crop_width);
372 return -1;
373 }
374
375 float dsdx = (float)screen_w/src_crop_width;
376 float dtdy = (float)screen_h/src_crop_height;
377
378 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
379 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
380 if(dsdx > scaleLimitMax ||
381 dtdy > scaleLimitMax ||
382 dsdx < 1/scaleLimitMin ||
383 dtdy < 1/scaleLimitMin) {
384 ALOGE("%s: greater than max supported size dsdx=%f dtdy=%f \
385 scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
386 scaleLimitMax,1/scaleLimitMin);
387 return -1;
388 }
389 if(dsdx > copybitsMaxScale ||
390 dtdy > copybitsMaxScale ||
391 dsdx < 1/copybitsMinScale ||
392 dtdy < 1/copybitsMinScale){
393 // The requested scale is out of the range the hardware
394 // can support.
395 ALOGE("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
396 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
397 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
398 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
399 src_crop_width,src_crop_height);
400
401 //Driver makes width and height as even
402 //that may cause wrong calculation of the ratio
403 //in display and crop.Hence we make
404 //crop width and height as even.
405 src_crop_width = (src_crop_width/2)*2;
406 src_crop_height = (src_crop_height/2)*2;
407
408 int tmp_w = src_crop_width;
409 int tmp_h = src_crop_height;
410
411 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
412 tmp_w = src_crop_width*copybitsMaxScale;
413 tmp_h = src_crop_height*copybitsMaxScale;
414 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
415 tmp_w = src_crop_width/copybitsMinScale;
416 tmp_h = src_crop_height/copybitsMinScale;
417 tmp_w = (tmp_w/2)*2;
418 tmp_h = (tmp_h/2)*2;
419 }
420 ALOGE("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
421
422 int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
423
424 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, fbHandle->format, usage)){
425 copybit_image_t tmp_dst;
426 copybit_rect_t tmp_rect;
427 tmp_dst.w = tmp_w;
428 tmp_dst.h = tmp_h;
429 tmp_dst.format = tmpHnd->format;
430 tmp_dst.handle = tmpHnd;
431 tmp_dst.horiz_padding = src.horiz_padding;
432 tmp_dst.vert_padding = src.vert_padding;
433 tmp_rect.l = 0;
434 tmp_rect.t = 0;
435 tmp_rect.r = tmp_dst.w;
436 tmp_rect.b = tmp_dst.h;
437 //create one clip region
438 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
439 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
440 region_iterator tmp_it(tmp_hwc_reg);
441 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
442 //TODO: once, we are able to read layer alpha, update this
443 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
444 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
445 &srcRect, &tmp_it);
446 if(err < 0){
447 ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
448 __LINE__);
449 if(tmpHnd)
450 free_buffer(tmpHnd);
451 return err;
452 }
453 // copy new src and src rect crop
454 src = tmp_dst;
455 srcRect = tmp_rect;
456 }
457 }
458 // Copybit region
459 hwc_region_t region = layer->visibleRegionScreen;
460 region_iterator copybitRegion(region);
461
462 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
463 renderBuffer->width);
464 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
465 renderBuffer->height);
466 copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
467 layer->transform);
468 //TODO: once, we are able to read layer alpha, update this
469 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
470 copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
471 layer->blending);
472 copybit->set_parameter(copybit, COPYBIT_DITHER,
473 (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
474 COPYBIT_ENABLE : COPYBIT_DISABLE);
475 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
476 COPYBIT_ENABLE);
477 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
478 ©bitRegion);
479 copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
480 COPYBIT_DISABLE);
481
482 if(tmpHnd)
483 free_buffer(tmpHnd);
484
485 if(err < 0)
486 ALOGE("%s: copybit stretch failed",__FUNCTION__);
487 return err;
488 }
489
getLayerResolution(const hwc_layer_1_t * layer,unsigned int & width,unsigned int & height)490 void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
491 unsigned int& width, unsigned int& height)
492 {
493 hwc_rect_t displayFrame = layer->displayFrame;
494
495 width = displayFrame.right - displayFrame.left;
496 height = displayFrame.bottom - displayFrame.top;
497 }
498
validateParams(hwc_context_t * ctx,const hwc_display_contents_1_t * list)499 bool CopyBit::validateParams(hwc_context_t *ctx,
500 const hwc_display_contents_1_t *list) {
501 //Validate parameters
502 if (!ctx) {
503 ALOGE("%s:Invalid HWC context", __FUNCTION__);
504 return false;
505 } else if (!list) {
506 ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
507 return false;
508 }
509 return true;
510 }
511
512
allocRenderBuffers(int w,int h,int f)513 int CopyBit::allocRenderBuffers(int w, int h, int f)
514 {
515 int ret = 0;
516 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
517 if (mRenderBuffer[i] == NULL) {
518 ret = alloc_buffer(&mRenderBuffer[i],
519 w, h, f,
520 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
521 }
522 if(ret < 0) {
523 freeRenderBuffers();
524 break;
525 }
526 }
527 return ret;
528 }
529
freeRenderBuffers()530 void CopyBit::freeRenderBuffers()
531 {
532 for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
533 if(mRenderBuffer[i]) {
534 free_buffer(mRenderBuffer[i]);
535 mRenderBuffer[i] = NULL;
536 }
537 }
538 }
539
getCurrentRenderBuffer()540 private_handle_t * CopyBit::getCurrentRenderBuffer() {
541 return mRenderBuffer[mCurRenderBufferIndex];
542 }
543
setReleaseFd(int fd)544 void CopyBit::setReleaseFd(int fd) {
545 if(mRelFd[0] >=0)
546 close(mRelFd[0]);
547 mRelFd[0] = mRelFd[1];
548 mRelFd[1] = dup(fd);
549 }
550
getCopyBitDevice()551 struct copybit_device_t* CopyBit::getCopyBitDevice() {
552 return mEngine;
553 }
554
CopyBit()555 CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
556 mCurRenderBufferIndex(0){
557 hw_module_t const *module;
558 for (int i = 0; i < NUM_RENDER_BUFFERS; i++)
559 mRenderBuffer[i] = NULL;
560 mRelFd[0] = -1;
561 mRelFd[1] = -1;
562
563 char value[PROPERTY_VALUE_MAX];
564 property_get("debug.hwc.dynThreshold", value, "2");
565 mDynThreshold = atof(value);
566
567 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
568 if(copybit_open(module, &mEngine) < 0) {
569 ALOGE("FATAL ERROR: copybit open failed.");
570 }
571 } else {
572 ALOGE("FATAL ERROR: copybit hw module not found");
573 }
574 }
575
~CopyBit()576 CopyBit::~CopyBit()
577 {
578 freeRenderBuffers();
579 if(mRelFd[0] >=0)
580 close(mRelFd[0]);
581 if(mRelFd[1] >=0)
582 close(mRelFd[1]);
583 if(mEngine)
584 {
585 copybit_close(mEngine);
586 mEngine = NULL;
587 }
588 }
589 }; //namespace qhwc
590