]> git.sesse.net Git - nageru/blobdiff - theme.cpp
Shorten white_balance_effect in the Lua code.
[nageru] / theme.cpp
index 53a96d2475ac38d929edc97fad201a69cd589358..defdca171b54448bba2ddce336260ca3e2a95dbe 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -37,7 +37,28 @@ extern Mixer *global_mixer;
 
 namespace {
 
-vector<LiveInputWrapper *> live_inputs;
+// 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:
@@ -92,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);
@@ -261,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);
@@ -403,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)
@@ -445,24 +501,19 @@ LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool overri
 void LiveInputWrapper::connect_signal(int signal_num)
 {
        if (global_mixer == nullptr) {
+               // No data yet.
                return;
        }
 
        signal_num = theme->map_signal(signal_num);
 
-       Mixer::BufferedFrame frame = global_mixer->get_buffered_frame(signal_num, 0);
+       BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
        const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
 
        input->set_texture_num(0, userdata->tex_y[frame.field_number]);
        input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
        input->set_width(userdata->last_width[frame.field_number]);
        input->set_height(userdata->last_height[frame.field_number]);
-
-       // Hold on to the refcount so that we don't release the input frame
-       // until we're done rendering from it.
-       if (theme->used_input_frames_collector != nullptr) {
-               theme->used_input_frames_collector->push_back(frame.frame);
-       }
 }
 
 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
@@ -481,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);
@@ -507,16 +559,16 @@ 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);
 }
 
-Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
+Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state) 
 {
        Chain chain;
 
@@ -527,13 +579,12 @@ 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->used_input_frames_collector = &chain.input_frames;
-       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);
        }
-       this->used_input_frames_collector = nullptr;
 
        chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
        if (!lua_isfunction(L, -1)) {
@@ -545,9 +596,11 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he
        lua_pop(L, 2);
        assert(lua_gettop(L) == 0);
 
-       chain.setup_chain = [this, funcref]{
+       chain.setup_chain = [this, funcref, input_state]{
                unique_lock<mutex> lock(m);
 
+               this->input_state = &input_state;
+
                // Set up state, including connecting signals.
                lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
                if (lua_pcall(L, 0, 0, 0) != 0) {
@@ -557,6 +610,14 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he
                assert(lua_gettop(L) == 0);
        };
 
+       // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
+       // Actually, setup_chain does maybe hold all the references we need now anyway?
+       for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
+               for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
+                       chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
+               }
+       }
+
        return chain;
 }