OSDN Git Service

Make Blitter part of Renderer.
[android-x86/external-swiftshader.git] / src / OpenGL / libGLESv2 / Context.h
index 9b3a30d..bbe6ddd 100644 (file)
-// SwiftShader Software Renderer\r
-//\r
-// Copyright(c) 2005-2013 TransGaming Inc.\r
-//\r
-// All rights reserved. No part of this software may be copied, distributed, transmitted,\r
-// transcribed, stored in a retrieval system, translated into any human or computer\r
-// language by any means, or disclosed to third parties without the explicit written\r
-// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express\r
-// or implied, including but not limited to any patent rights, are granted to you.\r
-//\r
-\r
-// Context.h: Defines the Context class, managing all GL state and performing\r
-// rendering operations. It is the GLES2 specific implementation of EGLContext.\r
-\r
-#ifndef LIBGLESV2_CONTEXT_H_\r
-#define LIBGLESV2_CONTEXT_H_\r
-\r
-#include "libEGL/Context.hpp"\r
-#include "ResourceManager.h"\r
-#include "common/NameSpace.hpp"\r
-#include "common/Object.hpp"\r
-#include "common/Image.hpp"\r
-#include "Renderer/Sampler.hpp"\r
-#include "TransformFeedback.h"\r
-\r
-#include <GLES2/gl2.h>\r
-#include <GLES2/gl2ext.h>\r
-#include <GLES3/gl3.h>\r
-#include <EGL/egl.h>\r
-\r
-#include <map>\r
-#include <string>\r
-\r
-namespace egl\r
-{\r
-class Display;\r
-class Surface;\r
-class Config;\r
-}\r
-\r
-namespace es2\r
-{\r
-struct TranslatedAttribute;\r
-struct TranslatedIndexData;\r
-\r
-class Device;\r
-class Buffer;\r
-class Shader;\r
-class Program;\r
-class Texture;\r
-class Texture2D;\r
-class Texture3D;\r
-class TextureCubeMap;\r
-class TextureExternal;\r
-class Framebuffer;\r
-class Renderbuffer;\r
-class RenderbufferStorage;\r
-class Colorbuffer;\r
-class Depthbuffer;\r
-class StreamingIndexBuffer;\r
-class Stencilbuffer;\r
-class DepthStencilbuffer;\r
-class VertexDataManager;\r
-class IndexDataManager;\r
-class Fence;\r
-class Query;\r
-class Sampler;\r
-class VertexArray;\r
-\r
-enum\r
-{\r
-    MAX_VERTEX_ATTRIBS = 16,\r
-       MAX_UNIFORM_VECTORS = 256,   // Device limit\r
-    MAX_VERTEX_UNIFORM_VECTORS = VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange\r
-    MAX_VARYING_VECTORS = 10,\r
-    MAX_TEXTURE_IMAGE_UNITS = TEXTURE_IMAGE_UNITS,\r
-    MAX_VERTEX_TEXTURE_IMAGE_UNITS = VERTEX_TEXTURE_IMAGE_UNITS,\r
-    MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,\r
-    MAX_FRAGMENT_UNIFORM_VECTORS = FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange\r
-    MAX_DRAW_BUFFERS = 1,\r
-       MAX_ELEMENT_INDEX = 0x7FFFFFFF,\r
-       MAX_ELEMENTS_INDICES = 0x7FFFFFFF,\r
-       MAX_ELEMENTS_VERTICES = 0x7FFFFFFF\r
-};\r
-\r
-const GLenum compressedTextureFormats[] =\r
-{\r
-       GL_ETC1_RGB8_OES,\r
-#if (S3TC_SUPPORT)\r
-       GL_COMPRESSED_RGB_S3TC_DXT1_EXT,\r
-       GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,\r
-       GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE,\r
-       GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE,\r
-#endif\r
-#if (GL_ES_VERSION_3_0)\r
-       GL_COMPRESSED_R11_EAC,\r
-       GL_COMPRESSED_SIGNED_R11_EAC,\r
-       GL_COMPRESSED_RG11_EAC,\r
-       GL_COMPRESSED_SIGNED_RG11_EAC,\r
-       GL_COMPRESSED_RGB8_ETC2,\r
-       GL_COMPRESSED_SRGB8_ETC2,\r
-       GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,\r
-       GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,\r
-       GL_COMPRESSED_RGBA8_ETC2_EAC,\r
-       GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,\r
-#endif\r
-};\r
-\r
-const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);\r
-\r
-const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f;\r
-const float ALIASED_LINE_WIDTH_RANGE_MAX = 1.0f;\r
-const float ALIASED_POINT_SIZE_RANGE_MIN = 0.125f;\r
-const float ALIASED_POINT_SIZE_RANGE_MAX = 8192.0f;\r
-const float MAX_TEXTURE_MAX_ANISOTROPY = 16.0f;\r
-\r
-enum QueryType\r
-{\r
-    QUERY_ANY_SAMPLES_PASSED,\r
-    QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,\r
-    QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,\r
-\r
-    QUERY_TYPE_COUNT\r
-};\r
-\r
-struct Color\r
-{\r
-    float red;\r
-    float green;\r
-    float blue;\r
-    float alpha;\r
-};\r
-\r
-// Helper structure describing a single vertex attribute\r
-class VertexAttribute\r
-{\r
-  public:\r
-    VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mDivisor(0), mPointer(NULL), mArrayEnabled(false)\r
-    {\r
-        mCurrentValue[0].f = 0.0f;\r
-        mCurrentValue[1].f = 0.0f;\r
-        mCurrentValue[2].f = 0.0f;\r
-        mCurrentValue[3].f = 1.0f;\r
-               mCurrentValueType = ValueUnion::FloatType;\r
-    }\r
-\r
-    int typeSize() const\r
-    {\r
-        switch (mType)\r
-        {\r
-        case GL_BYTE:           return mSize * sizeof(GLbyte);\r
-        case GL_UNSIGNED_BYTE:  return mSize * sizeof(GLubyte);\r
-        case GL_SHORT:          return mSize * sizeof(GLshort);\r
-        case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort);\r
-        case GL_FIXED:          return mSize * sizeof(GLfixed);\r
-        case GL_FLOAT:          return mSize * sizeof(GLfloat);\r
-        default: UNREACHABLE(); return mSize * sizeof(GLfloat);\r
-        }\r
-    }\r
-\r
-    GLsizei stride() const\r
-    {\r
-        return mStride ? mStride : typeSize();\r
-    }\r
-\r
-       inline float getCurrentValue(int i) const\r
-       {\r
-               switch(mCurrentValueType)\r
-               {\r
-               case ValueUnion::FloatType:     return mCurrentValue[i].f;\r
-               case ValueUnion::IntType:       return static_cast<float>(mCurrentValue[i].i);\r
-               case ValueUnion::UIntType:      return static_cast<float>(mCurrentValue[i].ui);\r
-               default: UNREACHABLE();         return mCurrentValue[i].f;\r
-               }\r
-       }\r
-\r
-       inline GLint getCurrentValueI(int i) const\r
-       {\r
-               switch(mCurrentValueType)\r
-               {\r
-               case ValueUnion::FloatType:     return static_cast<GLint>(mCurrentValue[i].f);\r
-               case ValueUnion::IntType:       return mCurrentValue[i].i;\r
-               case ValueUnion::UIntType:      return static_cast<GLint>(mCurrentValue[i].ui);\r
-               default: UNREACHABLE();         return mCurrentValue[i].i;\r
-               }\r
-       }\r
-\r
-       inline GLuint getCurrentValueUI(int i) const\r
-       {\r
-               switch(mCurrentValueType)\r
-               {\r
-               case ValueUnion::FloatType:     return static_cast<GLuint>(mCurrentValue[i].f);\r
-               case ValueUnion::IntType:       return static_cast<GLuint>(mCurrentValue[i].i);\r
-               case ValueUnion::UIntType:      return mCurrentValue[i].ui;\r
-               default: UNREACHABLE();         return mCurrentValue[i].ui;\r
-               }\r
-       }\r
-\r
-       inline void setCurrentValue(const GLfloat *values)\r
-       {\r
-               mCurrentValue[0].f = values[0];\r
-               mCurrentValue[1].f = values[1];\r
-               mCurrentValue[2].f = values[2];\r
-               mCurrentValue[3].f = values[3];\r
-               mCurrentValueType = ValueUnion::FloatType;\r
-       }\r
-\r
-       inline void setCurrentValue(const GLint *values)\r
-       {\r
-               mCurrentValue[0].i = values[0];\r
-               mCurrentValue[1].i = values[1];\r
-               mCurrentValue[2].i = values[2];\r
-               mCurrentValue[3].i = values[3];\r
-               mCurrentValueType = ValueUnion::IntType;\r
-       }\r
-\r
-       inline void setCurrentValue(const GLuint *values)\r
-       {\r
-               mCurrentValue[0].ui = values[0];\r
-               mCurrentValue[1].ui = values[1];\r
-               mCurrentValue[2].ui = values[2];\r
-               mCurrentValue[3].ui = values[3];\r
-               mCurrentValueType = ValueUnion::UIntType;\r
-       }\r
-\r
-    // From glVertexAttribPointer\r
-    GLenum mType;\r
-    GLint mSize;\r
-    bool mNormalized;\r
-    GLsizei mStride;   // 0 means natural stride\r
-    GLuint mDivisor;   // From glVertexAttribDivisor\r
-\r
-    union\r
-    {\r
-        const void *mPointer;\r
-        intptr_t mOffset;\r
-    };\r
-\r
-    gl::BindingPointer<Buffer> mBoundBuffer;   // Captured when glVertexAttribPointer is called.\r
-\r
-    bool mArrayEnabled;   // From glEnable/DisableVertexAttribArray\r
-private:\r
-       union ValueUnion\r
-       {\r
-               enum Type { FloatType, IntType, UIntType };\r
-\r
-               float f;\r
-               GLint i;\r
-               GLuint ui;\r
-       };\r
-       ValueUnion mCurrentValue[4];   // From glVertexAttrib\r
-       ValueUnion::Type mCurrentValueType;\r
-};\r
-\r
-typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];\r
-\r
-// Helper structure to store all raw state\r
-struct State\r
-{\r
-    Color colorClearValue;\r
-    GLclampf depthClearValue;\r
-    int stencilClearValue;\r
-\r
-    bool cullFace;\r
-    GLenum cullMode;\r
-    GLenum frontFace;\r
-    bool depthTest;\r
-    GLenum depthFunc;\r
-    bool blend;\r
-    GLenum sourceBlendRGB;\r
-    GLenum destBlendRGB;\r
-    GLenum sourceBlendAlpha;\r
-    GLenum destBlendAlpha;\r
-    GLenum blendEquationRGB;\r
-    GLenum blendEquationAlpha;\r
-    Color blendColor;\r
-    bool stencilTest;\r
-    GLenum stencilFunc;\r
-    GLint stencilRef;\r
-    GLuint stencilMask;\r
-    GLenum stencilFail;\r
-    GLenum stencilPassDepthFail;\r
-    GLenum stencilPassDepthPass;\r
-    GLuint stencilWritemask;\r
-    GLenum stencilBackFunc;\r
-    GLint stencilBackRef;\r
-    GLuint stencilBackMask;\r
-    GLenum stencilBackFail;\r
-    GLenum stencilBackPassDepthFail;\r
-    GLenum stencilBackPassDepthPass;\r
-    GLuint stencilBackWritemask;\r
-    bool polygonOffsetFill;\r
-    GLfloat polygonOffsetFactor;\r
-    GLfloat polygonOffsetUnits;\r
-    bool sampleAlphaToCoverage;\r
-    bool sampleCoverage;\r
-    GLclampf sampleCoverageValue;\r
-    bool sampleCoverageInvert;\r
-    bool scissorTest;\r
-    bool dither;\r
-    bool primitiveRestartFixedIndex;\r
-    bool rasterizerDiscard;\r
-\r
-    GLfloat lineWidth;\r
-\r
-    GLenum generateMipmapHint;\r
-    GLenum fragmentShaderDerivativeHint;\r
-\r
-    GLint viewportX;\r
-    GLint viewportY;\r
-    GLsizei viewportWidth;\r
-    GLsizei viewportHeight;\r
-    float zNear;\r
-    float zFar;\r
-\r
-    GLint scissorX;\r
-    GLint scissorY;\r
-    GLsizei scissorWidth;\r
-    GLsizei scissorHeight;\r
-\r
-    bool colorMaskRed;\r
-    bool colorMaskGreen;\r
-    bool colorMaskBlue;\r
-    bool colorMaskAlpha;\r
-    bool depthMask;\r
-\r
-    unsigned int activeSampler;   // Active texture unit selector - GL_TEXTURE0\r
-    gl::BindingPointer<Buffer> arrayBuffer;\r
-       gl::BindingPointer<Buffer> copyReadBuffer;\r
-       gl::BindingPointer<Buffer> copyWriteBuffer;\r
-       gl::BindingPointer<Buffer> pixelPackBuffer;\r
-       gl::BindingPointer<Buffer> pixelUnpackBuffer;\r
-       gl::BindingPointer<Buffer> uniformBuffer;\r
-\r
-    GLuint readFramebuffer;\r
-    GLuint drawFramebuffer;\r
-    GLuint readFramebufferColorIndex;\r
-    GLuint drawFramebufferColorIndices[MAX_COLOR_ATTACHMENTS];\r
-    gl::BindingPointer<Renderbuffer> renderbuffer;\r
-    GLuint currentProgram;\r
-       GLuint vertexArray;\r
-       GLuint transformFeedback;\r
-       gl::BindingPointer<Sampler> sampler[MAX_COMBINED_TEXTURE_IMAGE_UNITS];\r
-\r
-    VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS];\r
-    gl::BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][MAX_COMBINED_TEXTURE_IMAGE_UNITS];\r
-       gl::BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT];\r
-\r
-    GLint unpackAlignment;\r
-    GLint packAlignment;\r
-};\r
-\r
-class Context : public egl::Context\r
-{\r
-public:\r
-    Context(const egl::Config *config, const Context *shareContext, EGLint clientVersion);\r
-\r
-       virtual void makeCurrent(egl::Surface *surface);\r
-       virtual EGLint getClientVersion() const;\r
-\r
-    void markAllStateDirty();\r
-\r
-    // State manipulation\r
-    void setClearColor(float red, float green, float blue, float alpha);\r
-    void setClearDepth(float depth);\r
-    void setClearStencil(int stencil);\r
-\r
-    void setCullFace(bool enabled);\r
-    bool isCullFaceEnabled() const;\r
-    void setCullMode(GLenum mode);\r
-    void setFrontFace(GLenum front);\r
-\r
-    void setDepthTest(bool enabled);\r
-    bool isDepthTestEnabled() const;\r
-    void setDepthFunc(GLenum depthFunc);\r
-    void setDepthRange(float zNear, float zFar);\r
-    \r
-    void setBlend(bool enabled);\r
-    bool isBlendEnabled() const;\r
-    void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);\r
-    void setBlendColor(float red, float green, float blue, float alpha);\r
-    void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);\r
-\r
-    void setStencilTest(bool enabled);\r
-    bool isStencilTestEnabled() const;\r
-    void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);\r
-    void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);\r
-    void setStencilWritemask(GLuint stencilWritemask);\r
-    void setStencilBackWritemask(GLuint stencilBackWritemask);\r
-    void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);\r
-    void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);\r
-\r
-    void setPolygonOffsetFill(bool enabled);\r
-    bool isPolygonOffsetFillEnabled() const;\r
-    void setPolygonOffsetParams(GLfloat factor, GLfloat units);\r
-\r
-    void setSampleAlphaToCoverage(bool enabled);\r
-    bool isSampleAlphaToCoverageEnabled() const;\r
-    void setSampleCoverage(bool enabled);\r
-    bool isSampleCoverageEnabled() const;\r
-    void setSampleCoverageParams(GLclampf value, bool invert);\r
-\r
-    void setDither(bool enabled);\r
-    bool isDitherEnabled() const;\r
-\r
-    void setPrimitiveRestartFixedIndex(bool enabled);\r
-    bool isPrimitiveRestartFixedIndexEnabled() const;\r
-\r
-    void setRasterizerDiscard(bool enabled);\r
-    bool isRasterizerDiscardEnabled() const;\r
-\r
-    void setLineWidth(GLfloat width);\r
-\r
-    void setGenerateMipmapHint(GLenum hint);\r
-    void setFragmentShaderDerivativeHint(GLenum hint);\r
-\r
-    void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);\r
-\r
-       void setScissorTest(bool enabled);\r
-    bool isScissorTestEnabled() const;\r
-    void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);\r
-\r
-    void setColorMask(bool red, bool green, bool blue, bool alpha);\r
-    void setDepthMask(bool mask);\r
-\r
-    void setActiveSampler(unsigned int active);\r
-\r
-    GLuint getReadFramebufferName() const;\r
-    GLuint getDrawFramebufferName() const;\r
-    GLuint getRenderbufferName() const;\r
-\r
-       void setReadFramebufferColorIndex(GLuint index);\r
-       void setDrawFramebufferColorIndices(GLsizei n, const GLenum *bufs);\r
-       GLuint getReadFramebufferColorIndex() const;\r
-\r
-       GLuint getActiveQuery(GLenum target) const;\r
-\r
-    GLuint getArrayBufferName() const;\r
-       GLuint getElementArrayBufferName() const;\r
-\r
-    void setEnableVertexAttribArray(unsigned int attribNum, bool enabled);\r
-    void setVertexAttribDivisor(unsigned int attribNum, GLuint divisor);\r
-    const VertexAttribute &getVertexAttribState(unsigned int attribNum) const;\r
-    void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,\r
-                              bool normalized, GLsizei stride, const void *pointer);\r
-    const void *getVertexAttribPointer(unsigned int attribNum) const;\r
-\r
-       const VertexAttributeArray &getVertexArrayAttributes();\r
-       // Context attribute current values can be queried independently from VAO current values\r
-       const VertexAttributeArray &getCurrentVertexAttributes();\r
-\r
-    void setUnpackAlignment(GLint alignment);\r
-    GLint getUnpackAlignment() const;\r
-\r
-    void setPackAlignment(GLint alignment);\r
-    GLint getPackAlignment() const;\r
-\r
-    // These create  and destroy methods are merely pass-throughs to \r
-    // ResourceManager, which owns these object types\r
-    GLuint createBuffer();\r
-    GLuint createShader(GLenum type);\r
-    GLuint createProgram();\r
-    GLuint createTexture();\r
-    GLuint createRenderbuffer();\r
-\r
-    void deleteBuffer(GLuint buffer);\r
-    void deleteShader(GLuint shader);\r
-    void deleteProgram(GLuint program);\r
-    void deleteTexture(GLuint texture);\r
-    void deleteRenderbuffer(GLuint renderbuffer);\r
-\r
-    // Framebuffers are owned by the Context, so these methods do not pass through\r
-    GLuint createFramebuffer();\r
-    void deleteFramebuffer(GLuint framebuffer);\r
-\r
-    // Fences are owned by the Context\r
-    GLuint createFence();\r
-    void deleteFence(GLuint fence);\r
-\r
-       // Queries are owned by the Context\r
-    GLuint createQuery();\r
-    void deleteQuery(GLuint query);\r
-\r
-       // Vertex arrays are owned by the Context\r
-       GLuint createVertexArray();\r
-       void deleteVertexArray(GLuint array);\r
-\r
-       // Transform feedbacks are owned by the Context\r
-       GLuint createTransformFeedback();\r
-       void deleteTransformFeedback(GLuint transformFeedback);\r
-\r
-       // Samplers are owned by the Context\r
-       GLuint createSampler();\r
-       void deleteSampler(GLuint sampler);\r
-\r
-    void bindArrayBuffer(GLuint buffer);\r
-    void bindElementArrayBuffer(GLuint buffer);\r
-       void bindCopyReadBuffer(GLuint buffer);\r
-       void bindCopyWriteBuffer(GLuint buffer);\r
-       void bindPixelPackBuffer(GLuint buffer);\r
-       void bindPixelUnpackBuffer(GLuint buffer);\r
-       void bindTransformFeedbackBuffer(GLuint buffer);\r
-       void bindUniformBuffer(GLuint buffer);\r
-    void bindTexture2D(GLuint texture);\r
-    void bindTextureCubeMap(GLuint texture);\r
-    void bindTextureExternal(GLuint texture);\r
-       void bindTexture3D(GLuint texture);\r
-    void bindReadFramebuffer(GLuint framebuffer);\r
-    void bindDrawFramebuffer(GLuint framebuffer);\r
-    void bindRenderbuffer(GLuint renderbuffer);\r
-       bool bindVertexArray(GLuint array);\r
-       bool bindTransformFeedback(GLuint transformFeedback);\r
-       bool bindSampler(GLuint unit, GLuint sampler);\r
-    void useProgram(GLuint program);\r
-\r
-       void beginQuery(GLenum target, GLuint query);\r
-    void endQuery(GLenum target);\r
-\r
-    void setFramebufferZero(Framebuffer *framebuffer);\r
-\r
-    void setRenderbufferStorage(RenderbufferStorage *renderbuffer);\r
-\r
-    void setVertexAttrib(GLuint index, const GLfloat *values);\r
-    void setVertexAttrib(GLuint index, const GLint *values);\r
-    void setVertexAttrib(GLuint index, const GLuint *values);\r
-\r
-       Buffer *getBuffer(GLuint handle) const;\r
-       Fence *getFence(GLuint handle) const;\r
-       Shader *getShader(GLuint handle) const;\r
-       Program *getProgram(GLuint handle) const;\r
-       virtual Texture *getTexture(GLuint handle) const;\r
-       Framebuffer *getFramebuffer(GLuint handle) const;\r
-       virtual Renderbuffer *getRenderbuffer(GLuint handle) const;\r
-       Query *getQuery(GLuint handle) const;\r
-       VertexArray *getVertexArray(GLuint array) const;\r
-       VertexArray *getCurrentVertexArray() const;\r
-       TransformFeedback *getTransformFeedback(GLuint transformFeedback) const;\r
-       TransformFeedback *getTransformFeedback() const;\r
-       Sampler *getSampler(GLuint sampler) const;\r
-\r
-       Buffer *getArrayBuffer() const;\r
-       Buffer *getElementArrayBuffer() const;\r
-       Buffer *getCopyReadBuffer() const;\r
-       Buffer *getCopyWriteBuffer() const;\r
-       Buffer *getPixelPackBuffer() const;\r
-       Buffer *getPixelUnpackBuffer() const;\r
-       Buffer *getUniformBuffer() const;\r
-       bool getBuffer(GLenum target, es2::Buffer **buffer) const;\r
-       Program *getCurrentProgram() const;\r
-       Texture2D *getTexture2D() const;\r
-       Texture3D *getTexture3D() const;\r
-       TextureCubeMap *getTextureCubeMap() const;\r
-       TextureExternal *getTextureExternal() const;\r
-       Texture *getSamplerTexture(unsigned int sampler, TextureType type) const;\r
-       Framebuffer *getReadFramebuffer() const;\r
-       Framebuffer *getDrawFramebuffer() const;\r
-\r
-       bool getFloatv(GLenum pname, GLfloat *params) const;\r
-       template<typename T> bool getIntegerv(GLenum pname, T *params) const;\r
-       bool getBooleanv(GLenum pname, GLboolean *params) const;\r
-       template<typename T> bool getTransformFeedbackiv(GLuint xfb, GLenum pname, T *param) const;\r
-\r
-       bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const;\r
-\r
-       bool hasZeroDivisor() const;\r
-\r
-    void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);\r
-    void clear(GLbitfield mask);\r
-    void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount = 1);\r
-    void drawElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount = 1);\r
-    void finish();\r
-    void flush();\r
-\r
-    void recordInvalidEnum();\r
-    void recordInvalidValue();\r
-    void recordInvalidOperation();\r
-    void recordOutOfMemory();\r
-    void recordInvalidFramebufferOperation();\r
-\r
-    GLenum getError();\r
-\r
-    static int getSupportedMultiSampleDepth(sw::Format format, int requested);\r
-    \r
-    void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, \r
-                         GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,\r
-                         GLbitfield mask);\r
-\r
-       virtual void bindTexImage(egl::Surface *surface);\r
-       virtual EGLenum validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel);\r
-       virtual egl::Image *createSharedImage(EGLenum target, GLuint name, GLuint textureLevel);\r
-\r
-       Device *getDevice();\r
-\r
-       const GLubyte* getExtensions(GLuint index, GLuint* numExt = nullptr) const;\r
-\r
-private:\r
-       virtual ~Context();\r
-\r
-    bool applyRenderTarget();\r
-    void applyState(GLenum drawMode);\r
-       GLenum applyVertexBuffer(GLint base, GLint first, GLsizei count, GLsizei instanceId);\r
-    GLenum applyIndexBuffer(const void *indices, GLuint start, GLuint end, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);\r
-    void applyShaders();\r
-    void applyTextures();\r
-    void applyTextures(sw::SamplerType type);\r
-       void applyTexture(sw::SamplerType type, int sampler, Texture *texture);\r
-\r
-    void detachBuffer(GLuint buffer);\r
-    void detachTexture(GLuint texture);\r
-    void detachFramebuffer(GLuint framebuffer);\r
-    void detachRenderbuffer(GLuint renderbuffer);\r
-\r
-    bool cullSkipsDraw(GLenum drawMode);\r
-    bool isTriangleMode(GLenum drawMode);\r
-\r
-       Query *createQuery(GLuint handle, GLenum type);\r
-\r
-       const EGLint clientVersion;\r
-    const egl::Config *const mConfig;\r
-\r
-    State mState;\r
-\r
-       gl::BindingPointer<Texture2D> mTexture2DZero;\r
-       gl::BindingPointer<Texture3D> mTexture3DZero;\r
-       gl::BindingPointer<TextureCubeMap> mTextureCubeMapZero;\r
-    gl::BindingPointer<TextureExternal> mTextureExternalZero;\r
-\r
-    typedef std::map<GLint, Framebuffer*> FramebufferMap;\r
-    FramebufferMap mFramebufferMap;\r
-    gl::NameSpace mFramebufferNameSpace;\r
-\r
-    typedef std::map<GLint, Fence*> FenceMap;\r
-    FenceMap mFenceMap;\r
-    gl::NameSpace mFenceNameSpace;\r
-\r
-       typedef std::map<GLint, Query*> QueryMap;\r
-    QueryMap mQueryMap;\r
-    gl::NameSpace mQueryNameSpace;\r
-\r
-       typedef std::map<GLint, VertexArray*> VertexArrayMap;\r
-       VertexArrayMap mVertexArrayMap;\r
-       gl::NameSpace mVertexArrayNameSpace;\r
-\r
-       typedef std::map<GLint, TransformFeedback*> TransformFeedbackMap;\r
-       TransformFeedbackMap mTransformFeedbackMap;\r
-       gl::NameSpace mTransformFeedbackNameSpace;\r
-\r
-       typedef std::map<GLint, Sampler*> SamplerMap;\r
-       SamplerMap mSamplerMap;\r
-       gl::NameSpace mSamplerNameSpace;\r
-\r
-    VertexDataManager *mVertexDataManager;\r
-    IndexDataManager *mIndexDataManager;\r
-\r
-    // Recorded errors\r
-    bool mInvalidEnum;\r
-    bool mInvalidValue;\r
-    bool mInvalidOperation;\r
-    bool mOutOfMemory;\r
-    bool mInvalidFramebufferOperation;\r
-\r
-    bool mHasBeenCurrent;\r
-\r
-    unsigned int mAppliedProgramSerial;\r
-    \r
-    // state caching flags\r
-    bool mDepthStateDirty;\r
-    bool mMaskStateDirty;\r
-    bool mPixelPackingStateDirty;\r
-    bool mBlendStateDirty;\r
-    bool mStencilStateDirty;\r
-    bool mPolygonOffsetStateDirty;\r
-    bool mSampleStateDirty;\r
-    bool mFrontFaceDirty;\r
-    bool mDitherStateDirty;\r
-\r
-       Device *device;\r
-    ResourceManager *mResourceManager;\r
-};\r
-}\r
-\r
-#endif   // INCLUDE_CONTEXT_H_\r
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Context.h: Defines the Context class, managing all GL state and performing
+// rendering operations. It is the GLES2 specific implementation of EGLContext.
+
+#ifndef LIBGLESV2_CONTEXT_H_
+#define LIBGLESV2_CONTEXT_H_
+
+#include "ResourceManager.h"
+#include "Buffer.h"
+#include "libEGL/Context.hpp"
+#include "common/NameSpace.hpp"
+#include "common/Object.hpp"
+#include "common/Image.hpp"
+#include "Renderer/Sampler.hpp"
+
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+#include <GLES3/gl3.h>
+#include <EGL/egl.h>
+
+#include <map>
+#include <string>
+
+namespace egl
+{
+class Display;
+class Config;
+}
+
+namespace es2
+{
+struct TranslatedAttribute;
+struct TranslatedIndexData;
+
+class Device;
+class Shader;
+class Program;
+class Texture;
+class Texture2D;
+class Texture3D;
+class Texture2DArray;
+class TextureCubeMap;
+class TextureExternal;
+class Framebuffer;
+class Renderbuffer;
+class RenderbufferStorage;
+class Colorbuffer;
+class Depthbuffer;
+class StreamingIndexBuffer;
+class Stencilbuffer;
+class DepthStencilbuffer;
+class VertexDataManager;
+class IndexDataManager;
+class Fence;
+class FenceSync;
+class Query;
+class Sampler;
+class VertexArray;
+class TransformFeedback;
+
+enum
+{
+       MAX_VERTEX_ATTRIBS = sw::MAX_VERTEX_INPUTS,
+       MAX_UNIFORM_VECTORS = 256,   // Device limit
+       MAX_VERTEX_UNIFORM_VECTORS = sw::VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange
+       MAX_VARYING_VECTORS = MIN(sw::MAX_FRAGMENT_INPUTS, sw::MAX_VERTEX_OUTPUTS),
+       MAX_TEXTURE_IMAGE_UNITS = sw::TEXTURE_IMAGE_UNITS,
+       MAX_VERTEX_TEXTURE_IMAGE_UNITS = sw::VERTEX_TEXTURE_IMAGE_UNITS,
+       MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,
+       MAX_FRAGMENT_UNIFORM_VECTORS = sw::FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange
+       MAX_ELEMENT_INDEX = 0x7FFFFFFF,
+       MAX_ELEMENTS_INDICES = 0x7FFFFFFF,
+       MAX_ELEMENTS_VERTICES = 0x7FFFFFFF,
+       MAX_VERTEX_OUTPUT_VECTORS = 16,
+       MAX_FRAGMENT_INPUT_VECTORS = 15,
+       MIN_PROGRAM_TEXEL_OFFSET = sw::MIN_PROGRAM_TEXEL_OFFSET,
+       MAX_PROGRAM_TEXEL_OFFSET = sw::MAX_PROGRAM_TEXEL_OFFSET,
+       MAX_DRAW_BUFFERS = sw::RENDERTARGETS,
+       MAX_COLOR_ATTACHMENTS = MAX(MAX_DRAW_BUFFERS, 8),
+       MAX_FRAGMENT_UNIFORM_BLOCKS = sw::MAX_FRAGMENT_UNIFORM_BLOCKS,
+       MAX_VERTEX_UNIFORM_BLOCKS = sw::MAX_VERTEX_UNIFORM_BLOCKS,
+       MAX_FRAGMENT_UNIFORM_COMPONENTS = sw::FRAGMENT_UNIFORM_VECTORS * 4,
+       MAX_VERTEX_UNIFORM_COMPONENTS = sw::VERTEX_UNIFORM_VECTORS * 4,
+       MAX_UNIFORM_BLOCK_SIZE = sw::MAX_UNIFORM_BLOCK_SIZE,
+       MAX_FRAGMENT_UNIFORM_BLOCKS_COMPONENTS = sw::MAX_FRAGMENT_UNIFORM_BLOCKS * MAX_UNIFORM_BLOCK_SIZE / 4,
+       MAX_VERTEX_UNIFORM_BLOCKS_COMPONENTS = MAX_VERTEX_UNIFORM_BLOCKS * MAX_UNIFORM_BLOCK_SIZE / 4,
+       MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS = MAX_FRAGMENT_UNIFORM_BLOCKS_COMPONENTS + MAX_FRAGMENT_UNIFORM_COMPONENTS,
+       MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS = MAX_VERTEX_UNIFORM_BLOCKS_COMPONENTS + MAX_VERTEX_UNIFORM_COMPONENTS,
+       MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS = 4,
+       MAX_UNIFORM_BUFFER_BINDINGS = sw::MAX_UNIFORM_BUFFER_BINDINGS,
+       UNIFORM_BUFFER_OFFSET_ALIGNMENT = 1,
+       NUM_PROGRAM_BINARY_FORMATS = 0,
+};
+
+const GLenum compressedTextureFormats[] =
+{
+       GL_ETC1_RGB8_OES,
+#if (S3TC_SUPPORT)
+       GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
+       GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
+       GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE,
+       GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE,
+#endif
+#if (GL_ES_VERSION_3_0)
+       GL_COMPRESSED_R11_EAC,
+       GL_COMPRESSED_SIGNED_R11_EAC,
+       GL_COMPRESSED_RG11_EAC,
+       GL_COMPRESSED_SIGNED_RG11_EAC,
+       GL_COMPRESSED_RGB8_ETC2,
+       GL_COMPRESSED_SRGB8_ETC2,
+       GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
+       GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
+       GL_COMPRESSED_RGBA8_ETC2_EAC,
+       GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
+       GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
+       GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
+       GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
+       GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
+       GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
+       GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
+       GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
+       GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
+       GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
+       GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
+       GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
+       GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
+       GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
+       GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
+       GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
+#endif
+};
+
+const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);
+
+const GLint multisampleCount[] = {4, 2, 1};
+const GLint NUM_MULTISAMPLE_COUNTS = sizeof(multisampleCount) / sizeof(multisampleCount[0]);
+const GLint IMPLEMENTATION_MAX_SAMPLES = multisampleCount[0];
+
+const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f;
+const float ALIASED_LINE_WIDTH_RANGE_MAX = 1.0f;
+const float ALIASED_POINT_SIZE_RANGE_MIN = 0.125f;
+const float ALIASED_POINT_SIZE_RANGE_MAX = 8192.0f;
+const float MAX_TEXTURE_MAX_ANISOTROPY = 16.0f;
+
+enum QueryType
+{
+       QUERY_ANY_SAMPLES_PASSED,
+       QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,
+       QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
+
+       QUERY_TYPE_COUNT
+};
+
+struct Color
+{
+       float red;
+       float green;
+       float blue;
+       float alpha;
+};
+
+// Helper structure describing a single vertex attribute
+class VertexAttribute
+{
+public:
+       VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mDivisor(0), mPointer(nullptr), mArrayEnabled(false)
+       {
+               mCurrentValue[0].f = 0.0f;
+               mCurrentValue[1].f = 0.0f;
+               mCurrentValue[2].f = 0.0f;
+               mCurrentValue[3].f = 1.0f;
+               mCurrentValueType = GL_FLOAT;
+       }
+
+       int typeSize() const
+       {
+               switch(mType)
+               {
+               case GL_BYTE:           return mSize * sizeof(GLbyte);
+               case GL_UNSIGNED_BYTE:  return mSize * sizeof(GLubyte);
+               case GL_SHORT:          return mSize * sizeof(GLshort);
+               case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort);
+               case GL_INT:            return mSize * sizeof(GLint);
+               case GL_UNSIGNED_INT:   return mSize * sizeof(GLuint);
+               case GL_FIXED:          return mSize * sizeof(GLfixed);
+               case GL_FLOAT:          return mSize * sizeof(GLfloat);
+               case GL_HALF_FLOAT:     return mSize * sizeof(GLhalf);
+               case GL_INT_2_10_10_10_REV:          return sizeof(GLint);
+               case GL_UNSIGNED_INT_2_10_10_10_REV: return sizeof(GLuint);
+               default: UNREACHABLE(mType); return mSize * sizeof(GLfloat);
+               }
+       }
+
+       GLenum currentValueType() const
+       {
+               return mCurrentValueType;
+       }
+
+       GLsizei stride() const
+       {
+               return mStride ? mStride : typeSize();
+       }
+
+       inline float getCurrentValueBitsAsFloat(int i) const
+       {
+               return mCurrentValue[i].f;
+       }
+
+       inline float getCurrentValueF(int i) const
+       {
+               switch(mCurrentValueType)
+               {
+               case GL_FLOAT:        return mCurrentValue[i].f;
+               case GL_INT:          return static_cast<float>(mCurrentValue[i].i);
+               case GL_UNSIGNED_INT: return static_cast<float>(mCurrentValue[i].ui);
+               default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].f;
+               }
+       }
+
+       inline GLint getCurrentValueI(int i) const
+       {
+               switch(mCurrentValueType)
+               {
+               case GL_FLOAT:        return static_cast<GLint>(mCurrentValue[i].f);
+               case GL_INT:          return mCurrentValue[i].i;
+               case GL_UNSIGNED_INT: return static_cast<GLint>(mCurrentValue[i].ui);
+               default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].i;
+               }
+       }
+
+       inline GLuint getCurrentValueUI(int i) const
+       {
+               switch(mCurrentValueType)
+               {
+               case GL_FLOAT:        return static_cast<GLuint>(mCurrentValue[i].f);
+               case GL_INT:          return static_cast<GLuint>(mCurrentValue[i].i);
+               case GL_UNSIGNED_INT: return mCurrentValue[i].ui;
+               default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].ui;
+               }
+       }
+
+       inline void setCurrentValue(const GLfloat *values)
+       {
+               mCurrentValue[0].f = values[0];
+               mCurrentValue[1].f = values[1];
+               mCurrentValue[2].f = values[2];
+               mCurrentValue[3].f = values[3];
+               mCurrentValueType = GL_FLOAT;
+       }
+
+       inline void setCurrentValue(const GLint *values)
+       {
+               mCurrentValue[0].i = values[0];
+               mCurrentValue[1].i = values[1];
+               mCurrentValue[2].i = values[2];
+               mCurrentValue[3].i = values[3];
+               mCurrentValueType = GL_INT;
+       }
+
+       inline void setCurrentValue(const GLuint *values)
+       {
+               mCurrentValue[0].ui = values[0];
+               mCurrentValue[1].ui = values[1];
+               mCurrentValue[2].ui = values[2];
+               mCurrentValue[3].ui = values[3];
+               mCurrentValueType = GL_UNSIGNED_INT;
+       }
+
+       // From glVertexAttribPointer
+       GLenum mType;
+       GLint mSize;
+       bool mNormalized;
+       GLsizei mStride;   // 0 means natural stride
+       GLuint mDivisor;   // From glVertexAttribDivisor
+
+       union
+       {
+               const void *mPointer;
+               intptr_t mOffset;
+       };
+
+       gl::BindingPointer<Buffer> mBoundBuffer;   // Captured when glVertexAttribPointer is called.
+
+       bool mArrayEnabled;   // From glEnable/DisableVertexAttribArray
+
+private:
+       union ValueUnion
+       {
+               float f;
+               GLint i;
+               GLuint ui;
+       };
+
+       ValueUnion mCurrentValue[4];   // From glVertexAttrib
+       GLenum mCurrentValueType;
+};
+
+typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
+
+// Helper structure to store all raw state
+struct State
+{
+       Color colorClearValue;
+       GLclampf depthClearValue;
+       int stencilClearValue;
+
+       bool cullFaceEnabled;
+       GLenum cullMode;
+       GLenum frontFace;
+       bool depthTestEnabled;
+       GLenum depthFunc;
+       bool blendEnabled;
+       GLenum sourceBlendRGB;
+       GLenum destBlendRGB;
+       GLenum sourceBlendAlpha;
+       GLenum destBlendAlpha;
+       GLenum blendEquationRGB;
+       GLenum blendEquationAlpha;
+       Color blendColor;
+       bool stencilTestEnabled;
+       GLenum stencilFunc;
+       GLint stencilRef;
+       GLuint stencilMask;
+       GLenum stencilFail;
+       GLenum stencilPassDepthFail;
+       GLenum stencilPassDepthPass;
+       GLuint stencilWritemask;
+       GLenum stencilBackFunc;
+       GLint stencilBackRef;
+       GLuint stencilBackMask;
+       GLenum stencilBackFail;
+       GLenum stencilBackPassDepthFail;
+       GLenum stencilBackPassDepthPass;
+       GLuint stencilBackWritemask;
+       bool polygonOffsetFillEnabled;
+       GLfloat polygonOffsetFactor;
+       GLfloat polygonOffsetUnits;
+       bool sampleAlphaToCoverageEnabled;
+       bool sampleCoverageEnabled;
+       GLclampf sampleCoverageValue;
+       bool sampleCoverageInvert;
+       bool scissorTestEnabled;
+       bool ditherEnabled;
+       bool primitiveRestartFixedIndexEnabled;
+       bool rasterizerDiscardEnabled;
+       bool colorLogicOpEnabled;
+       GLenum logicalOperation;
+
+       GLfloat lineWidth;
+
+       GLenum generateMipmapHint;
+       GLenum fragmentShaderDerivativeHint;
+
+       GLint viewportX;
+       GLint viewportY;
+       GLsizei viewportWidth;
+       GLsizei viewportHeight;
+       float zNear;
+       float zFar;
+
+       GLint scissorX;
+       GLint scissorY;
+       GLsizei scissorWidth;
+       GLsizei scissorHeight;
+
+       bool colorMaskRed;
+       bool colorMaskGreen;
+       bool colorMaskBlue;
+       bool colorMaskAlpha;
+       bool depthMask;
+
+       unsigned int activeSampler;   // Active texture unit selector - GL_TEXTURE0
+       gl::BindingPointer<Buffer> arrayBuffer;
+       gl::BindingPointer<Buffer> copyReadBuffer;
+       gl::BindingPointer<Buffer> copyWriteBuffer;
+       gl::BindingPointer<Buffer> pixelPackBuffer;
+       gl::BindingPointer<Buffer> pixelUnpackBuffer;
+       gl::BindingPointer<Buffer> genericUniformBuffer;
+       BufferBinding uniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
+
+       GLuint readFramebuffer;
+       GLuint drawFramebuffer;
+       gl::BindingPointer<Renderbuffer> renderbuffer;
+       GLuint currentProgram;
+       GLuint vertexArray;
+       GLuint transformFeedback;
+       gl::BindingPointer<Sampler> sampler[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
+
+       VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS];
+       gl::BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][MAX_COMBINED_TEXTURE_IMAGE_UNITS];
+       gl::BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT];
+
+       egl::Image::UnpackInfo unpackInfo;
+       GLint packAlignment;
+       GLint packRowLength;
+       GLint packImageHeight;
+       GLint packSkipPixels;
+       GLint packSkipRows;
+       GLint packSkipImages;
+};
+
+class [[clang::lto_visibility_public]] Context : public egl::Context
+{
+public:
+       Context(egl::Display *display, const Context *shareContext, EGLint clientVersion, const egl::Config *config);
+
+       void makeCurrent(gl::Surface *surface) override;
+       EGLint getClientVersion() const override;
+       EGLint getConfigID() const override;
+
+       void markAllStateDirty();
+
+       // State manipulation
+       void setClearColor(float red, float green, float blue, float alpha);
+       void setClearDepth(float depth);
+       void setClearStencil(int stencil);
+
+       void setCullFaceEnabled(bool enabled);
+       bool isCullFaceEnabled() const;
+       void setCullMode(GLenum mode);
+       void setFrontFace(GLenum front);
+
+       void setDepthTestEnabled(bool enabled);
+       bool isDepthTestEnabled() const;
+       void setDepthFunc(GLenum depthFunc);
+       void setDepthRange(float zNear, float zFar);
+
+       void setBlendEnabled(bool enabled);
+       bool isBlendEnabled() const;
+       void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);
+       void setBlendColor(float red, float green, float blue, float alpha);
+       void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);
+
+       void setStencilTestEnabled(bool enabled);
+       bool isStencilTestEnabled() const;
+       void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);
+       void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);
+       void setStencilWritemask(GLuint stencilWritemask);
+       void setStencilBackWritemask(GLuint stencilBackWritemask);
+       void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);
+       void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);
+
+       void setPolygonOffsetFillEnabled(bool enabled);
+       bool isPolygonOffsetFillEnabled() const;
+       void setPolygonOffsetParams(GLfloat factor, GLfloat units);
+
+       void setSampleAlphaToCoverageEnabled(bool enabled);
+       bool isSampleAlphaToCoverageEnabled() const;
+       void setSampleCoverageEnabled(bool enabled);
+       bool isSampleCoverageEnabled() const;
+       void setSampleCoverageParams(GLclampf value, bool invert);
+
+       void setDitherEnabled(bool enabled);
+       bool isDitherEnabled() const;
+
+       void setPrimitiveRestartFixedIndexEnabled(bool enabled);
+       bool isPrimitiveRestartFixedIndexEnabled() const;
+
+       void setRasterizerDiscardEnabled(bool enabled);
+       bool isRasterizerDiscardEnabled() const;
+
+       void setLineWidth(GLfloat width);
+
+       void setGenerateMipmapHint(GLenum hint);
+       void setFragmentShaderDerivativeHint(GLenum hint);
+
+       void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
+
+       void setScissorTestEnabled(bool enabled);
+       bool isScissorTestEnabled() const;
+       void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
+
+       void setColorMask(bool red, bool green, bool blue, bool alpha);
+       unsigned int getColorMask() const;
+       void setDepthMask(bool mask);
+
+       void setActiveSampler(unsigned int active);
+
+       GLuint getReadFramebufferName() const;
+       GLuint getDrawFramebufferName() const;
+       GLuint getRenderbufferName() const;
+
+       void setFramebufferReadBuffer(GLenum buf);
+       void setFramebufferDrawBuffers(GLsizei n, const GLenum *bufs);
+       GLuint getReadFramebufferColorIndex() const;
+
+       GLuint getActiveQuery(GLenum target) const;
+
+       GLuint getArrayBufferName() const;
+       GLuint getElementArrayBufferName() const;
+
+       void setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled);
+       void setVertexAttribDivisor(unsigned int attribNum, GLuint divisor);
+       const VertexAttribute &getVertexAttribState(unsigned int attribNum) const;
+       void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,
+                                 bool normalized, GLsizei stride, const void *pointer);
+       const void *getVertexAttribPointer(unsigned int attribNum) const;
+
+       const VertexAttributeArray &getVertexArrayAttributes();
+       // Context attribute current values can be queried independently from VAO current values
+       const VertexAttributeArray &getCurrentVertexAttributes();
+
+       void setUnpackAlignment(GLint alignment);
+       void setUnpackRowLength(GLint rowLength);
+       void setUnpackImageHeight(GLint imageHeight);
+       void setUnpackSkipPixels(GLint skipPixels);
+       void setUnpackSkipRows(GLint skipRows);
+       void setUnpackSkipImages(GLint skipImages);
+       const egl::Image::UnpackInfo& getUnpackInfo() const;
+
+       void setPackAlignment(GLint alignment);
+       void setPackRowLength(GLint rowLength);
+       void setPackImageHeight(GLint imageHeight);
+       void setPackSkipPixels(GLint skipPixels);
+       void setPackSkipRows(GLint skipRows);
+       void setPackSkipImages(GLint skipImages);
+
+       // These create and destroy methods are merely pass-throughs to
+       // ResourceManager, which owns these object types
+       GLuint createBuffer();
+       GLuint createShader(GLenum type);
+       GLuint createProgram();
+       GLuint createTexture();
+       GLuint createRenderbuffer();
+       GLuint createSampler();
+       GLsync createFenceSync(GLenum condition, GLbitfield flags);
+
+       void deleteBuffer(GLuint buffer);
+       void deleteShader(GLuint shader);
+       void deleteProgram(GLuint program);
+       void deleteTexture(GLuint texture);
+       void deleteRenderbuffer(GLuint renderbuffer);
+       void deleteSampler(GLuint sampler);
+       void deleteFenceSync(GLsync fenceSync);
+
+       // Framebuffers are owned by the Context, so these methods do not pass through
+       GLuint createFramebuffer();
+       void deleteFramebuffer(GLuint framebuffer);
+
+       // Fences are owned by the Context
+       GLuint createFence();
+       void deleteFence(GLuint fence);
+
+       // Queries are owned by the Context
+       GLuint createQuery();
+       void deleteQuery(GLuint query);
+
+       // Vertex arrays are owned by the Context
+       GLuint createVertexArray();
+       void deleteVertexArray(GLuint array);
+
+       // Transform feedbacks are owned by the Context
+       GLuint createTransformFeedback();
+       void deleteTransformFeedback(GLuint transformFeedback);
+
+       void bindArrayBuffer(GLuint buffer);
+       void bindElementArrayBuffer(GLuint buffer);
+       void bindCopyReadBuffer(GLuint buffer);
+       void bindCopyWriteBuffer(GLuint buffer);
+       void bindPixelPackBuffer(GLuint buffer);
+       void bindPixelUnpackBuffer(GLuint buffer);
+       void bindTransformFeedbackBuffer(GLuint buffer);
+       void bindTexture2D(GLuint texture);
+       void bindTextureCubeMap(GLuint texture);
+       void bindTextureExternal(GLuint texture);
+       void bindTexture3D(GLuint texture);
+       void bindTexture2DArray(GLuint texture);
+       void bindReadFramebuffer(GLuint framebuffer);
+       void bindDrawFramebuffer(GLuint framebuffer);
+       void bindRenderbuffer(GLuint renderbuffer);
+       void bindVertexArray(GLuint array);
+       void bindGenericUniformBuffer(GLuint buffer);
+       void bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size);
+       void bindGenericTransformFeedbackBuffer(GLuint buffer);
+       void bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size);
+       void bindTransformFeedback(GLuint transformFeedback);
+       bool bindSampler(GLuint unit, GLuint sampler);
+       void useProgram(GLuint program);
+
+       void beginQuery(GLenum target, GLuint query);
+       void endQuery(GLenum target);
+
+       void setFramebufferZero(Framebuffer *framebuffer);
+
+       void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
+
+       void setVertexAttrib(GLuint index, const GLfloat *values);
+       void setVertexAttrib(GLuint index, const GLint *values);
+       void setVertexAttrib(GLuint index, const GLuint *values);
+
+       Buffer *getBuffer(GLuint handle) const;
+       Fence *getFence(GLuint handle) const;
+       FenceSync *getFenceSync(GLsync handle) const;
+       Shader *getShader(GLuint handle) const;
+       Program *getProgram(GLuint handle) const;
+       virtual Texture *getTexture(GLuint handle) const;
+       Framebuffer *getFramebuffer(GLuint handle) const;
+       virtual Renderbuffer *getRenderbuffer(GLuint handle) const;
+       Query *getQuery(GLuint handle) const;
+       VertexArray *getVertexArray(GLuint array) const;
+       VertexArray *getCurrentVertexArray() const;
+       bool isVertexArray(GLuint array) const;
+       TransformFeedback *getTransformFeedback(GLuint transformFeedback) const;
+       TransformFeedback *getTransformFeedback() const;
+       Sampler *getSampler(GLuint sampler) const;
+       bool isSampler(GLuint sampler) const;
+
+       Buffer *getArrayBuffer() const;
+       Buffer *getElementArrayBuffer() const;
+       Buffer *getCopyReadBuffer() const;
+       Buffer *getCopyWriteBuffer() const;
+       Buffer *getPixelPackBuffer() const;
+       Buffer *getPixelUnpackBuffer() const;
+       Buffer *getGenericUniformBuffer() const;
+       const GLvoid* getPixels(const GLvoid* data) const;
+       bool getBuffer(GLenum target, es2::Buffer **buffer) const;
+       Program *getCurrentProgram() const;
+       Texture2D *getTexture2D() const;
+       Texture3D *getTexture3D() const;
+       Texture2DArray *getTexture2DArray() const;
+       TextureCubeMap *getTextureCubeMap() const;
+       TextureExternal *getTextureExternal() const;
+       Texture *getSamplerTexture(unsigned int sampler, TextureType type) const;
+       Framebuffer *getReadFramebuffer() const;
+       Framebuffer *getDrawFramebuffer() const;
+
+       bool getFloatv(GLenum pname, GLfloat *params) const;
+       template<typename T> bool getIntegerv(GLenum pname, T *params) const;
+       bool getBooleanv(GLenum pname, GLboolean *params) const;
+       template<typename T> bool getTransformFeedbackiv(GLuint index, GLenum pname, T *param) const;
+       template<typename T> bool getUniformBufferiv(GLuint index, GLenum pname, T *param) const;
+       void samplerParameteri(GLuint sampler, GLenum pname, GLint param);
+       void samplerParameterf(GLuint sampler, GLenum pname, GLfloat param);
+       GLint getSamplerParameteri(GLuint sampler, GLenum pname);
+       GLfloat getSamplerParameterf(GLuint sampler, GLenum pname);
+
+       bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const;
+
+       bool hasZeroDivisor() const;
+
+       void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount = 1);
+       void drawElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount = 1);
+       void blit(sw::Surface *source, const sw::SliceRect &sRect, sw::Surface *dest, const sw::SliceRect &dRect) override;
+       void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);
+       void clear(GLbitfield mask);
+       void clearColorBuffer(GLint drawbuffer, const GLint *value);
+       void clearColorBuffer(GLint drawbuffer, const GLuint *value);
+       void clearColorBuffer(GLint drawbuffer, const GLfloat *value);
+       void clearDepthBuffer(const GLfloat value);
+       void clearStencilBuffer(const GLint value);
+       void finish() override;
+       void flush();
+
+       void recordInvalidEnum();
+       void recordInvalidValue();
+       void recordInvalidOperation();
+       void recordOutOfMemory();
+       void recordInvalidFramebufferOperation();
+
+       GLenum getError();
+
+       static int getSupportedMultisampleCount(int requested);
+
+       void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
+                            GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
+                            GLbitfield mask, bool filter, bool allowPartialDepthStencilBlit);
+
+       void bindTexImage(gl::Surface *surface) override;
+       EGLenum validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel) override;
+       egl::Image *createSharedImage(EGLenum target, GLuint name, GLuint textureLevel) override;
+       egl::Image *getSharedImage(GLeglImageOES image);
+
+       Device *getDevice();
+
+       const GLubyte *getExtensions(GLuint index, GLuint *numExt = nullptr) const;
+
+private:
+       ~Context() override;
+
+       void applyScissor(int width, int height);
+       bool applyRenderTarget();
+       void applyState(GLenum drawMode);
+       GLenum applyVertexBuffer(GLint base, GLint first, GLsizei count, GLsizei instanceId);
+       GLenum applyIndexBuffer(const void *indices, GLuint start, GLuint end, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
+       void applyShaders();
+       void applyTextures();
+       void applyTextures(sw::SamplerType type);
+       void applyTexture(sw::SamplerType type, int sampler, Texture *texture);
+       void clearColorBuffer(GLint drawbuffer, void *value, sw::Format format);
+
+       void detachBuffer(GLuint buffer);
+       void detachTexture(GLuint texture);
+       void detachFramebuffer(GLuint framebuffer);
+       void detachRenderbuffer(GLuint renderbuffer);
+       void detachSampler(GLuint sampler);
+
+       bool cullSkipsDraw(GLenum drawMode);
+       bool isTriangleMode(GLenum drawMode);
+
+       Query *createQuery(GLuint handle, GLenum type);
+
+       const EGLint clientVersion;
+       const egl::Config *const config;
+
+       State mState;
+
+       gl::BindingPointer<Texture2D> mTexture2DZero;
+       gl::BindingPointer<Texture3D> mTexture3DZero;
+       gl::BindingPointer<Texture2DArray> mTexture2DArrayZero;
+       gl::BindingPointer<TextureCubeMap> mTextureCubeMapZero;
+       gl::BindingPointer<TextureExternal> mTextureExternalZero;
+
+       gl::NameSpace<Framebuffer> mFramebufferNameSpace;
+       gl::NameSpace<Fence, 0> mFenceNameSpace;
+       gl::NameSpace<Query> mQueryNameSpace;
+       gl::NameSpace<VertexArray> mVertexArrayNameSpace;
+       gl::NameSpace<TransformFeedback> mTransformFeedbackNameSpace;
+
+       VertexDataManager *mVertexDataManager;
+       IndexDataManager *mIndexDataManager;
+
+       // Recorded errors
+       bool mInvalidEnum;
+       bool mInvalidValue;
+       bool mInvalidOperation;
+       bool mOutOfMemory;
+       bool mInvalidFramebufferOperation;
+
+       bool mHasBeenCurrent;
+
+       unsigned int mAppliedProgramSerial;
+
+       // state caching flags
+       bool mDepthStateDirty;
+       bool mMaskStateDirty;
+       bool mBlendStateDirty;
+       bool mStencilStateDirty;
+       bool mPolygonOffsetStateDirty;
+       bool mSampleStateDirty;
+       bool mFrontFaceDirty;
+       bool mDitherStateDirty;
+
+       Device *device;
+       ResourceManager *mResourceManager;
+};
+}
+
+#endif   // INCLUDE_CONTEXT_H_