1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef libGLES_CM_hpp 16 #define libGLES_CM_hpp 17 18 #include <GLES/gl.h> 19 #include <GLES/glext.h> 20 #include <EGL/egl.h> 21 22 #include "Common/SharedLibrary.hpp" 23 24 namespace sw 25 { 26 class FrameBuffer; 27 enum Format : unsigned char; 28 } 29 30 namespace egl 31 { 32 class Context; 33 class Image; 34 class Config; 35 } 36 37 class LibGLES_CMexports 38 { 39 public: 40 LibGLES_CMexports(); 41 42 void (*glActiveTexture)(GLenum texture); 43 void (*glAlphaFunc)(GLenum func, GLclampf ref); 44 void (*glAlphaFuncx)(GLenum func, GLclampx ref); 45 void (*glBindBuffer)(GLenum target, GLuint buffer); 46 void (*glBindFramebuffer)(GLenum target, GLuint framebuffer); 47 void (*glBindFramebufferOES)(GLenum target, GLuint framebuffer); 48 void (*glBindRenderbufferOES)(GLenum target, GLuint renderbuffer); 49 void (*glBindTexture)(GLenum target, GLuint texture); 50 void (*glBlendEquationOES)(GLenum mode); 51 void (*glBlendEquationSeparateOES)(GLenum modeRGB, GLenum modeAlpha); 52 void (*glBlendFunc)(GLenum sfactor, GLenum dfactor); 53 void (*glBlendFuncSeparateOES)(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha); 54 void (*glBufferData)(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage); 55 void (*glBufferSubData)(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data); 56 GLenum (*glCheckFramebufferStatusOES)(GLenum target); 57 void (*glClear)(GLbitfield mask); 58 void (*glClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); 59 void (*glClearColorx)(GLclampx red, GLclampx green, GLclampx blue, GLclampx alpha); 60 void (*glClearDepthf)(GLclampf depth); 61 void (*glClearDepthx)(GLclampx depth); 62 void (*glClearStencil)(GLint s); 63 void (*glClientActiveTexture)(GLenum texture); 64 void (*glClipPlanef)(GLenum plane, const GLfloat *equation); 65 void (*glClipPlanex)(GLenum plane, const GLfixed *equation); 66 void (*glColor4f)(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); 67 void (*glColor4ub)(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha); 68 void (*glColor4x)(GLfixed red, GLfixed green, GLfixed blue, GLfixed alpha); 69 void (*glColorMask)(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); 70 void (*glColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer); 71 void (*glCompressedTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, 72 GLint border, GLsizei imageSize, const GLvoid* data); 73 void (*glCompressedTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, 74 GLenum format, GLsizei imageSize, const GLvoid* data); 75 void (*glCopyTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border); 76 void (*glCopyTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height); 77 void (*glCullFace)(GLenum mode); 78 void (*glDeleteBuffers)(GLsizei n, const GLuint* buffers); 79 void (*glDeleteFramebuffersOES)(GLsizei n, const GLuint* framebuffers); 80 void (*glDeleteRenderbuffersOES)(GLsizei n, const GLuint* renderbuffers); 81 void (*glDeleteTextures)(GLsizei n, const GLuint* textures); 82 void (*glDepthFunc)(GLenum func); 83 void (*glDepthMask)(GLboolean flag); 84 void (*glDepthRangex)(GLclampx zNear, GLclampx zFar); 85 void (*glDepthRangef)(GLclampf zNear, GLclampf zFar); 86 void (*glDisable)(GLenum cap); 87 void (*glDisableClientState)(GLenum array); 88 void (*glDrawArrays)(GLenum mode, GLint first, GLsizei count); 89 void (*glDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices); 90 void (*glEnable)(GLenum cap); 91 void (*glEnableClientState)(GLenum array); 92 void (*glFinish)(void); 93 void (*glFlush)(void); 94 void (*glFramebufferRenderbufferOES)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); 95 void (*glFramebufferTexture2DOES)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); 96 void (*glFogf)(GLenum pname, GLfloat param); 97 void (*glFogfv)(GLenum pname, const GLfloat *params); 98 void (*glFogx)(GLenum pname, GLfixed param); 99 void (*glFogxv)(GLenum pname, const GLfixed *params); 100 void (*glFrontFace)(GLenum mode); 101 void (*glFrustumf)(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar); 102 void (*glFrustumx)(GLfixed left, GLfixed right, GLfixed bottom, GLfixed top, GLfixed zNear, GLfixed zFar); 103 void (*glGenerateMipmapOES)(GLenum target); 104 void (*glGenBuffers)(GLsizei n, GLuint* buffers); 105 void (*glGenFramebuffersOES)(GLsizei n, GLuint* framebuffers); 106 void (*glGenRenderbuffersOES)(GLsizei n, GLuint* renderbuffers); 107 void (*glGenTextures)(GLsizei n, GLuint* textures); 108 void (*glGetRenderbufferParameterivOES)(GLenum target, GLenum pname, GLint* params); 109 void (*glGetBooleanv)(GLenum pname, GLboolean* params); 110 void (*glGetBufferParameteriv)(GLenum target, GLenum pname, GLint* params); 111 void (*glGetClipPlanef)(GLenum pname, GLfloat eqn[4]); 112 void (*glGetClipPlanex)(GLenum pname, GLfixed eqn[4]); 113 GLenum (*glGetError)(void); 114 void (*glGetFixedv)(GLenum pname, GLfixed *params); 115 void (*glGetFloatv)(GLenum pname, GLfloat* params); 116 void (*glGetFramebufferAttachmentParameterivOES)(GLenum target, GLenum attachment, GLenum pname, GLint* params); 117 void (*glGetIntegerv)(GLenum pname, GLint* params); 118 void (*glGetLightfv)(GLenum light, GLenum pname, GLfloat *params); 119 void (*glGetLightxv)(GLenum light, GLenum pname, GLfixed *params); 120 void (*glGetMaterialfv)(GLenum face, GLenum pname, GLfloat *params); 121 void (*glGetMaterialxv)(GLenum face, GLenum pname, GLfixed *params); 122 void (*glGetPointerv)(GLenum pname, GLvoid **params); 123 const GLubyte* (*glGetString)(GLenum name); 124 void (*glGetTexParameterfv)(GLenum target, GLenum pname, GLfloat* params); 125 void (*glGetTexParameteriv)(GLenum target, GLenum pname, GLint* params); 126 void (*glGetTexEnvfv)(GLenum env, GLenum pname, GLfloat *params); 127 void (*glGetTexEnviv)(GLenum env, GLenum pname, GLint *params); 128 void (*glGetTexEnvxv)(GLenum env, GLenum pname, GLfixed *params); 129 void (*glGetTexParameterxv)(GLenum target, GLenum pname, GLfixed *params); 130 void (*glHint)(GLenum target, GLenum mode); 131 GLboolean (*glIsBuffer)(GLuint buffer); 132 GLboolean (*glIsEnabled)(GLenum cap); 133 GLboolean (*glIsFramebufferOES)(GLuint framebuffer); 134 GLboolean (*glIsTexture)(GLuint texture); 135 GLboolean (*glIsRenderbufferOES)(GLuint renderbuffer); 136 void (*glLightModelf)(GLenum pname, GLfloat param); 137 void (*glLightModelfv)(GLenum pname, const GLfloat *params); 138 void (*glLightModelx)(GLenum pname, GLfixed param); 139 void (*glLightModelxv)(GLenum pname, const GLfixed *params); 140 void (*glLightf)(GLenum light, GLenum pname, GLfloat param); 141 void (*glLightfv)(GLenum light, GLenum pname, const GLfloat *params); 142 void (*glLightx)(GLenum light, GLenum pname, GLfixed param); 143 void (*glLightxv)(GLenum light, GLenum pname, const GLfixed *params); 144 void (*glLineWidth)(GLfloat width); 145 void (*glLineWidthx)(GLfixed width); 146 void (*glLoadIdentity)(void); 147 void (*glLoadMatrixf)(const GLfloat *m); 148 void (*glLoadMatrixx)(const GLfixed *m); 149 void (*glLogicOp)(GLenum opcode); 150 void (*glMaterialf)(GLenum face, GLenum pname, GLfloat param); 151 void (*glMaterialfv)(GLenum face, GLenum pname, const GLfloat *params); 152 void (*glMaterialx)(GLenum face, GLenum pname, GLfixed param); 153 void (*glMaterialxv)(GLenum face, GLenum pname, const GLfixed *params); 154 void (*glMatrixMode)(GLenum mode); 155 void (*glMultMatrixf)(const GLfloat *m); 156 void (*glMultMatrixx)(const GLfixed *m); 157 void (*glMultiTexCoord4f)(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q); 158 void (*glMultiTexCoord4x)(GLenum target, GLfixed s, GLfixed t, GLfixed r, GLfixed q); 159 void (*glNormal3f)(GLfloat nx, GLfloat ny, GLfloat nz); 160 void (*glNormal3x)(GLfixed nx, GLfixed ny, GLfixed nz); 161 void (*glNormalPointer)(GLenum type, GLsizei stride, const GLvoid *pointer); 162 void (*glOrthof)(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar); 163 void (*glOrthox)(GLfixed left, GLfixed right, GLfixed bottom, GLfixed top, GLfixed zNear, GLfixed zFar); 164 void (*glPixelStorei)(GLenum pname, GLint param); 165 void (*glPointParameterf)(GLenum pname, GLfloat param); 166 void (*glPointParameterfv)(GLenum pname, const GLfloat *params); 167 void (*glPointParameterx)(GLenum pname, GLfixed param); 168 void (*glPointParameterxv)(GLenum pname, const GLfixed *params); 169 void (*glPointSize)(GLfloat size); 170 void (*glPointSizePointerOES)(GLenum type, GLsizei stride, const GLvoid *pointer); 171 void (*glPointSizex)(GLfixed size); 172 void (*glPolygonOffset)(GLfloat factor, GLfloat units); 173 void (*glPolygonOffsetx)(GLfixed factor, GLfixed units); 174 void (*glPopMatrix)(void); 175 void (*glPushMatrix)(void); 176 void (*glReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels); 177 void (*glRenderbufferStorageOES)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height); 178 void (*glRotatef)(GLfloat angle, GLfloat x, GLfloat y, GLfloat z); 179 void (*glRotatex)(GLfixed angle, GLfixed x, GLfixed y, GLfixed z); 180 void (*glSampleCoverage)(GLclampf value, GLboolean invert); 181 void (*glSampleCoveragex)(GLclampx value, GLboolean invert); 182 void (*glScalef)(GLfloat x, GLfloat y, GLfloat z); 183 void (*glScalex)(GLfixed x, GLfixed y, GLfixed z); 184 void (*glScissor)(GLint x, GLint y, GLsizei width, GLsizei height); 185 void (*glShadeModel)(GLenum mode); 186 void (*glStencilFunc)(GLenum func, GLint ref, GLuint mask); 187 void (*glStencilMask)(GLuint mask); 188 void (*glStencilOp)(GLenum fail, GLenum zfail, GLenum zpass); 189 void (*glTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer); 190 void (*glTexEnvf)(GLenum target, GLenum pname, GLfloat param); 191 void (*glTexEnvfv)(GLenum target, GLenum pname, const GLfloat *params); 192 void (*glTexEnvi)(GLenum target, GLenum pname, GLint param); 193 void (*glTexEnvx)(GLenum target, GLenum pname, GLfixed param); 194 void (*glTexEnviv)(GLenum target, GLenum pname, const GLint *params); 195 void (*glTexEnvxv)(GLenum target, GLenum pname, const GLfixed *params); 196 void (*glTexImage2D)(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, 197 GLint border, GLenum format, GLenum type, const GLvoid* pixels); 198 void (*glTexParameterf)(GLenum target, GLenum pname, GLfloat param); 199 void (*glTexParameterfv)(GLenum target, GLenum pname, const GLfloat* params); 200 void (*glTexParameteri)(GLenum target, GLenum pname, GLint param); 201 void (*glTexParameteriv)(GLenum target, GLenum pname, const GLint* params); 202 void (*glTexParameterx)(GLenum target, GLenum pname, GLfixed param); 203 void (*glTexParameterxv)(GLenum target, GLenum pname, const GLfixed *params); 204 void (*glTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, 205 GLenum format, GLenum type, const GLvoid* pixels); 206 void (*glTranslatef)(GLfloat x, GLfloat y, GLfloat z); 207 void (*glTranslatex)(GLfixed x, GLfixed y, GLfixed z); 208 void (*glVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer); 209 void (*glViewport)(GLint x, GLint y, GLsizei width, GLsizei height); 210 void (*glEGLImageTargetTexture2DOES)(GLenum target, GLeglImageOES image); 211 void (*glEGLImageTargetRenderbufferStorageOES)(GLenum target, GLeglImageOES image); 212 void (*glDrawTexsOES)(GLshort x, GLshort y, GLshort z, GLshort width, GLshort height); 213 void (*glDrawTexiOES)(GLint x, GLint y, GLint z, GLint width, GLint height); 214 void (*glDrawTexxOES)(GLfixed x, GLfixed y, GLfixed z, GLfixed width, GLfixed height); 215 void (*glDrawTexsvOES)(const GLshort *coords); 216 void (*glDrawTexivOES)(const GLint *coords); 217 void (*glDrawTexxvOES)(const GLfixed *coords); 218 void (*glDrawTexfOES)(GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height); 219 void (*glDrawTexfvOES)(const GLfloat *coords); 220 221 egl::Context *(*es1CreateContext)(const egl::Config *config, const egl::Context *shareContext); 222 __eglMustCastToProperFunctionPointerType (*es1GetProcAddress)(const char *procname); 223 egl::Image *(*createBackBuffer)(int width, int height, const egl::Config *config); 224 egl::Image *(*createDepthStencil)(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard); 225 sw::FrameBuffer *(*createFrameBuffer)(void *display, EGLNativeWindowType window, int width, int height); 226 }; 227 228 class LibGLES_CM 229 { 230 public: 231 LibGLES_CM() 232 { 233 libGLES_CM = nullptr; 234 libGLES_CMexports = nullptr; 235 } 236 237 ~LibGLES_CM() 238 { 239 freeLibrary(libGLES_CM); 240 } 241 242 operator bool() 243 { 244 return loadExports() != nullptr; 245 } 246 247 LibGLES_CMexports *operator->() 248 { 249 return loadExports(); 250 } 251 252 private: 253 LibGLES_CMexports *loadExports() 254 { 255 if(!libGLES_CM) 256 { 257 #if defined(_WIN32) 258 #if defined(__LP64__) 259 const char *libGLES_CM_lib[] = {"libGLES_CM.dll", "lib64GLES_CM_translator.dll"}; 260 #else 261 const char *libGLES_CM_lib[] = {"libGLES_CM.dll", "libGLES_CM_translator.dll"}; 262 #endif 263 #elif defined(__ANDROID__) 264 #if defined(__LP64__) 265 const char *libGLES_CM_lib[] = {"/vendor/lib64/egl/libGLESv1_CM_swiftshader.so"}; 266 #else 267 const char *libGLES_CM_lib[] = {"/vendor/lib/egl/libGLESv1_CM_swiftshader.so"}; 268 #endif 269 #elif defined(__linux__) 270 #if defined(__LP64__) 271 const char *libGLES_CM_lib[] = {"lib64GLES_CM_translator.so", "libGLES_CM.so.1", "libGLES_CM.so"}; 272 #else 273 const char *libGLES_CM_lib[] = {"libGLES_CM_translator.so", "libGLES_CM.so.1", "libGLES_CM.so"}; 274 #endif 275 #elif defined(__APPLE__) 276 #if defined(__LP64__) 277 const char *libGLES_CM_lib[] = {"lib64GLES_CM_translator.dylib", "libGLES_CM.dylib"}; 278 #else 279 const char *libGLES_CM_lib[] = {"libGLES_CM_translator.dylib", "libGLES_CM.dylib"}; 280 #endif 281 #else 282 #error "libGLES_CM::loadExports unimplemented for this platform" 283 #endif 284 285 libGLES_CM = loadLibrary(libGLES_CM_lib, "libGLES_CM_swiftshader"); 286 287 if(libGLES_CM) 288 { 289 auto libGLES_CM_swiftshader = (LibGLES_CMexports *(*)())getProcAddress(libGLES_CM, "libGLES_CM_swiftshader"); 290 libGLES_CMexports = libGLES_CM_swiftshader(); 291 } 292 } 293 294 return libGLES_CMexports; 295 } 296 297 void *libGLES_CM; 298 LibGLES_CMexports *libGLES_CMexports; 299 }; 300 301 #endif // libGLES_CM_hpp 302