bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list, EGLDisplay dpy,
                                                               EGLSurface sur){
    // draw layers marked for COPYBIT
    int retVal = true;

    if(sCopyBitDraw == false) // there is no any layer marked for copybit
       return true ;

    android_native_buffer_t *renderBuffer =
          qdutils::eglHandles::getInstance().getAndroidNativeRenderBuffer(dpy);
    if (!renderBuffer) {
        ALOGE("%s: eglGetRenderBufferANDROID returned NULL buffer",
             __FUNCTION__);
        return -1;
    }

    // Invoke a glFinish if we are rendering any layers using copybit.
    // We call glFinish instead of locking the renderBuffer because the
    // GPU could take longer than the genlock timeout value to complete
    // rendering
    glFinish();

    for (size_t i=0; i<list->numHwLayers; i++) {
        if (list->hwLayers[i].compositionType == HWC_USE_COPYBIT) {
            retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
                                                     (EGLDisplay)dpy,
                                                     (EGLSurface)sur,
                                                        renderBuffer);
           if(retVal<0) {
              ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
           }
        }
    }
    return true;
}
bool CopyBit::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
                                                        int dpy, int32_t *fd) {
    // draw layers marked for COPYBIT
    int retVal = true;
    int copybitLayerCount = 0;
    LayerProp *layerProp = ctx->layerProp[dpy];

    if(mCopyBitDraw == false) // there is no layer marked for copybit
        return false ;

    //render buffer
    private_handle_t *renderBuffer = getCurrentRenderBuffer();
    if (!renderBuffer) {
        ALOGE("%s: Render buffer layer handle is NULL", __FUNCTION__);
        return false;
    }

    //Wait for the previous frame to complete before rendering onto it
    if(mRelFd[mCurRenderBufferIndex] >= 0) {
        sync_wait(mRelFd[mCurRenderBufferIndex], 1000);
        close(mRelFd[mCurRenderBufferIndex]);
        mRelFd[mCurRenderBufferIndex] = -1;
    }

    //Clear the visible region on the render buffer
    //XXX: Do this only when needed.
    hwc_rect_t clearRegion;
    getNonWormholeRegion(list, clearRegion);
    clear(renderBuffer, clearRegion);

    int renderTransform = list->hwLayers[list->numHwLayers - 1].transform;
    
    for (int i = 0; i < ctx->listStats[dpy].numAppLayers; i++) {
        hwc_layer_1_t *layer = &list->hwLayers[i];
        if(!(layerProp[i].mFlags & HWC_COPYBIT)) {
            ALOGD_IF(DEBUG_COPYBIT, "%s: Not Marked for copybit", __FUNCTION__);
            continue;
        }
        int ret = -1;
        if (list->hwLayers[i].acquireFenceFd != -1 ) {
            // Wait for acquire Fence on the App buffers.
            ret = sync_wait(list->hwLayers[i].acquireFenceFd, 1000);
            if(ret < 0) {
                ALOGE("%s: sync_wait error!! error no = %d err str = %s",
                                    __FUNCTION__, errno, strerror(errno));
            }
            close(list->hwLayers[i].acquireFenceFd);
            list->hwLayers[i].acquireFenceFd = -1;
        }
        retVal = drawLayerUsingCopybit(ctx, &(list->hwLayers[i]),
                                                    renderBuffer, 
                                                    renderTransform,
                                                    dpy);
        copybitLayerCount++;
        if(retVal < 0) {
            ALOGE("%s : drawLayerUsingCopybit failed", __FUNCTION__);
        }
    }

    if (copybitLayerCount) {
        copybit_device_t *copybit = getCopyBitDevice();
        // Async mode
        if (copybit->flush_get_fence(copybit, fd) < 0)
            *fd = -1;
    }
    return true;
}