~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to examples/buffer_render_target.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
Import upstream version 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License version 3 as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
17
 
 */
18
 
 
19
 
#include "buffer_render_target.h"
20
 
#include "mir/graphics/buffer.h"
21
 
 
22
 
#include <GLES2/gl2ext.h>
23
 
#include <stdexcept>
24
 
 
25
 
namespace geom = mir::geometry;
26
 
namespace mg = mir::graphics;
27
 
namespace mt = mir::tools;
28
 
 
29
 
mt::BufferRenderTarget::BufferRenderTarget(mg::Buffer& buffer)
30
 
    : buffer(buffer), old_fbo(), old_viewport()
31
 
{
32
 
    /*
33
 
     * With the new lazy buffer allocation method, we may be executing inside
34
 
     * the compositor's GL context. So be careful to save and restore what
35
 
     * we change...
36
 
     */
37
 
    glGetIntegerv(GL_VIEWPORT, old_viewport);
38
 
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
39
 
    resources.setup(buffer);
40
 
}
41
 
 
42
 
mt::BufferRenderTarget::~BufferRenderTarget()
43
 
{
44
 
    glFinish();
45
 
    glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
46
 
    glViewport(old_viewport[0], old_viewport[1],
47
 
               old_viewport[2], old_viewport[3]);
48
 
}
49
 
 
50
 
void mt::BufferRenderTarget::make_current()
51
 
{
52
 
    geom::Size buf_size = buffer.size();
53
 
 
54
 
    glBindFramebuffer(GL_FRAMEBUFFER, resources.fbo);
55
 
    glViewport(0, 0, buf_size.width.as_uint32_t(), buf_size.height.as_uint32_t());
56
 
}
57
 
 
58
 
mt::BufferRenderTarget::Resources::~Resources()
59
 
{
60
 
    if (color_tex != 0)
61
 
        glDeleteTextures(1, &color_tex);
62
 
    if (depth_rbo != 0)
63
 
        glDeleteRenderbuffers(1, &depth_rbo);
64
 
    if (fbo != 0)
65
 
        glDeleteFramebuffers(1, &fbo);
66
 
}
67
 
 
68
 
void mt::BufferRenderTarget::Resources::setup(mg::Buffer& buffer)
69
 
{
70
 
    geom::Size buf_size = buffer.size();
71
 
 
72
 
    if (fbo == 0)
73
 
    {
74
 
        glGenFramebuffers(1, &fbo);
75
 
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);
76
 
 
77
 
        /* Set up color buffer... */
78
 
        glGenTextures(1, &color_tex);
79
 
        glBindTexture(GL_TEXTURE_2D, color_tex);
80
 
        buffer.gl_bind_to_texture();
81
 
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
82
 
                               GL_TEXTURE_2D, color_tex, 0);
83
 
 
84
 
        /* and depth buffer */
85
 
        glGenRenderbuffers(1, &depth_rbo);
86
 
        glBindRenderbuffer(GL_RENDERBUFFER, depth_rbo);
87
 
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16,
88
 
                              buf_size.width.as_uint32_t(), buf_size.height.as_uint32_t());
89
 
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
90
 
                                  GL_RENDERBUFFER, depth_rbo);
91
 
 
92
 
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
93
 
            throw std::runtime_error("Failed to create FBO for GBM buffer");
94
 
    }
95
 
}