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() override; 41 static void initGlobal(EGLiface* eglIface); 42 GLEScmContext(int maj, int min, GlobalNameSpace* globalNameSpace, 43 android::base::Stream* stream); 44 void setActiveTexture(GLenum tex) override; 45 void setClientActiveTexture(GLenum tex); getActiveTexture()46 GLenum getActiveTexture() { return GL_TEXTURE0 + m_activeTexture;}; getClientActiveTexture()47 GLenum getClientActiveTexture() { return GL_TEXTURE0 + m_clientActiveTexture;}; 48 void setBindedTexture(GLenum target, unsigned int texture, unsigned int globalTexName); 49 void setupArraysPointers(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct) override; 50 void drawPointsArrs(GLESConversionArrays& arrs,GLint first,GLsizei count); 51 void drawPointsElems(GLESConversionArrays& arrs,GLsizei count,GLenum type,const GLvoid* indices); 52 virtual const GLESpointer* getPointer(GLenum arrType) override; 53 int getMaxTexUnits() override; 54 55 virtual void initDefaultFBO( 56 GLint width, GLint height, GLint colorFormat, GLint depthstencilFormat, GLint multisamples, 57 GLuint* eglSurfaceRBColorId, GLuint* eglSurfaceRBDepthId, 58 GLuint readWidth, GLint readHeight, GLint readColorFormat, GLint readDepthStencilFormat, GLint readMultisamples, 59 GLuint* eglReadSurfaceRBColorId, GLuint* eglReadSurfaceRBDepthId) override; 60 ~GLEScmContext(); 61 static void setMaxGlesVersion(GLESVersion version); 62 63 // Emulated GLES1 64 65 // Errors coming from emulation on core profile 66 GLint getErrorCoreProfile(); 67 68 // API 69 virtual bool glGetIntegerv(GLenum pname, GLint *params) override; 70 virtual bool glGetBooleanv(GLenum pname, GLboolean *params) override; 71 virtual bool glGetFloatv(GLenum pname, GLfloat *params) override; 72 virtual bool glGetFixedv(GLenum pname, GLfixed *params) override; 73 74 void enable(GLenum cap); 75 void disable(GLenum cap); 76 77 void shadeModel(GLenum mode); getShadeModel()78 GLenum getShadeModel() const { return mShadeModel; } 79 80 void matrixMode(GLenum mode); 81 void loadIdentity(); 82 void loadMatrixf(const GLfloat* m); 83 void pushMatrix(); 84 void popMatrix(); 85 void multMatrixf(const GLfloat* m); 86 87 void orthof(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar); 88 void frustumf(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar); 89 90 void texEnvf(GLenum target, GLenum pname, GLfloat param); 91 void texEnvfv(GLenum target, GLenum pname, const GLfloat* params); 92 void texEnvi(GLenum target, GLenum pname, GLint param); 93 void texEnviv(GLenum target, GLenum pname, const GLint* params); 94 void getTexEnvfv(GLenum env, GLenum pname, GLfloat* params); 95 void getTexEnviv(GLenum env, GLenum pname, GLint* params); 96 97 void texGenf(GLenum coord, GLenum pname, GLfloat param); 98 void texGenfv(GLenum coord, GLenum pname, const GLfloat* params); 99 void texGeni(GLenum coord, GLenum pname, GLint param); 100 void texGeniv(GLenum coord, GLenum pname, const GLint* params); 101 void getTexGeniv(GLenum coord, GLenum pname, GLint* params); 102 void getTexGenfv(GLenum coord, GLenum pname, GLfloat* params); 103 104 void materialf(GLenum face, GLenum pname, GLfloat param); 105 void materialfv(GLenum face, GLenum pname, const GLfloat* params); 106 void getMaterialfv(GLenum face, GLenum pname, GLfloat* params); 107 108 void lightModelf(GLenum pname, GLfloat param); 109 void lightModelfv(GLenum pname, const GLfloat* params); 110 void lightf(GLenum light, GLenum pname, GLfloat param); 111 void lightfv(GLenum light, GLenum pname, const GLfloat* params); 112 void getLightfv(GLenum light, GLenum pname, GLfloat* params); 113 114 void multiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q); 115 void normal3f(GLfloat nx, GLfloat ny, GLfloat nz); 116 117 void fogf(GLenum pname, GLfloat param); 118 void fogfv(GLenum pname, const GLfloat* params); 119 120 void enableClientState(GLenum clientState); 121 void disableClientState(GLenum clientState); 122 123 void drawTexOES(float x, float y, float z, float width, float height); 124 125 void rotatef(float angle, float x, float y, float z); 126 void scalef(float x, float y, float z); 127 void translatef(float x, float y, float z); 128 129 void color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); 130 void color4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha); 131 132 void drawArrays(GLenum mode, GLint first, GLsizei count); 133 void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices); 134 135 void clientActiveTexture(GLenum texture); 136 137 bool doConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id); 138 139 std::vector<float> getColor() const; 140 std::vector<float> getNormal() const; 141 std::vector<float> getMultiTexCoord(uint32_t index) const; 142 GLenum getTextureEnvMode(); 143 GLenum getTextureGenMode(); 144 145 glm::mat4 getProjMatrix(); 146 glm::mat4 getModelviewMatrix(); 147 glm::mat4 getTextureMatrix(); 148 149 struct Material { 150 GLfloat ambient[4] = { 0.2f, 0.2f, 0.2f, 1.0f }; 151 GLfloat diffuse[4] = { 0.8f, 0.8f, 0.8f, 1.0f }; 152 GLfloat specular[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; 153 GLfloat emissive[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; 154 GLfloat specularExponent = 0.0f; 155 }; 156 157 struct LightModel { 158 GLfloat color[4] = { 0.2f, 0.2f, 0.2f, 1.0f }; 159 bool twoSided = false; 160 }; 161 162 struct Light { 163 GLfloat ambient[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; 164 GLfloat diffuse[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; 165 GLfloat specular[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; 166 GLfloat position[4] = { 0.0f, 0.0f, 1.0f, 0.0f }; 167 GLfloat direction[3] = { 0.0f, 0.0f, -1.0f }; 168 GLfloat spotlightExponent = 0.0f; 169 GLfloat spotlightCutoffAngle = 180.0f; 170 GLfloat attenuationConst = 1.0f; 171 GLfloat attenuationLinear = 0.0f; 172 GLfloat attenuationQuadratic = 0.0f; 173 }; 174 175 struct Fog { 176 GLenum mode = GL_EXP; 177 float density = 1.0f; 178 float start = 0.0f; 179 float end = 1.0f; 180 GLfloat color[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 181 }; 182 183 static constexpr int kMaxLights = 8; 184 const Material& getMaterialInfo(); 185 const LightModel& getLightModelInfo(); 186 const Light& getLightInfo(uint32_t lightIndex); 187 const Fog& getFogInfo(); 188 189 virtual void onSave(android::base::Stream* stream) const override; 190 191 protected: 192 virtual void postLoadRestoreCtx() override; 193 194 static const GLint kMaxTextureUnits = 4; 195 static const GLint kMaxMatrixStackSize = 16; 196 197 bool needConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) override; 198 private: 199 void setupArrayPointerHelper(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLenum array_id,GLESpointer* p); 200 void setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized, int pointsIndex = -1, bool isInt = false); 201 void drawPoints(PointSizeIndices* points); 202 void drawPointsData(GLESConversionArrays& arrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices_in,bool isElemsDraw); 203 void initExtensionString() override; 204 void restoreVertexAttrib(GLenum attrib); core()205 CoreProfileEngine& core() { return *m_coreProfileEngine; } 206 207 GLESpointer* m_texCoords = nullptr; 208 int m_pointsIndex = -1; 209 unsigned int m_clientActiveTexture = 0; 210 211 GLenum mShadeModel = GL_SMOOTH; 212 213 union GLVal { 214 GLfloat floatVal[4]; 215 GLint intVal[4]; 216 GLubyte ubyteVal[4]; 217 GLenum enumVal[4]; 218 }; 219 220 struct GLValTyped { 221 GLenum type; 222 GLVal val; 223 }; 224 225 using TexEnv = std::unordered_map<GLenum, GLValTyped>; 226 using TexUnitEnvs = std::vector<TexEnv>; 227 using TexGens = std::vector<TexEnv>; 228 using MatrixStack = std::vector<glm::mat4>; 229 230 GLenum mCurrMatrixMode = GL_PROJECTION; 231 232 GLValTyped mColor; 233 GLValTyped mNormal; 234 GLVal mMultiTexCoord[kMaxTextureUnits] = {}; 235 236 TexUnitEnvs mTexUnitEnvs; 237 TexGens mTexGens; 238 239 MatrixStack mProjMatrices; 240 MatrixStack mModelviewMatrices; 241 std::vector<MatrixStack> mTextureMatrices; 242 glm::mat4& currMatrix(); 243 MatrixStack& currMatrixStack(); 244 void restoreMatrixStack(const MatrixStack& matrices); 245 246 Material mMaterial; 247 LightModel mLightModel; 248 Light mLights[kMaxLights] = {}; 249 Fog mFog = {}; 250 251 // Core profile stuff 252 CoreProfileEngine* m_coreProfileEngine = nullptr; 253 }; 254 255 #endif 256