hwc_copybit.cpp revision 513ddc2124abf90c63af41999201f0d2031af0c8
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 <copybit.h>
23#include <utils/Timers.h>
24#include <mdp_version.h>
25#include "hwc_copybit.h"
26#include "comptype.h"
27#include "gr.h"
28
29namespace qhwc {
30
31struct 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
68void CopyBit::reset() {
69    mIsModeOn = false;
70    mCopyBitDraw = false;
71}
72
73bool 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
81bool 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
119unsigned 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    // 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++) {
127         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
138bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
139                                                            int dpy) {
140
141    if(mEngine == NULL) {
142        // No copybit device found - cannot use copybit
143        return false;
144    }
145    int compositionType = qdutils::QCCompositionType::
146                                    getInstance().getCompositionType();
147
148    if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
149        (compositionType == qdutils::COMPOSITION_TYPE_CPU))   {
150        //GPU/CPU composition, don't change layer composition type
151        return true;
152    }
153
154    if(!(validateParams(ctx, list))) {
155        ALOGE("%s:Invalid Params", __FUNCTION__);
156        return false;
157    }
158
159    if(ctx->listStats[dpy].skipCount) {
160        //GPU will be anyways used
161        return false;
162    }
163
164    if (ctx->listStats[dpy].numAppLayers > MAX_NUM_LAYERS) {
165        // Reached max layers supported by HWC.
166        return false;
167    }
168
169    bool useCopybitForYUV = canUseCopybitForYUV(ctx);
170    bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
171    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
191
192    // numAppLayers-1, as we iterate till 0th layer index
193    for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
194        private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
195
196        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
207        }
208    }
209    return true;
210}
211
212int 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
231bool 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;
236    LayerProp *layerProp = ctx->layerProp[dpy];
237
238    if(mCopyBitDraw == false) // there is no layer marked for copybit
239        return false ;
240
241    //render buffer
242    private_handle_t *renderBuffer = getCurrentRenderBuffer();
243    if (!renderBuffer) {
244        ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
245        return false;
246    }
247
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    }
254
255    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    }
262    // numAppLayers-1, as we iterate from 0th layer index with HWC_COPYBIT flag
263    for (int i = 0; i <= (ctx->listStats[dpy].numAppLayers-1); i++) {
264        hwc_layer_1_t *layer = &list->hwLayers[i];
265        if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
266            ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
267            continue;
268        }
269        int ret = -1;
270        if (list->hwLayers[i].acquireFenceFd != -1 ) {
271            // Wait for acquire Fence on the App buffers.
272            ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
273            if(ret < 0) {
274                ALOGE("%s: sync_wait error!! error no = %d err str = %s",
275                                    __FUNCTION__, errno, strerror(errno));
276            }
277            close(list->hwLayers[i].acquireFenceFd);
278            list->hwLayers[i].acquireFenceFd = -1;
279        }
280        retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
281                                                    renderBuffer, dpy);
282        copybitLayerCount++;
283        if(retVal < 0) {
284            ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
285        }
286    }
287
288    if (copybitLayerCount) {
289        copybit_device_t *copybit = getCopyBitDevice();
290        // Async mode
291        copybit->flush_get_fence(copybit, fd);
292    }
293    return true;
294}
295
296int  CopyBit::drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
297                                     private_handle_t *renderBuffer, int dpy)
298{
299    hwc_context_t* ctx = (hwc_context_t*)(dev);
300    int err = 0;
301    if(!ctx) {
302         ALOGE("%s: null context ", __FUNCTION__);
303         return -1;
304    }
305
306    private_handle_t *hnd = (private_handle_t *)layer->handle;
307    if(!hnd) {
308        ALOGE("%s: invalid handle", __FUNCTION__);
309        return -1;
310    }
311
312    private_handle_t *fbHandle = (private_handle_t *)renderBuffer;
313    if(!fbHandle) {
314        ALOGE("%s: Framebuffer handle is NULL", __FUNCTION__);
315        return -1;
316    }
317
318    // Set the copybit source:
319    copybit_image_t src;
320    src.w = hnd->width;
321    src.h = hnd->height;
322    src.format = hnd->format;
323    src.base = (void *)hnd->base;
324    src.handle = (native_handle_t *)layer->handle;
325    src.horiz_padding = src.w - hnd->width;
326    // Initialize vertical padding to zero for now,
327    // this needs to change to accomodate vertical stride
328    // if needed in the future
329    src.vert_padding = 0;
330
331    // Copybit source rect
332    hwc_rect_t sourceCrop = layer->sourceCrop;
333    copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
334                              sourceCrop.right,
335                              sourceCrop.bottom};
336
337    // Copybit destination rect
338    hwc_rect_t displayFrame = layer->displayFrame;
339    copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
340                              displayFrame.right,
341                              displayFrame.bottom};
342
343    // Copybit dst
344    copybit_image_t dst;
345    dst.w = ALIGN(fbHandle->width,32);
346    dst.h = fbHandle->height;
347    dst.format = fbHandle->format;
348    dst.base = (void *)fbHandle->base;
349    dst.handle = (native_handle_t *)fbHandle;
350
351    copybit_device_t *copybit = mEngine;
352
353    int32_t screen_w        = displayFrame.right - displayFrame.left;
354    int32_t screen_h        = displayFrame.bottom - displayFrame.top;
355    int32_t src_crop_width  = sourceCrop.right - sourceCrop.left;
356    int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
357
358    // Copybit dst
359    float copybitsMaxScale =
360                      (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
361    float copybitsMinScale =
362                       (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
363
364    if((layer->transform == HWC_TRANSFORM_ROT_90) ||
365                           (layer->transform == HWC_TRANSFORM_ROT_270)) {
366        //swap screen width and height
367        int tmp = screen_w;
368        screen_w  = screen_h;
369        screen_h = tmp;
370    }
371    private_handle_t *tmpHnd = NULL;
372
373    if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
374        ALOGE("%s: wrong params for display screen_w=%d src_crop_width=%d \
375        screen_w=%d src_crop_width=%d", __FUNCTION__, screen_w,
376                                src_crop_width,screen_w,src_crop_width);
377        return -1;
378    }
379
380    float dsdx = (float)screen_w/src_crop_width;
381    float dtdy = (float)screen_h/src_crop_height;
382
383    float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
384    float scaleLimitMin = copybitsMinScale * copybitsMinScale;
385    if(dsdx > scaleLimitMax ||
386        dtdy > scaleLimitMax ||
387        dsdx < 1/scaleLimitMin ||
388        dtdy < 1/scaleLimitMin) {
389        ALOGE("%s: greater than max supported size dsdx=%f dtdy=%f \
390              scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,
391                                          scaleLimitMax,1/scaleLimitMin);
392        return -1;
393    }
394    if(dsdx > copybitsMaxScale ||
395        dtdy > copybitsMaxScale ||
396        dsdx < 1/copybitsMinScale ||
397        dtdy < 1/copybitsMinScale){
398        // The requested scale is out of the range the hardware
399        // can support.
400       ALOGE("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,\
401                                 copybitsMinScale=%f,screen_w=%d,screen_h=%d \
402                  src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
403              dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,
404                                              src_crop_width,src_crop_height);
405
406       //Driver makes width and height as even
407       //that may cause wrong calculation of the ratio
408       //in display and crop.Hence we make
409       //crop width and height as even.
410       src_crop_width  = (src_crop_width/2)*2;
411       src_crop_height = (src_crop_height/2)*2;
412
413       int tmp_w =  src_crop_width;
414       int tmp_h =  src_crop_height;
415
416       if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
417         tmp_w = src_crop_width*copybitsMaxScale;
418         tmp_h = src_crop_height*copybitsMaxScale;
419       }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
420         tmp_w = src_crop_width/copybitsMinScale;
421         tmp_h = src_crop_height/copybitsMinScale;
422         tmp_w  = (tmp_w/2)*2;
423         tmp_h = (tmp_h/2)*2;
424       }
425       ALOGE("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
426
427       int usage = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
428
429       if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, fbHandle->format, usage)){
430            copybit_image_t tmp_dst;
431            copybit_rect_t tmp_rect;
432            tmp_dst.w = tmp_w;
433            tmp_dst.h = tmp_h;
434            tmp_dst.format = tmpHnd->format;
435            tmp_dst.handle = tmpHnd;
436            tmp_dst.horiz_padding = src.horiz_padding;
437            tmp_dst.vert_padding = src.vert_padding;
438            tmp_rect.l = 0;
439            tmp_rect.t = 0;
440            tmp_rect.r = tmp_dst.w;
441            tmp_rect.b = tmp_dst.h;
442            //create one clip region
443            hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
444            hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
445            region_iterator tmp_it(tmp_hwc_reg);
446            copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
447            //TODO: once, we are able to read layer alpha, update this
448            copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
449            err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect,
450                                                           &srcRect, &tmp_it);
451            if(err < 0){
452                ALOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,
453                                                             __LINE__);
454                if(tmpHnd)
455                    free_buffer(tmpHnd);
456                return err;
457            }
458            // copy new src and src rect crop
459            src = tmp_dst;
460            srcRect = tmp_rect;
461      }
462    }
463    // Copybit region
464    hwc_region_t region = layer->visibleRegionScreen;
465    region_iterator copybitRegion(region);
466
467    copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH,
468                                          renderBuffer->width);
469    copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT,
470                                          renderBuffer->height);
471    copybit->set_parameter(copybit, COPYBIT_TRANSFORM,
472                                              layer->transform);
473    //TODO: once, we are able to read layer alpha, update this
474    copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255);
475    copybit->set_parameter(copybit, COPYBIT_BLEND_MODE,
476                                              layer->blending);
477    copybit->set_parameter(copybit, COPYBIT_DITHER,
478                             (dst.format == HAL_PIXEL_FORMAT_RGB_565)?
479                                             COPYBIT_ENABLE : COPYBIT_DISABLE);
480    copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
481                                                COPYBIT_ENABLE);
482    err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect,
483                                                   &copybitRegion);
484    copybit->set_parameter(copybit, COPYBIT_BLIT_TO_FRAMEBUFFER,
485                                               COPYBIT_DISABLE);
486
487    if(tmpHnd)
488        free_buffer(tmpHnd);
489
490    if(err < 0)
491        ALOGE("%s: copybit stretch failed",__FUNCTION__);
492    return err;
493}
494
495void CopyBit::getLayerResolution(const hwc_layer_1_t* layer,
496                                 unsigned int& width, unsigned int& height)
497{
498    hwc_rect_t displayFrame  = layer->displayFrame;
499
500    width = displayFrame.right - displayFrame.left;
501    height = displayFrame.bottom - displayFrame.top;
502}
503
504bool CopyBit::validateParams(hwc_context_t *ctx,
505                                        const hwc_display_contents_1_t *list) {
506    //Validate parameters
507    if (!ctx) {
508        ALOGE("%s:Invalid HWC context", __FUNCTION__);
509        return false;
510    } else if (!list) {
511        ALOGE("%s:Invalid HWC layer list", __FUNCTION__);
512        return false;
513    }
514    return true;
515}
516
517
518int CopyBit::allocRenderBuffers(int w, int h, int f)
519{
520    int ret = 0;
521    for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
522        if (mRenderBuffer[i] == NULL) {
523            ret = alloc_buffer(&mRenderBuffer[i],
524                               w, h, f,
525                               GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
526        }
527        if(ret < 0) {
528            freeRenderBuffers();
529            break;
530        }
531    }
532    return ret;
533}
534
535void CopyBit::freeRenderBuffers()
536{
537    for (int i = 0; i < NUM_RENDER_BUFFERS; i++) {
538        if(mRenderBuffer[i]) {
539            free_buffer(mRenderBuffer[i]);
540            mRenderBuffer[i] = NULL;
541        }
542    }
543}
544
545private_handle_t * CopyBit::getCurrentRenderBuffer() {
546    return mRenderBuffer[mCurRenderBufferIndex];
547}
548
549void CopyBit::setReleaseFd(int fd) {
550    if(mRelFd[0] >=0)
551        close(mRelFd[0]);
552    mRelFd[0] = mRelFd[1];
553    mRelFd[1] = dup(fd);
554}
555
556struct copybit_device_t* CopyBit::getCopyBitDevice() {
557    return mEngine;
558}
559
560CopyBit::CopyBit():mIsModeOn(false), mCopyBitDraw(false),
561    mCurRenderBufferIndex(0){
562    hw_module_t const *module;
563    for (int i = 0; i < NUM_RENDER_BUFFERS; i++)
564        mRenderBuffer[i] = NULL;
565    mRelFd[0] = -1;
566    mRelFd[1] = -1;
567
568    char value[PROPERTY_VALUE_MAX];
569    property_get("debug.hwc.dynThreshold", value, "2");
570    mDynThreshold = atof(value);
571
572    if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
573        if(copybit_open(module, &mEngine) < 0) {
574            ALOGE("FATAL ERROR: copybit open failed.");
575        }
576    } else {
577        ALOGE("FATAL ERROR: copybit hw module not found");
578    }
579}
580
581CopyBit::~CopyBit()
582{
583    freeRenderBuffers();
584    if(mRelFd[0] >=0)
585        close(mRelFd[0]);
586    if(mRelFd[1] >=0)
587        close(mRelFd[1]);
588    if(mEngine)
589    {
590        copybit_close(mEngine);
591        mEngine = NULL;
592    }
593}
594}; //namespace qhwc
595