aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/OpenGLTexPartitioner.hh
blob: d37f744481ebcb4350b24516daea56d76e1919ee (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
/** OpenGLTexPartitioner.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_OPENGLTEXPARTITIONER_HH
#define FBCOMPOSITOR_OPENGLTEXPARTITIONER_HH


#include "Exceptions.hh"
#include "OpenGLResources.hh"

#include "FbTk/RefCount.hh"

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

#include <X11/Xlib.h>

#include <algorithm>
#include <vector>


namespace FbCompositor {

    class OpenGLScreen;


    //--- CONSTANTS ------------------------------------------------------------
    
    /** North border flag. */
    const int BORDER_NORTH = 1 << 0;

    /** East border flag. */
    const int BORDER_EAST = 1 << 1;

    /** South border flag. */
    const int BORDER_SOUTH = 1 << 2;

    /** West border flag. */
    const int BORDER_WEST = 1 << 3;

    /** A bitfield with all border flags set. */
    const int BORDER_ALL = BORDER_NORTH | BORDER_EAST | BORDER_SOUTH | BORDER_WEST;


    //--- SUPPORTING STRUCTURES ------------------------------------------------

    /**
     * A single 2D texture partition.
     */
    struct TexturePart {
        OpenGL2DTexturePtr texture;     ///< Partition's contents.
        unsigned int borders;           ///< A bitfield showing borders, adjacent to this partition.
    };


    //--- TEXTURE PARTITIONER --------------------------------------------------

    /**
     * A wrapper that automatically splits large textures into manageable (i.e.
     * with supported size) parts.
     */
    class OpenGL2DTexturePartition {
    public:
        //--- CONSTRUCTORS AND DESTRUCTORS -------------------------------------

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

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


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

        /** \returns the partitions of the current texture. */
        const std::vector<TexturePart> &partitions() const;


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

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


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

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


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

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

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


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

        /** Maximum supported texture size. */
        int m_max_texture_size;

        /** Whether alpha channel should be swizzled to one. */
        bool m_swizzle_alpha_to_one;

        
        /** Partitions of the texture. */
        std::vector<TexturePart> m_partitions;

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


        /** Full texture height. */
        int m_full_height;

        /** Full texture width. */
        int m_full_width;


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

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

    // Returns the full height of the current texture.
    inline int OpenGL2DTexturePartition::fullHeight() const {
        return m_full_height;
    }

    // Returns the full width of the current texture.
    inline int OpenGL2DTexturePartition::fullWidth() const {
        return m_full_width;
    }

    // Returns the partitions of the current texture.
    inline const std::vector<TexturePart> &OpenGL2DTexturePartition::partitions() const {
        return m_partitions;
    }


    //--- SUPPORTING FUNCTIONS -------------------------------------------------

    /** Returns the border bitfield of the given partition. */
    unsigned int getBorderBitfield(int unit_width, int unit_height, int x, int y);

    /** Space partitioning function. */
    std::vector<XRectangle> partitionSpace(int x, int y, int width, int height, int max_partition_size,
                                           int *unit_width_return = 0, int *unit_height_return = 0);

    /** Partitions space directly to buffers. */
    std::vector<OpenGLBufferPtr> partitionSpaceToBuffers(const OpenGLScreen &screen, int x, int y,
                                                         int width, int height);


    //--- TYPEDEFS -------------------------------------------------------------

    /** OpenGL texture partition smart pointer. */
    typedef FbTk::RefCount<OpenGL2DTexturePartition> OpenGL2DTexturePartitionPtr;
}

#endif  // FBCOMPOSITOR_OPENGLTEXPARTITIONER_HH