already_AddRefed<gfx::SourceSurface> GLImage::GetAsSourceSurface() { MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread"); if (!sSnapshotContext) { nsCString discardFailureId; sSnapshotContext = GLContextProvider::CreateHeadless(CreateContextFlags::NONE, &discardFailureId); if (!sSnapshotContext) { NS_WARNING("Failed to create snapshot GLContext"); return nullptr; } } sSnapshotContext->MakeCurrent(); ScopedTexture scopedTex(sSnapshotContext); ScopedBindTexture boundTex(sSnapshotContext, scopedTex.Texture()); gfx::IntSize size = GetSize(); sSnapshotContext->fTexImage2D(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_RGBA, size.width, size.height, 0, LOCAL_GL_RGBA, LOCAL_GL_UNSIGNED_BYTE, nullptr); ScopedFramebufferForTexture autoFBForTex(sSnapshotContext, scopedTex.Texture()); if (!autoFBForTex.IsComplete()) { MOZ_CRASH("GFX: ScopedFramebufferForTexture failed."); } const gl::OriginPos destOrigin = gl::OriginPos::TopLeft; if (!sSnapshotContext->BlitHelper()->BlitImageToFramebuffer(this, size, autoFBForTex.FB(), destOrigin)) { return nullptr; } RefPtr<gfx::DataSourceSurface> source = gfx::Factory::CreateDataSourceSurface(size, gfx::SurfaceFormat::B8G8R8A8); if (NS_WARN_IF(!source)) { return nullptr; } ScopedBindFramebuffer bind(sSnapshotContext, autoFBForTex.FB()); ReadPixelsIntoDataSurface(sSnapshotContext, source); return source.forget(); }
TemporaryRef<gfx::SourceSurface> GLImage::GetAsSourceSurface() { MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread"); if (!sSnapshotContext) { sSnapshotContext = GLContextProvider::CreateHeadless(false); if (!sSnapshotContext) { NS_WARNING("Failed to create snapshot GLContext"); return nullptr; } } sSnapshotContext->MakeCurrent(); ScopedTexture scopedTex(sSnapshotContext); ScopedBindTexture boundTex(sSnapshotContext, scopedTex.Texture()); gfx::IntSize size = GetSize(); sSnapshotContext->fTexImage2D(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_RGBA, size.width, size.height, 0, LOCAL_GL_RGBA, LOCAL_GL_UNSIGNED_BYTE, nullptr); ScopedFramebufferForTexture fb(sSnapshotContext, scopedTex.Texture()); GLBlitHelper helper(sSnapshotContext); if (!helper.BlitImageToFramebuffer(this, size, fb.FB(), true)) { return nullptr; } RefPtr<gfx::DataSourceSurface> source = gfx::Factory::CreateDataSourceSurface(size, gfx::SurfaceFormat::B8G8R8A8); if (NS_WARN_IF(!source)) { return nullptr; } ScopedBindFramebuffer bind(sSnapshotContext, fb.FB()); ReadPixelsIntoDataSurface(sSnapshotContext, source); return source.forget(); }