1 // 2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // Context.h: Defines the gl::Context class, managing all GL state and performing 8 // rendering operations. It is the GLES2 specific implementation of EGLContext. 9 10 #ifndef LIBGLESV2_CONTEXT_H_ 11 #define LIBGLESV2_CONTEXT_H_ 12 13 #include "common/angleutils.h" 14 #include "common/RefCountObject.h" 15 #include "libGLESv2/Caps.h" 16 #include "libGLESv2/Error.h" 17 #include "libGLESv2/HandleAllocator.h" 18 #include "libGLESv2/angletypes.h" 19 #include "libGLESv2/Constants.h" 20 #include "libGLESv2/VertexAttribute.h" 21 #include "libGLESv2/State.h" 22 23 #include "angle_gl.h" 24 25 #include <string> 26 #include <set> 27 #include <map> 28 #include <unordered_map> 29 #include <array> 30 31 namespace rx 32 { 33 class Renderer; 34 } 35 36 namespace egl 37 { 38 class Surface; 39 } 40 41 namespace gl 42 { 43 class Shader; 44 class Program; 45 class ProgramBinary; 46 class Texture; 47 class Texture2D; 48 class TextureCubeMap; 49 class Texture3D; 50 class Texture2DArray; 51 class Framebuffer; 52 class Renderbuffer; 53 class RenderbufferStorage; 54 class Colorbuffer; 55 class Depthbuffer; 56 class Stencilbuffer; 57 class DepthStencilbuffer; 58 class FenceNV; 59 class FenceSync; 60 class Query; 61 class ResourceManager; 62 class Buffer; 63 struct VertexAttribute; 64 class VertexArray; 65 class Sampler; 66 class TransformFeedback; 67 68 class Context 69 { 70 public: 71 Context(int clientVersion, const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess); 72 73 virtual ~Context(); 74 75 void makeCurrent(egl::Surface *surface); 76 77 virtual void markContextLost(); 78 bool isContextLost(); 79 80 // These create and destroy methods are merely pass-throughs to 81 // ResourceManager, which owns these object types 82 GLuint createBuffer(); 83 GLuint createShader(GLenum type); 84 GLuint createProgram(); 85 GLuint createTexture(); 86 GLuint createRenderbuffer(); 87 GLuint createSampler(); 88 GLuint createTransformFeedback(); 89 GLsync createFenceSync(GLenum condition); 90 91 void deleteBuffer(GLuint buffer); 92 void deleteShader(GLuint shader); 93 void deleteProgram(GLuint program); 94 void deleteTexture(GLuint texture); 95 void deleteRenderbuffer(GLuint renderbuffer); 96 void deleteSampler(GLuint sampler); 97 void deleteTransformFeedback(GLuint transformFeedback); 98 void deleteFenceSync(GLsync fenceSync); 99 100 // Framebuffers are owned by the Context, so these methods do not pass through 101 GLuint createFramebuffer(); 102 void deleteFramebuffer(GLuint framebuffer); 103 104 // NV Fences are owned by the Context. 105 GLuint createFenceNV(); 106 void deleteFenceNV(GLuint fence); 107 108 // Queries are owned by the Context; 109 GLuint createQuery(); 110 void deleteQuery(GLuint query); 111 112 // Vertex arrays are owned by the Context 113 GLuint createVertexArray(); 114 void deleteVertexArray(GLuint vertexArray); 115 116 void bindArrayBuffer(GLuint buffer); 117 void bindElementArrayBuffer(GLuint buffer); 118 void bindTexture(GLenum target, GLuint texture); 119 void bindReadFramebuffer(GLuint framebuffer); 120 void bindDrawFramebuffer(GLuint framebuffer); 121 void bindRenderbuffer(GLuint renderbuffer); 122 void bindVertexArray(GLuint vertexArray); 123 void bindSampler(GLuint textureUnit, GLuint sampler); 124 void bindGenericUniformBuffer(GLuint buffer); 125 void bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size); 126 void bindGenericTransformFeedbackBuffer(GLuint buffer); 127 void bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size); 128 void bindCopyReadBuffer(GLuint buffer); 129 void bindCopyWriteBuffer(GLuint buffer); 130 void bindPixelPackBuffer(GLuint buffer); 131 void bindPixelUnpackBuffer(GLuint buffer); 132 void useProgram(GLuint program); 133 void linkProgram(GLuint program); 134 void setProgramBinary(GLuint program, GLenum binaryFormat, const void *binary, GLint length); 135 void bindTransformFeedback(GLuint transformFeedback); 136 137 Error beginQuery(GLenum target, GLuint query); 138 Error endQuery(GLenum target); 139 140 void setFramebufferZero(Framebuffer *framebuffer); 141 142 void setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples); 143 144 void setVertexAttribDivisor(GLuint index, GLuint divisor); 145 146 void samplerParameteri(GLuint sampler, GLenum pname, GLint param); 147 void samplerParameterf(GLuint sampler, GLenum pname, GLfloat param); 148 GLint getSamplerParameteri(GLuint sampler, GLenum pname); 149 GLfloat getSamplerParameterf(GLuint sampler, GLenum pname); 150 151 Buffer *getBuffer(GLuint handle); 152 FenceNV *getFenceNV(GLuint handle); 153 FenceSync *getFenceSync(GLsync handle) const; 154 Shader *getShader(GLuint handle) const; 155 Program *getProgram(GLuint handle) const; 156 Texture *getTexture(GLuint handle) const; 157 Framebuffer *getFramebuffer(GLuint handle) const; 158 Renderbuffer *getRenderbuffer(GLuint handle); 159 VertexArray *getVertexArray(GLuint handle) const; 160 Sampler *getSampler(GLuint handle) const; 161 Query *getQuery(GLuint handle, bool create, GLenum type); 162 TransformFeedback *getTransformFeedback(GLuint handle) const; 163 164 Texture *getTargetTexture(GLenum target) const; 165 Texture2D *getTexture2D() const; 166 TextureCubeMap *getTextureCubeMap() const; 167 Texture3D *getTexture3D() const; 168 Texture2DArray *getTexture2DArray() const; 169 170 Texture *getSamplerTexture(unsigned int sampler, GLenum type) const; 171 172 bool isSampler(GLuint samplerName) const; 173 174 void getBooleanv(GLenum pname, GLboolean *params); 175 void getFloatv(GLenum pname, GLfloat *params); 176 void getIntegerv(GLenum pname, GLint *params); 177 void getInteger64v(GLenum pname, GLint64 *params); 178 179 bool getIndexedIntegerv(GLenum target, GLuint index, GLint *data); 180 bool getIndexedInteger64v(GLenum target, GLuint index, GLint64 *data); 181 182 bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams); 183 bool getIndexedQueryParameterInfo(GLenum target, GLenum *type, unsigned int *numParams); 184 185 Error clear(GLbitfield mask); 186 Error clearBufferfv(GLenum buffer, int drawbuffer, const float *values); 187 Error clearBufferuiv(GLenum buffer, int drawbuffer, const unsigned int *values); 188 Error clearBufferiv(GLenum buffer, int drawbuffer, const int *values); 189 Error clearBufferfi(GLenum buffer, int drawbuffer, float depth, int stencil); 190 191 Error readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels); 192 void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances); 193 void drawElements(GLenum mode, GLsizei count, GLenum type, 194 const GLvoid *indices, GLsizei instances, 195 const rx::RangeUI &indexRange); 196 void sync(bool block); // flush/finish 197 198 void recordError(const Error &error); 199 200 GLenum getError(); 201 GLenum getResetStatus(); 202 virtual bool isResetNotificationEnabled(); 203 204 virtual int getClientVersion() const; 205 206 const Caps &getCaps() const; 207 const TextureCapsMap &getTextureCaps() const; 208 const Extensions &getExtensions() const; 209 210 const std::string &getRendererString() const; 211 212 const std::string &getExtensionString() const; 213 const std::string &getExtensionString(size_t idx) const; 214 size_t getExtensionStringCount() const; 215 216 void getCurrentReadFormatType(GLenum *internalFormat, GLenum *format, GLenum *type); 217 218 void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, 219 GLbitfield mask, GLenum filter); 220 getRenderer()221 rx::Renderer *getRenderer() { return mRenderer; } 222 getState()223 State &getState() { return mState; } getState()224 const State &getState() const { return mState; } 225 226 void releaseShaderCompiler(); 227 228 private: 229 DISALLOW_COPY_AND_ASSIGN(Context); 230 231 // TODO: std::array may become unavailable using older versions of GCC 232 typedef std::array<unsigned int, IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS> FramebufferTextureSerialArray; 233 234 void applyRenderTarget(GLenum drawMode, bool ignoreViewport); 235 void applyState(GLenum drawMode); 236 void applyShaders(ProgramBinary *programBinary, bool transformFeedbackActive); 237 void applyTextures(ProgramBinary *programBinary, SamplerType shaderType, const FramebufferTextureSerialArray &framebufferSerials, 238 size_t framebufferSerialCount); 239 void applyTextures(ProgramBinary *programBinary); 240 bool applyUniformBuffers(); 241 bool applyTransformFeedbackBuffers(); 242 void markTransformFeedbackUsage(); 243 244 void detachBuffer(GLuint buffer); 245 void detachTexture(GLuint texture); 246 void detachFramebuffer(GLuint framebuffer); 247 void detachRenderbuffer(GLuint renderbuffer); 248 void detachVertexArray(GLuint vertexArray); 249 void detachTransformFeedback(GLuint transformFeedback); 250 void detachSampler(GLuint sampler); 251 252 void generateSwizzles(ProgramBinary *programBinary, SamplerType type); 253 void generateSwizzles(ProgramBinary *programBinary); 254 255 Texture *getIncompleteTexture(GLenum type); 256 257 bool skipDraw(GLenum drawMode); 258 259 void initRendererString(); 260 void initExtensionStrings(); 261 262 size_t getBoundFramebufferTextureSerials(FramebufferTextureSerialArray *outSerialArray); 263 264 void initCaps(GLuint clientVersion); 265 266 // Caps to use for validation 267 Caps mCaps; 268 TextureCapsMap mTextureCaps; 269 Extensions mExtensions; 270 271 rx::Renderer *const mRenderer; 272 State mState; 273 274 int mClientVersion; 275 276 typedef std::map< GLenum, BindingPointer<Texture> > TextureMap; 277 TextureMap mZeroTextures; 278 TextureMap mIncompleteTextures; 279 280 typedef std::unordered_map<GLuint, Framebuffer*> FramebufferMap; 281 FramebufferMap mFramebufferMap; 282 HandleAllocator mFramebufferHandleAllocator; 283 284 typedef std::unordered_map<GLuint, FenceNV*> FenceNVMap; 285 FenceNVMap mFenceNVMap; 286 HandleAllocator mFenceNVHandleAllocator; 287 288 typedef std::unordered_map<GLuint, Query*> QueryMap; 289 QueryMap mQueryMap; 290 HandleAllocator mQueryHandleAllocator; 291 292 typedef std::unordered_map<GLuint, VertexArray*> VertexArrayMap; 293 VertexArrayMap mVertexArrayMap; 294 HandleAllocator mVertexArrayHandleAllocator; 295 296 BindingPointer<TransformFeedback> mTransformFeedbackZero; 297 typedef std::unordered_map<GLuint, TransformFeedback*> TransformFeedbackMap; 298 TransformFeedbackMap mTransformFeedbackMap; 299 HandleAllocator mTransformFeedbackAllocator; 300 301 std::string mRendererString; 302 std::string mExtensionString; 303 std::vector<std::string> mExtensionStrings; 304 305 // Recorded errors 306 typedef std::set<GLenum> ErrorSet; 307 ErrorSet mErrors; 308 309 // Current/lost context flags 310 bool mHasBeenCurrent; 311 bool mContextLost; 312 GLenum mResetStatus; 313 GLenum mResetStrategy; 314 bool mRobustAccess; 315 316 ResourceManager *mResourceManager; 317 }; 318 } 319 320 #endif // INCLUDE_CONTEXT_H_ 321