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