aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/OpenGLResources.hh
blob: 6c27f99d9e23e3e8dbc5707facf05374ef842a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/** OpenGLResources.hh file for the fluxbox compositor. */

// Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#ifndef FBCOMPOSITOR_OPENGLRESOURCES_HH
#define FBCOMPOSITOR_OPENGLRESOURCES_HH


#include "FbTk/RefCount.hh"

#include <GL/glxew.h>
#include <GL/glx.h>

#include <X11/Xlib.h>


namespace FbCompositor {

    class OpenGLScreen;
    

    //--- OPENGL BUFFER WRAPPER ------------------------------------------------

    /**
     * A wrapper for OpenGL buffers.
     */
    class OpenGLBuffer {
    public:
        //--- CONSTRUCTORS AND DESTRUCTORS -------------------------------------

        /** Constructor. */
        OpenGLBuffer(const OpenGLScreen &screen, GLenum target_buffer);

        /** Destructor. */
        ~OpenGLBuffer();


        //--- ACCESSORS --------------------------------------------------------

        /** \returns the handle to the buffer held. */
        GLuint handle() const;

        /** \returns the target of the buffer. */
        GLenum target() const;


        //--- MUTATORS ---------------------------------------------------------

        /** Bind the buffer to its target. */
        void bind();

        /** Loads the given data into the buffer. */
        void bufferData(int element_size, const GLvoid *data, GLenum usage_hint);

        /** Sets the buffer's contents to be the rectangle's coordinates on the screen. */
        void bufferPosRectangle(int screen_width, int screen_height, XRectangle rect);


    private:
        //--- CONSTRUCTORS -----------------------------------------------------

        /** Copy constructor. */
        OpenGLBuffer(const OpenGLBuffer &other);

        /** Assignment operator. */
        OpenGLBuffer &operator=(const OpenGLBuffer &other);


        //--- INTERNALS --------------------------------------------------------

        /** The buffer in question. */
        GLuint m_buffer;

        /** The target buffer. */
        GLenum m_target;


        /** Screen that manages this buffer. */
        const OpenGLScreen &m_screen;
    };


    // Bind the buffer to its target.
    inline void OpenGLBuffer::bind() {
        glBindBuffer(m_target, m_buffer);
    }

    // Loads the given data into the buffer.
    inline void OpenGLBuffer::bufferData(int element_size, const GLvoid *data, GLenum usage_hint) {
        bind();
        glBufferData(m_target, element_size, data, usage_hint);
    }

    // Returns the handle to the buffer held.
    inline GLuint OpenGLBuffer::handle() const {
        return m_buffer;
    }

    // Returns the target of the buffer.
    inline GLenum OpenGLBuffer::target() const {
        return m_target;
    }


    /** OpenGL buffer wrapper smart pointer. */
    typedef FbTk::RefCount<OpenGLBuffer> OpenGLBufferPtr;


    //--- OPENGL TEXTURE WRAPPER -----------------------------------------------

    /**
     * OpenGL texture wrapper.
     */
    class OpenGL2DTexture {
    public:
        //--- CONSTRUCTORS AND DESTRUCTORS -------------------------------------

        /** Constructor. */
        OpenGL2DTexture(const OpenGLScreen &screen, bool swizzle_alpha_to_one);

        /** Destructor. */
        ~OpenGL2DTexture();


        //--- ACCESSORS --------------------------------------------------------

        /** \returns the handle to the texture held. */
        GLuint handle() const;


        /** \returns the height of the texture. */
        int height() const;

        /** \returns the width of the texture. */
        int width() const;


        //--- MUTATORS ---------------------------------------------------------

        /** Bind the texture to GL_TEXTURE_2D. */
        void bind();

        /** Sets the texture's contents to the given pixmap. */
        void setPixmap(Pixmap pixmap, bool manage_pixmap, int width, int height, bool force_direct = false);


    private:
        //--- CONSTRUCTORS -----------------------------------------------------

        /** Constructor. */
        OpenGL2DTexture(const OpenGL2DTexture &other);

        /** Assignment operator. */
        OpenGL2DTexture &operator=(const OpenGL2DTexture &other);


        //--- INTERNALS --------------------------------------------------------

        /** Whether this object manages the pixmap. */
        bool m_pixmap_managed;

        /** Pixmap of the texture's contents. */
        Pixmap m_pixmap;

        /** GLX pixmap of the texture's contents. */
        GLXPixmap m_glx_pixmap;

        /** The texture in question. */
        GLuint m_texture;


        /** Height of the texture. */
        int m_height;

        /** Width of the texture. */
        int m_width;


        /** Current connection to the X server. */
        Display *m_display;

        /** Screen that manages this texture. */
        const OpenGLScreen &m_screen;
    };


    // Bind the texture to its target.
    inline void OpenGL2DTexture::bind() {
        glBindTexture(GL_TEXTURE_2D, m_texture);
    }

    // Returns the handle to the texture held.
    inline GLuint OpenGL2DTexture::handle() const {
        return m_texture;
    }

    // Returns the height of the texture.
    inline int OpenGL2DTexture::height() const {
        return m_height;
    }

    // Returns the width of the texture.
    inline int OpenGL2DTexture::width() const {
        return m_width;
    }


    /** OpenGL texture wrapper smart pointer. */
    typedef FbTk::RefCount<OpenGL2DTexture> OpenGL2DTexturePtr;
}

#endif  // FBCOMPOSITOR_OPENGLRESOURCES_HH