QVGPaintEngine *qt_vg_create_paint_engine(void)
{
    QVGSharedContext *shared = sharedContext();
    if (!shared->engine)
        shared->engine = new QVGPaintEngine();
    return shared->engine;
}
void qt_vg_register_pixmap(QVGPixmapData *pd)
{
    QVGSharedContext *shared = sharedContext();
    pd->next = shared->firstPixmap;
    pd->prev = 0;
    if (shared->firstPixmap)
        shared->firstPixmap->prev = pd;
    shared->firstPixmap = pd;
}
void qt_vg_unregister_pixmap(QVGPixmapData *pd)
{
    if (pd->next)
        pd->next->prev = pd->prev;
    if (pd->prev) {
        pd->prev->next = pd->next;
    } else {
        QVGSharedContext *shared = sharedContext();
        if (shared)
           shared->firstPixmap = pd->next;
    }
}
QEglContext *qt_vg_create_context(QPaintDevice *device)
{
    QVGSharedContext *shared = sharedContext();
    if (shared->context) {
        ++(shared->refCount);
        return shared->context;
    } else {
        shared->context = createContext(device);
        shared->refCount = 1;
        return shared->context;
    }
}
QEglContext *qt_vg_create_context(QPaintDevice *device, int devType)
{
    QVGSharedContext *shared = sharedContext();
    if (devType == QInternal::Widget)
        ++(shared->widgetRefCount);
    if (shared->context) {
        ++(shared->refCount);
        return shared->context;
    } else {
        shared->context = createContext(device);
        shared->refCount = 1;
        return shared->context;
    }
}
void qt_vg_destroy_context(QEglContext *context)
{
    QVGSharedContext *shared = sharedContext();
    if (shared->context != context) {
        // This is not the shared context.  Shouldn't happen!
        delete context;
    } else if (--(shared->refCount) <= 0) {
        shared->context->makeCurrent(qt_vg_shared_surface());
        delete shared->engine;
        shared->engine = 0;
        shared->context->doneCurrent();
        if (shared->surface != EGL_NO_SURFACE) {
            eglDestroySurface(shared->context->display(), shared->surface);
            shared->surface = EGL_NO_SURFACE;
        }
        delete shared->context;
        shared->context = 0;
    }
}
void qt_vg_destroy_context(QEglContext *context, int devType)
{
    QVGSharedContext *shared = sharedContext();
    if (shared->context != context) {
        // This is not the shared context.  Shouldn't happen!
        delete context;
        return;
    }
    if (devType == QInternal::Widget)
        --(shared->widgetRefCount);
    if (--(shared->refCount) <= 0) {
        qt_vg_destroy_shared_context(shared);
    } else if (shared->widgetRefCount <= 0 && devType == QInternal::Widget) {
        // All of the widget window surfaces have been destroyed
        // but we still have VG pixmaps active.  Ask them to hibernate
        // to free up GPU resources until a widget is shown again.
        // This may eventually cause the EGLContext to be destroyed
        // because nothing in the system needs a context, which will
        // free up even more GPU resources.
        qt_vg_hibernate_pixmaps(shared);
    }
}
EGLSurface qt_vg_shared_surface(void)
{
    QVGSharedContext *shared = sharedContext();
    if (shared->surface == EGL_NO_SURFACE) {
        EGLint attribs[7];
        attribs[0] = EGL_WIDTH;
        attribs[1] = 16;
        attribs[2] = EGL_HEIGHT;
        attribs[3] = 16;
#ifdef EGL_VG_ALPHA_FORMAT_PRE_BIT
        if (isPremultipliedContext(shared->context)) {
            attribs[4] = EGL_VG_ALPHA_FORMAT;
            attribs[5] = EGL_VG_ALPHA_FORMAT_PRE;
            attribs[6] = EGL_NONE;
        } else
#endif
        {
            attribs[4] = EGL_NONE;
        }
        shared->surface = eglCreatePbufferSurface
            (QEgl::display(), shared->context->config(), attribs);
    }
    return shared->surface;
}