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