// Configure
bool FBUpdateHighRes::configure(hwc_context_t *ctx,
        hwc_display_contents_1 *list, int fbZorder) {
    bool ret = false;
    hwc_layer_1_t *layer = &list->hwLayers[list->numHwLayers - 1];
    if (LIKELY(ctx->mOverlay)) {
        int extOnlyLayerIndex = ctx->listStats[mDpy].extOnlyLayerIndex;
        // ext only layer present..
        if(extOnlyLayerIndex != -1) {
            layer = &list->hwLayers[extOnlyLayerIndex];
            layer->compositionType = HWC_OVERLAY;
        }
        overlay::Overlay& ov = *(ctx->mOverlay);

        ovutils::Whf info(mAlignedFBWidth,
                mAlignedFBHeight,
                ovutils::getMdpFormat(HAL_PIXEL_FORMAT_RGBA_8888));

        //Request left pipe
        ovutils::eDest destL = getPipeForFb(ctx, mDpy);
        if(destL == ovutils::OV_INVALID) { //None available
            ALOGE("%s: No pipes available to configure fb for dpy %d's left"
                    " mixer", __FUNCTION__, mDpy);
            return false;
        }
        //Request right pipe
        ovutils::eDest destR = getPipeForFb(ctx, mDpy);
        if(destR == ovutils::OV_INVALID) { //None available
            ALOGE("%s: No pipes available to configure fb for dpy %d's right"
                    " mixer", __FUNCTION__, mDpy);
            return false;
        }

        mDestLeft = destL;
        mDestRight = destR;

        ovutils::eMdpFlags mdpFlagsL = ovutils::OV_MDP_BLEND_FG_PREMULT;

        ovutils::eZorder zOrder = static_cast<ovutils::eZorder>(fbZorder);

        //XXX: FB layer plane alpha is currently sent as zero from
        //surfaceflinger
        ovutils::PipeArgs pargL(mdpFlagsL,
                info,
                zOrder,
                ovutils::IS_FG_OFF,
                ovutils::ROT_FLAGS_NONE,
                ovutils::DEFAULT_PLANE_ALPHA,
                (ovutils::eBlending) getBlending(layer->blending));
        ov.setSource(pargL, destL);

        ovutils::eMdpFlags mdpFlagsR = mdpFlagsL;
        ovutils::setMdpFlags(mdpFlagsR, ovutils::OV_MDSS_MDP_RIGHT_MIXER);
        ovutils::PipeArgs pargR(mdpFlagsR,
                info,
                zOrder,
                ovutils::IS_FG_OFF,
                ovutils::ROT_FLAGS_NONE,
                ovutils::DEFAULT_PLANE_ALPHA,
                (ovutils::eBlending) getBlending(layer->blending));
        ov.setSource(pargR, destR);

        hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
        hwc_rect_t displayFrame = layer->displayFrame;
        // Do not use getNonWormholeRegion() function to calculate the
        // sourceCrop during animation on external display.
        if(ctx->listStats[mDpy].isDisplayAnimating && mDpy) {
            sourceCrop = layer->displayFrame;
            displayFrame = sourceCrop;
        } else if(extOnlyLayerIndex == -1) {
            getNonWormholeRegion(list, sourceCrop);
            displayFrame = sourceCrop;
        }
        ovutils::Dim dcropL(sourceCrop.left, sourceCrop.top,
                            (sourceCrop.right - sourceCrop.left) / 2,
                            sourceCrop.bottom - sourceCrop.top);
        ovutils::Dim dcropR(
            sourceCrop.left + (sourceCrop.right - sourceCrop.left) / 2,
            sourceCrop.top,
            (sourceCrop.right - sourceCrop.left) / 2,
            sourceCrop.bottom - sourceCrop.top);
        ov.setCrop(dcropL, destL);
        ov.setCrop(dcropR, destR);

        int transform = layer->transform;
        ovutils::eTransform orient =
            static_cast<ovutils::eTransform>(transform);
        ov.setTransform(orient, destL);
        ov.setTransform(orient, destR);

        //For FB left, top will always be 0
        //That should also be the case if using 2 mixers for single display
        ovutils::Dim dposL(displayFrame.left,
                           displayFrame.top,
                           (displayFrame.right - displayFrame.left) / 2,
                           displayFrame.bottom - displayFrame.top);
        ov.setPosition(dposL, destL);
        ovutils::Dim dposR(0,
                           displayFrame.top,
                           (displayFrame.right - displayFrame.left) / 2,
                           displayFrame.bottom - displayFrame.top);
        ov.setPosition(dposR, destR);

        ret = true;
        if (!ov.commit(destL)) {
            ALOGE("%s: commit fails for left", __FUNCTION__);
            ret = false;
        }
        if (!ov.commit(destR)) {
            ALOGE("%s: commit fails for right", __FUNCTION__);
            ret = false;
        }
        if(ret == false) {
            ctx->mLayerRotMap[mDpy]->clear();
        }
    }
    return ret;
}
// Configure
bool FBUpdateNonSplit::configure(hwc_context_t *ctx, hwc_display_contents_1 *list,
                               hwc_rect_t fbUpdatingRect, int fbZorder) {
    bool ret = false;
    hwc_layer_1_t *layer = &list->hwLayers[list->numHwLayers - 1];
    if (LIKELY(ctx->mOverlay)) {
        int extOnlyLayerIndex = ctx->listStats[mDpy].extOnlyLayerIndex;
        // ext only layer present..
        if(extOnlyLayerIndex != -1) {
            layer = &list->hwLayers[extOnlyLayerIndex];
            layer->compositionType = HWC_OVERLAY;
        }
        overlay::Overlay& ov = *(ctx->mOverlay);

        ovutils::Whf info(mAlignedFBWidth, mAlignedFBHeight,
                ovutils::getMdpFormat(HAL_PIXEL_FORMAT_RGBA_8888,
                    mTileEnabled));

        Overlay::PipeSpecs pipeSpecs;
        pipeSpecs.formatClass = Overlay::FORMAT_RGB;
        pipeSpecs.needsScaling = qhwc::needsScaling(layer);
        pipeSpecs.dpy = mDpy;
        pipeSpecs.mixer = Overlay::MIXER_DEFAULT;
        pipeSpecs.fb = true;

        ovutils::eDest dest = ov.getPipe(pipeSpecs);
        if(dest == ovutils::OV_INVALID) { //None available
            ALOGE("%s: No pipes available to configure fb for dpy %d",
                __FUNCTION__, mDpy);
            return false;
        }
        mDest = dest;

        if((mDpy && ctx->deviceOrientation) &&
            ctx->listStats[mDpy].isDisplayAnimating) {
            fbZorder = 0;
        }

        ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_BLEND_FG_PREMULT;
        ovutils::eIsFg isFg = ovutils::IS_FG_OFF;
        ovutils::eZorder zOrder = static_cast<ovutils::eZorder>(fbZorder);

        hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
        hwc_rect_t displayFrame = layer->displayFrame;

        // No FB update optimization on (1) Custom FB resolution,
        // (2) External Mirror mode, (3) External orientation
        if(!ctx->dpyAttr[mDpy].customFBSize && !ctx->mBufferMirrorMode
           && !ctx->mExtOrientation) {
            sourceCrop = fbUpdatingRect;
            displayFrame = fbUpdatingRect;
        }

        int transform = layer->transform;
        int rotFlags = ovutils::ROT_FLAGS_NONE;

        ovutils::eTransform orient =
                    static_cast<ovutils::eTransform>(transform);
        // use ext orientation if any
        int extOrient = getExtOrientation(ctx);

        // Do not use getNonWormholeRegion() function to calculate the
        // sourceCrop during animation on external display and
        // Dont do wormhole calculation when extorientation is set on External
        // Dont do wormhole calculation when extDownscale is enabled on External
        if(ctx->listStats[mDpy].isDisplayAnimating && mDpy) {
            sourceCrop = layer->displayFrame;
        } else if((!mDpy ||
                  (mDpy && !extOrient
                  && !ctx->dpyAttr[mDpy].mDownScaleMode))
                  && (extOnlyLayerIndex == -1)) {
            if(ctx->mOverlay->isUIScalingOnExternalSupported() &&
                !ctx->dpyAttr[mDpy].customFBSize) {
                getNonWormholeRegion(list, sourceCrop);
                displayFrame = sourceCrop;
            }
        }
        calcExtDisplayPosition(ctx, NULL, mDpy, sourceCrop, displayFrame,
                                   transform, orient);
        //Store the displayFrame, will be used in getDisplayViewFrame
        ctx->dpyAttr[mDpy].mDstRect = displayFrame;
        setMdpFlags(layer, mdpFlags, 0, transform);
        // For External use rotator if there is a rotation value set
        ret = preRotateExtDisplay(ctx, layer, info,
                sourceCrop, mdpFlags, rotFlags);
        if(!ret) {
            ALOGE("%s: preRotate for external Failed!", __FUNCTION__);
            return false;
        }
        //For the mdp, since either we are pre-rotating or MDP does flips
        orient = ovutils::OVERLAY_TRANSFORM_0;
        transform = 0;
        ovutils::PipeArgs parg(mdpFlags, info, zOrder, isFg,
                               static_cast<ovutils::eRotFlags>(rotFlags),
                               ovutils::DEFAULT_PLANE_ALPHA,
                               (ovutils::eBlending)
                               getBlending(layer->blending));
        ret = true;
        if(configMdp(ctx->mOverlay, parg, orient, sourceCrop, displayFrame,
                    NULL, mDest) < 0) {
            ALOGE("%s: configMdp failed for dpy %d", __FUNCTION__, mDpy);
            ret = false;
        }
    }
    return ret;
}
// Configure
bool FBUpdateLowRes::configure(hwc_context_t *ctx, hwc_display_contents_1 *list,
                               int fbZorder) {
    bool ret = false;
    hwc_layer_1_t *layer = &list->hwLayers[list->numHwLayers - 1];
    if (LIKELY(ctx->mOverlay)) {
        int extOnlyLayerIndex = ctx->listStats[mDpy].extOnlyLayerIndex;
        // ext only layer present..
        if(extOnlyLayerIndex != -1) {
            layer = &list->hwLayers[extOnlyLayerIndex];
            layer->compositionType = HWC_OVERLAY;
        }
        overlay::Overlay& ov = *(ctx->mOverlay);

        ovutils::Whf info(mAlignedFBWidth,
                mAlignedFBHeight,
                ovutils::getMdpFormat(HAL_PIXEL_FORMAT_RGBA_8888));

        //Request a fb pipe
        ovutils::eDest dest = getPipeForFb(ctx, mDpy);
        if(dest == ovutils::OV_INVALID) { //None available
            ALOGE("%s: No pipes available to configure fb for dpy %d",
                __FUNCTION__, mDpy);
            return false;
        }
        mDest = dest;

        if((mDpy && ctx->deviceOrientation) &&
            ctx->listStats[mDpy].isDisplayAnimating) {
            fbZorder = 0;
        }

        ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_BLEND_FG_PREMULT;
        ovutils::eIsFg isFg = ovutils::IS_FG_OFF;
        ovutils::eZorder zOrder = static_cast<ovutils::eZorder>(fbZorder);

        hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
        hwc_rect_t displayFrame = layer->displayFrame;
        int transform = layer->transform;
        int rotFlags = ovutils::ROT_FLAGS_NONE;

        ovutils::eTransform orient =
                    static_cast<ovutils::eTransform>(transform);
        // use ext orientation if any
        int extOrient = getExtOrientation(ctx);

        // Do not use getNonWormholeRegion() function to calculate the
        // sourceCrop during animation on external display and
        // Dont do wormhole calculation when extorientation is set on External
        // Dont do wormhole calculation when extDownscale is enabled on External
        if(ctx->listStats[mDpy].isDisplayAnimating && mDpy) {
            sourceCrop = layer->displayFrame;
            displayFrame = sourceCrop;
        } else if((!mDpy ||
                   (mDpy && !extOrient
                   && !ctx->dpyAttr[mDpy].mDownScaleMode))
                   && (extOnlyLayerIndex == -1)) {
                getNonWormholeRegion(list, sourceCrop);
                displayFrame = sourceCrop;
        }
        calcExtDisplayPosition(ctx, NULL, mDpy, sourceCrop, displayFrame,
                                   transform, orient);
        //Store the displayFrame, will be used in getDisplayViewFrame
        ctx->dpyAttr[mDpy].mDstRect = displayFrame;
        setMdpFlags(layer, mdpFlags, 0, transform);
        // For External use rotator if there is a rotation value set
        ret = preRotateExtDisplay(ctx, layer, info,
                sourceCrop, mdpFlags, rotFlags);
        if(!ret) {
            ALOGE("%s: preRotate for external Failed!", __FUNCTION__);
            ctx->mOverlay->clear(mDpy);
            ctx->mLayerRotMap[mDpy]->clear();
            return false;
        }
        //For the mdp, since either we are pre-rotating or MDP does flips
        orient = ovutils::OVERLAY_TRANSFORM_0;
        transform = 0;

        //XXX: FB layer plane alpha is currently sent as zero from
        //surfaceflinger
        ovutils::PipeArgs parg(mdpFlags,
                info,
                zOrder,
                isFg,
                static_cast<ovutils::eRotFlags>(rotFlags),
                ovutils::DEFAULT_PLANE_ALPHA,
                (ovutils::eBlending) getBlending(layer->blending));

        ret = true;
        if(configMdp(ctx->mOverlay, parg, orient, sourceCrop, displayFrame,
                    NULL, mDest) < 0) {
            ALOGE("%s: configMdp failed for dpy %d", __FUNCTION__, mDpy);
            ctx->mLayerRotMap[mDpy]->clear();
            ret = false;
        }
    }
    return ret;
}
void HwcDebug::logLayer(size_t layerIndex, hwc_layer_1_t hwLayers[])
{
    if (NULL == hwLayers) {
        ALOGE("Display[%s] Layer[%zu] Error. No hwc layers to log.",
            mDisplayName, layerIndex);
        return;
    }

    hwc_layer_1_t *layer = &hwLayers[layerIndex];
    hwc_rect_t sourceCrop = integerizeSourceCrop(layer->sourceCropf);
    hwc_rect_t displayFrame = layer->displayFrame;
    size_t numHwcRects = layer->visibleRegionScreen.numRects;
    hwc_rect_t const *hwcRects = layer->visibleRegionScreen.rects;
    private_handle_t *hnd = (private_handle_t *)layer->handle;

    char pixFormatStr[32] = "None";
    String8 hwcVisRegsScrLog("[None]");

    for (size_t i = 0 ; (hwcRects && (i < numHwcRects)); i++) {
        if (0 == i)
            hwcVisRegsScrLog.clear();
        hwcVisRegsScrLog.appendFormat("[%dl, %dt, %dr, %db]",
                                        hwcRects[i].left, hwcRects[i].top,
                                        hwcRects[i].right, hwcRects[i].bottom);
    }

    if (hnd)
        getHalPixelFormatStr(hnd->format, pixFormatStr);

    // Log Line 1
    ALOGI("Display[%s] Layer[%zu] SrcBuff[%dx%d] SrcCrop[%dl, %dt, %dr, %db] "
        "DispFrame[%dl, %dt, %dr, %db] VisRegsScr%s", mDisplayName, layerIndex,
        (hnd)? getWidth(hnd) : -1, (hnd)? getHeight(hnd) : -1,
        sourceCrop.left, sourceCrop.top,
        sourceCrop.right, sourceCrop.bottom,
        displayFrame.left, displayFrame.top,
        displayFrame.right, displayFrame.bottom,
        hwcVisRegsScrLog.string());
    // Log Line 2
    ALOGI("Display[%s] Layer[%zu] LayerCompType = %s, Format = %s, "
        "Orientation = %s, Flags = %s%s%s, Hints = %s%s%s, "
        "Blending = %s%s%s", mDisplayName, layerIndex,
        (layer->compositionType == HWC_FRAMEBUFFER)? "Framebuffer(GPU)":
            (layer->compositionType == HWC_OVERLAY)? "Overlay":
            (layer->compositionType == HWC_BACKGROUND)? "Background":"???",
         pixFormatStr,
         (layer->transform == 0)? "ROT_0":
             (layer->transform == HWC_TRANSFORM_FLIP_H)? "FLIP_H":
             (layer->transform == HWC_TRANSFORM_FLIP_V)? "FLIP_V":
             (layer->transform == HWC_TRANSFORM_ROT_90)? "ROT_90":
                                                        "ROT_INVALID",
         (layer->flags)? "": "[None]",
         (layer->flags & HWC_SKIP_LAYER)? "[Skip layer]":"",
         (layer->flags & qhwc::HWC_MDPCOMP)? "[MDP Comp]":"",
         (layer->hints)? "":"[None]",
         (layer->hints & HWC_HINT_TRIPLE_BUFFER)? "[Triple Buffer]":"",
         (layer->hints & HWC_HINT_CLEAR_FB)? "[Clear FB]":"",
         (layer->blending == HWC_BLENDING_NONE)? "[None]":"",
         (layer->blending == HWC_BLENDING_PREMULT)? "[PreMult]":"",
         (layer->blending == HWC_BLENDING_COVERAGE)? "[Coverage]":"");
}