]> git.sesse.net Git - nageru/commitdiff
Clean up resources in the global ResourcePool on exit, in order to avoid some assert...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 5 Jan 2016 23:26:52 +0000 (00:26 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 5 Jan 2016 23:26:52 +0000 (00:26 +0100)
glwidget.cpp
glwidget.h
main.cpp
mainwindow.cpp
mainwindow.h
theme.cpp
theme.h

index 06675f346c85211030d0712097aa0db138b1fef6..01d372f1adb92f0ca6abee6f07119056a8dc9ed2 100644 (file)
@@ -7,6 +7,7 @@
 #include <epoxy/gl.h>
 #include <epoxy/egl.h>
 #include <QSurfaceFormat>
+#include <movit/resource_pool.h>
 
 #include "glwidget.h"
 
@@ -36,8 +37,12 @@ GLWidget::GLWidget(QWidget *parent)
 {
 }
 
-GLWidget::~GLWidget()
+void GLWidget::clean_context()
 {
+       if (resource_pool != nullptr) {
+               makeCurrent();
+               resource_pool->clean_context();
+       }
 }
 
 void GLWidget::initializeGL()
@@ -82,6 +87,12 @@ void GLWidget::paintGL()
        check_error();
        frame.chain->render_to_screen();
        check_error();
+
+       if (resource_pool == nullptr) {
+               resource_pool = frame.chain->get_resource_pool();
+       } else {
+               assert(resource_pool == frame.chain->get_resource_pool());
+       }
 }
 
 void GLWidget::mousePressEvent(QMouseEvent *event)
index ae4902c12b1f51464981a6a2bd8c55fb78071e5d..857d733c42cf90e77ee51bf97aecdecea03d2231 100644 (file)
 class QMouseEvent;
 class QWidget;
 
+namespace movit {
+
+class ResourcePool;
+
+}  // namespace movit
+
 // Note: We use the older QGLWidget instead of QOpenGLWidget as it is
 // much faster (does not go through a separate offscreen rendering step).
 //
@@ -23,13 +29,14 @@ class GLWidget : public QGLWidget
 
 public:
        GLWidget(QWidget *parent = 0);
-       ~GLWidget();
 
        void set_output(Mixer::Output output)
        {
                this->output = output;
        }
 
+       void clean_context();
+
 protected:
        void initializeGL() override;
        void resizeGL(int width, int height) override;
@@ -45,6 +52,7 @@ private:
        Mixer::Output output;
        GLuint vao, program_num;
        GLuint position_vbo, texcoord_vbo;
+       movit::ResourcePool *resource_pool = nullptr;
 };
 
 #endif
index b7679f90fb856255fbda9d3c4d8a1f7ef7deb0e9..f28b859fd842bdc501d238eccc743bf6667b5545 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -47,6 +47,7 @@ int main(int argc, char *argv[])
 
        int rc = app.exec();
        global_mixer->quit();
+       mainWindow.mixer_shutting_down();
        delete global_mixer;
        return rc;
 }
index 9f293342e8deda072defe176ea8baf308083190b..ede1fb6827de668b0264fedc8953eec3be05a429 100644 (file)
@@ -117,6 +117,15 @@ void MainWindow::mixer_created(Mixer *mixer)
        mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6));
 }
 
+void MainWindow::mixer_shutting_down()
+{
+       ui->me_live->clean_context();
+       ui->me_preview->clean_context();
+       for (Ui::Display *display : previews) {
+               display->display->clean_context();
+       }
+}
+
 void MainWindow::cutoff_knob_changed(int value)
 {
        float octaves = value * 0.1f;
index 8faa81299b0a542f8aafc7a0813886d81eef8bee..f768aaa31445ecb9bf92a54f314586d892b78876 100644 (file)
@@ -27,6 +27,10 @@ public:
        void resizeEvent(QResizeEvent *event) override;
        void mixer_created(Mixer *mixer);
 
+       // Used to release FBOs on the global ResourcePool. Call after the
+       // mixer has been shut down but not destroyed yet.
+       void mixer_shutting_down();
+
 public slots:
        void transition_clicked(int transition_number);
        void channel_clicked(int channel_number);
index 7bfe6cf57f555cece42fe9d34a1337a9bae11d38..001930019187478ff19c57e79bbc9dc7dbf0cf86 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -98,6 +98,28 @@ int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
        return 1;
 }
 
