1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10 #include <sal/log.hxx>
11
12 #include <opengl/framebuffer.hxx>
13
14 #include <vcl/opengl/OpenGLHelper.hxx>
15
OpenGLFramebuffer()16 OpenGLFramebuffer::OpenGLFramebuffer() :
17 mnId( 0 ),
18 mnWidth( 0 ),
19 mnHeight( 0 ),
20 mnAttachedTexture( 0 ),
21 mpPrevFramebuffer( nullptr )
22 {
23 glGenFramebuffers( 1, &mnId );
24 CHECK_GL_ERROR();
25 VCL_GL_INFO( "Created framebuffer " << static_cast<int>(mnId) );
26 }
27
~OpenGLFramebuffer()28 OpenGLFramebuffer::~OpenGLFramebuffer()
29 {
30 glDeleteFramebuffers( 1, &mnId );
31 VCL_GL_INFO( "Deleted framebuffer " << static_cast<int>(mnId) );
32 CHECK_GL_ERROR();
33 }
34
Bind(GLenum eTarget)35 void OpenGLFramebuffer::Bind(GLenum eTarget)
36 {
37 VCL_GL_INFO( "Binding framebuffer " << static_cast<int>(mnId) );
38 glBindFramebuffer(eTarget, mnId);
39 CHECK_GL_ERROR();
40 }
41
Unbind(GLenum eTarget)42 void OpenGLFramebuffer::Unbind(GLenum eTarget)
43 {
44 glBindFramebuffer(eTarget, 0);
45 CHECK_GL_ERROR();
46 VCL_GL_INFO( "Binding default framebuffer" );
47 }
48
IsFree() const49 bool OpenGLFramebuffer::IsFree() const
50 {
51 return !mnAttachedTexture;
52 }
53
IsAttached(GLuint nTexture) const54 bool OpenGLFramebuffer::IsAttached( GLuint nTexture ) const
55 {
56 return mnAttachedTexture == nTexture;
57 }
58
IsAttached(const OpenGLTexture & rTexture) const59 bool OpenGLFramebuffer::IsAttached( const OpenGLTexture& rTexture ) const
60 {
61 return mnAttachedTexture == rTexture.Id();
62 }
63
AttachTexture(const OpenGLTexture & rTexture)64 void OpenGLFramebuffer::AttachTexture( const OpenGLTexture& rTexture )
65 {
66 if( rTexture.Id() == mnAttachedTexture )
67 return;
68
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();
75
76 GLuint nStencil = rTexture.StencilId();
77 if( nStencil )
78 {
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();
83 }
84
85 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
86 CHECK_GL_ERROR();
87 if (status != GL_FRAMEBUFFER_COMPLETE)
88 {
89 SAL_WARN("vcl.opengl", "Framebuffer incomplete");
90 }
91 }
92
DetachTexture()93 void OpenGLFramebuffer::DetachTexture()
94 {
95 if( mnAttachedTexture != 0 )
96 {
97 mnAttachedTexture = 0;
98 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0 );
99 CHECK_GL_ERROR();
100
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();
105 }
106 }
107
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
109