• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 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 
17 #ifndef GLES_V2_CONTEXT_H
18 #define GLES_V2_CONTEXT_H
19 
20 #include <GLcommon/GLDispatch.h>
21 #include <GLcommon/GLEScontext.h>
22 #include <GLcommon/ShareGroup.h>
23 
24 #include <memory>
25 
26 // Extra desktop-specific OpenGL enums that we need to properly emulate OpenGL ES.
27 #define GL_FRAMEBUFFER_SRGB 0x8DB9
28 #define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
29 #define GL_DEPTH_CLAMP 0x864F
30 
31 class ProgramData;
32 class TransformFeedbackData;
33 
34 class GLESv2Context : public GLEScontext{
35 public:
36     virtual void init();
37     static void initGlobal(EGLiface* eglIface);
38     GLESv2Context(int maj, int min, GlobalNameSpace* globalNameSpace,
39             android::base::Stream* stream, GlLibrary* glLib);
40     virtual ~GLESv2Context();
41 
42     enum class DrawCallCmd {
43         Elements,
44         ElementsInstanced,
45         RangeElements,
46         Arrays,
47         ArraysInstanced,
48     };
49 
50     void drawWithEmulations(
51         DrawCallCmd cmd,
52         GLenum mode,
53         GLint first,
54         GLsizei count,
55         GLenum type,
56         const GLvoid* indices,
57         GLsizei primcount,
58         GLuint start,
59         GLuint end);
60 
61     void setupArraysPointers(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct);
62     void setVertexAttribDivisor(GLuint bindingindex, GLuint divisor);
63     void setVertexAttribBindingIndex(GLuint attribindex, GLuint bindingindex);
64     void setVertexAttribFormat(GLuint attribindex, GLint size, GLenum type, GLboolean normalized, GLuint reloffset, bool isInt = false);
65     void setBindSampler(GLuint unit, GLuint sampler);
66     int  getMaxCombinedTexUnits() override;
67     int  getMaxTexUnits() override;
68 
69     void setAttribValue(int idx, unsigned int count, const GLfloat* val);
70     // This whole att0 thing is about a incompatibility between GLES and OpenGL.
71     // GLES allows a vertex shader attribute to be in location 0 and have a
72     // current value, while OpenGL is not very clear about this, which results
73     // in each implementation doing something different.
74     void setAttribute0value(float x, float y, float z, float w);
75     bool needAtt0PreDrawValidation();
76     void validateAtt0PreDraw(unsigned int count);
77     void validateAtt0PostDraw(void);
getAtt0(void)78     const float* getAtt0(void) const {return m_attribute0value;}
79     void setUseProgram(GLuint program, const ObjectDataPtr& programData);
80     GLuint getCurrentProgram() const;
81     ProgramData* getUseProgram();
82 
83     virtual void onSave(android::base::Stream* stream) const override;
84     virtual ObjectDataPtr loadObject(NamedObjectType type,
85             ObjectLocalName localName, android::base::Stream* stream) const
86             override;
87 
88     virtual void initDefaultFBO(
89             GLint width, GLint height, GLint colorFormat, GLint depthstencilFormat, GLint multisamples,
90             GLuint* eglSurfaceRBColorId, GLuint* eglSurfaceRBDepthId,
91             GLuint readWidth, GLint readHeight, GLint readColorFormat, GLint readDepthStencilFormat, GLint readMultisamples,
92             GLuint* eglReadSurfaceRBColorId, GLuint* eglReadSurfaceRBDepthId) override;
93 
94     void initEmulatedBuffers();
95     void initEmulatedVAO();
96 
97     unsigned int getTransformFeedbackGlobalName(ObjectLocalName p_localName);
98     ObjectLocalName genTransformFeedbackName(ObjectLocalName p_localName = 0,
99                                              bool genLocal = 0);
100     void bindTransformFeedback(ObjectLocalName p_localName);
101     ObjectLocalName getTransformFeedbackBinding();
102     bool hasBoundTransformFeedback(ObjectLocalName transformFeedback);
103     void deleteTransformFeedback(ObjectLocalName p_localName);
104     GLuint getIndexedBuffer(GLenum target, GLuint index) override;
105     void bindIndexedBuffer(GLenum target,
106                            GLuint index,
107                            GLuint buffer,
108                            GLintptr offset,
109                            GLsizeiptr size,
110                            GLintptr stride = 0,
111                            bool isBindBase = false) override;
112     void bindIndexedBuffer(GLenum target, GLuint index, GLuint buffer) override;
113     void unbindBuffer(GLuint buffer) override;
114 
115     static void setMaxGlesVersion(GLESVersion version);
116 
117     TransformFeedbackData* boundTransformFeedback();
118 
119     void enableArr(GLenum arr, bool enable) override;
120     const GLESpointer* getPointer(GLenum arrType) override;
121 
122 protected:
123     void addVertexArrayObject(GLuint array) override;
124 
125     virtual void postLoadRestoreCtx();
126     bool needConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id);
127 private:
128     void setupArrWithDataSize(GLsizei datasize, const GLvoid* arr,
129                               GLenum arrayType, GLenum dataType,
130                               GLint size, GLsizei stride, GLboolean normalized, int index, bool isInt);
131     void initExtensionString();
132 
133     float m_attribute0value[4] = {};
134     bool m_attribute0valueChanged = true;
135     std::unique_ptr<GLfloat[]> m_att0Array = {};
136     unsigned int m_att0ArrayLength = 0;
137     bool m_att0NeedsDisable = false;
138 
139     ObjectDataPtr m_useProgramData = {};
140     std::unordered_map<GLuint, GLuint> m_bindSampler;
141 
142     std::vector<GLuint> m_emulatedClientVBOs;
143     GLuint m_emulatedClientIBO = 0;
144 
145     NameSpace* m_transformFeedbackNameSpace = nullptr;
146     ObjectLocalName m_bindTransformFeedback = 0;
147     bool m_transformFeedbackDeletePending = false;
148 };
149 
150 #endif
151