Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / opengl / framebuffer.cxx
blobdb957d1a6cd0392a8a392be75b6d7670ee9adfaf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/log.hxx>
12 #include <opengl/framebuffer.hxx>
14 #include <vcl/opengl/OpenGLHelper.hxx>
16 OpenGLFramebuffer::OpenGLFramebuffer() :
17 mnId( 0 ),
18 mnWidth( 0 ),
19 mnHeight( 0 ),
20 mnAttachedTexture( 0 ),
21 mpPrevFramebuffer( nullptr )
23 glGenFramebuffers( 1, &mnId );
24 CHECK_GL_ERROR();
25 VCL_GL_INFO( "Created framebuffer " << static_cast<int>(mnId) );
28 OpenGLFramebuffer::~OpenGLFramebuffer()
30 glDeleteFramebuffers( 1, &mnId );
31 VCL_GL_INFO( "Deleted framebuffer " << static_cast<int>(mnId) );
32 CHECK_GL_ERROR();
35 void OpenGLFramebuffer::Bind(GLenum eTarget)
37 VCL_GL_INFO( "Binding framebuffer " << static_cast<int>(mnId) );
38 glBindFramebuffer(eTarget, mnId);
39 CHECK_GL_ERROR();
42 void OpenGLFramebuffer::Unbind(GLenum eTarget)
44 glBindFramebuffer(eTarget, 0);
45 CHECK_GL_ERROR();
46 VCL_GL_INFO( "Binding default framebuffer" );
49 bool OpenGLFramebuffer::IsFree() const
51 return !mnAttachedTexture;
54 bool OpenGLFramebuffer::IsAttached( GLuint nTexture ) const
56 return mnAttachedTexture == nTexture;
59 bool OpenGLFramebuffer::IsAttached( const OpenGLTexture& rTexture ) const
61 return mnAttachedTexture == rTexture.Id();
64 void OpenGLFramebuffer::AttachTexture( const OpenGLTexture& rTexture )
66 if( rTexture.Id() == mnAttachedTexture )
67 return;
69 VCL_GL_INFO( "Attaching texture " << rTexture.Id() << " to framebuffer " << static_cast<int>(mnId) );
70 mnAttachedTexture = rTexture.Id();
71 mnWidth = rTexture.GetWidth();
72 mnHeight = rTexture.GetHeight();
73 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mnAttachedTexture, 0);
74 CHECK_GL_ERROR();
76 GLuint nStencil = rTexture.StencilId();
77 if( nStencil )
79 VCL_GL_INFO( "Attaching stencil " << nStencil << " to framebuffer " << static_cast<int>(mnId) );
80 glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
81 GL_RENDERBUFFER, nStencil );
82 CHECK_GL_ERROR();
85 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
86 CHECK_GL_ERROR();
87 if (status != GL_FRAMEBUFFER_COMPLETE)
89 SAL_WARN("vcl.opengl", "Framebuffer incomplete");
93 void OpenGLFramebuffer::DetachTexture()
95 if( mnAttachedTexture != 0 )
97 mnAttachedTexture = 0;
98 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0 );
99 CHECK_GL_ERROR();
101 // FIXME: we could make this conditional on having a stencil ?
102 glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
103 GL_RENDERBUFFER, 0 );
104 CHECK_GL_ERROR();
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */