]> git.sesse.net Git - nageru/blobdiff - theme.cpp
Hook up MixEffect, and add fades to the theme.
[nageru] / theme.cpp
index 410ac746c4f713f8b43dc32d0174483b8a682f25..50adca52c770c856792c2fcb0fdb78aad2b4255c 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -11,6 +11,8 @@
 #include <movit/resample_effect.h>
 #include <movit/padding_effect.h>
 #include <movit/overlay_effect.h>
+#include <movit/resize_effect.h>
+#include <movit/mix_effect.h>
 
 #include "theme.h"
 
@@ -50,7 +52,9 @@ Effect *get_effect(lua_State *L, int idx)
            luaL_testudata(L, idx, "ResampleEffect") ||
            luaL_testudata(L, idx, "PaddingEffect") ||
            luaL_testudata(L, idx, "IntegralPaddingEffect") ||
-           luaL_testudata(L, idx, "OverlayEffect")) {
+           luaL_testudata(L, idx, "OverlayEffect") ||
+           luaL_testudata(L, idx, "ResizeEffect") ||
+           luaL_testudata(L, idx, "MixEffect")) {
                return (Effect *)lua_touserdata(L, idx);
        }
        fprintf(stderr, "Error: Index #%d was not an Effect type\n", idx);
@@ -87,18 +91,27 @@ int EffectChain_add_effect(lua_State* L)
 
        // TODO: Better error reporting.
        Effect *effect = get_effect(L, 2);
-       vector<Effect *> inputs;
-       for (int idx = 3; idx <= lua_gettop(L); ++idx) {
-               if (luaL_testudata(L, idx, "LiveInputWrapper")) {
-                       LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
-                       inputs.push_back(input->get_input());
-               } else {
-                       inputs.push_back(get_effect(L, idx));
+       if (lua_gettop(L) == 2) {
+               chain->add_effect(effect);
+       } else {
+               vector<Effect *> inputs;
+               for (int idx = 3; idx <= lua_gettop(L); ++idx) {
+                       if (luaL_testudata(L, idx, "LiveInputWrapper")) {
+                               LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
+                               inputs.push_back(input->get_input());
+                       } else {
+                               inputs.push_back(get_effect(L, idx));
+                       }
                }
+               chain->add_effect(effect, inputs);
        }
-       chain->add_effect(effect, inputs);
 
        lua_settop(L, 2);  // Return the effect itself.
+
+       // Make sure Lua doesn't garbage-collect it away.
+       lua_pushvalue(L, -1);
+       luaL_ref(L, LUA_REGISTRYINDEX);  // TODO: leak?
+
        return 1;
 }
 
@@ -116,7 +129,7 @@ int EffectChain_finalize(lua_State* L)
                YCbCrFormat output_ycbcr_format;
                output_ycbcr_format.chroma_subsampling_x = 1;
                output_ycbcr_format.chroma_subsampling_y = 1;
-               output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
+               output_ycbcr_format.luma_coefficients = YCBCR_REC_709;
                output_ycbcr_format.full_range = false;
 
                chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
@@ -168,6 +181,18 @@ int OverlayEffect_new(lua_State* L)
        return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
 }
 
+int ResizeEffect_new(lua_State* L)
+{
+       assert(lua_gettop(L) == 0);
+       return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
+}
+
+int MixEffect_new(lua_State* L)
+{
+       assert(lua_gettop(L) == 0);
+       return wrap_lua_object<MixEffect>(L, "MixEffect");
+}
+
 int Effect_set_float(lua_State *L)
 {
        assert(lua_gettop(L) == 3);
@@ -192,6 +217,22 @@ int Effect_set_int(lua_State *L)
        return 0;
 }
 
