]> git.sesse.net Git - nageru/blobdiff - theme.cpp
Shorten white_balance_effect in the Lua code.
[nageru] / theme.cpp
index 2fe9d899f0cafd26b4362683551065ab33f13eae..defdca171b54448bba2ddce336260ca3e2a95dbe 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -37,6 +37,29 @@ extern Mixer *global_mixer;
 
 namespace {
 
+// Contains basically the same data as InputState, but does not hold on to
+// a reference to the frames. This is important so that we can release them
+// without having to wait for Lua's GC.
+struct InputStateInfo {
+       InputStateInfo(const InputState& input_state);
+
+       unsigned last_width[MAX_CARDS], last_height[MAX_CARDS];
+};
+
+InputStateInfo::InputStateInfo(const InputState &input_state)
+{
+       for (unsigned signal_num = 0; signal_num < MAX_CARDS; ++signal_num) {
+               BufferedFrame frame = input_state.buffered_frames[signal_num][0];
+               if (frame.frame == nullptr) {
+                       last_width[signal_num] = last_height[signal_num] = 0;
+                       continue;
+               }
+               const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+               last_width[signal_num] = userdata->last_width[frame.field_number];
+               last_height[signal_num] = userdata->last_height[frame.field_number];
+       }
+}
+
 class LuaRefWithDeleter {
 public:
        LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
@@ -90,6 +113,15 @@ Effect *get_effect(lua_State *L, int idx)
        return nullptr;
 }
 
+InputStateInfo *get_input_state_info(lua_State *L, int idx)
+{
+       if (luaL_testudata(L, idx, "InputStateInfo")) {
+               return (InputStateInfo *)lua_touserdata(L, idx);
+       }
+       luaL_error(L, "Error: Index #%d was not InputStateInfo\n", idx);
+       return nullptr;
+}
+
 bool checkbool(lua_State* L, int idx)
 {
        luaL_checktype(L, idx, LUA_TBOOLEAN);
@@ -210,22 +242,6 @@ int LiveInputWrapper_connect_signal(lua_State* L)
        return 0;
 }
 
-int LiveInputWrapper_get_width(lua_State* L)
-{
-       assert(lua_gettop(L) == 1);
-       LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
-       lua_pushnumber(L, input->get_width());
-       return 1;
-}
-
-int LiveInputWrapper_get_height(lua_State* L)
-{
-       assert(lua_gettop(L) == 1);
-       LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
-       lua_pushnumber(L, input->get_height());
-       return 1;
-}
-
 int ImageInput_new(lua_State* L)
 {
        assert(lua_gettop(L) == 1);
@@ -275,6 +291,26 @@ int MixEffect_new(lua_State* L)
        return wrap_lua_object<MixEffect>(L, "MixEffect");
 }
 
+int InputStateInfo_get_width(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushnumber(L, input_state_info->last_width[signal_num]);
+       return 1;
+}
+
+int InputStateInfo_get_height(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+       lua_pushnumber(L, input_state_info->last_height[signal_num]);
+       return 1;
+}
+
 int Effect_set_float(lua_State *L)
 {
        assert(lua_gettop(L) == 3);
@@ -342,9 +378,6 @@ const luaL_Reg EffectChain_funcs[] = {
 
 const luaL_Reg LiveInputWrapper_funcs[] = {
        { "connect_signal", LiveInputWrapper_connect_signal },
-       // These are only valid during calls to get_chain and the setup chain callback.
-       { "get_width", LiveInputWrapper_get_width },
-       { "get_height", LiveInputWrapper_get_height },
        { NULL, NULL }
 };
 
@@ -420,6 +453,12 @@ const luaL_Reg MixEffect_funcs[] = {
        { NULL, NULL }
 };
 
+const luaL_Reg InputStateInfo_funcs[] = {
+       { "get_width", InputStateInfo_get_width },
+       { "get_height", InputStateInfo_get_height },
+       { NULL, NULL }
+};
+
 }  // namespace
 
 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
@@ -477,26 +516,6 @@ void LiveInputWrapper::connect_signal(int signal_num)
        input->set_height(userdata->last_height[frame.field_number]);
 }
 
-unsigned LiveInputWrapper::get_width() const
-{
-       if (last_connected_signal_num == -1) {
-               return 0;
-       }
-       BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
-       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
-       return userdata->last_width[frame.field_number];
-}
-
-unsigned LiveInputWrapper::get_height() const
-{
-       if (last_connected_signal_num == -1) {
-               return 0;
-       }
-       BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
-       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
-       return userdata->last_height[frame.field_number];
-}
-
 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
        : resource_pool(resource_pool), num_cards(num_cards)
 {
@@ -513,6 +532,7 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_car
        register_class("OverlayEffect", OverlayEffect_funcs);
        register_class("ResizeEffect", ResizeEffect_funcs);
        register_class("MixEffect", MixEffect_funcs);
+       register_class("InputStateInfo", InputStateInfo_funcs);
 
        // Run script.
        lua_settop(L, 0);
@@ -539,12 +559,12 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_car
 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
 {
        assert(lua_gettop(L) == 0);
-       luaL_newmetatable(L, class_name);
+       luaL_newmetatable(L, class_name);  // mt = {}
        lua_pushlightuserdata(L, this);
-       luaL_setfuncs(L, funcs, 1);
+       luaL_setfuncs(L, funcs, 1);        // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
        lua_pushvalue(L, -1);
-       lua_setfield(L, -2, "__index");
-       lua_setglobal(L, class_name);
+       lua_setfield(L, -2, "__index");    // mt.__index = mt
+       lua_setglobal(L, class_name);      // ClassName = mt
        assert(lua_gettop(L) == 0);
 }
 
@@ -559,9 +579,9 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he
        lua_pushnumber(L, t);
        lua_pushnumber(L, width);
        lua_pushnumber(L, height);
+       wrap_lua_object<InputStateInfo>(L, "InputStateInfo", input_state);
 
-       this->input_state = &input_state;
-       if (lua_pcall(L, 4, 2, 0) != 0) {
+       if (lua_pcall(L, 5, 2, 0) != 0) {
                fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
                exit(1);
        }