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(); }
void GLBlitHelper::BlitTextureToFramebuffer(GLuint srcTex, GLuint destFB, const gfxIntSize& srcSize, const gfxIntSize& destSize, GLenum srcTarget) { MOZ_ASSERT(mGL->fIsTexture(srcTex)); MOZ_ASSERT(!destFB || mGL->fIsFramebuffer(destFB)); if (mGL->IsSupported(GLFeature::framebuffer_blit)) { ScopedFramebufferForTexture srcWrapper(mGL, srcTex, srcTarget); MOZ_ASSERT(srcWrapper.IsComplete()); BlitFramebufferToFramebuffer(srcWrapper.FB(), destFB, srcSize, destSize); return; } ScopedBindFramebuffer boundFB(mGL, destFB); // UseTexQuadProgram initializes a shader that reads // from texture unit 0. ScopedBindTextureUnit boundTU(mGL, LOCAL_GL_TEXTURE0); ScopedBindTexture boundTex(mGL, srcTex, srcTarget); GLuint boundProgram = 0; mGL->GetUIntegerv(LOCAL_GL_CURRENT_PROGRAM, &boundProgram); GLuint boundBuffer = 0; mGL->GetUIntegerv(LOCAL_GL_ARRAY_BUFFER_BINDING, &boundBuffer); /* * mGL->fGetVertexAttribiv takes: * VERTEX_ATTRIB_ARRAY_ENABLED * VERTEX_ATTRIB_ARRAY_SIZE, * VERTEX_ATTRIB_ARRAY_STRIDE, * VERTEX_ATTRIB_ARRAY_TYPE, * VERTEX_ATTRIB_ARRAY_NORMALIZED, * VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, * CURRENT_VERTEX_ATTRIB * * CURRENT_VERTEX_ATTRIB is vertex shader state. \o/ * Others appear to be vertex array state, * or alternatively in the internal vertex array state * for a buffer object. */ GLint attrib0_enabled = 0; GLint attrib0_size = 0; GLint attrib0_stride = 0; GLint attrib0_type = 0; GLint attrib0_normalized = 0; GLint attrib0_bufferBinding = 0; void* attrib0_pointer = nullptr; mGL->fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_ENABLED, &attrib0_enabled); mGL->fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_SIZE, &attrib0_size); mGL->fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_STRIDE, &attrib0_stride); mGL->fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_TYPE, &attrib0_type); mGL->fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &attrib0_normalized); mGL->fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &attrib0_bufferBinding); mGL->fGetVertexAttribPointerv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_POINTER, &attrib0_pointer); // Note that uniform values are program state, so we don't need to rebind those. ScopedGLState blend (mGL, LOCAL_GL_BLEND, false); ScopedGLState cullFace (mGL, LOCAL_GL_CULL_FACE, false); ScopedGLState depthTest (mGL, LOCAL_GL_DEPTH_TEST, false); ScopedGLState dither (mGL, LOCAL_GL_DITHER, false); ScopedGLState polyOffsFill(mGL, LOCAL_GL_POLYGON_OFFSET_FILL, false); ScopedGLState sampleAToC (mGL, LOCAL_GL_SAMPLE_ALPHA_TO_COVERAGE, false); ScopedGLState sampleCover (mGL, LOCAL_GL_SAMPLE_COVERAGE, false); ScopedGLState scissor (mGL, LOCAL_GL_SCISSOR_TEST, false); ScopedGLState stencil (mGL, LOCAL_GL_STENCIL_TEST, false); realGLboolean colorMask[4]; mGL->fGetBooleanv(LOCAL_GL_COLOR_WRITEMASK, colorMask); mGL->fColorMask(LOCAL_GL_TRUE, LOCAL_GL_TRUE, LOCAL_GL_TRUE, LOCAL_GL_TRUE); GLint viewport[4]; mGL->fGetIntegerv(LOCAL_GL_VIEWPORT, viewport); mGL->fViewport(0, 0, destSize.width, destSize.height); // Does destructive things to (only!) what we just saved above. bool good = UseTexQuadProgram(srcTarget, srcSize); if (!good) { // We're up against the wall, so bail. // This should really be MOZ_CRASH(why) or MOZ_RUNTIME_ASSERT(good). printf_stderr("[%s:%d] Fatal Error: Failed to prepare to blit texture->framebuffer.\n", __FILE__, __LINE__); MOZ_CRASH(); } mGL->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4); mGL->fViewport(viewport[0], viewport[1], viewport[2], viewport[3]); mGL->fColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]); if (attrib0_enabled) mGL->fEnableVertexAttribArray(0); mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, attrib0_bufferBinding); mGL->fVertexAttribPointer(0, attrib0_size, attrib0_type, attrib0_normalized, attrib0_stride, attrib0_pointer); mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, boundBuffer); mGL->fUseProgram(boundProgram); }