already_AddRefed<GLContext>
GLContextProviderGLX::CreateOffscreen(const gfxIntSize& aSize,
                                      const ContextFormat& aFormat)
{

    nsRefPtr<GLContextGLX> glContext =
        CreateOffscreenPixmapContext(aSize, aFormat, PR_TRUE);

    if (!glContext) {
        return nsnull;
    }

    if (!glContext->GetSharedContext()) {
        // no point in returning anything if sharing failed, we can't
        // render from this
        return nsnull;
    }

    if (!glContext->ResizeOffscreenFBO(aSize)) {
        // we weren't able to create the initial
        // offscreen FBO, so this is dead
        return nsnull;
    }

    return glContext.forget();
}
GLContext *
GLContextProviderGLX::GetGlobalContext()
{
    static bool triedToCreateContext = false;
    if (!triedToCreateContext && !gGlobalContext) {
        triedToCreateContext = true;
        gGlobalContext = CreateOffscreenPixmapContext(gfxIntSize(1, 1),
                                                      ContextFormat(ContextFormat::BasicRGB24),
                                                      PR_FALSE);
        if (gGlobalContext)
            gGlobalContext->SetIsGlobalSharedContext(PR_TRUE);
    }

    return gGlobalContext;
}
already_AddRefed<GLContext>
GLContextProviderGLX::CreateOffscreen(const gfxIntSize& size,
                                      const SurfaceCaps& caps,
                                      ContextFlags flags)
{
    LibType libType = GLXLibrary::SelectLibrary(flags);
    gCurrLib = libType;

    gfxIntSize dummySize = gfxIntSize(16, 16);
    nsRefPtr<GLContextGLX> glContext =
        CreateOffscreenPixmapContext(dummySize, libType);

    if (!glContext)
        return nullptr;

    if (!glContext->InitOffscreen(size, caps))
        return nullptr;

    return glContext.forget();
}
GLContext*
GLContextProviderGLX::GetGlobalContext(const ContextFlags aFlag)
{
    // TODO: get GLX context sharing to work well with multiple threads
    if (!gUseContextSharing) {
        return nullptr;
    }

    LibType libType = GLXLibrary::SelectLibrary(aFlag);
    static bool triedToCreateContext[GLXLibrary::LIBS_MAX] = {false, false};
    if (!triedToCreateContext[libType] && !gGlobalContext[libType]) {
        triedToCreateContext[libType] = true;

        gfxIntSize dummySize = gfxIntSize(16, 16);
        gGlobalContext[libType] = CreateOffscreenPixmapContext(dummySize, libType);
        if (gGlobalContext[libType])
            gGlobalContext[libType]->SetIsGlobalSharedContext(true);
    }

    return gGlobalContext[libType];
}