#pragma once #include #include #include #include #include #include namespace mbgl { namespace gl { constexpr GLsizei TextureMax = 64; class Context : private util::noncopyable { public: ~Context(); UniqueProgram createProgram() { return UniqueProgram { MBGL_CHECK_ERROR(glCreateProgram()), { this } }; } UniqueShader createShader(GLenum type) { return UniqueShader { MBGL_CHECK_ERROR(glCreateShader(type)), { this } }; } UniqueBuffer createBuffer() { GLuint id = 0; MBGL_CHECK_ERROR(glGenBuffers(1, &id)); return UniqueBuffer { std::move(id), { this } }; } UniqueTexture createTexture() { if (pooledTextures.empty()) { pooledTextures.resize(TextureMax); MBGL_CHECK_ERROR(glGenTextures(TextureMax, pooledTextures.data())); } GLuint id = pooledTextures.back(); pooledTextures.pop_back(); return UniqueTexture { std::move(id), { this } }; } UniqueVAO createVAO() { GLuint id = 0; MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id)); return UniqueVAO { std::move(id), { this } }; } UniqueFBO createFBO() { GLuint id = 0; MBGL_CHECK_ERROR(glGenFramebuffers(1, &id)); return UniqueFBO { std::move(id), { this } }; } // Actually remove the objects we marked as abandoned with the above methods. // Only call this while the OpenGL context is exclusive to this thread. void performCleanup(); // Drain pools and remove abandoned objects, in preparation for destroying the store. // Only call this while the OpenGL context is exclusive to this thread. void reset(); bool empty() const { return pooledTextures.empty() && abandonedPrograms.empty() && abandonedShaders.empty() && abandonedBuffers.empty() && abandonedTextures.empty() && abandonedVAOs.empty() && abandonedFBOs.empty(); } void resetState(); void setDirtyState(); State stencilFunc; State stencilMask; State stencilTest; State stencilOp; State depthRange; State depthMask; State depthTest; State depthFunc; State blend; State blendFunc; State blendColor; State colorMask; State clearDepth; State clearColor; State clearStencil; State program; State lineWidth; State activeTexture; State bindFramebuffer; State viewport; #ifndef GL_ES_VERSION_2_0 State pixelZoom; State rasterPos; #endif // GL_ES_VERSION_2_0 std::array, 2> texture; State> vertexBuffer; State> elementBuffer; State vertexArrayObject; private: friend detail::ProgramDeleter; friend detail::ShaderDeleter; friend detail::BufferDeleter; friend detail::TextureDeleter; friend detail::VAODeleter; friend detail::FBODeleter; std::vector pooledTextures; std::vector abandonedPrograms; std::vector abandonedShaders; std::vector abandonedBuffers; std::vector abandonedTextures; std::vector abandonedVAOs; std::vector abandonedFBOs; }; } // namespace gl } // namespace mbgl