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