tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / OpenGLShims.cpp
1 /*
2  *  Copyright (C) 2011 Igalia S.L.
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2.1 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with this library; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #if ENABLE(WEBGL) || defined(QT_OPENGL_SHIMS) || ENABLE(TIZEN_WEBKIT2)
21
22 #define DISABLE_SHIMS
23 #include "OpenGLShims.h"
24
25 #include <dlfcn.h>
26 #include <wtf/text/CString.h>
27 #include <wtf/text/WTFString.h>
28
29 namespace WebCore {
30
31 #if ENABLE(TIZEN_WEBKIT2)
32 OpenGLFunctionTable* openGLFunctionTable()
33 {
34     return 0;
35 }
36
37 bool initializeOpenGLShims()
38 {
39     return true;
40 }
41 #else
42 OpenGLFunctionTable* openGLFunctionTable()
43 {
44     static OpenGLFunctionTable table;
45     return &table;
46 }
47
48 #if PLATFORM(QT) && defined(QT_OPENGL_ES_2)
49 #define ASSIGN_FUNCTION_TABLE_ENTRY(FunctionName, success) \
50     openGLFunctionTable()->FunctionName = ::FunctionName
51 #else
52
53 #if PLATFORM(QT)
54 static void* getProcAddress(const char* procName)
55 {
56 #if HAVE(QT5)
57     return reinterpret_cast<void*>(QOpenGLContext::currentContext()->getProcAddress(procName));
58 #else
59     return reinterpret_cast<void*>(QGLContext::currentContext()->getProcAddress(QString::fromLatin1(procName)));
60 #endif
61 }
62 #else
63 typedef void* (*glGetProcAddressType) (const char* procName);
64 static void* getProcAddress(const char* procName)
65 {
66     static bool initialized = false;
67     static glGetProcAddressType getProcAddressFunction = 0;
68
69     if (!initialized) {
70         getProcAddressFunction = reinterpret_cast<glGetProcAddressType>(dlsym(RTLD_DEFAULT, "glXGetProcAddress"));
71         if (!getProcAddressFunction)
72             getProcAddressFunction = reinterpret_cast<glGetProcAddressType>(dlsym(RTLD_DEFAULT, "glXGetProcAddressARB"));
73     }
74
75     if (!getProcAddressFunction)
76         return dlsym(RTLD_DEFAULT, procName);
77     return getProcAddressFunction(procName);
78 }
79 #endif
80
81 static void* lookupOpenGLFunctionAddress(const char* functionName, bool& success)
82 {
83     if (!success)
84         return 0;
85
86     void* target = getProcAddress(functionName);
87     if (target)
88         return target;
89
90     String fullFunctionName(functionName);
91     fullFunctionName.append("ARB");
92     target = getProcAddress(fullFunctionName.utf8().data());
93     if (target)
94         return target;
95
96     fullFunctionName = functionName;
97     fullFunctionName.append("EXT");
98     target = getProcAddress(fullFunctionName.utf8().data());
99
100     // A null address is still a failure case.
101     if (!target)
102         success = false;
103
104     return target;
105 }
106
107 #define ASSIGN_FUNCTION_TABLE_ENTRY(FunctionName, success) \
108     openGLFunctionTable()->FunctionName = reinterpret_cast<FunctionName##Type>(lookupOpenGLFunctionAddress(#FunctionName, success))
109 #endif
110
111 bool initializeOpenGLShims()
112 {
113     static bool success = true;
114     static bool initialized = false;
115     if (initialized)
116         return success;
117
118     initialized = true;
119     ASSIGN_FUNCTION_TABLE_ENTRY(glActiveTexture, success);
120     ASSIGN_FUNCTION_TABLE_ENTRY(glAttachShader, success);
121     ASSIGN_FUNCTION_TABLE_ENTRY(glBindAttribLocation, success);
122     ASSIGN_FUNCTION_TABLE_ENTRY(glBindBuffer, success);
123     ASSIGN_FUNCTION_TABLE_ENTRY(glBindFramebuffer, success);
124     ASSIGN_FUNCTION_TABLE_ENTRY(glBindRenderbuffer, success);
125     ASSIGN_FUNCTION_TABLE_ENTRY(glBlendColor, success);
126     ASSIGN_FUNCTION_TABLE_ENTRY(glBlendEquation, success);
127     ASSIGN_FUNCTION_TABLE_ENTRY(glBlendEquationSeparate, success);
128     ASSIGN_FUNCTION_TABLE_ENTRY(glBlendFuncSeparate, success);
129 #if defined(GL_ES_VERSION_2_0)
130
131 #if defined(GL_ANGLE_framebuffer_blit)
132     openGLFunctionTable()->glBlitFramebuffer = ::GL_ANGLE_framebuffer_blit;
133 #else
134     openGLFunctionTable()->glBlitFramebuffer = 0;
135 #endif
136
137 #else
138     ASSIGN_FUNCTION_TABLE_ENTRY(glBlitFramebuffer, success);
139 #endif
140     ASSIGN_FUNCTION_TABLE_ENTRY(glBufferData, success);
141     ASSIGN_FUNCTION_TABLE_ENTRY(glBufferSubData, success);
142     ASSIGN_FUNCTION_TABLE_ENTRY(glCheckFramebufferStatus, success);
143     ASSIGN_FUNCTION_TABLE_ENTRY(glCompileShader, success);
144     ASSIGN_FUNCTION_TABLE_ENTRY(glCreateProgram, success);
145     ASSIGN_FUNCTION_TABLE_ENTRY(glCreateShader, success);
146     ASSIGN_FUNCTION_TABLE_ENTRY(glDeleteBuffers, success);
147     ASSIGN_FUNCTION_TABLE_ENTRY(glDeleteFramebuffers, success);
148     ASSIGN_FUNCTION_TABLE_ENTRY(glDeleteProgram, success);
149     ASSIGN_FUNCTION_TABLE_ENTRY(glDeleteRenderbuffers, success);
150     ASSIGN_FUNCTION_TABLE_ENTRY(glDeleteShader, success);
151     ASSIGN_FUNCTION_TABLE_ENTRY(glDetachShader, success);
152     ASSIGN_FUNCTION_TABLE_ENTRY(glDisableVertexAttribArray, success);
153     ASSIGN_FUNCTION_TABLE_ENTRY(glEnableVertexAttribArray, success);
154     ASSIGN_FUNCTION_TABLE_ENTRY(glFramebufferRenderbuffer, success);
155     ASSIGN_FUNCTION_TABLE_ENTRY(glFramebufferTexture2D, success);
156     ASSIGN_FUNCTION_TABLE_ENTRY(glGenBuffers, success);
157     ASSIGN_FUNCTION_TABLE_ENTRY(glGenerateMipmap, success);
158     ASSIGN_FUNCTION_TABLE_ENTRY(glGenFramebuffers, success);
159     ASSIGN_FUNCTION_TABLE_ENTRY(glGenRenderbuffers, success);
160     ASSIGN_FUNCTION_TABLE_ENTRY(glGetActiveAttrib, success);
161     ASSIGN_FUNCTION_TABLE_ENTRY(glGetActiveUniform, success);
162     ASSIGN_FUNCTION_TABLE_ENTRY(glGetAttachedShaders, success);
163     ASSIGN_FUNCTION_TABLE_ENTRY(glGetAttribLocation, success);
164     ASSIGN_FUNCTION_TABLE_ENTRY(glGetBufferParameteriv, success);
165     ASSIGN_FUNCTION_TABLE_ENTRY(glGetFramebufferAttachmentParameteriv, success);
166     ASSIGN_FUNCTION_TABLE_ENTRY(glGetProgramInfoLog, success);
167     ASSIGN_FUNCTION_TABLE_ENTRY(glGetProgramiv, success);
168     ASSIGN_FUNCTION_TABLE_ENTRY(glGetRenderbufferParameteriv, success);
169     ASSIGN_FUNCTION_TABLE_ENTRY(glGetShaderInfoLog, success);
170     ASSIGN_FUNCTION_TABLE_ENTRY(glGetShaderiv, success);
171     ASSIGN_FUNCTION_TABLE_ENTRY(glGetShaderSource, success);
172     ASSIGN_FUNCTION_TABLE_ENTRY(glGetUniformfv, success);
173     ASSIGN_FUNCTION_TABLE_ENTRY(glGetUniformiv, success);
174     ASSIGN_FUNCTION_TABLE_ENTRY(glGetUniformLocation, success);
175     ASSIGN_FUNCTION_TABLE_ENTRY(glGetVertexAttribfv, success);
176     ASSIGN_FUNCTION_TABLE_ENTRY(glGetVertexAttribiv, success);
177     ASSIGN_FUNCTION_TABLE_ENTRY(glGetVertexAttribPointerv, success);
178     ASSIGN_FUNCTION_TABLE_ENTRY(glIsBuffer, success);
179     ASSIGN_FUNCTION_TABLE_ENTRY(glIsFramebuffer, success);
180     ASSIGN_FUNCTION_TABLE_ENTRY(glIsProgram, success);
181     ASSIGN_FUNCTION_TABLE_ENTRY(glIsRenderbuffer, success);
182     ASSIGN_FUNCTION_TABLE_ENTRY(glIsShader, success);
183     ASSIGN_FUNCTION_TABLE_ENTRY(glLinkProgram, success);
184     ASSIGN_FUNCTION_TABLE_ENTRY(glRenderbufferStorage, success);
185 #if defined(GL_ES_VERSION_2_0)
186
187 #if defined(GL_APPLE_framebuffer_multisample)
188     openGLFunctionTable()->glRenderbufferStorageMultisample = ::glRenderbufferStorageMultisampleAPPLE;
189 #elif defined(GL_ANGLE_framebuffer_multisample)
190     openGLFunctionTable()->glRenderbufferStorageMultisample = ::glRenderbufferStorageMultisampleANGLE;
191 #else
192     openGLFunctionTable()->glRenderbufferStorageMultisample = 0;
193 #endif
194
195 #else
196     ASSIGN_FUNCTION_TABLE_ENTRY(glRenderbufferStorageMultisample, success);
197 #endif
198     ASSIGN_FUNCTION_TABLE_ENTRY(glSampleCoverage, success);
199     ASSIGN_FUNCTION_TABLE_ENTRY(glShaderSource, success);
200     ASSIGN_FUNCTION_TABLE_ENTRY(glStencilFuncSeparate, success);
201     ASSIGN_FUNCTION_TABLE_ENTRY(glStencilMaskSeparate, success);
202     ASSIGN_FUNCTION_TABLE_ENTRY(glStencilOpSeparate, success);
203     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform1f, success);
204     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform1fv, success);
205     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform1i, success);
206     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform1iv, success);
207     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform2f, success);
208     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform2fv, success);
209     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform2i, success);
210     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform2iv, success);
211     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform3f, success);
212     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform3fv, success);
213     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform3i, success);
214     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform3iv, success);
215     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform4f, success);
216     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform4fv, success);
217     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform4i, success);
218     ASSIGN_FUNCTION_TABLE_ENTRY(glUniform4iv, success);
219     ASSIGN_FUNCTION_TABLE_ENTRY(glUniformMatrix2fv, success);
220     ASSIGN_FUNCTION_TABLE_ENTRY(glUniformMatrix3fv, success);
221     ASSIGN_FUNCTION_TABLE_ENTRY(glUniformMatrix4fv, success);
222     ASSIGN_FUNCTION_TABLE_ENTRY(glUseProgram, success);
223     ASSIGN_FUNCTION_TABLE_ENTRY(glValidateProgram, success);
224     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib1f, success);
225     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib1fv, success);
226     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib2f, success);
227     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib2fv, success);
228     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib3f, success);
229     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib3fv, success);
230     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib4f, success);
231     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttrib4fv, success);
232     ASSIGN_FUNCTION_TABLE_ENTRY(glVertexAttribPointer, success);
233
234     if (!success)
235         LOG_ERROR("Could not initialize OpenGL shims");
236     return success;
237 }
238 #endif // ENABLE(TIZEN_WEBKIT2)
239
240 } // namespace WebCore
241
242 #endif // USE(3D_GRAPHICS)