+int Effect_set_vec4(lua_State *L)
+{
+       assert(lua_gettop(L) == 6);
+       Effect *effect = (Effect *)get_effect(L, 1);
+       size_t len;
+       const char* cstr = lua_tolstring(L, 2, &len);
+       std::string key(cstr, len);
+       float v[4];
+       v[0] = luaL_checknumber(L, 3);
+       v[1] = luaL_checknumber(L, 4);
+       v[2] = luaL_checknumber(L, 5);
+       v[3] = luaL_checknumber(L, 6);
+       (void)effect->set_vec4(key, v);
+       return 0;
+}
+
 const luaL_Reg EffectChain_funcs[] = {
        { "new", EffectChain_new },
        { "add_live_input", EffectChain_add_live_input },
@@ -209,6 +250,7 @@ const luaL_Reg WhiteBalanceEffect_funcs[] = {
        { "new", WhiteBalanceEffect_new },
        { "set_float", Effect_set_float },
        { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
        { NULL, NULL }
 };
 
@@ -216,6 +258,7 @@ const luaL_Reg ResampleEffect_funcs[] = {
        { "new", ResampleEffect_new },
        { "set_float", Effect_set_float },
        { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
        { NULL, NULL }
 };
 
@@ -223,6 +266,7 @@ const luaL_Reg PaddingEffect_funcs[] = {
        { "new", PaddingEffect_new },
        { "set_float", Effect_set_float },
        { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
        { NULL, NULL }
 };
 
@@ -230,6 +274,7 @@ const luaL_Reg IntegralPaddingEffect_funcs[] = {
        { "new", IntegralPaddingEffect_new },
        { "set_float", Effect_set_float },
        { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
        { NULL, NULL }
 };
 
@@ -237,6 +282,23 @@ const luaL_Reg OverlayEffect_funcs[] = {
        { "new", OverlayEffect_new },
        { "set_float", Effect_set_float },
        { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
+       { NULL, NULL }
+};
+
+const luaL_Reg ResizeEffect_funcs[] = {
+       { "new", ResizeEffect_new },
+       { "set_float", Effect_set_float },
+       { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
+       { NULL, NULL }
+};
+
+const luaL_Reg MixEffect_funcs[] = {
+       { "new", MixEffect_new },
+       { "set_float", Effect_set_float },
+       { "set_int", Effect_set_int },
+       { "set_vec4", Effect_set_vec4 },
        { NULL, NULL }
 };
 
@@ -283,6 +345,8 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool)
        register_class("PaddingEffect", PaddingEffect_funcs);
        register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
        register_class("OverlayEffect", OverlayEffect_funcs);
+       register_class("ResizeEffect", ResizeEffect_funcs);
+       register_class("MixEffect", MixEffect_funcs);
 
        // Run script.
        lua_settop(L, 0);
@@ -297,7 +361,7 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool)
        lua_getglobal(L, "num_channels");
 
        if (lua_pcall(L, 0, 1, 0) != 0) {
-               fprintf(stderr, "error running function `num_channels': %s", lua_tostring(L, -1));
+               fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
                exit(1);
        }
 
@@ -325,7 +389,7 @@ Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
        lua_pushnumber(L, height);
 
        if (lua_pcall(L, 4, 2, 0) != 0) {
-               fprintf(stderr, "error running function `get_chain': %s", lua_tostring(L, -1));
+               fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
                exit(1);
        }
 
@@ -342,10 +406,32 @@ Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
 
                // Set up state, including connecting signals.
                lua_rawgeti(L, LUA_REGISTRYINDEX, funcref);
-               lua_pcall(L, 0, 0, 0);
+               if (lua_pcall(L, 0, 0, 0) != 0) {
+                       fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
+                       exit(1);
+               }
        });
 }
 
+std::vector<std::string> Theme::get_transition_names(float t)
+{
+       unique_lock<mutex> lock(m);
+       lua_getglobal(L, "get_transitions");
+       lua_pushnumber(L, t);
+       if (lua_pcall(L, 1, 1, 0) != 0) {
+               fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
+               exit(1);
+       }
+
+       std::vector<std::string> ret;
+       lua_pushnil(L);
+       while (lua_next(L, -2) != 0) {
+               ret.push_back(lua_tostring(L, -1));
+               lua_pop(L, 1);
+       }
+       return ret;
+}      
+
 void Theme::connect_signal(YCbCrInput *input, int signal_num)
 {
        input->set_texture_num(0, input_textures[signal_num].tex_y);
@@ -360,7 +446,19 @@ void Theme::transition_clicked(int transition_num, float t)
        lua_pushnumber(L, t);
 
        if (lua_pcall(L, 2, 0, 0) != 0) {
-               fprintf(stderr, "error running function `transition_clicked': %s", lua_tostring(L, -1));
+               fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
+               exit(1);
+       }
+}
+
+void Theme::channel_clicked(int preview_num)
+{
+       unique_lock<mutex> lock(m);
+       lua_getglobal(L, "channel_clicked");
+       lua_pushnumber(L, preview_num);
+
+       if (lua_pcall(L, 1, 0, 0) != 0) {
+               fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
                exit(1);
        }
 }