obs-studio/libobs/obs-scene.c

883 lines
21 KiB
C
Raw Normal View History

2013-10-01 02:37:13 +00:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
2013-10-01 02:37:13 +00:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "util/threading.h"
2013-10-01 02:37:13 +00:00
#include "graphics/math-defs.h"
#include "obs-scene.h"
static const char *obs_scene_signals[] = {
"void item_add(ptr scene, ptr item)",
"void item_remove(ptr scene, ptr item)",
2014-05-16 00:40:53 +00:00
"void item_move_up(ptr scene, ptr item)",
"void item_move_down(ptr scene, ptr item)",
"void item_move_top(ptr scene, ptr item)",
"void item_move_bottom(ptr scene, ptr item)",
"void item_select(ptr scene, ptr item)",
"void item_deselect(ptr scene, ptr item)",
"void item_transform(ptr scene, ptr item)",
NULL
};
static inline void signal_item_remove(struct obs_scene_item *item)
{
struct calldata params = {0};
calldata_set_ptr(&params, "scene", item->parent);
calldata_set_ptr(&params, "item", item);
signal_handler_signal(item->parent->source->context.signals,
"item_remove", &params);
calldata_free(&params);
}
static const char *scene_getname(void)
{
/* TODO: locale */
return "Scene";
}
static void *scene_create(obs_data_t settings, struct obs_source *source)
2013-10-01 02:37:13 +00:00
{
pthread_mutexattr_t attr;
2013-10-01 02:37:13 +00:00
struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
scene->source = source;
scene->first_item = NULL;
signal_handler_add_array(obs_source_get_signal_handler(source),
obs_scene_signals);
if (pthread_mutexattr_init(&attr) != 0)
goto fail;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
goto fail;
if (pthread_mutex_init(&scene->mutex, &attr) != 0) {
blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
goto fail;
}
2013-10-01 02:37:13 +00:00
UNUSED_PARAMETER(settings);
2013-10-01 02:37:13 +00:00
return scene;
fail:
pthread_mutexattr_destroy(&attr);
bfree(scene);
return NULL;
2013-10-01 02:37:13 +00:00
}
static void remove_all_items(struct obs_scene *scene)
2013-10-01 02:37:13 +00:00
{
2014-01-30 19:41:11 +00:00
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
2013-10-01 02:37:13 +00:00
2014-01-30 19:41:11 +00:00
item = scene->first_item;
while (item) {
struct obs_scene_item *del_item = item;
item = item->next;
obs_sceneitem_remove(del_item);
}
2013-10-01 02:37:13 +00:00
pthread_mutex_unlock(&scene->mutex);
}
static void scene_destroy(void *data)
{
struct obs_scene *scene = data;
remove_all_items(scene);
pthread_mutex_destroy(&scene->mutex);
2013-10-01 02:37:13 +00:00
bfree(scene);
}
static void scene_enum_sources(void *data,
obs_source_enum_proc_t enum_callback,
void *param)
{
struct obs_scene *scene = data;
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
struct obs_scene_item *next = item->next;
obs_sceneitem_addref(item);
enum_callback(scene->source, item->source, param);
obs_sceneitem_release(item);
item = next;
}
pthread_mutex_unlock(&scene->mutex);
}
static inline void detach_sceneitem(struct obs_scene_item *item)
{
if (item->prev)
item->prev->next = item->next;
else
item->parent->first_item = item->next;
if (item->next)
item->next->prev = item->prev;
item->parent = NULL;
}
2014-05-16 00:40:53 +00:00
static inline void attach_sceneitem(struct obs_scene *parent,
struct obs_scene_item *item, struct obs_scene_item *prev)
{
2014-05-16 00:40:53 +00:00
item->prev = prev;
item->parent = parent;
if (prev) {
item->next = prev->next;
if (prev->next)
prev->next->prev = item;
prev->next = item;
} else {
item->next = item->parent->first_item;
item->parent->first_item = item;
}
}
static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
{
if (align & OBS_ALIGN_RIGHT)
v->x += (float)cx;
else if ((align & OBS_ALIGN_LEFT) == 0)
v->x += (float)(cx / 2);
if (align & OBS_ALIGN_BOTTOM)
v->y += (float)cy;
else if ((align & OBS_ALIGN_TOP) == 0)
v->y += (float)(cy / 2);
}
static void calculate_bounds_data(struct obs_scene_item *item,
struct vec2 *origin, struct vec2 *scale,
uint32_t *cx, uint32_t *cy)
{
UI: Add scene editing So, scene editing was interesting (and by interesting I mean excruciating). I almost implemented 'manipulator' visuals (ala 3dsmax for example), and used 3 modes for controlling position/rotation/size, but in a 2D editing, it felt clunky, so I defaulted back to simply click-and-drag for movement, and then took a similar though slightly different looking approach for handling scaling and reszing. I also added a number of menu item helpers related to positioning, scaling, rotating, flipping, and resetting the transform back to default. There is also a new 'transform' dialog (accessible via menu) which will allow you to manually edit every single transform variable of a scene item directly if desired. If a scene item does not have bounds active, pulling on the sides of a source will cause it to resize it via base scale rather than by the bounding box system (if the source resizes that scale will apply). If bounds are active, it will modify the bounding box only instead. How a source scales when a bounding box is active depends on the type of bounds being used. You can set it to scale to the inner bounds, the outer bounds, scale to bounds width only, scale to bounds height only, and a setting to stretch to bounds (which forces a source to always draw at the bounding box size rather than be affected by its internal size). You can also set it to be used as a 'maximum' size, so that the source doesn't necessarily get scaled unless it extends beyond the bounds. Like in OBS1, objects will snap to the edges unless the control key is pressed. However, this will now happen even if the object is rotated or oriented in any strange way. Snapping will also occur when stretching or changing the bounding box size.
2014-06-15 07:54:48 +00:00
float width = (float)(*cx) * fabsf(scale->x);
float height = (float)(*cy) * fabsf(scale->y);
float item_aspect = width / height;
float bounds_aspect = item->bounds.x / item->bounds.y;
uint32_t bounds_type = item->bounds_type;
float width_diff, height_diff;
if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
if (width > item->bounds.x || height > item->bounds.y)
bounds_type = OBS_BOUNDS_SCALE_INNER;
if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
bounds_type == OBS_BOUNDS_SCALE_OUTER) {
bool use_width = (bounds_aspect < item_aspect);
float mul;
if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
use_width = !use_width;
mul = use_width ?
item->bounds.x / width :
item->bounds.y / height;
vec2_mulf(scale, scale, mul);
UI: Add scene editing So, scene editing was interesting (and by interesting I mean excruciating). I almost implemented 'manipulator' visuals (ala 3dsmax for example), and used 3 modes for controlling position/rotation/size, but in a 2D editing, it felt clunky, so I defaulted back to simply click-and-drag for movement, and then took a similar though slightly different looking approach for handling scaling and reszing. I also added a number of menu item helpers related to positioning, scaling, rotating, flipping, and resetting the transform back to default. There is also a new 'transform' dialog (accessible via menu) which will allow you to manually edit every single transform variable of a scene item directly if desired. If a scene item does not have bounds active, pulling on the sides of a source will cause it to resize it via base scale rather than by the bounding box system (if the source resizes that scale will apply). If bounds are active, it will modify the bounding box only instead. How a source scales when a bounding box is active depends on the type of bounds being used. You can set it to scale to the inner bounds, the outer bounds, scale to bounds width only, scale to bounds height only, and a setting to stretch to bounds (which forces a source to always draw at the bounding box size rather than be affected by its internal size). You can also set it to be used as a 'maximum' size, so that the source doesn't necessarily get scaled unless it extends beyond the bounds. Like in OBS1, objects will snap to the edges unless the control key is pressed. However, this will now happen even if the object is rotated or oriented in any strange way. Snapping will also occur when stretching or changing the bounding box size.
2014-06-15 07:54:48 +00:00
} else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
vec2_mulf(scale, scale, item->bounds.x / width);
UI: Add scene editing So, scene editing was interesting (and by interesting I mean excruciating). I almost implemented 'manipulator' visuals (ala 3dsmax for example), and used 3 modes for controlling position/rotation/size, but in a 2D editing, it felt clunky, so I defaulted back to simply click-and-drag for movement, and then took a similar though slightly different looking approach for handling scaling and reszing. I also added a number of menu item helpers related to positioning, scaling, rotating, flipping, and resetting the transform back to default. There is also a new 'transform' dialog (accessible via menu) which will allow you to manually edit every single transform variable of a scene item directly if desired. If a scene item does not have bounds active, pulling on the sides of a source will cause it to resize it via base scale rather than by the bounding box system (if the source resizes that scale will apply). If bounds are active, it will modify the bounding box only instead. How a source scales when a bounding box is active depends on the type of bounds being used. You can set it to scale to the inner bounds, the outer bounds, scale to bounds width only, scale to bounds height only, and a setting to stretch to bounds (which forces a source to always draw at the bounding box size rather than be affected by its internal size). You can also set it to be used as a 'maximum' size, so that the source doesn't necessarily get scaled unless it extends beyond the bounds. Like in OBS1, objects will snap to the edges unless the control key is pressed. However, this will now happen even if the object is rotated or oriented in any strange way. Snapping will also occur when stretching or changing the bounding box size.
2014-06-15 07:54:48 +00:00
} else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
vec2_mulf(scale, scale, item->bounds.y / height);
UI: Add scene editing So, scene editing was interesting (and by interesting I mean excruciating). I almost implemented 'manipulator' visuals (ala 3dsmax for example), and used 3 modes for controlling position/rotation/size, but in a 2D editing, it felt clunky, so I defaulted back to simply click-and-drag for movement, and then took a similar though slightly different looking approach for handling scaling and reszing. I also added a number of menu item helpers related to positioning, scaling, rotating, flipping, and resetting the transform back to default. There is also a new 'transform' dialog (accessible via menu) which will allow you to manually edit every single transform variable of a scene item directly if desired. If a scene item does not have bounds active, pulling on the sides of a source will cause it to resize it via base scale rather than by the bounding box system (if the source resizes that scale will apply). If bounds are active, it will modify the bounding box only instead. How a source scales when a bounding box is active depends on the type of bounds being used. You can set it to scale to the inner bounds, the outer bounds, scale to bounds width only, scale to bounds height only, and a setting to stretch to bounds (which forces a source to always draw at the bounding box size rather than be affected by its internal size). You can also set it to be used as a 'maximum' size, so that the source doesn't necessarily get scaled unless it extends beyond the bounds. Like in OBS1, objects will snap to the edges unless the control key is pressed. However, this will now happen even if the object is rotated or oriented in any strange way. Snapping will also occur when stretching or changing the bounding box size.
2014-06-15 07:54:48 +00:00
} else if (bounds_type == OBS_BOUNDS_STRETCH) {
scale->x = item->bounds.x / (float)(*cx);
scale->y = item->bounds.y / (float)(*cy);
}
width = (float)(*cx) * scale->x;
height = (float)(*cy) * scale->y;
width_diff = item->bounds.x - width;
height_diff = item->bounds.y - height;
*cx = (uint32_t)item->bounds.x;
*cy = (uint32_t)item->bounds.y;
add_alignment(origin, item->bounds_align,
(int)-width_diff, (int)-height_diff);
}
static void update_item_transform(struct obs_scene_item *item)
{
uint32_t width = obs_source_get_width(item->source);
uint32_t height = obs_source_get_height(item->source);
uint32_t cx = width;
uint32_t cy = height;
struct vec2 base_origin;
struct vec2 origin;
struct vec2 scale = item->scale;
struct calldata params = {0};
vec2_zero(&base_origin);
vec2_zero(&origin);
/* ----------------------- */
if (item->bounds_type != OBS_BOUNDS_NONE) {
calculate_bounds_data(item, &origin, &scale, &cx, &cy);
} else {
cx = (uint32_t)((float)cx * scale.x);
cy = (uint32_t)((float)cy * scale.y);
}
add_alignment(&origin, item->align, (int)cx, (int)cy);
matrix4_identity(&item->draw_transform);
matrix4_scale3f(&item->draw_transform, &item->draw_transform,
scale.x, scale.y, 1.0f);
matrix4_translate3f(&item->draw_transform, &item->draw_transform,
-origin.x, -origin.y, 0.0f);
matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
0.0f, 0.0f, 1.0f, RAD(item->rot));
matrix4_translate3f(&item->draw_transform, &item->draw_transform,
item->pos.x, item->pos.y, 0.0f);
/* ----------------------- */
if (item->bounds_type != OBS_BOUNDS_NONE) {
vec2_copy(&scale, &item->bounds);
} else {
scale.x = (float)width * item->scale.x;
scale.y = (float)height * item->scale.y;
}
add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
matrix4_identity(&item->box_transform);
matrix4_scale3f(&item->box_transform, &item->box_transform,
scale.x, scale.y, 1.0f);
matrix4_translate3f(&item->box_transform, &item->box_transform,
-base_origin.x, -base_origin.y, 0.0f);
matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
0.0f, 0.0f, 1.0f, RAD(item->rot));
matrix4_translate3f(&item->box_transform, &item->box_transform,
item->pos.x, item->pos.y, 0.0f);
/* ----------------------- */
item->last_width = width;
item->last_height = height;
calldata_set_ptr(&params, "scene", item->parent);
calldata_set_ptr(&params, "item", item);
signal_handler_signal(item->parent->source->context.signals,
"item_transform", &params);
calldata_free(&params);
}
static inline bool source_size_changed(struct obs_scene_item *item)
{
uint32_t width = obs_source_get_width(item->source);
uint32_t height = obs_source_get_height(item->source);
return item->last_width != width || item->last_height != height;
}
(API Change) Improve graphics API consistency Summary: - Prefix all graphics subsystem names with gs_ or GS_ - Unsquish funciton names (for example _setfloat to _set_float) - Changed create functions to be more consistent with the rest of the API elsewhere. For exmaple, instead of gs_create_texture/gs_texture_destroy, it's now gs_texture_create/gs_texture_destroy - Renamed gs_stencil_op enum to gs_stencil_op_type From: To: ----------------------------------------------------------- tvertarray gs_tvertarray vb_data gs_vb_data vbdata_create gs_vbdata_create vbdata_destroy gs_vbdata_destroy shader_param gs_shader_param gs_effect gs_effect effect_technique gs_effect_technique effect_pass gs_effect_pass effect_param gs_effect_param texture_t gs_texture_t stagesurf_t gs_stagesurf_t zstencil_t gs_zstencil_t vertbuffer_t gs_vertbuffer_t indexbuffer_t gs_indexbuffer_t samplerstate_t gs_samplerstate_t swapchain_t gs_swapchain_t texrender_t gs_texrender_t shader_t gs_shader_t sparam_t gs_sparam_t effect_t gs_effect_t technique_t gs_technique_t eparam_t gs_eparam_t device_t gs_device_t graphics_t graphics_t shader_param_type gs_shader_param_type SHADER_PARAM_UNKNOWN GS_SHADER_PARAM_UNKNOWN SHADER_PARAM_BOOL GS_SHADER_PARAM_BOOL SHADER_PARAM_FLOAT GS_SHADER_PARAM_FLOAT SHADER_PARAM_INT GS_SHADER_PARAM_INT SHADER_PARAM_STRING GS_SHADER_PARAM_STRING SHADER_PARAM_VEC2 GS_SHADER_PARAM_VEC2 SHADER_PARAM_VEC3 GS_SHADER_PARAM_VEC3 SHADER_PARAM_VEC4 GS_SHADER_PARAM_VEC4 SHADER_PARAM_MATRIX4X4 GS_SHADER_PARAM_MATRIX4X4 SHADER_PARAM_TEXTURE GS_SHADER_PARAM_TEXTURE shader_param_info gs_shader_param_info shader_type gs_shader_type SHADER_VERTEX GS_SHADER_VERTEX SHADER_PIXEL GS_SHADER_PIXEL shader_destroy gs_shader_destroy shader_numparams gs_shader_get_num_params shader_getparambyidx gs_shader_get_param_by_idx shader_getparambyname gs_shader_get_param_by_name shader_getviewprojmatrix gs_shader_get_viewproj_matrix shader_getworldmatrix gs_shader_get_world_matrix shader_getparaminfo gs_shader_get_param_info shader_setbool gs_shader_set_bool shader_setfloat gs_shader_set_float shader_setint gs_shader_set_int shader_setmatrix3 gs_shader_setmatrix3 shader_setmatrix4 gs_shader_set_matrix4 shader_setvec2 gs_shader_set_vec2 shader_setvec3 gs_shader_set_vec3 shader_setvec4 gs_shader_set_vec4 shader_settexture gs_shader_set_texture shader_setval gs_shader_set_val shader_setdefault gs_shader_set_default effect_property_type gs_effect_property_type EFFECT_NONE GS_EFFECT_NONE EFFECT_BOOL GS_EFFECT_BOOL EFFECT_FLOAT GS_EFFECT_FLOAT EFFECT_COLOR GS_EFFECT_COLOR EFFECT_TEXTURE GS_EFFECT_TEXTURE effect_param_info gs_effect_param_info effect_destroy gs_effect_destroy effect_gettechnique gs_effect_get_technique technique_begin gs_technique_begin technique_end gs_technique_end technique_beginpass gs_technique_begin_pass technique_beginpassbyname gs_technique_begin_pass_by_name technique_endpass gs_technique_end_pass effect_numparams gs_effect_get_num_params effect_getparambyidx gs_effect_get_param_by_idx effect_getparambyname gs_effect_get_param_by_name effect_updateparams gs_effect_update_params effect_getviewprojmatrix gs_effect_get_viewproj_matrix effect_getworldmatrix gs_effect_get_world_matrix effect_getparaminfo gs_effect_get_param_info effect_setbool gs_effect_set_bool effect_setfloat gs_effect_set_float effect_setint gs_effect_set_int effect_setmatrix4 gs_effect_set_matrix4 effect_setvec2 gs_effect_set_vec2 effect_setvec3 gs_effect_set_vec3 effect_setvec4 gs_effect_set_vec4 effect_settexture gs_effect_set_texture effect_setval gs_effect_set_val effect_setdefault gs_effect_set_default texrender_create gs_texrender_create texrender_destroy gs_texrender_destroy texrender_begin gs_texrender_begin texrender_end gs_texrender_end texrender_reset gs_texrender_reset texrender_gettexture gs_texrender_get_texture GS_BUILDMIPMAPS GS_BUILD_MIPMAPS GS_RENDERTARGET GS_RENDER_TARGET gs_device_name gs_get_device_name gs_device_type gs_get_device_type gs_entercontext gs_enter_context gs_leavecontext gs_leave_context gs_getcontext gs_get_context gs_renderstart gs_render_start gs_renderstop gs_render_stop gs_rendersave gs_render_save gs_getinput gs_get_input gs_geteffect gs_get_effect gs_create_effect_from_file gs_effect_create_from_file gs_create_effect gs_effect_create gs_create_vertexshader_from_file gs_vertexshader_create_from_file gs_create_pixelshader_from_file gs_pixelshader_create_from_file gs_create_texture_from_file gs_texture_create_from_file gs_resetviewport gs_reset_viewport gs_set2dmode gs_set_2d_mode gs_set3dmode gs_set_3d_mode gs_create_swapchain gs_swapchain_create gs_getsize gs_get_size gs_getwidth gs_get_width gs_getheight gs_get_height gs_create_texture gs_texture_create gs_create_cubetexture gs_cubetexture_create gs_create_volumetexture gs_voltexture_create gs_create_zstencil gs_zstencil_create gs_create_stagesurface gs_stagesurface_create gs_create_samplerstate gs_samplerstate_create gs_create_vertexshader gs_vertexshader_create gs_create_pixelshader gs_pixelshader_create gs_create_vertexbuffer gs_vertexbuffer_create gs_create_indexbuffer gs_indexbuffer_create gs_gettexturetype gs_get_texture_type gs_load_defaultsamplerstate gs_load_default_samplerstate gs_getvertexshader gs_get_vertex_shader gs_getpixelshader gs_get_pixel_shader gs_getrendertarget gs_get_render_target gs_getzstenciltarget gs_get_zstencil_target gs_setrendertarget gs_set_render_target gs_setcuberendertarget gs_set_cube_render_target gs_beginscene gs_begin_scene gs_draw gs_draw gs_endscene gs_end_scene gs_setcullmode gs_set_cull_mode gs_getcullmode gs_get_cull_mode gs_enable_depthtest gs_enable_depth_test gs_enable_stenciltest gs_enable_stencil_test gs_enable_stencilwrite gs_enable_stencil_write gs_blendfunction gs_blend_function gs_depthfunction gs_depth_function gs_stencilfunction gs_stencil_function gs_stencilop gs_stencil_op gs_setviewport gs_set_viewport gs_getviewport gs_get_viewport gs_setscissorrect gs_set_scissor_rect gs_create_texture_from_iosurface gs_texture_create_from_iosurface gs_create_gdi_texture gs_texture_create_gdi gs_is_compressed_format gs_is_compressed_format gs_num_total_levels gs_get_total_levels texture_setimage gs_texture_set_image cubetexture_setimage gs_cubetexture_set_image swapchain_destroy gs_swapchain_destroy texture_destroy gs_texture_destroy texture_getwidth gs_texture_get_width texture_getheight gs_texture_get_height texture_getcolorformat gs_texture_get_color_format texture_map gs_texture_map texture_unmap gs_texture_unmap texture_isrect gs_texture_is_rect texture_getobj gs_texture_get_obj cubetexture_destroy gs_cubetexture_destroy cubetexture_getsize gs_cubetexture_get_size cubetexture_getcolorformat gs_cubetexture_get_color_format volumetexture_destroy gs_voltexture_destroy volumetexture_getwidth gs_voltexture_get_width volumetexture_getheight gs_voltexture_get_height volumetexture_getdepth gs_voltexture_getdepth volumetexture_getcolorformat gs_voltexture_get_color_format stagesurface_destroy gs_stagesurface_destroy stagesurface_getwidth gs_stagesurface_get_width stagesurface_getheight gs_stagesurface_get_height stagesurface_getcolorformat gs_stagesurface_get_color_format stagesurface_map gs_stagesurface_map stagesurface_unmap gs_stagesurface_unmap zstencil_destroy gs_zstencil_destroy samplerstate_destroy gs_samplerstate_destroy vertexbuffer_destroy gs_vertexbuffer_destroy vertexbuffer_flush gs_vertexbuffer_flush vertexbuffer_getdata gs_vertexbuffer_get_data indexbuffer_destroy gs_indexbuffer_destroy indexbuffer_flush gs_indexbuffer_flush indexbuffer_getdata gs_indexbuffer_get_data indexbuffer_numindices gs_indexbuffer_get_num_indices indexbuffer_gettype gs_indexbuffer_get_type texture_rebind_iosurface gs_texture_rebind_iosurface texture_get_dc gs_texture_get_dc texture_release_dc gs_texture_release_dc
2014-08-08 06:42:07 +00:00
static void scene_video_render(void *data, gs_effect_t effect)
2013-10-01 02:37:13 +00:00
{
struct obs_scene *scene = data;
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
2013-10-01 02:37:13 +00:00
while (item) {
if (obs_source_removed(item->source)) {
struct obs_scene_item *del_item = item;
item = item->next;
obs_sceneitem_remove(del_item);
continue;
}
if (source_size_changed(item))
update_item_transform(item);
2013-10-01 02:37:13 +00:00
gs_matrix_push();
gs_matrix_mul(&item->draw_transform);
obs_source_video_render(item->source);
2013-10-01 02:37:13 +00:00
gs_matrix_pop();
item = item->next;
2013-10-01 02:37:13 +00:00
}
pthread_mutex_unlock(&scene->mutex);
UNUSED_PARAMETER(effect);
}
static void scene_load_item(struct obs_scene *scene, obs_data_t item_data)
{
const char *name = obs_data_get_string(item_data, "name");
obs_source_t source = obs_get_source_by_name(name);
struct obs_scene_item *item;
if (!source) {
blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
name);
return;
}
item = obs_scene_add(scene, source);
obs_data_set_default_int(item_data, "align",
OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
item->rot = (float)obs_data_get_double(item_data, "rot");
item->align = (uint32_t)obs_data_get_int(item_data, "align");
item->visible = obs_data_get_bool(item_data, "visible");
obs_data_get_vec2(item_data, "pos", &item->pos);
obs_data_get_vec2(item_data, "scale", &item->scale);
item->bounds_type =
(enum obs_bounds_type)obs_data_get_int(item_data,
"bounds_type");
item->bounds_align =
(uint32_t)obs_data_get_int(item_data, "bounds_align");
obs_data_get_vec2(item_data, "bounds", &item->bounds);
obs_source_release(source);
update_item_transform(item);
}
static void scene_load(void *scene, obs_data_t settings)
{
obs_data_array_t items = obs_data_get_array(settings, "items");
size_t count, i;
remove_all_items(scene);
if (!items) return;
count = obs_data_array_count(items);
for (i = 0; i < count; i++) {
obs_data_t item_data = obs_data_array_item(items, i);
scene_load_item(scene, item_data);
obs_data_release(item_data);
}
obs_data_array_release(items);
}
2014-04-28 07:15:26 +00:00
static void scene_save_item(obs_data_array_t array, struct obs_scene_item *item)
{
obs_data_t item_data = obs_data_create();
const char *name = obs_source_get_name(item->source);
obs_data_set_string(item_data, "name", name);
obs_data_set_bool (item_data, "visible", item->visible);
obs_data_set_double(item_data, "rot", item->rot);
obs_data_set_vec2 (item_data, "pos", &item->pos);
obs_data_set_vec2 (item_data, "scale", &item->scale);
obs_data_set_int (item_data, "align", (int)item->align);
obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
obs_data_set_vec2 (item_data, "bounds", &item->bounds);
obs_data_array_push_back(array, item_data);
obs_data_release(item_data);
}
static void scene_save(void *data, obs_data_t settings)
{
struct obs_scene *scene = data;
obs_data_array_t array = obs_data_array_create();
struct obs_scene_item *item;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
2014-04-28 07:15:26 +00:00
scene_save_item(array, item);
item = item->next;
}
pthread_mutex_unlock(&scene->mutex);
obs_data_set_array(settings, "items", array);
obs_data_array_release(array);
}
static uint32_t scene_getwidth(void *data)
{
UNUSED_PARAMETER(data);
return obs->video.base_width;
2013-10-01 02:37:13 +00:00
}
static uint32_t scene_getheight(void *data)
2013-10-01 02:37:13 +00:00
{
UNUSED_PARAMETER(data);
return obs->video.base_height;
2013-10-01 02:37:13 +00:00
}
const struct obs_source_info scene_info =
2013-10-01 02:37:13 +00:00
{
.id = "scene",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
.get_name = scene_getname,
.create = scene_create,
.destroy = scene_destroy,
.video_render = scene_video_render,
.get_width = scene_getwidth,
.get_height = scene_getheight,
.load = scene_load,
.save = scene_save,
.enum_sources = scene_enum_sources
2013-10-01 02:37:13 +00:00
};
obs_scene_t obs_scene_create(const char *name)
2013-10-01 02:37:13 +00:00
{
struct obs_source *source =
obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL);
return source->context.data;
2013-10-01 02:37:13 +00:00
}
void obs_scene_addref(obs_scene_t scene)
{
if (scene)
obs_source_addref(scene->source);
}
void obs_scene_release(obs_scene_t scene)
2013-10-01 02:37:13 +00:00
{
if (scene)
obs_source_release(scene->source);
2013-10-01 02:37:13 +00:00
}
obs_source_t obs_scene_get_source(obs_scene_t scene)
2013-10-01 02:37:13 +00:00
{
return scene ? scene->source : NULL;
2013-10-01 02:37:13 +00:00
}
obs_scene_t obs_scene_from_source(obs_source_t source)
{
if (!source || source->info.id != scene_info.id)
return NULL;
return source->context.data;
}
obs_sceneitem_t obs_scene_find_source(obs_scene_t scene, const char *name)
{
struct obs_scene_item *item;
2014-02-24 05:39:33 +00:00
if (!scene)
return NULL;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
if (strcmp(item->source->context.name, name) == 0)
break;
item = item->next;
}
pthread_mutex_unlock(&scene->mutex);
return item;
}
void obs_scene_enum_items(obs_scene_t scene,
bool (*callback)(obs_scene_t, obs_sceneitem_t, void*),
void *param)
{
struct obs_scene_item *item;
2014-02-24 05:39:33 +00:00
if (!scene || !callback)
return;
pthread_mutex_lock(&scene->mutex);
item = scene->first_item;
while (item) {
struct obs_scene_item *next = item->next;
obs_sceneitem_addref(item);
if (!callback(scene, item, param)) {
obs_sceneitem_release(item);
break;
}
obs_sceneitem_release(item);
item = next;
}
pthread_mutex_unlock(&scene->mutex);
}
obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
2013-10-01 02:37:13 +00:00
{
struct obs_scene_item *last;
struct obs_scene_item *item;
struct calldata params = {0};
if (!scene)
return NULL;
if (!source) {
blog(LOG_ERROR, "Tried to add a NULL source to a scene");
return NULL;
}
item = bzalloc(sizeof(struct obs_scene_item));
2013-10-01 02:37:13 +00:00
item->source = source;
item->visible = true;
item->parent = scene;
item->ref = 1;
item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
2013-10-01 02:37:13 +00:00
vec2_set(&item->scale, 1.0f, 1.0f);
matrix4_identity(&item->draw_transform);
matrix4_identity(&item->box_transform);
2013-10-01 02:37:13 +00:00
obs_source_addref(source);
obs_source_add_child(scene->source, source);
pthread_mutex_lock(&scene->mutex);
last = scene->first_item;
if (!last) {
scene->first_item = item;
} else {
while (last->next)
last = last->next;
last->next = item;
item->prev = last;
}
pthread_mutex_unlock(&scene->mutex);
calldata_set_ptr(&params, "scene", scene);
calldata_set_ptr(&params, "item", item);
signal_handler_signal(scene->source->context.signals, "item_add",
&params);
calldata_free(&params);
2013-10-01 02:37:13 +00:00
return item;
}
static void obs_sceneitem_destroy(obs_sceneitem_t item)
2013-10-01 02:37:13 +00:00
{
if (item) {
if (item->source)
obs_source_release(item->source);
2013-10-01 02:37:13 +00:00
bfree(item);
}
}
void obs_sceneitem_addref(obs_sceneitem_t item)
{
if (item)
os_atomic_inc_long(&item->ref);
}
void obs_sceneitem_release(obs_sceneitem_t item)
{
if (!item)
return;
if (os_atomic_dec_long(&item->ref) == 0)
obs_sceneitem_destroy(item);
}
void obs_sceneitem_remove(obs_sceneitem_t item)
{
obs_scene_t scene;
if (!item)
return;
scene = item->parent;
if (scene)
pthread_mutex_lock(&scene->mutex);
if (item->removed) {
if (scene)
pthread_mutex_unlock(&scene->mutex);
return;
}
item->removed = true;
assert(scene != NULL);
assert(scene->source != NULL);
obs_source_remove_child(scene->source, item->source);
signal_item_remove(item);
detach_sceneitem(item);
pthread_mutex_unlock(&scene->mutex);
obs_sceneitem_release(item);
}
obs_scene_t obs_sceneitem_get_scene(obs_sceneitem_t item)
{
2014-02-24 05:39:33 +00:00
return item ? item->parent : NULL;
}
obs_source_t obs_sceneitem_get_source(obs_sceneitem_t item)
{
2014-02-24 05:39:33 +00:00
return item ? item->source : NULL;
2013-10-01 02:37:13 +00:00
}
void obs_sceneitem_select(obs_sceneitem_t item, bool select)
{
struct calldata params = {0};
const char *command = select ? "item_select" : "item_deselect";
if (!item || item->selected == select)
return;
item->selected = select;
calldata_set_ptr(&params, "scene", item->parent);
calldata_set_ptr(&params, "item", item);
signal_handler_signal(item->parent->source->context.signals,
command, &params);
calldata_free(&params);
}
bool obs_sceneitem_selected(obs_sceneitem_t item)
{
return item ? item->selected : false;
}
void obs_sceneitem_set_pos(obs_sceneitem_t item, const struct vec2 *pos)
2013-10-01 02:37:13 +00:00
{
if (item) {
2014-02-24 05:39:33 +00:00
vec2_copy(&item->pos, pos);
update_item_transform(item);
}
2013-10-01 02:37:13 +00:00
}
void obs_sceneitem_set_rot(obs_sceneitem_t item, float rot)
2013-10-01 02:37:13 +00:00
{
if (item) {
2014-02-24 05:39:33 +00:00
item->rot = rot;
update_item_transform(item);
}
2013-10-01 02:37:13 +00:00
}
void obs_sceneitem_set_scale(obs_sceneitem_t item, const struct vec2 *scale)
2013-10-01 02:37:13 +00:00
{
if (item) {
vec2_copy(&item->scale, scale);
update_item_transform(item);
}
2013-10-01 02:37:13 +00:00
}
void obs_sceneitem_set_alignment(obs_sceneitem_t item, uint32_t alignment)
2013-10-01 02:37:13 +00:00
{
if (item) {
item->align = alignment;
update_item_transform(item);
}
2013-10-01 02:37:13 +00:00
}
2014-05-16 00:40:53 +00:00
static inline void signal_move_dir(struct obs_scene_item *item,
enum obs_order_movement movement)
2014-05-16 00:40:53 +00:00
{
const char *command = NULL;
2014-05-16 00:40:53 +00:00
struct calldata params = {0};
switch (movement) {
case OBS_ORDER_MOVE_UP: command = "item_move_up"; break;
case OBS_ORDER_MOVE_DOWN: command = "item_move_down"; break;
case OBS_ORDER_MOVE_TOP: command = "item_move_top"; break;
case OBS_ORDER_MOVE_BOTTOM: command = "item_move_bottom"; break;
2014-05-16 00:40:53 +00:00
}
calldata_set_ptr(&params, "scene", item->parent);
calldata_set_ptr(&params, "item", item);
2014-05-16 00:40:53 +00:00
signal_handler_signal(item->parent->source->context.signals,
command, &params);
calldata_free(&params);
}
void obs_sceneitem_set_order(obs_sceneitem_t item,
enum obs_order_movement movement)
2013-10-01 02:37:13 +00:00
{
2014-02-24 05:39:33 +00:00
if (!item) return;
2014-05-16 00:40:53 +00:00
struct obs_scene_item *next, *prev;
2013-10-01 02:37:13 +00:00
struct obs_scene *scene = item->parent;
obs_scene_addref(scene);
pthread_mutex_lock(&scene->mutex);
2014-05-16 00:40:53 +00:00
next = item->next;
prev = item->prev;
detach_sceneitem(item);
if (movement == OBS_ORDER_MOVE_DOWN) {
2014-05-16 00:40:53 +00:00
attach_sceneitem(scene, item, prev ? prev->prev : NULL);
2013-10-01 02:37:13 +00:00
} else if (movement == OBS_ORDER_MOVE_UP) {
2014-05-16 00:40:53 +00:00
attach_sceneitem(scene, item, next ? next : prev);
2013-10-01 02:37:13 +00:00
} else if (movement == OBS_ORDER_MOVE_TOP) {
2014-05-16 00:40:53 +00:00
struct obs_scene_item *last = next;
if (!last) {
2014-05-16 00:40:53 +00:00
last = prev;
} else {
while (last->next)
last = last->next;
}
2013-10-01 02:37:13 +00:00
2014-05-16 00:40:53 +00:00
attach_sceneitem(scene, item, last);
} else if (movement == OBS_ORDER_MOVE_BOTTOM) {
2014-05-16 00:40:53 +00:00
attach_sceneitem(scene, item, NULL);
2013-10-01 02:37:13 +00:00
}
2014-05-16 00:40:53 +00:00
signal_move_dir(item, movement);
pthread_mutex_unlock(&scene->mutex);
obs_scene_release(scene);
2013-10-01 02:37:13 +00:00
}
void obs_sceneitem_set_bounds_type(obs_sceneitem_t item,
enum obs_bounds_type type)
{
if (item) {
item->bounds_type = type;
update_item_transform(item);
}
}
void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t item,
uint32_t alignment)
{
if (item) {
item->bounds_align = alignment;
update_item_transform(item);
}
}
void obs_sceneitem_set_bounds(obs_sceneitem_t item, const struct vec2 *bounds)
{
if (item) {
item->bounds = *bounds;
update_item_transform(item);
}
}
void obs_sceneitem_get_pos(obs_sceneitem_t item, struct vec2 *pos)
2013-10-01 02:37:13 +00:00
{
2014-02-24 05:39:33 +00:00
if (item)
vec2_copy(pos, &item->pos);
2013-10-01 02:37:13 +00:00
}
float obs_sceneitem_get_rot(obs_sceneitem_t item)
2013-10-01 02:37:13 +00:00
{
2014-02-24 05:39:33 +00:00
return item ? item->rot : 0.0f;
2013-10-01 02:37:13 +00:00
}
void obs_sceneitem_get_scale(obs_sceneitem_t item, struct vec2 *scale)
2013-10-01 02:37:13 +00:00
{
2014-02-24 05:39:33 +00:00
if (item)
vec2_copy(scale, &item->scale);
2013-10-01 02:37:13 +00:00
}
uint32_t obs_sceneitem_get_alignment(obs_sceneitem_t item)
{
return item ? item->align : 0;
}
enum obs_bounds_type obs_sceneitem_get_bounds_type(obs_sceneitem_t item)
{
return item ? item->bounds_type : OBS_BOUNDS_NONE;
}
uint32_t obs_sceneitem_get_bounds_alignment(obs_sceneitem_t item)
{
return item ? item->bounds_align : 0;
}
void obs_sceneitem_get_bounds(obs_sceneitem_t item, struct vec2 *bounds)
2013-10-01 02:37:13 +00:00
{
2014-02-24 05:39:33 +00:00
if (item)
*bounds = item->bounds;
}
void obs_sceneitem_get_info(obs_sceneitem_t item,
struct obs_transform_info *info)
{
if (item && info) {
info->pos = item->pos;
info->rot = item->rot;
info->scale = item->scale;
info->alignment = item->align;
info->bounds_type = item->bounds_type;
info->bounds_alignment = item->bounds_align;
info->bounds = item->bounds;
}
}
void obs_sceneitem_set_info(obs_sceneitem_t item,
const struct obs_transform_info *info)
{
if (item && info) {
item->pos = info->pos;
item->rot = info->rot;
item->scale = info->scale;
item->align = info->alignment;
item->bounds_type = info->bounds_type;
item->bounds_align = info->bounds_alignment;
item->bounds = info->bounds;
update_item_transform(item);
}
}
void obs_sceneitem_get_draw_transform(obs_sceneitem_t item,
struct matrix4 *transform)
{
if (item)
matrix4_copy(transform, &item->draw_transform);
}
void obs_sceneitem_get_box_transform(obs_sceneitem_t item,
struct matrix4 *transform)
{
if (item)
matrix4_copy(transform, &item->box_transform);
2013-10-01 02:37:13 +00:00
}