+// Like wrap_lua_object, but the object is not owned by Lua; ie. it's not freed
+// by Lua GC. This is typically the case for Effects, which are owned by EffectChain
+// and expected to be destructed by it. The object will be of type T** instead of T*
+// when exposed to Lua.
+//
+// Note that we currently leak if you allocate an Effect in this way and never call
+// add_effect. We should see if there's a way to e.g. set __gc on it at construction time
+// and then release that once add_effect() takes ownership.
+template<class T, class... Args>
+int wrap_lua_object_nonowned(lua_State* L, const char *class_name, Args&&... args)
+{
+       // Construct the pointer ot the C++ object and put it on the stack.
+       T **obj = (T **)lua_newuserdata(L, sizeof(T *));
+       *obj = new T(std::forward<Args>(args)...);
+
+       // Look up the metatable named <class_name>, and set it on the new object.
+       luaL_getmetatable(L, class_name);
+       lua_setmetatable(L, -2);
+
+       return 1;
+}
+
 Theme *get_theme_updata(lua_State* L)
 {      
        luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
@@ -114,7 +136,7 @@ Effect *get_effect(lua_State *L, int idx)
            luaL_testudata(L, idx, "ResizeEffect") ||
            luaL_testudata(L, idx, "MixEffect") ||
            luaL_testudata(L, idx, "ImageInput")) {
-               return (Effect *)lua_touserdata(L, idx);
+               return *(Effect **)lua_touserdata(L, idx);
        }
        luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
        return nullptr;
@@ -152,6 +174,14 @@ int EffectChain_new(lua_State* L)
        return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h, theme->get_resource_pool());
 }
 
+int EffectChain_gc(lua_State* L)
+{
+       assert(lua_gettop(L) == 1);
+       EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
+       chain->~EffectChain();
+       return 0;
+}
+
 int EffectChain_add_live_input(lua_State* L)
 {
        assert(lua_gettop(L) == 3);
@@ -255,49 +285,49 @@ int ImageInput_new(lua_State* L)
 {
        assert(lua_gettop(L) == 1);
        std::string filename = checkstdstring(L, 1);
-       return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
+       return wrap_lua_object_nonowned<ImageInput>(L, "ImageInput", filename);
 }
 
 int WhiteBalanceEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
+       return wrap_lua_object_nonowned<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
 }
 
 int ResampleEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
+       return wrap_lua_object_nonowned<ResampleEffect>(L, "ResampleEffect");
 }
 
 int PaddingEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
+       return wrap_lua_object_nonowned<PaddingEffect>(L, "PaddingEffect");
 }
 
 int IntegralPaddingEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
+       return wrap_lua_object_nonowned<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
 }
 
 int OverlayEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
+       return wrap_lua_object_nonowned<OverlayEffect>(L, "OverlayEffect");
 }
 
 int ResizeEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
+       return wrap_lua_object_nonowned<ResizeEffect>(L, "ResizeEffect");
 }
 
 int MixEffect_new(lua_State* L)
 {
        assert(lua_gettop(L) == 0);
-       return wrap_lua_object<MixEffect>(L, "MixEffect");
+       return wrap_lua_object_nonowned<MixEffect>(L, "MixEffect");
 }
 
 int InputStateInfo_get_width(lua_State* L)
@@ -409,6 +439,7 @@ int Effect_set_vec4(lua_State *L)
 
 const luaL_Reg EffectChain_funcs[] = {
        { "new", EffectChain_new },
+       { "__gc", EffectChain_gc },
        { "add_live_input", EffectChain_add_live_input },
        { "add_effect", EffectChain_add_effect },
        { "finalize", EffectChain_finalize },
@@ -655,6 +686,11 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_car
        assert(lua_gettop(L) == 0);
 }
 
+Theme::~Theme()
+{
+       lua_close(L);
+}
+
 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
 {
        assert(lua_gettop(L) == 0);
diff --git a/theme.h b/theme.h
index 1f4f429eec72387f44b4f3f26f56dca0a543c9a3..48039734a840f58f90924b514eeaaf27f1f61657 100644 (file)
--- a/theme.h
+++ b/theme.h
@@ -40,6 +40,7 @@ public:
 class Theme {
 public:
        Theme(const char *filename, movit::ResourcePool *resource_pool, unsigned num_cards);
+       ~Theme();
 
        struct Chain {
                movit::EffectChain *chain;