bool TngGrallocBufferMapper::gttMap(void *vaddr,
                                      uint32_t size,
                                      uint32_t gttAlign,
                                      int *offset)
{
    struct psb_gtt_mapping_arg arg;
    bool ret;

    ALOGTRACE("vaddr = %p, size = %d", vaddr, size);

    if (!vaddr || !size || !offset) {
        VLOGTRACE("invalid parameters");
        return false;
    }

    arg.type = PSB_GTT_MAP_TYPE_VIRTUAL;
    arg.page_align = gttAlign;
    arg.vaddr = (uint32_t)vaddr;
    arg.size = size;

    Drm *drm = Hwcomposer::getInstance().getDrm();
    ret = drm->writeReadIoctl(DRM_PSB_GTT_MAP, &arg, sizeof(arg));
    if (ret == false) {
        ELOGTRACE("gtt mapping failed");
        return false;
    }

    VLOGTRACE("offset = %#x", arg.offset_pages);
    *offset =  arg.offset_pages;
    return true;
}
bool TngSpritePlane::isDisabled()
{
    RETURN_FALSE_IF_NOT_INIT();

    struct drm_psb_register_rw_arg arg;
    memset(&arg, 0, sizeof(struct drm_psb_register_rw_arg));

    if (mType == DisplayPlane::PLANE_SPRITE)
        arg.plane.type = DC_SPRITE_PLANE;
    else
        arg.plane.type = DC_PRIMARY_PLANE;

    arg.get_plane_state_mask = 1;
    arg.plane.index = mIndex;
    arg.plane.ctx = 0;

    // issue ioctl
    Drm *drm = Hwcomposer::getInstance().getDrm();
    bool ret = drm->writeReadIoctl(DRM_PSB_REGISTER_RW, &arg, sizeof(arg));
    if (ret == false) {
        WTRACE("plane state query failed with error code %d", ret);
        return false;
    }

    return arg.plane.ctx == PSB_DC_PLANE_DISABLED;
}
bool TngSpritePlane::enablePlane(bool enabled)
{
    RETURN_FALSE_IF_NOT_INIT();

    struct drm_psb_register_rw_arg arg;
    memset(&arg, 0, sizeof(struct drm_psb_register_rw_arg));
    if (enabled) {
        arg.plane_enable_mask = 1;
    } else {
        arg.plane_disable_mask = 1;
    }
    arg.plane.type = DC_SPRITE_PLANE;
    arg.plane.index = mIndex;
    arg.plane.ctx = 0;

    // issue ioctl
    Drm *drm = Hwcomposer::getInstance().getDrm();
    bool ret = drm->writeReadIoctl(DRM_PSB_REGISTER_RW, &arg, sizeof(arg));
    if (ret == false) {
        WTRACE("sprite enabling (%d) failed with error code %d", enabled, ret);
        return false;
    }

    Hwcomposer& hwc = Hwcomposer::getInstance();
    DisplayPlaneManager *pm = hwc.getPlaneManager();
    void *config = pm->getZOrderConfig();
    if (config != NULL) {
        struct intel_dc_plane_zorder *zorder =  (struct intel_dc_plane_zorder *)config;
        zorder->abovePrimary = 0;
    }

    return true;

}
bool DisplayPlane::assignToDevice(int disp)
{
    RETURN_FALSE_IF_NOT_INIT();
    ALOGTRACE("disp = %d", disp);

    mDevice = disp;

    Drm *drm = Hwcomposer::getInstance().getDrm();
    if (!drm->getModeInfo(mDevice, mModeInfo)) {
        ELOGTRACE("failed to get mode info");
    }

    mPanelOrientation = drm->getPanelOrientation(mDevice);

    mForceScaling = DisplayQuery::forceFbScaling(disp);
    drm->getDisplayResolution(disp, mDisplayWidth, mDisplayHeight);

    if (mForceScaling) {
        mModeInfo.hdisplay = mDisplayWidth;
        mModeInfo.vdisplay = mDisplayHeight;
        mPosition.x = 0;
        mPosition.y = 0;
        mPosition.w = mDisplayWidth;
        mPosition.h = mDisplayHeight;
    }
    mDisplayCrop.x = 0;
    mDisplayCrop.y = 0;
    mDisplayCrop.w = mDisplayWidth;
    mDisplayCrop.h = mDisplayHeight;

    return true;
}
bool VsyncControl::wait(int disp, int64_t& timestamp)
{
    ATRACE("disp = %d", disp);

    struct drm_psb_vsync_set_arg arg;
    memset(&arg, 0, sizeof(struct drm_psb_vsync_set_arg));

    arg.vsync_operation_mask = VSYNC_WAIT;

    // pipe equals to disp
    arg.vsync.pipe = disp;

    Drm *drm = Hwcomposer::getInstance().getDrm();
    bool ret = drm->writeReadIoctl(DRM_PSB_VSYNC_SET, &arg, sizeof(arg));
    timestamp = (int64_t)arg.vsync.timestamp;
    return ret;
}
bool TngDisplayContext::setCursorPosition(int disp, int x, int y)
{
    DLOGTRACE("setCursorPosition");
    struct intel_dc_cursor_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.pipe = disp;
    if (x < 0) {
        ctx.pos |= 1 << 15;
        x = -x;
    }
    if (y < 0) {
        ctx.pos |= 1 << 31;
        y = -y;
    }
    ctx.pos |= (y & 0xfff) << 16 | (x & 0xfff);
    Drm *drm = Hwcomposer::getInstance().getDrm();
    return drm->writeIoctl(DRM_PSB_UPDATE_CURSOR_POS, &ctx, sizeof(ctx));
}
bool VsyncControl::control(int disp, bool enabled)
{
    ATRACE("disp = %d, enabled = %d", disp, enabled);

    struct drm_psb_vsync_set_arg arg;
    memset(&arg, 0, sizeof(struct drm_psb_vsync_set_arg));

    // pipe equals to disp
    arg.vsync.pipe = disp;

    if (enabled) {
        arg.vsync_operation_mask = VSYNC_ENABLE;
    } else {
        arg.vsync_operation_mask = VSYNC_DISABLE;
    }
    Drm *drm = Hwcomposer::getInstance().getDrm();
    return drm->writeReadIoctl(DRM_PSB_VSYNC_SET, &arg, sizeof(arg));
}
bool TngGrallocBufferMapper::gttUnmap(void *vaddr)
{
    struct psb_gtt_mapping_arg arg;
    bool ret;

    ALOGTRACE("vaddr = %p", vaddr);

    if (!vaddr) {
        ELOGTRACE("invalid parameter");
        return false;
    }

    arg.type = PSB_GTT_MAP_TYPE_VIRTUAL;
    arg.vaddr = (uint32_t)vaddr;

    Drm *drm = Hwcomposer::getInstance().getDrm();
    ret = drm->writeIoctl(DRM_PSB_GTT_UNMAP, &arg, sizeof(arg));
    if (ret == false) {
        ELOGTRACE("gtt unmapping failed");
        return false;
    }

    return true;
}