bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list) {

    sCopyBitDraw = false;

    int compositionType =
        qdutils::QCCompositionType::getInstance().getCompositionType();

    if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
        (compositionType == qdutils::COMPOSITION_TYPE_CPU))   {
        //GPU/CPU composition, don't change layer composition type
        return true;
    }

    if(!(validateParams(ctx, list))) {
       ALOGE("%s:Invalid Params", __FUNCTION__);
       return false;
    }

    if(sIsSkipLayerPresent) {
        //GPU will be anyways used
        return false;
    }

    bool useCopybitForYUV = canUseCopybitForYUV(ctx);
    bool useCopybitForRGB = canUseCopybitForRGB(ctx, list);

    for (int i=list->numHwLayers-1; i >= 0 ; i--) {
        private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;

        if (hnd->bufferType == BUFFER_TYPE_VIDEO) {
          //YUV layer, check, if copybit can be used
          // mark the video layer to gpu when all layer is
          // going to gpu in case of dynamic composition.
          if (useCopybitForYUV && useCopybitForRGB) {
              list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
              sCopyBitDraw = true;
          }
       } else if (hnd->bufferType == BUFFER_TYPE_UI) {
          //RGB layer, check, if copybit can be used
          if (useCopybitForRGB) {
              list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
              sCopyBitDraw = true;
          }
       }
    }
    return true;
}
bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list) {

    int compositionType =
        qdutils::QCCompositionType::getInstance().getCompositionType();

    if ((compositionType & qdutils::COMPOSITION_TYPE_GPU) ||
        (compositionType & qdutils::COMPOSITION_TYPE_CPU))   {
        //GPU/CPU composition, don't change layer composition type
        return true;
    }

    bool useCopybitForYUV = canUseCopybitForYUV(ctx);
    bool useCopybitForRGB = canUseCopybitForRGB(ctx, list);

    if(!(validateParams(ctx, list))) {
       ALOGE("%s:Invalid Params", __FUNCTION__);
       return false;
    }
    sCopyBitDraw = false;
    for (int i=list->numHwLayers-1; i >= 0 ; i--) {
        private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;

        if (isSkipLayer(&list->hwLayers[i])) {
            return true;
        } else if (hnd->bufferType == BUFFER_TYPE_VIDEO) {
          //YUV layer, check, if copybit can be used
          if (useCopybitForYUV) {
              list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
              sCopyBitDraw = true;
          }
       } else if (hnd->bufferType == BUFFER_TYPE_UI) {
          //RGB layer, check, if copybit can be used
          if (useCopybitForRGB) {
              list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
              sCopyBitDraw = true;
          }
       }
    }
    return true;
}
bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list, int dpy) {

    if(mEngine == NULL) {
        // No copybit device found - cannot use copybit
        return false;
    }
    int compositionType = qdutils::QCCompositionType::
                                    getInstance().getCompositionType();

    if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
        (compositionType == qdutils::COMPOSITION_TYPE_CPU))   {
        //GPU/CPU composition, don't change layer composition type
        return true;
    }

    if(!(validateParams(ctx, list))) {
        ALOGE("%s:Invalid Params", __FUNCTION__);
        return false;
    }

    if(ctx->listStats[dpy].skipCount) {
        //GPU will be anyways used
        return false;
    }
    
    if (ctx->listStats[dpy].numAppLayers > MAX_NUM_LAYERS) {
        // Reached max layers supported by HWC.
        return false;
    }
    
    if (!ctx->mFBUpdate[dpy]->isUsed())
        return false;

    bool useCopybitForYUV = canUseCopybitForYUV(ctx);
    bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
    LayerProp *layerProp = ctx->layerProp[dpy];
    size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
    hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
    private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;

    //Allocate render buffers if they're not allocated
    if (useCopybitForYUV || useCopybitForRGB) {
        int ret = allocRenderBuffers(fbHnd->width,
                                     fbHnd->height,
                                     fbHnd->format);
        if (ret < 0) {
            return false;
        }
    }

    int i;
    if (useCopybitForYUV) {
        for (i = 0; i < ctx->listStats[dpy].numAppLayers; i++) {
            if (list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
                private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
                if (hnd->bufferType == BUFFER_TYPE_VIDEO) {
                    layerProp[i].mFlags |= HWC_COPYBIT;
                    list->hwLayers[i].compositionType = HWC_OVERLAY;
                    list->hwLayers[i].hints |= HWC_HINT_CLEAR_FB;
                    mCopyBitDraw = true;
                }
            }
        }
    }
    if (useCopybitForRGB || mCopyBitDraw) {
        for (i = 0; i < ctx->listStats[dpy].numAppLayers; i++) {
            if (list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
                private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
                if (hnd->bufferType == BUFFER_TYPE_UI) {
                    layerProp[i].mFlags |= HWC_COPYBIT;
                    list->hwLayers[i].compositionType = HWC_OVERLAY;
                    list->hwLayers[i].hints |= HWC_HINT_CLEAR_FB;
                    mCopyBitDraw = true;
                }
            }
        }
    }
    
    if (mCopyBitDraw) {
        mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) % NUM_RENDER_BUFFERS;
    }
    
    return true;
}
bool CopyBit::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
                                                            int dpy) {

    if(mEngine == NULL) {
        // No copybit device found - cannot use copybit
        return false;
    }
    int compositionType = qdutils::QCCompositionType::
                                    getInstance().getCompositionType();

    if ((compositionType == qdutils::COMPOSITION_TYPE_GPU) ||
        (compositionType == qdutils::COMPOSITION_TYPE_CPU))   {
        //GPU/CPU composition, don't change layer composition type
        return true;
    }

    if(!(validateParams(ctx, list))) {
        ALOGE("%s:Invalid Params", __FUNCTION__);
        return false;
    }

    if(ctx->listStats[dpy].skipCount) {
        //GPU will be anyways used
        return false;
    }

    if (ctx->listStats[dpy].numAppLayers > MAX_NUM_LAYERS) {
        // Reached max layers supported by HWC.
        return false;
    }

    bool useCopybitForYUV = canUseCopybitForYUV(ctx);
    bool useCopybitForRGB = canUseCopybitForRGB(ctx, list, dpy);
    LayerProp *layerProp = ctx->layerProp[dpy];
    size_t fbLayerIndex = ctx->listStats[dpy].fbLayerIndex;
    hwc_layer_1_t *fbLayer = &list->hwLayers[fbLayerIndex];
    private_handle_t *fbHnd = (private_handle_t *)fbLayer->handle;



    //Allocate render buffers if they're not allocated
    if (useCopybitForYUV || useCopybitForRGB) {
        int ret = allocRenderBuffers(fbHnd->width,
                                     fbHnd->height,
                                     fbHnd->format);
        if (ret < 0) {
            return false;
        } else {
            mCurRenderBufferIndex = (mCurRenderBufferIndex + 1) %
                NUM_RENDER_BUFFERS;
        }
    }


    // numAppLayers-1, as we iterate till 0th layer index
    for (int i = ctx->listStats[dpy].numAppLayers-1; i >= 0 ; i--) {
        private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;

        if ((hnd->bufferType == BUFFER_TYPE_VIDEO && useCopybitForYUV) ||
            (hnd->bufferType == BUFFER_TYPE_UI && useCopybitForRGB)) {
            layerProp[i].mFlags |= HWC_COPYBIT;
            list->hwLayers[i].compositionType = HWC_OVERLAY;
            mCopyBitDraw = true;
        } else {
            // We currently cannot mix copybit layers with layers marked to
            // be drawn on the framebuffer or that are on the layer cache.
            mCopyBitDraw = false;
            //There is no need to reset layer properties here as we return in
            //draw if mCopyBitDraw is false
        }
    }
    return true;
}