[Scummvm-git-logs] scummvm master -> 82fc499b74c998b4073355c1789bf6b2b4e9115b

aquadran noreply at scummvm.org
Tue Feb 14 18:30:04 UTC 2023


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
82fc499b74 TINYGL: Added multi context support


Commit: 82fc499b74c998b4073355c1789bf6b2b4e9115b
    https://github.com/scummvm/scummvm/commit/82fc499b74c998b4073355c1789bf6b2b4e9115b
Author: Paweł Kołodziejski (aquadran at gmail.com)
Date: 2023-02-14T19:29:55+01:00

Commit Message:
TINYGL: Added multi context support

Changed paths:
    engines/playground3d/gfx_tinygl.cpp
    engines/playground3d/gfx_tinygl.h
    graphics/tinygl/init.cpp
    graphics/tinygl/tinygl.h


diff --git a/engines/playground3d/gfx_tinygl.cpp b/engines/playground3d/gfx_tinygl.cpp
index b3c66df54d8..9e51c5f756e 100644
--- a/engines/playground3d/gfx_tinygl.cpp
+++ b/engines/playground3d/gfx_tinygl.cpp
@@ -76,6 +76,7 @@ Renderer *CreateGfxTinyGL(OSystem *system) {
 
 TinyGLRenderer::TinyGLRenderer(OSystem *system) :
 		Renderer(system),
+		_context(nullptr),
 		_blitImageRgba(nullptr),
 		_blitImageRgb(nullptr),
 		_blitImageRgb565(nullptr),
@@ -84,7 +85,7 @@ TinyGLRenderer::TinyGLRenderer(OSystem *system) :
 }
 
 TinyGLRenderer::~TinyGLRenderer() {
-	TinyGL::destroyContext();
+	TinyGL::destroyContext(_context);
 }
 
 void TinyGLRenderer::init() {
@@ -92,7 +93,8 @@ void TinyGLRenderer::init() {
 
 	computeScreenViewport();
 
-	TinyGL::createContext(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat(), 512, true, ConfMan.getBool("dirtyrects"));
+	_context = TinyGL::createContext(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat(), 512, true, ConfMan.getBool("dirtyrects"));
+	TinyGL::setContext(_context);
 
 	tglMatrixMode(TGL_PROJECTION);
 	tglLoadIdentity();
diff --git a/engines/playground3d/gfx_tinygl.h b/engines/playground3d/gfx_tinygl.h
index f38afd257e7..98b8d7744d1 100644
--- a/engines/playground3d/gfx_tinygl.h
+++ b/engines/playground3d/gfx_tinygl.h
@@ -60,6 +60,7 @@ public:
 	void flipBuffer() override;
 
 private:
+	TinyGL::ContextHandle *_context;
 	Math::Vector3d _pos;
 	TGLuint _textureRgbaId[5];
 	TGLuint _textureRgbId[5];
diff --git a/graphics/tinygl/init.cpp b/graphics/tinygl/init.cpp
index d26f97fbc47..f88579abe84 100644
--- a/graphics/tinygl/init.cpp
+++ b/graphics/tinygl/init.cpp
@@ -25,14 +25,109 @@
  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
  */
 
+#include "common/singleton.h"
+#include "common/array.h"
+
+#include "graphics/tinygl/tinygl.h"
 #include "graphics/tinygl/zgl.h"
 #include "graphics/tinygl/zblit.h"
 #include "graphics/tinygl/zdirtyrect.h"
 
 namespace TinyGL {
 
+class GLContextArray : public Common::Singleton<GLContextArray> {
+private:
+
+	Common::Array<GLContext *> _glContextArray;
+
+public:
+
+	GLContext *createContext() {
+		GLContext *ctx = new GLContext;
+		_glContextArray.push_back(ctx);
+		return ctx;
+	}
+
+	void destroyContext(ContextHandle *handle) {
+		for (Common::Array<GLContext *>::iterator it = _glContextArray.begin(); it != _glContextArray.end(); it++) {
+			if (*it == (GLContext *)handle) {
+				(*it)->deinit();
+				delete *it;
+				_glContextArray.erase(it);
+				break;
+			}
+		}
+	}
+
+	void destroyContexts() {
+		for (Common::Array<GLContext *>::iterator it = _glContextArray.begin(); it != _glContextArray.end(); it++) {
+			if (*it != nullptr) {
+				(*it)->deinit();
+				delete *it;
+			}
+		}
+		_glContextArray.clear();
+	}
+
+	bool existsContexts() {
+		return _glContextArray.size() != 0;
+	}
+
+	GLContext *getContext(ContextHandle *handle) {
+		for (Common::Array<GLContext *>::iterator it = _glContextArray.begin(); it != _glContextArray.end(); it++) {
+			if ((*it) == (GLContext *)handle) {
+				return *it;
+			}
+		}
+		return nullptr;
+	}
+};
+
+} // end of namespace TinyGL
+
+namespace Common {
+DECLARE_SINGLETON(TinyGL::GLContextArray);
+}
+
+namespace TinyGL {
+
 GLContext *gl_ctx;
 
+GLContext *gl_get_context() {
+	assert(gl_ctx);
+	return gl_ctx;
+}
+
+ContextHandle *createContext(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize,
+							 bool enableStencilBuffer, bool dirtyRectsEnable, uint32 drawCallMemorySize) {
+	gl_ctx = GLContextArray::instance().createContext();
+	gl_ctx->init(screenW, screenH, pixelFormat, textureSize, enableStencilBuffer,
+				 dirtyRectsEnable, drawCallMemorySize);
+	return (ContextHandle *)gl_ctx;
+}
+
+void destroyContext() {
+	GLContextArray::instance().destroyContexts();
+	GLContextArray::destroy();
+	gl_ctx = nullptr;
+}
+
+void destroyContext(ContextHandle *handle) {
+	GLContextArray::instance().destroyContext(handle);
+	if ((GLContext *)handle == gl_ctx)
+		gl_ctx = nullptr;
+	if (!GLContextArray::instance().existsContexts())
+		GLContextArray::destroy();
+}
+
+void setContext(ContextHandle *handle) {
+	GLContext *ctx = GLContextArray::instance().getContext(handle);
+	if (ctx == nullptr) {
+		error("TinyGL: makeContextCurrent() Failed get context");
+	}
+	gl_ctx = ctx;
+}
+
 void GLContext::initSharedState() {
 	GLSharedState *s = &shared_state;
 	s->lists = (GLList **)gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS);
@@ -50,13 +145,6 @@ void GLContext::endSharedState() {
 	gl_free(s->texture_hash_table);
 }
 
-void createContext(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize,
-	           bool enableStencilBuffer, bool dirtyRectsEnable, uint32 drawCallMemorySize) {
-	assert(gl_ctx == nullptr);
-	gl_ctx = new GLContext();
-	gl_ctx->init(screenW, screenH, pixelFormat, textureSize, enableStencilBuffer, dirtyRectsEnable, drawCallMemorySize);
-}
-
 void GLContext::init(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize,
 	             bool enableStencilBuffer, bool dirtyRectsEnable, uint32 drawCallMemorySize) {
 	GLViewport *v;
@@ -268,18 +356,6 @@ void GLContext::init(int screenW, int screenH, Graphics::PixelFormat pixelFormat
 	TinyGL::Internal::tglBlitResetScissorRect();
 }
 
-GLContext *gl_get_context() {
-	return gl_ctx;
-}
-
-void destroyContext() {
-	GLContext *c = gl_get_context();
-	assert(c);
-	c->deinit();
-	delete c;
-	gl_ctx = nullptr;
-}
-
 void GLContext::deinit() {
 	disposeDrawCallLists();
 	disposeResources();
diff --git a/graphics/tinygl/tinygl.h b/graphics/tinygl/tinygl.h
index 7a403345b3a..61574a86b83 100644
--- a/graphics/tinygl/tinygl.h
+++ b/graphics/tinygl/tinygl.h
@@ -29,10 +29,14 @@
 
 namespace TinyGL {
 
-void createContext(int screenW, int screenH, Graphics::PixelFormat pixelFormat,
+typedef void *ContextHandle;
+
+ContextHandle *createContext(int screenW, int screenH, Graphics::PixelFormat pixelFormat,
                    int textureSize, bool enableStencilBuffer, bool dirtyRectsEnable,
                    uint32 drawCallMemorySize = 5 * 1024 * 1024);
 void destroyContext();
+void destroyContext(ContextHandle *handle);
+void setContext(ContextHandle *handle);
 void presentBuffer();
 void presentBuffer(Common::List<Common::Rect> &dirtyAreas);
 void getSurfaceRef(Graphics::Surface &surface);




More information about the Scummvm-git-logs mailing list