GrGLSharedSurface SkNativeSharedGLContext::stealSurface() { // Render the texture to the default framebuffer. int viewport[4]; SK_GL(*this, GetIntegerv(GR_GL_VIEWPORT, viewport)); int width = viewport[2], height = viewport[3]; SK_GL(*this, BindFramebuffer(GR_GL_READ_FRAMEBUFFER, fFBO)); SK_GL(*this, BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, 0)); SK_GL(*this, BlitFramebuffer(0, 0, width, height, 0, 0, width, height, GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); SK_GL(*this, Flush()); SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); Pixmap pixmap = fPixmap; destroyGLResources(); fPixmap = 0; return pixmap; }
static void setup_framebuffer(const GrGLInterface* gl, int screenWidth, int screenHeight) { //Setup framebuffer GrGLuint texture; GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); GR_GL_CALL(gl, PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); GR_GL_CALL(gl, GenTextures(1, &texture)); GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE15)); GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texture)); GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST)); GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE)); GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE)); GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, //level GR_GL_RGBA8, //internal format screenWidth, // width screenHeight, // height 0, //border GR_GL_RGBA, //format GR_GL_UNSIGNED_BYTE, // type NULL)); // bind framebuffer GrGLuint framebuffer; GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); GR_GL_CALL(gl, GenFramebuffers(1, &framebuffer)); GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, framebuffer)); GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D, texture, 0)); GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); GR_GL_CALL(gl, Viewport(0, 0, screenWidth, screenHeight)); }
void GLVec4ScalarBench::teardown(const GrGLInterface* gl) { GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0)); GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); GR_GL_CALL(gl, DeleteTextures(1, &fFboTextureId)); GR_GL_CALL(gl, DeleteProgram(fProgram)); GR_GL_CALL(gl, DeleteBuffers(1, &fVboId)); }
void GLCpuPosInstancedArraysBench::teardown(const GrGLInterface* gl) { GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0)); GR_GL_CALL(gl, BindVertexArray(0)); GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); GR_GL_CALL(gl, DeleteTextures(1, &fTexture)); GR_GL_CALL(gl, DeleteProgram(fProgram)); GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin())); GR_GL_CALL(gl, DeleteVertexArrays(1, &fVAO)); fBuffers.reset(); }
bool GrGLRenderTarget::completeStencilAttachment() { GrGLGpu* gpu = this->getGLGpu(); const GrGLInterface* interface = gpu->glInterface(); GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); if (nullptr == stencil) { GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, 0)); GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, GR_GL_RENDERBUFFER, 0)); #ifdef SK_DEBUG if (kChromium_GrGLDriver != gpu->glContext().driver()) { // This check can cause problems in Chromium if the context has been asynchronously // abandoned (see skbug.com/5200) GrGLenum status; GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status); } #endif return true; } else { const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil); GrGLuint rb = glStencil->renderbufferID(); gpu->invalidateBoundRenderTarget(); gpu->stats()->incRenderTargetBinds(); GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID())); GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, rb)); if (glStencil->format().fPacked) { GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, GR_GL_RENDERBUFFER, rb)); } else { GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, GR_GL_RENDERBUFFER, 0)); } #ifdef SK_DEBUG if (kChromium_GrGLDriver != gpu->glContext().driver()) { // This check can cause problems in Chromium if the context has been asynchronously // abandoned (see skbug.com/5200) GrGLenum status; GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status); } #endif return true; } }
GrGLuint SkNativeSharedGLContext::stealTextureID() { // Unbind the texture from the framebuffer. if (fGL && fFBO) { SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO)); SK_GL(*this, FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D, 0, 0)); } GrGLuint textureID = fTextureID; fTextureID = 0; return textureID; }
void GLInstancedArraysBench::onPerCanvasPostDraw(SkCanvas* canvas) { // This bench exclusively tests GL calls directly const GrGLContext* ctx = get_gl_context(canvas); if (!ctx) { return; } const GrGLInterface* gl = ctx->interface(); // teardown GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0)); GR_GL_CALL(gl, BindVertexArray(0)); GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); GR_GL_CALL(gl, DeleteTextures(1, &fTexture)); this->teardown(gl); }
bool SkGLContextHelper::init(int width, int height) { if (fGL) { fGL->unref(); this->destroyGLContext(); } fGL = this->createGLContext(); if (fGL) { const GrGLubyte* temp; GrGLBinding bindingInUse = GrGLGetBindingInUse(this->gl()); if (!fGL->validate(bindingInUse) || !fExtensions.init(bindingInUse, fGL)) { fGL = NULL; this->destroyGLContext(); return false; } SK_GL_RET(*this, temp, GetString(GR_GL_VERSION)); const char* versionStr = reinterpret_cast<const char*>(temp); GrGLVersion version = GrGLGetVersionFromString(versionStr); // clear any existing GL erorrs GrGLenum error; do { SK_GL_RET(*this, error, GetError()); } while (GR_GL_NO_ERROR != error); SK_GL(*this, GenFramebuffers(1, &fFBO)); SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO)); SK_GL(*this, GenRenderbuffers(1, &fColorBufferID)); SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fColorBufferID)); if (kES_GrGLBinding == bindingInUse) { SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA8, width, height)); } else { SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA, width, height)); } SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER, fColorBufferID)); SK_GL(*this, GenRenderbuffers(1, &fDepthStencilBufferID)); SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fDepthStencilBufferID)); // Some drivers that support packed depth stencil will only succeed // in binding a packed format an FBO. However, we can't rely on packed // depth stencil being available. bool supportsPackedDepthStencil; if (kES_GrGLBinding == bindingInUse) { supportsPackedDepthStencil = version >= GR_GL_VER(3,0) || this->hasExtension("GL_OES_packed_depth_stencil"); } else { supportsPackedDepthStencil = version >= GR_GL_VER(3,0) || this->hasExtension("GL_EXT_packed_depth_stencil") || this->hasExtension("GL_ARB_framebuffer_object"); } if (supportsPackedDepthStencil) { // ES2 requires sized internal formats for RenderbufferStorage // On Desktop we let the driver decide. GrGLenum format = kES_GrGLBinding == bindingInUse ? GR_GL_DEPTH24_STENCIL8 : GR_GL_DEPTH_STENCIL; SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, format, width, height)); SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, GR_GL_RENDERBUFFER, fDepthStencilBufferID)); } else { GrGLenum format = kES_GrGLBinding == bindingInUse ? GR_GL_STENCIL_INDEX8 : GR_GL_STENCIL_INDEX; SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, format, width, height)); } SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, fDepthStencilBufferID)); SK_GL(*this, Viewport(0, 0, width, height)); SK_GL(*this, ClearStencil(0)); SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT)); SK_GL_RET(*this, error, GetError()); GrGLenum status; SK_GL_RET(*this, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); if (GR_GL_FRAMEBUFFER_COMPLETE != status || GR_GL_NO_ERROR != error) { fFBO = 0; fColorBufferID = 0; fDepthStencilBufferID = 0; fGL->unref(); fGL = NULL; this->destroyGLContext(); return false; } else { return true; } } return false; }
void GL::SetFramebuffer(GLenum aFramebufferTarget, GLuint aFramebuffer) { MOZ_ASSERT(IsCurrent()); bool clearTextureFramebuffer1D; bool clearTextureFramebuffer2D; switch (aFramebufferTarget) { case GL_FRAMEBUFFER: if (mReadFramebuffer == aFramebuffer && mDrawFramebuffer == aFramebuffer) { return; } BindFramebuffer(GL_FRAMEBUFFER, aFramebuffer); clearTextureFramebuffer1D = (aFramebuffer != mTextureFramebuffer1D) && (mDrawFramebuffer == mTextureFramebuffer1D || mReadFramebuffer == mTextureFramebuffer1D); clearTextureFramebuffer2D = (aFramebuffer != mTextureFramebuffer2D) && (mDrawFramebuffer == mTextureFramebuffer2D || mReadFramebuffer == mTextureFramebuffer2D); mReadFramebuffer = mDrawFramebuffer = aFramebuffer; break; case GL_READ_FRAMEBUFFER: if (mReadFramebuffer == aFramebuffer) { return; } BindFramebuffer(GL_READ_FRAMEBUFFER, aFramebuffer); clearTextureFramebuffer1D = (mReadFramebuffer == mTextureFramebuffer1D); clearTextureFramebuffer2D = (mReadFramebuffer == mTextureFramebuffer2D); mReadFramebuffer = aFramebuffer; break; case GL_DRAW_FRAMEBUFFER: if (mDrawFramebuffer == aFramebuffer) { return; } BindFramebuffer(GL_DRAW_FRAMEBUFFER, aFramebuffer); clearTextureFramebuffer1D = (mDrawFramebuffer == mTextureFramebuffer1D); clearTextureFramebuffer2D = (mDrawFramebuffer == mTextureFramebuffer2D); mDrawFramebuffer = aFramebuffer; break; default: MOZ_ASSERT(!"Invalid framebuffer target."); break; } if (clearTextureFramebuffer1D) { NamedFramebufferTexture1DEXT(mTextureFramebuffer1D, GL_COLOR_ATTACHMENT0, GL_TEXTURE_1D, 0, 0); } if (clearTextureFramebuffer2D) { NamedFramebufferTexture2DEXT(mTextureFramebuffer2D, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); } }
bool SkGLContext::init(int width, int height) { if (fGL) { fGL->unref(); this->destroyGLContext(); } fGL = this->createGLContext(); if (fGL) { // clear any existing GL erorrs GrGLenum error; do { error = SK_GL(*this, GetError()); } while (GR_GL_NO_ERROR != error); GrGLuint cbID; GrGLuint dsID; SK_GL(*this, GenFramebuffers(1, &fFBO)); SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO)); SK_GL(*this, GenRenderbuffers(1, &cbID)); SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, cbID)); if (fGL->supportsES2()) { SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA8, width, height)); } else { SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA, width, height)); } SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER, cbID)); SK_GL(*this, GenRenderbuffers(1, &dsID)); SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, dsID)); if (fGL->supportsES2()) { SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_STENCIL_INDEX8, width, height)); } else { SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_DEPTH_STENCIL, width, height)); SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, GR_GL_RENDERBUFFER, dsID)); } SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, dsID)); SK_GL(*this, Viewport(0, 0, width, height)); SK_GL(*this, ClearStencil(0)); SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT)); error = SK_GL(*this, GetError()); GrGLenum status = SK_GL(*this, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); if (GR_GL_FRAMEBUFFER_COMPLETE != status || GR_GL_NO_ERROR != error) { fFBO = 0; fGL->unref(); fGL = NULL; this->destroyGLContext(); return false; } else { return true; } } return false; }