already_AddRefed<GLContext>
GLContextProviderWGL::CreateOffscreen(const gfxIntSize& size,
                                      const SurfaceCaps& caps)
{
    if (!sWGLLib.EnsureInitialized()) {
        return nullptr;
    }

    nsRefPtr<GLContextWGL> glContext;

    // Always try to create a pbuffer context first, because we
    // want the context isolation.
    if (sWGLLib.fCreatePbuffer &&
        sWGLLib.fChoosePixelFormat)
    {
        gfxIntSize dummySize = gfxIntSize(16, 16);
        glContext = CreatePBufferOffscreenContext(dummySize, GetGlobalContextWGL());
    }

    // If it failed, then create a window context and use a FBO.
    if (!glContext) {
        glContext = CreateWindowOffscreenContext();
    }

    if (!glContext ||
        !glContext->Init())
    {
        return nullptr;
    }

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

    return glContext.forget();
}
already_AddRefed<GLContext>
GLContextProviderWGL::CreateOffscreen(const gfxIntSize& aSize,
                                      const ContextFormat& aFormat,
                                      const ContextFlags aFlags)
{
    LibType libToUse = WGLLibrary::SelectLibrary(aFlags);
    
    if (!sWGLLib[libToUse].EnsureInitialized(libToUse == WGLLibrary::MESA_LLVMPIPE_LIB)) {
        return nullptr;
    }

    nsRefPtr<GLContextWGL> glContext;

    // Always try to create a pbuffer context first, because we
    // want the context isolation.
    NS_ENSURE_TRUE(Preferences::GetRootBranch(), nullptr);
    const bool preferFBOs = Preferences::GetBool("wgl.prefer-fbo", false);
    if (!preferFBOs &&
        sWGLLib[libToUse].fCreatePbuffer &&
        sWGLLib[libToUse].fChoosePixelFormat)
    {
        glContext = CreatePBufferOffscreenContext(aSize, aFormat,libToUse);
    }

    // If it failed, then create a window context and use a FBO.
    if (!glContext) {
        glContext = CreateWindowOffscreenContext(aFormat, aFlags);
    }

    if (!glContext ||
        !glContext->Init())
    {
        return nullptr;
    }

    if (!glContext->ResizeOffscreenFBOs(aSize, !glContext->mPBuffer))
        return nullptr;

    glContext->mOffscreenSize = aSize;
    glContext->mOffscreenActualSize = aSize;

    return glContext.forget();
}
already_AddRefed<GLContext>
GLContextProviderWGL::CreateOffscreen(const gfxIntSize& aSize,
                                      const ContextFormat& aFormat)
{
    if (!sWGLLibrary.EnsureInitialized()) {
        return nsnull;
    }

    nsRefPtr<GLContextWGL> glContext;

    // Always try to create a pbuffer context first, because we
    // want the context isolation.
    if (sWGLLibrary.fCreatePbuffer &&
        sWGLLibrary.fChoosePixelFormat)
    {
        glContext = CreatePBufferOffscreenContext(aSize, aFormat);
    }

    // If it failed, then create a window context and use a FBO.
    if (!glContext) {
        glContext = CreateWindowOffscreenContext(aSize, aFormat);
    }

    if (!glContext ||
        !glContext->Init())
    {
        return nsnull;
    }

    glContext->mOffscreenSize = aSize;
    glContext->mOffscreenActualSize = aSize;

    if (!glContext->mPBuffer &&
        !glContext->ResizeOffscreenFBO(aSize))
    {
        return nsnull;
    }

    return glContext.forget();
}
already_AddRefed<GLContext>
GLContextProviderWGL::CreateOffscreen(const gfxIntSize& size,
                                      const SurfaceCaps& caps,
                                      ContextFlags flags)
{
    LibType libToUse = WGLLibrary::SelectLibrary(flags);
    
    if (!sWGLLib[libToUse].EnsureInitialized(libToUse == WGLLibrary::MESA_LLVMPIPE_LIB)) {
        return nullptr;
    }

    nsRefPtr<GLContextWGL> glContext;

    // Always try to create a pbuffer context first, because we
    // want the context isolation.
    if (sWGLLib[libToUse].fCreatePbuffer &&
        sWGLLib[libToUse].fChoosePixelFormat)
    {
        gfxIntSize dummySize = gfxIntSize(16, 16);
        glContext = CreatePBufferOffscreenContext(dummySize, libToUse);
    }

    // If it failed, then create a window context and use a FBO.
    if (!glContext) {
        glContext = CreateWindowOffscreenContext(flags);
    }

    if (!glContext ||
        !glContext->Init())
    {
        return nullptr;
    }

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

    return glContext.forget();
}