summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/context.cpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-09-27 17:52:14 +0200
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-09-27 11:03:29 -0700
commit44c7e9d05edbe6fee9e8f98b91380b6c07e57ac7 (patch)
treecb2ee7fed51efe737543bb6f2444fac885571c41 /src/mbgl/gl/context.cpp
parentce42d22984d19fa020e6fba77e2585c0fd9dacf4 (diff)
downloadqtlocation-mapboxgl-44c7e9d05edbe6fee9e8f98b91380b6c07e57ac7.tar.gz
[core] merge gl::ObjectStore into gl::Context
Diffstat (limited to 'src/mbgl/gl/context.cpp')
-rw-r--r--src/mbgl/gl/context.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp
new file mode 100644
index 0000000000..101e11f4d8
--- /dev/null
+++ b/src/mbgl/gl/context.cpp
@@ -0,0 +1,95 @@
+#include <mbgl/gl/context.hpp>
+
+namespace mbgl {
+namespace gl {
+
+Context::~Context() {
+ reset();
+}
+
+void Context::reset() {
+ std::copy(pooledTextures.begin(), pooledTextures.end(), std::back_inserter(abandonedTextures));
+ pooledTextures.resize(0);
+ performCleanup();
+}
+
+namespace {
+
+template <typename Fn>
+void applyStateFunction(Context& context, Fn&& fn) {
+ fn(context.stencilFunc);
+ fn(context.stencilMask);
+ fn(context.stencilTest);
+ fn(context.stencilOp);
+ fn(context.depthRange);
+ fn(context.depthMask);
+ fn(context.depthTest);
+ fn(context.depthFunc);
+ fn(context.blend);
+ fn(context.blendFunc);
+ fn(context.blendColor);
+ fn(context.colorMask);
+ fn(context.clearDepth);
+ fn(context.clearColor);
+ fn(context.clearStencil);
+ fn(context.program);
+ fn(context.lineWidth);
+ fn(context.activeTexture);
+ fn(context.bindFramebuffer);
+ fn(context.viewport);
+#ifndef GL_ES_VERSION_2_0
+ fn(context.pixelZoom);
+ fn(context.rasterPos);
+#endif // GL_ES_VERSION_2_0
+ for (auto& tex : context.texture) {
+ fn(tex);
+ }
+ fn(context.vertexBuffer);
+ fn(context.elementBuffer);
+ fn(context.vertexArrayObject);
+}
+
+} // namespace
+
+void Context::resetState() {
+ applyStateFunction(*this, [](auto& state) { state.reset(); });
+}
+
+void Context::setDirtyState() {
+ applyStateFunction(*this, [](auto& state) { state.setDirty(); });
+}
+
+void Context::performCleanup() {
+ for (GLuint id : abandonedPrograms) {
+ MBGL_CHECK_ERROR(glDeleteProgram(id));
+ }
+ abandonedPrograms.clear();
+
+ for (GLuint id : abandonedShaders) {
+ MBGL_CHECK_ERROR(glDeleteShader(id));
+ }
+ abandonedShaders.clear();
+
+ if (!abandonedBuffers.empty()) {
+ MBGL_CHECK_ERROR(glDeleteBuffers(int(abandonedBuffers.size()), abandonedBuffers.data()));
+ abandonedBuffers.clear();
+ }
+
+ if (!abandonedTextures.empty()) {
+ MBGL_CHECK_ERROR(glDeleteTextures(int(abandonedTextures.size()), abandonedTextures.data()));
+ abandonedTextures.clear();
+ }
+
+ if (!abandonedVAOs.empty()) {
+ MBGL_CHECK_ERROR(gl::DeleteVertexArrays(int(abandonedVAOs.size()), abandonedVAOs.data()));
+ abandonedVAOs.clear();
+ }
+
+ if (!abandonedFBOs.empty()) {
+ MBGL_CHECK_ERROR(glDeleteFramebuffers(int(abandonedFBOs.size()), abandonedFBOs.data()));
+ abandonedFBOs.clear();
+ }
+}
+
+} // namespace gl
+} // namespace mbgl