diff options
Diffstat (limited to 'util/fbcompose/OpenGLResources.cc')
-rw-r--r-- | util/fbcompose/OpenGLResources.cc | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/util/fbcompose/OpenGLResources.cc b/util/fbcompose/OpenGLResources.cc new file mode 100644 index 0000000..9558e45 --- /dev/null +++ b/util/fbcompose/OpenGLResources.cc | |||
@@ -0,0 +1,177 @@ | |||
1 | /** OpenGLResources.cc file for the fluxbox compositor. */ | ||
2 | |||
3 | // Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com) | ||
4 | // | ||
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
6 | // of this software and associated documentation files (the "Software"), to deal | ||
7 | // in the Software without restriction, including without limitation the rights | ||
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
9 | // copies of the Software, and to permit persons to whom the Software is | ||
10 | // furnished to do so, subject to the following conditions: | ||
11 | // | ||
12 | // The above copyright notice and this permission notice shall be included in | ||
13 | // all copies or substantial portions of the Software. | ||
14 | // | ||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
21 | // THE SOFTWARE. | ||
22 | |||
23 | |||
24 | #include "OpenGLResources.hh" | ||
25 | |||
26 | #include "Logging.hh" | ||
27 | #include "OpenGLScreen.hh" | ||
28 | #include "OpenGLUtility.hh" | ||
29 | #include "Utility.hh" | ||
30 | |||
31 | using namespace FbCompositor; | ||
32 | |||
33 | |||
34 | //--- CONSTANTS ---------------------------------------------------------------- | ||
35 | |||
36 | // Attributes of the textures' GLX pixmaps. | ||
37 | const int TEX_PIXMAP_ATTRIBUTES[] = { | ||
38 | #ifdef GLXEW_EXT_texture_from_pixmap | ||
39 | GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, | ||
40 | GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT, | ||
41 | None | ||
42 | #else | ||
43 | None | ||
44 | #endif // GLXEW_EXT_texture_from_pixmap | ||
45 | }; | ||
46 | |||
47 | |||
48 | //--- OPENGL BUFFER WRAPPER ---------------------------------------------------- | ||
49 | |||
50 | //------- CONSTRUCTORS AND DESTRUCTORS ----------------------------------------- | ||
51 | |||
52 | // Constructor. | ||
53 | OpenGLBuffer::OpenGLBuffer(const OpenGLScreen &screen, GLenum target_buffer) : | ||
54 | m_screen(screen) { | ||
55 | |||
56 | m_target = target_buffer; | ||
57 | |||
58 | glGenBuffers(1, &m_buffer); | ||
59 | } | ||
60 | |||
61 | // Destructor. | ||
62 | OpenGLBuffer::~OpenGLBuffer() { | ||
63 | glDeleteBuffers(1, &m_buffer); | ||
64 | } | ||
65 | |||
66 | |||
67 | //------- MUTATORS ------------------------------------------------------------- | ||
68 | |||
69 | // Sets the buffer's contents to be the rectangle's coordinates on the screen. | ||
70 | void OpenGLBuffer::bufferPosRectangle(int screen_width, int screen_height, XRectangle rect) { | ||
71 | static GLfloat x_low, x_high, y_low, y_high; | ||
72 | static GLfloat temp_pos_array[8]; | ||
73 | |||
74 | toOpenGLCoords(screen_width, screen_height, rect, &x_low, &x_high, &y_low, &y_high); | ||
75 | |||
76 | temp_pos_array[0] = temp_pos_array[4] = x_low; | ||
77 | temp_pos_array[2] = temp_pos_array[6] = x_high; | ||
78 | temp_pos_array[1] = temp_pos_array[3] = y_low; | ||
79 | temp_pos_array[5] = temp_pos_array[7] = y_high; | ||
80 | |||
81 | bufferData(sizeof(temp_pos_array), (const GLvoid*)(temp_pos_array), GL_STATIC_DRAW); | ||
82 | } | ||
83 | |||
84 | |||
85 | //--- OPENGL TEXTURE WRAPPER --------------------------------------------------- | ||
86 | |||
87 | //------- CONSTRUCTORS AND DESTRUCTORS ----------------------------------------- | ||
88 | |||
89 | // Constructor. | ||
90 | OpenGL2DTexture::OpenGL2DTexture(const OpenGLScreen &screen, bool swizzle_alpha_to_one) : | ||
91 | m_screen(screen) { | ||
92 | |||
93 | m_display = (Display*)(screen.display()); | ||
94 | m_glx_pixmap = 0; | ||
95 | m_pixmap = None; | ||
96 | m_pixmap_managed = false; | ||
97 | |||
98 | glGenTextures(1, &m_texture); | ||
99 | bind(); | ||
100 | |||
101 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | ||
102 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||
103 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
104 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
105 | |||
106 | if (swizzle_alpha_to_one) { | ||
107 | #ifdef GL_ARB_texture_swizzle | ||
108 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE); | ||
109 | #else | ||
110 | #ifdef GL_EXT_texture_swizzle | ||
111 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A_EXT, GL_ONE); | ||
112 | #endif // GL_EXT_texture_swizzle | ||
113 | #endif // GL_ARB_texture_swizzle | ||
114 | } | ||
115 | } | ||
116 | |||
117 | // Destructor. | ||
118 | OpenGL2DTexture::~OpenGL2DTexture() { | ||
119 | glDeleteTextures(1, &m_texture); | ||
120 | |||
121 | if (m_glx_pixmap) { | ||
122 | glXDestroyPixmap(m_display, m_glx_pixmap); | ||
123 | } | ||
124 | if (m_pixmap_managed && m_pixmap) { | ||
125 | XFreePixmap(m_display, m_pixmap); | ||
126 | } | ||
127 | } | ||
128 | |||
129 | |||
130 | //------- MUTATORS ------------------------------------------------------------- | ||
131 | |||
132 | // Sets the texture's contents to the given pixmap. | ||
133 | void OpenGL2DTexture::setPixmap(Pixmap pixmap, bool manage_pixmap, int width, int height, bool force_direct) { | ||
134 | bind(); | ||
135 | |||
136 | if (m_pixmap != pixmap) { | ||
137 | #ifdef GLXEW_EXT_texture_from_pixmap | ||
138 | if (m_glx_pixmap) { | ||
139 | glXReleaseTexImageEXT(m_display, m_glx_pixmap, GLX_BACK_LEFT_EXT); | ||
140 | glXDestroyPixmap(m_display, m_glx_pixmap); | ||
141 | m_glx_pixmap = 0; | ||
142 | } | ||
143 | #endif // GLXEW_EXT_texture_from_pixmap | ||
144 | |||
145 | if (m_pixmap_managed && m_pixmap) { | ||
146 | XFreePixmap(m_display, m_pixmap); | ||
147 | m_pixmap = None; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | m_height = height; | ||
152 | m_pixmap_managed = manage_pixmap; | ||
153 | m_pixmap = pixmap; | ||
154 | m_width = width; | ||
155 | |||
156 | #ifdef GLXEW_EXT_texture_from_pixmap | ||
157 | if (!force_direct) { | ||
158 | if (!m_glx_pixmap) { | ||
159 | m_glx_pixmap = glXCreatePixmap(m_display, m_screen.fbConfig(), m_pixmap, TEX_PIXMAP_ATTRIBUTES); | ||
160 | glXBindTexImageEXT(m_display, m_glx_pixmap, GLX_BACK_LEFT_EXT, NULL); | ||
161 | } | ||
162 | } else | ||
163 | #else | ||
164 | MARK_PARAMETER_UNUSED(force_direct); | ||
165 | #endif // GLXEW_EXT_texture_from_pixmap | ||
166 | |||
167 | { | ||
168 | XImage *image = XGetImage(m_display, pixmap, 0, 0, width, height, AllPlanes, ZPixmap); | ||
169 | if (!image) { | ||
170 | fbLog_info << "Could not create XImage for pixmap to texture conversion." << std::endl; | ||
171 | return; | ||
172 | } | ||
173 | |||
174 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)(&(image->data[0]))); | ||
175 | XDestroyImage(image); | ||
176 | } | ||
177 | } | ||