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_CONTEXT_H
18#define GLES_CONTEXT_H
19
20#include "GLDispatch.h"
21#include "GLESpointer.h"
22#include "objectNameManager.h"
23#include <utils/threads.h>
24#include <string>
25
26typedef std::map<GLenum,GLESpointer*>  ArraysMap;
27
28enum TextureTarget {
29TEXTURE_2D,
30TEXTURE_CUBE_MAP,
31NUM_TEXTURE_TARGETS
32};
33
34typedef struct _textureTargetState {
35    GLuint texture;
36    GLboolean enabled;
37} textureTargetState;
38
39typedef textureTargetState textureUnitState[NUM_TEXTURE_TARGETS];
40
41class Version{
42public:
43    Version();
44    Version(int major,int minor,int release);
45    Version(const char* versionString);
46    Version(const Version& ver);
47    bool operator<(const Version& ver) const;
48    Version& operator=(const Version& ver);
49private:
50    int m_major;
51    int m_minor;
52    int m_release;
53};
54
55struct GLSupport {
56    GLSupport():maxLights(0),maxVertexAttribs(0),maxClipPlane(0),maxTexUnits(0), \
57                maxTexImageUnits(0),maxTexSize(0) , \
58                GL_EXT_TEXTURE_FORMAT_BGRA8888(false), GL_EXT_FRAMEBUFFER_OBJECT(false), \
59                GL_ARB_VERTEX_BLEND(false), GL_ARB_MATRIX_PALETTE(false), \
60                GL_EXT_PACKED_DEPTH_STENCIL(false) , GL_OES_READ_FORMAT(false), \
61                GL_ARB_HALF_FLOAT_PIXEL(false), GL_NV_HALF_FLOAT(false), \
62                GL_ARB_HALF_FLOAT_VERTEX(false),GL_SGIS_GENERATE_MIPMAP(false),
63                GL_ARB_ES2_COMPATIBILITY(false),GL_OES_STANDARD_DERIVATIVES(false) {} ;
64    int  maxLights;
65    int  maxVertexAttribs;
66    int  maxClipPlane;
67    int  maxTexUnits;
68    int  maxTexImageUnits;
69    int  maxTexSize;
70    Version glslVersion;
71    bool GL_EXT_TEXTURE_FORMAT_BGRA8888;
72    bool GL_EXT_FRAMEBUFFER_OBJECT;
73    bool GL_ARB_VERTEX_BLEND;
74    bool GL_ARB_MATRIX_PALETTE;
75    bool GL_EXT_PACKED_DEPTH_STENCIL;
76    bool GL_OES_READ_FORMAT;
77    bool GL_ARB_HALF_FLOAT_PIXEL;
78    bool GL_NV_HALF_FLOAT;
79    bool GL_ARB_HALF_FLOAT_VERTEX;
80    bool GL_SGIS_GENERATE_MIPMAP;
81    bool GL_ARB_ES2_COMPATIBILITY;
82    bool GL_OES_STANDARD_DERIVATIVES;
83
84};
85
86struct ArrayData{
87    ArrayData():data(NULL),
88                type(0),
89                stride(0),
90                allocated(false){};
91
92    void*        data;
93    GLenum       type;
94    unsigned int stride;
95    bool         allocated;
96};
97
98class GLESConversionArrays
99{
100public:
101    GLESConversionArrays():m_current(0){};
102    void setArr(void* data,unsigned int stride,GLenum type);
103    void allocArr(unsigned int size,GLenum type);
104    ArrayData& operator[](int i);
105    void* getCurrentData();
106    ArrayData& getCurrentArray();
107    unsigned int getCurrentIndex();
108    void operator++();
109
110    ~GLESConversionArrays();
111private:
112    std::map<GLenum,ArrayData> m_arrays;
113    unsigned int m_current;
114};
115
116class GLEScontext{
117public:
118    virtual void init();
119    GLEScontext();
120    GLenum getGLerror();
121    void setGLerror(GLenum err);
122    void setShareGroup(ShareGroupPtr grp){m_shareGroup = grp;};
123    ShareGroupPtr shareGroup() const { return m_shareGroup; }
124    virtual void setActiveTexture(GLenum tex);
125    unsigned int getBindedTexture(GLenum target);
126    unsigned int getBindedTexture(GLenum unit,GLenum target);
127    void setBindedTexture(GLenum target,unsigned int tex);
128    bool isTextureUnitEnabled(GLenum unit);
129    void setTextureEnabled(GLenum target, GLenum enable);
130    ObjectLocalName getDefaultTextureName(GLenum target);
131    bool isInitialized() { return m_initialized; };
132    void setUnpackAlignment(GLint param){ m_unpackAlignment = param; };
133    GLint getUnpackAlignment(){ return m_unpackAlignment; };
134
135    bool  isArrEnabled(GLenum);
136    void  enableArr(GLenum arr,bool enable);
137    const GLvoid* setPointer(GLenum arrType,GLint size,GLenum type,GLsizei stride,const GLvoid* data,bool normalize = false);
138    virtual const GLESpointer* getPointer(GLenum arrType);
139    virtual void setupArraysPointers(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct) = 0;
140    void bindBuffer(GLenum target,GLuint buffer);
141    void unbindBuffer(GLuint buffer);
142    bool isBuffer(GLuint buffer);
143    bool isBindedBuffer(GLenum target);
144    GLvoid* getBindedBuffer(GLenum target);
145    void getBufferSize(GLenum target,GLint* param);
146    void getBufferUsage(GLenum target,GLint* param);
147    bool setBufferData(GLenum target,GLsizeiptr size,const GLvoid* data,GLenum usage);
148    bool setBufferSubData(GLenum target,GLintptr offset,GLsizeiptr size,const GLvoid* data);
149    const char * getExtensionString();
150    const char * getVendorString() const;
151    const char * getRendererString() const;
152    const char * getVersionString() const;
153    void getGlobalLock();
154    void releaseGlobalLock();
155    virtual GLSupport*  getCaps(){return &s_glSupport;};
156    virtual ~GLEScontext();
157    virtual int getMaxTexUnits() = 0;
158    virtual void drawValidate(void);
159
160    void setRenderbufferBinding(GLuint rb) { m_renderbuffer = rb; }
161    GLuint getRenderbufferBinding() const { return m_renderbuffer; }
162    void setFramebufferBinding(GLuint fb) { m_framebuffer = fb; }
163    GLuint getFramebufferBinding() const { return m_framebuffer; }
164
165    static GLDispatch& dispatcher(){return s_glDispatch;};
166
167    static int getMaxLights(){return s_glSupport.maxLights;}
168    static int getMaxClipPlanes(){return s_glSupport.maxClipPlane;}
169    static int getMaxTexSize(){return s_glSupport.maxTexSize;}
170    static Version glslVersion(){return s_glSupport.glslVersion;}
171    static bool isAutoMipmapSupported(){return s_glSupport.GL_SGIS_GENERATE_MIPMAP;}
172    static TextureTarget GLTextureTargetToLocal(GLenum target);
173    static int findMaxIndex(GLsizei count,GLenum type,const GLvoid* indices);
174
175    virtual bool glGetIntegerv(GLenum pname, GLint *params);
176    virtual bool glGetBooleanv(GLenum pname, GLboolean *params);
177    virtual bool glGetFloatv(GLenum pname, GLfloat *params);
178    virtual bool glGetFixedv(GLenum pname, GLfixed *params);
179
180protected:
181    static void buildStrings(const char* baseVendor, const char* baseRenderer, const char* baseVersion, const char* version);
182    virtual bool needConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) = 0;
183    void convertDirect(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum array_id,GLESpointer* p);
184    void convertDirectVBO(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum array_id,GLESpointer* p);
185    void convertIndirect(GLESConversionArrays& fArrs,GLsizei count,GLenum type,const GLvoid* indices,GLenum array_id,GLESpointer* p);
186    void convertIndirectVBO(GLESConversionArrays& fArrs,GLsizei count,GLenum indices_type,const GLvoid* indices,GLenum array_id,GLESpointer* p);
187    void initCapsLocked(const GLubyte * extensionString);
188    virtual void initExtensionString() =0;
189
190    static android::Mutex s_lock;
191    static GLDispatch     s_glDispatch;
192    bool                  m_initialized;
193    unsigned int          m_activeTexture;
194    GLint                 m_unpackAlignment;
195    ArraysMap             m_map;
196    static std::string*   s_glExtensions;
197    static GLSupport      s_glSupport;
198
199private:
200
201    virtual void setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride, GLboolean normalized, int pointsIndex = -1) = 0 ;
202    GLuint getBuffer(GLenum target);
203
204    ShareGroupPtr         m_shareGroup;
205    GLenum                m_glError;
206    textureUnitState*     m_texState;
207    unsigned int          m_arrayBuffer;
208    unsigned int          m_elementBuffer;
209    GLuint                m_renderbuffer;
210    GLuint                m_framebuffer;
211
212    static std::string    s_glVendor;
213    static std::string    s_glRenderer;
214    static std::string    s_glVersion;
215};
216
217#endif
218
219