new lua bindings for textures

- output:renderer
- renderer:texture_from_pixels
- renderer:draw_texture

We need to be able to access the renderer outside of the post_render
hook (hence output:renderer) because we can't call
wlr_texture_from_pixels while rendering

texture_from_pixels accepts a string as the data argument not a
userdata, because that matches what we getk from GdkPixbuf.Pixbuf
new_from_file when using gobject-introspection.
phoen
Daniel Barlow 2022-04-28 20:27:09 +01:00
parent 12d0e93a3e
commit f626324825
2 changed files with 92 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include "luak/kiwmi_lua_callback.h"
#include "luak/lua_compat.h"
#include "server.h"
#include "luak/kiwmi_renderer.h"
static int
l_kiwmi_output_auto(lua_State *L)
@ -188,6 +189,36 @@ l_kiwmi_output_usable_area(lua_State *L)
return 1;
}
static int
l_kiwmi_output_renderer(lua_State *L)
{
struct kiwmi_object *obj =
*(struct kiwmi_object **)luaL_checkudata(L, 1, "kiwmi_output");
if (!obj->valid) {
return luaL_error(L, "kiwmi_output no longer valid");
}
struct kiwmi_output *output = obj->object;
struct kiwmi_desktop *desktop = output->desktop;
struct kiwmi_server *server = wl_container_of(desktop, server, desktop);
struct wlr_renderer *renderer = server->renderer;
lua_pushcfunction(L, luaK_kiwmi_renderer_new);
lua_pushlightuserdata(L, server->lua);
lua_pushlightuserdata(L, renderer);
lua_pushlightuserdata(L, output->wlr_output);
if (lua_pcall(L, 3, 1, 0)) {
wlr_log(WLR_ERROR, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
return 0;
}
return 1;
}
static const luaL_Reg kiwmi_output_methods[] = {
{"auto", l_kiwmi_output_auto},
{"move", l_kiwmi_output_move},
@ -195,6 +226,7 @@ static const luaL_Reg kiwmi_output_methods[] = {
{"on", luaK_callback_register_dispatch},
{"pos", l_kiwmi_output_pos},
{"redraw", l_kiwmi_output_redraw},
{"renderer", l_kiwmi_output_renderer},
{"size", l_kiwmi_output_size},
{"set_mode", l_kiwmi_output_set_mode},
{"usable_area", l_kiwmi_output_usable_area},

View File

@ -15,6 +15,8 @@
#include <wlr/types/wlr_output.h>
#include <wlr/util/log.h>
#include <drm/drm_fourcc.h>
#include "color.h"
#include "desktop/output.h"
#include "luak/lua_compat.h"
@ -57,8 +59,66 @@ l_kiwmi_renderer_draw_rect(lua_State *L)
return 0;
}
static int
l_kiwmi_renderer_draw_texture(lua_State *L)
{
struct kiwmi_renderer *renderer =
(struct kiwmi_renderer *) luaL_checkudata(L, 1, "kiwmi_renderer");
struct wlr_texture *texture = (struct wlr_texture *) lua_touserdata(L, 2);
float projection[9];
for(int i=0; i<9; i++) {
lua_pushnumber(L, i+1);
lua_gettable(L, 3);
projection[i] = lua_tointeger(L, -1);
}
luaL_checktype(L, 4, LUA_TNUMBER); /* x */
luaL_checktype(L, 5, LUA_TNUMBER); /* y */
luaL_checktype(L, 6, LUA_TNUMBER); // alpha
struct wlr_renderer *wlr_renderer = renderer->wlr_renderer;
wlr_render_texture(wlr_renderer, texture, projection,
lua_tonumber(L, 4),
lua_tonumber(L, 5),
lua_tonumber(L, 6));
return 0;
}
static int
l_kiwmi_renderer_texture_from_pixels(lua_State *L)
{
struct kiwmi_renderer *renderer =
(struct kiwmi_renderer *)luaL_checkudata(L, 1, "kiwmi_renderer");
struct wlr_renderer *wlr_renderer = renderer->wlr_renderer;
struct wlr_texture * texture;
luaL_checktype(L, 2, LUA_TNUMBER); // stride
luaL_checktype(L, 3, LUA_TNUMBER); // width
luaL_checktype(L, 4, LUA_TNUMBER); // height
char *data = lua_tostring(L, 5);
texture = wlr_texture_from_pixels(wlr_renderer,
DRM_FORMAT_ARGB8888,
lua_tonumber(L, 2),
lua_tonumber(L, 3),
lua_tonumber(L, 4),
data);
lua_pushlightuserdata(L, texture);
return 1;
}
static const luaL_Reg kiwmi_renderer_methods[] = {
{"draw_rect", l_kiwmi_renderer_draw_rect},
{"draw_texture", l_kiwmi_renderer_draw_texture},
{"texture_from_pixels", l_kiwmi_renderer_texture_from_pixels},
{NULL, NULL},
};