|
1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 * |
|
3 * Permission is hereby granted, free of charge, to any person obtaining a |
|
4 * copy of this software and /or associated documentation files |
|
5 * (the "Materials "), to deal in the Materials without restriction, |
|
6 * including without limitation the rights to use, copy, modify, merge, |
|
7 * publish, distribute, sublicense, and/or sell copies of the Materials, |
|
8 * and to permit persons to whom the Materials are furnished to do so, |
|
9 * subject to the following conditions: |
|
10 * |
|
11 * The above copyright notice and this permission notice shall be included |
|
12 * in all copies or substantial portions of the Materials. |
|
13 * |
|
14 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
|
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR |
|
20 * THE USE OR OTHER DEALINGS IN THE MATERIALS. |
|
21 * |
|
22 * Initial Contributors: |
|
23 * Nokia Corporation - initial contribution. |
|
24 * |
|
25 * Contributors: |
|
26 * |
|
27 * Description: |
|
28 * |
|
29 */ |
|
30 |
|
31 #ifndef _GLESCONTEXT_H_ |
|
32 #define _GLESCONTEXT_H_ |
|
33 |
|
34 #include <GLES/gl.h> |
|
35 #include <map> |
|
36 #include <iostream> |
|
37 #include "GLESArray.h" |
|
38 #include "glesInternal.h" |
|
39 #include "GLESDesktopGL.h" |
|
40 #include "glesOS.h" |
|
41 #include "EGLInterface.h" |
|
42 |
|
43 struct GLESBuffer; |
|
44 class GLESTexture; |
|
45 struct GLESTextureLevel; |
|
46 |
|
47 namespace |
|
48 { |
|
49 typedef std::map<unsigned int, GLESBuffer*> BufferMap; |
|
50 typedef std::map<unsigned int, GLESTexture*> TextureMap; |
|
51 } |
|
52 |
|
53 enum GLESArrayFlag |
|
54 { |
|
55 GLES_INVALID_ARRAY = 0, |
|
56 GLES_VERTEX_ARRAY = 1 << 0, |
|
57 GLES_NORMAL_ARRAY = 1 << 1, |
|
58 GLES_COLOR_ARRAY = 1 << 2, |
|
59 GLES_POINT_SIZE_ARRAY = 1 << 3, |
|
60 GLES_TEXTURE_COORD_ARRAY = 1 << 4 |
|
61 }; |
|
62 |
|
63 class GLESContext { |
|
64 public: |
|
65 GLESContext(void* nativeContext); |
|
66 ~GLESContext(); |
|
67 |
|
68 bool Initialize(); |
|
69 |
|
70 bool IsInitialized() const { return m_initialized; } |
|
71 const GLESDesktopGL& DGL() const { return m_dgl; } |
|
72 void* NativeContext() const { return m_nativeContext; } |
|
73 GLenum Error() const { return m_error; } |
|
74 GLenum GetHostError(); |
|
75 unsigned int MaxTextureLevel() const { return m_maxTextureLevel; } |
|
76 unsigned int MaxTextureUnits() const { return m_maxTextureUnits; } |
|
77 unsigned int MaxClipPlanes() const { return m_maxClipPlanes; } |
|
78 unsigned int MaxLights() const { return m_maxLights; } |
|
79 int ClientActiveTexture() const { return m_clientActiveTexture; } |
|
80 GLESArray& VertexArray() { return m_vertexArray; } |
|
81 const GLESArray& VertexArray() const { return m_vertexArray; } |
|
82 GLESArray& NormalArray() { return m_normalArray; } |
|
83 const GLESArray& NormalArray() const { return m_normalArray; } |
|
84 GLESArray& ColorArray() { return m_colorArray; } |
|
85 const GLESArray& ColorArray() const { return m_colorArray; } |
|
86 const GLESArray& PointSizeArray() const { return m_pointSizeArray; } |
|
87 GLESArray& TexCoordArray(unsigned int texture); |
|
88 GLESArray& TexCoordArray(); |
|
89 const GLESArray& TexCoordArray(unsigned int texture) const; |
|
90 const GLESArray& TexCoordArray() const; |
|
91 unsigned int ArrayBufferBinding() const { return m_arrayBufferBinding; } |
|
92 GLESBuffer* ArrayBuffer(); |
|
93 unsigned int ElementArrayBufferBinding() const { return m_elementArrayBufferBinding; } |
|
94 GLESBuffer* ElementArrayBuffer(); |
|
95 const GLESBuffer* Buffer(unsigned int buffer) const; |
|
96 unsigned int TextureBinding() const { return m_textureBinding; } |
|
97 GLESTexture* Texture(unsigned int name); |
|
98 const GLESTextureLevel* TextureLevel(GLint level) const; |
|
99 |
|
100 bool IsArrayEnabled(GLESArrayFlag array) const { return !!(m_enabledArrays & array); } |
|
101 |
|
102 void SetError(GLenum error) { m_error = error; } |
|
103 void SetClientActiveTexture(int texture) { m_clientActiveTexture = texture; } |
|
104 void SetVertexArray(int size, GLenum type, int stride, const void *pointer); |
|
105 void SetNormalArray(GLenum type, int stride, const void *pointer); |
|
106 void SetColorArray(int size, GLenum type, int stride, const void *pointer); |
|
107 void SetPointerSizeArray(GLenum type, int stride, const void *pointer); |
|
108 void SetTexCoordArray(int size, GLenum type, int stride, const void *pointer); |
|
109 |
|
110 void EnableArray(GLESArrayFlag array) { m_enabledArrays |= array; } |
|
111 void DisableArray(GLESArrayFlag array) { m_enabledArrays &= ~array; } |
|
112 |
|
113 void ReserveBufferNames(int num, unsigned int* names); |
|
114 void DeleteBuffer(unsigned int buffer); |
|
115 |
|
116 bool BindArrayBuffer(unsigned int buffer); |
|
117 bool BindElementArrayBuffer(unsigned int buffer); |
|
118 |
|
119 void DeleteTexture(unsigned int texture); |
|
120 bool BindTexture(unsigned int texture); |
|
121 |
|
122 private: |
|
123 bool m_initialized; |
|
124 void* m_nativeContext; |
|
125 GLESDesktopGL m_dgl; |
|
126 GLenum m_error; |
|
127 unsigned int m_maxTextureLevel; |
|
128 unsigned int m_maxTextureUnits; |
|
129 unsigned int m_maxClipPlanes; |
|
130 unsigned int m_maxLights; |
|
131 int m_clientActiveTexture; |
|
132 GLESArray m_vertexArray; |
|
133 GLESArray m_normalArray; |
|
134 GLESArray m_colorArray; |
|
135 GLESArray m_pointSizeArray; |
|
136 GLESArray* m_texCoordArray; |
|
137 unsigned int m_enabledArrays; |
|
138 unsigned int m_arrayBufferBinding; |
|
139 unsigned int m_elementArrayBufferBinding; |
|
140 unsigned int m_textureBinding; |
|
141 BufferMap m_buffers; |
|
142 TextureMap m_textures; |
|
143 }; |
|
144 |
|
145 void* glesGetCurrentGLESContext(); |
|
146 |
|
147 #define GLES_ENTER_RET(RETVAL) \ |
|
148 GLESContext* ctx; \ |
|
149 do \ |
|
150 { \ |
|
151 glesGetLock(); \ |
|
152 ctx = (GLESContext*)glesGetCurrentGLESContext(); \ |
|
153 if(!ctx) \ |
|
154 { \ |
|
155 glesReleaseLock(); \ |
|
156 return RETVAL; \ |
|
157 } \ |
|
158 if(!ctx->IsInitialized()) \ |
|
159 { \ |
|
160 if(!ctx->Initialize()) \ |
|
161 { \ |
|
162 std::cerr << "Failed to initialize context" << std::endl; \ |
|
163 exit(1); \ |
|
164 } \ |
|
165 } \ |
|
166 } while(0) |
|
167 |
|
168 #define GLES_ENTER() GLES_ENTER_RET(GLES_NO_RETVAL) |
|
169 |
|
170 // Check the host error after every API function call so that the error flag |
|
171 // in the wrapper always contains the latest error. |
|
172 #define GLES_LEAVE_RET(RETVAL) \ |
|
173 do \ |
|
174 { \ |
|
175 ctx->GetHostError(); \ |
|
176 glesReleaseLock(); \ |
|
177 return RETVAL; \ |
|
178 } while(0) |
|
179 |
|
180 #define GLES_LEAVE() GLES_LEAVE_RET(GLES_NO_RETVAL) |
|
181 |
|
182 #define GLES_LEAVE_NO_ERROR_CHECK_RET(RETVAL) \ |
|
183 do \ |
|
184 { \ |
|
185 glesReleaseLock(); \ |
|
186 return RETVAL; \ |
|
187 } while(0) |
|
188 |
|
189 #define GLES_LEAVE_NO_ERROR_CHECK() GLES_LEAVE_NO_ERROR_CHECK_RET(GLES_NO_RETVAL) |
|
190 |
|
191 #define GLES_ERROR_IF_RET(COND, ERROR, RETVAL) \ |
|
192 do \ |
|
193 { \ |
|
194 if(COND) \ |
|
195 { \ |
|
196 ctx->SetError(ERROR); \ |
|
197 return RETVAL; \ |
|
198 } \ |
|
199 } while(0) |
|
200 |
|
201 #define GLES_ERROR_IF(COND, ERROR) GLES_ERROR_IF_RET(COND, ERROR, GLES_NO_RETVAL) |
|
202 #define GLES_ERROR_RET(ERROR, RETVAL) GLES_ERROR_IF_RET(true, ERROR, RETVAL) |
|
203 #define GLES_ERROR(ERROR) GLES_ERROR_IF(true, ERROR) |
|
204 |
|
205 #endif // _GLESCONTEXT_H_ |