1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef GLES_CM_CONTEX_H
17 #define GLES_CM_CONTEX_H
18 
19 #include "CoreProfileEngine.h"
20 
21 #include <GLcommon/GLDispatch.h>
22 #include <GLcommon/GLESpointer.h>
23 #include <GLcommon/GLESbuffer.h>
24 #include <GLcommon/GLEScontext.h>
25 
26 #include <glm/mat4x4.hpp>
27 
28 #include <vector>
29 #include <string>
30 #include <unordered_map>
31 
32 typedef std::unordered_map<GLfloat,std::vector<int> > PointSizeIndices;
33 
34 class GlLibrary;
35 class CoreProfileEngine;
36 
37 class GLEScmContext: public GLEScontext
38 {
39 public:
40     virtual void init(bool nativeTextureDecompressionEnabled) override;
getCaps()41     virtual const GLSupport* getCaps() const override { return &(GLEScontext::s_glSupportGles1); }
42     static void initGlobal(EGLiface* eglIface);
43     GLEScmContext(int maj, int min, GlobalNameSpace* globalNameSpace,
44             android::base::Stream* stream);
45     void setActiveTexture(GLenum tex) override;
46     void  setClientActiveTexture(GLenum tex);
getActiveTexture()47     GLenum  getActiveTexture() { return GL_TEXTURE0 + m_activeTexture;};
getClientActiveTexture()48     GLenum  getClientActiveTexture() { return GL_TEXTURE0 + m_clientActiveTexture;};
49     void setBindedTexture(GLenum target, unsigned int texture, unsigned int globalTexName);
50     void setupArraysPointers(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct, bool* needEnablingPostDraw) override;
51     void drawPointsArrs(GLESConversionArrays& arrs,GLint first,GLsizei count);
52     void drawPointsElems(GLESConversionArrays& arrs,GLsizei count,GLenum type,const GLvoid* indices);
53     virtual const GLESpointer* getPointer(GLenum arrType) override;
54     int  getMaxTexUnits() override;
55 
56     virtual void initDefaultFBO(
57             GLint width, GLint height, GLint colorFormat, GLint depthstencilFormat, GLint multisamples,
58             GLuint* eglSurfaceRBColorId, GLuint* eglSurfaceRBDepthId,
59             GLuint readWidth, GLint readHeight, GLint readColorFormat, GLint readDepthStencilFormat, GLint readMultisamples,
60             GLuint* eglReadSurfaceRBColorId, GLuint* eglReadSurfaceRBDepthId) override;
61     ~GLEScmContext();
62     static void setMaxGlesVersion(GLESVersion version);
63 
64     // Emulated GLES1
65 
66     // Errors coming from emulation on core profile
67     GLint getErrorCoreProfile();
68 
69     // API
70     virtual bool glGetIntegerv(GLenum pname, GLint *params) override;
71     virtual bool glGetBooleanv(GLenum pname, GLboolean *params) override;
72     virtual bool glGetFloatv(GLenum pname, GLfloat *params) override;
73     virtual bool glGetFixedv(GLenum pname, GLfixed *params) override;
74 
75     void enable(GLenum cap);
76     void disable(GLenum cap);
77 
78     void shadeModel(GLenum mode);
getShadeModel()79     GLenum getShadeModel() const { return mShadeModel; }
80 
81     void matrixMode(GLenum mode);
82     void loadIdentity();
83     void loadMatrixf(const GLfloat* m);
84     void pushMatrix();
85     void popMatrix();
86     void multMatrixf(const GLfloat* m);
87 
88     void orthof(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar);
89     void frustumf(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar);
90 
91     void texEnvf(GLenum target, GLenum pname, GLfloat param);
92     void texEnvfv(GLenum target, GLenum pname, const GLfloat* params);
93     void texEnvi(GLenum target, GLenum pname, GLint param);
94     void texEnviv(GLenum target, GLenum pname, const GLint* params);
95     void getTexEnvfv(GLenum env, GLenum pname, GLfloat* params);
96     void getTexEnviv(GLenum env, GLenum pname, GLint* params);
97 
98     void texGenf(GLenum coord, GLenum pname, GLfloat param);
99     void texGenfv(GLenum coord, GLenum pname, const GLfloat* params);
100     void texGeni(GLenum coord, GLenum pname, GLint param);
101     void texGeniv(GLenum coord, GLenum pname, const GLint* params);
102     void getTexGeniv(GLenum coord, GLenum pname, GLint* params);
103     void getTexGenfv(GLenum coord, GLenum pname, GLfloat* params);
104 
105     void materialf(GLenum face, GLenum pname, GLfloat param);
106     void materialfv(GLenum face, GLenum pname, const GLfloat* params);
107     void getMaterialfv(GLenum face, GLenum pname, GLfloat* params);
108 
109     void lightModelf(GLenum pname, GLfloat param);
110     void lightModelfv(GLenum pname, const GLfloat* params);
111     void lightf(GLenum light, GLenum pname, GLfloat param);
112     void lightfv(GLenum light, GLenum pname, const GLfloat* params);
113     void getLightfv(GLenum light, GLenum pname, GLfloat* params);
114 
115     void multiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q);
116     void normal3f(GLfloat nx, GLfloat ny, GLfloat nz);
117 
118     void fogf(GLenum pname, GLfloat param);
119     void fogfv(GLenum pname, const GLfloat* params);
120 
121     void enableClientState(GLenum clientState);
122     void disableClientState(GLenum clientState);
123 
124     void drawTexOES(float x, float y, float z, float width, float height);
125 
126     void rotatef(float angle, float x, float y, float z);
127     void scalef(float x, float y, float z);
128     void translatef(float x, float y, float z);
129 
130     void color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
131     void color4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha);
132 
133     void drawArrays(GLenum mode, GLint first, GLsizei count);
134     void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices);
135 
136     void clientActiveTexture(GLenum texture);
137 
138     bool doConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id);
139 
140     void getColor(uint32_t count, std::vector<float>& out) const;
141     void getNormal(uint32_t count, std::vector<float>& out) const;
142     void getMultiTexCoord(uint32_t count, uint32_t index, std::vector<float>& out) const;
143     GLenum getTextureEnvMode();
144     GLenum getTextureGenMode();
145 
146     glm::mat4 getProjMatrix();
147     glm::mat4 getModelviewMatrix();
148     glm::mat4 getTextureMatrix();
149 
150     struct Material {
151         GLfloat ambient[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
152         GLfloat diffuse[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
153         GLfloat specular[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
154         GLfloat emissive[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
155         GLfloat specularExponent = 0.0f;
156     };
157 
158     struct LightModel {
159         GLfloat color[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
160         bool twoSided = false;
161     };
162 
163     struct Light {
164         GLfloat ambient[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
165         GLfloat diffuse[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
166         GLfloat specular[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
167         GLfloat position[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
168         GLfloat direction[3] = { 0.0f, 0.0f, -1.0f };
169         GLfloat spotlightExponent = 0.0f;
170         GLfloat spotlightCutoffAngle = 180.0f;
171         GLfloat attenuationConst = 1.0f;
172         GLfloat attenuationLinear = 0.0f;
173         GLfloat attenuationQuadratic = 0.0f;
174     };
175 
176     struct Fog {
177         GLenum mode = GL_EXP;
178         float density = 1.0f;
179         float start = 0.0f;
180         float end = 1.0f;
181         GLfloat color[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
182     };
183 
184     static constexpr int kMaxLights = 8;
185     const Material& getMaterialInfo();
186     const LightModel& getLightModelInfo();
187     const Light& getLightInfo(uint32_t lightIndex);
188     const Fog& getFogInfo();
189 
190     virtual void onSave(android::base::Stream* stream) const override;
191 
192 protected:
193     virtual void postLoadRestoreCtx() override;
194 
195     static const GLint kMaxTextureUnits = 4;
196     static const GLint kMaxMatrixStackSize = 16;
197 
198     bool needConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) override;
199 private:
200     void setupArrayPointerHelper(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLenum array_id,GLESpointer* p);
201     void setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized, int pointsIndex = -1, bool isInt = false);
202     void drawPoints(PointSizeIndices* points);
203     // appendRepeatedVector appends the input vector "in" count times
204     // to the vector "out".
205     void appendRepeatedVector(uint32_t count, std::vector<float>& in, std::vector<float>& out) const;
206     void drawPointsData(GLESConversionArrays& arrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices_in,bool isElemsDraw);
207     void initExtensionString() override;
208     void restoreVertexAttrib(GLenum attrib);
core()209     CoreProfileEngine& core() { return *m_coreProfileEngine; }
210 
211     GLESpointer*          m_texCoords = nullptr;
212     int                   m_pointsIndex = -1;
213     unsigned int          m_clientActiveTexture = 0;
214 
215     GLenum mShadeModel = GL_SMOOTH;
216 
217     union GLVal {
218         GLfloat floatVal[4];
219         GLint intVal[4];
220         GLubyte ubyteVal[4];
221         GLenum enumVal[4];
222     };
223 
224     struct GLValTyped {
225         GLenum type;
226         GLVal val;
227     };
228 
229     using TexEnv = std::unordered_map<GLenum, GLValTyped>;
230     using TexUnitEnvs = std::vector<TexEnv>;
231     using TexGens = std::vector<TexEnv>;
232     using MatrixStack = std::vector<glm::mat4>;
233 
234     GLenum mCurrMatrixMode = GL_PROJECTION;
235 
236     GLValTyped mColor;
237     GLValTyped mNormal;
238     GLVal mMultiTexCoord[kMaxTextureUnits] = {};
239 
240     TexUnitEnvs mTexUnitEnvs;
241     TexGens mTexGens;
242 
243     MatrixStack mProjMatrices;
244     MatrixStack mModelviewMatrices;
245     std::vector<MatrixStack> mTextureMatrices;
246     glm::mat4& currMatrix();
247     MatrixStack& currMatrixStack();
248     void restoreMatrixStack(const MatrixStack& matrices);
249 
250     Material mMaterial;
251     LightModel mLightModel;
252     Light mLights[kMaxLights] = {};
253     Fog mFog = {};
254 
255     // Core profile stuff
256     CoreProfileEngine*    m_coreProfileEngine = nullptr;
257 };
258 
259 #endif
260