forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-scene.c
3764 lines (3032 loc) · 92.3 KB
/
obs-scene.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
Copyright (C) 2013-2015 by Hugh Bailey <obs.jim@gmail.com>
Philippe Groarke <philippe.groarke@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
(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"
#include "util/util_uint64.h"
#include "graphics/math-defs.h"
#include "obs-scene.h"
#include "obs-internal.h"
const struct obs_source_info group_info;
static void resize_group(obs_sceneitem_t *group);
static void resize_scene(obs_scene_t *scene);
static void signal_parent(obs_scene_t *parent, const char *name,
calldata_t *params);
static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
struct vec2 *scale, float *rot);
static inline bool crop_enabled(const struct obs_sceneitem_crop *crop);
static inline bool item_texture_enabled(const struct obs_scene_item *item);
static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
const char *name);
/* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
* lock the graphics mutex inside either of the scene mutexes.
*
* Another thing that must be done to prevent that cross-lock (and improve
* performance), is to not create/release/update sources within the scene
* mutexes.
*
* It's okay to lock the graphics mutex before locking either of the scene
* mutexes, but not after.
*/
static const char *obs_scene_signals[] = {
"void item_add(ptr scene, ptr item)",
"void item_remove(ptr scene, ptr item)",
"void reorder(ptr scene)",
"void refresh(ptr scene)",
"void item_visible(ptr scene, ptr item, bool visible)",
"void item_select(ptr scene, ptr item)",
"void item_deselect(ptr scene, ptr item)",
"void item_transform(ptr scene, ptr item)",
"void item_locked(ptr scene, ptr item, bool locked)",
NULL,
};
static const struct {
enum gs_blend_type src_color;
enum gs_blend_type src_alpha;
enum gs_blend_type dst_color;
enum gs_blend_type dst_alpha;
enum gs_blend_op_type op;
} obs_blend_mode_params[] = {
/* clang-format off */
// OBS_BLEND_NORMAL
{
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_INVSRCALPHA,
GS_BLEND_INVSRCALPHA,
GS_BLEND_OP_ADD,
},
// OBS_BLEND_ADDITIVE
{
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_OP_ADD,
},
// OBS_BLEND_SUBTRACT
{
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_OP_REVERSE_SUBTRACT,
},
// OBS_BLEND_SCREEN
{
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_INVSRCCOLOR,
GS_BLEND_INVSRCALPHA,
GS_BLEND_OP_ADD
},
// OBS_BLEND_MULTIPLY
{
GS_BLEND_DSTCOLOR,
GS_BLEND_DSTALPHA,
GS_BLEND_INVSRCALPHA,
GS_BLEND_INVSRCALPHA,
GS_BLEND_OP_ADD
},
// OBS_BLEND_LIGHTEN
{
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_OP_MAX,
},
// OBS_BLEND_DARKEN
{
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_ONE,
GS_BLEND_OP_MIN,
},
/* clang-format on */
};
static inline void signal_item_remove(struct obs_scene_item *item)
{
struct calldata params;
uint8_t stack[128];
calldata_init_fixed(¶ms, stack, sizeof(stack));
calldata_set_ptr(¶ms, "item", item);
signal_parent(item->parent, "item_remove", ¶ms);
}
static const char *scene_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return "Scene";
}
static const char *group_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return "Group";
}
static void *scene_create(obs_data_t *settings, struct obs_source *source)
{
struct obs_scene *scene = bzalloc(sizeof(struct obs_scene));
scene->source = source;
if (strcmp(source->info.id, group_info.id) == 0) {
scene->is_group = true;
scene->custom_size = true;
scene->cx = 0;
scene->cy = 0;
}
signal_handler_add_array(obs_source_get_signal_handler(source),
obs_scene_signals);
if (pthread_mutex_init_recursive(&scene->audio_mutex) != 0) {
blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
"mutex");
goto fail;
}
if (pthread_mutex_init_recursive(&scene->video_mutex) != 0) {
blog(LOG_ERROR, "scene_create: Couldn't initialize video "
"mutex");
goto fail;
}
UNUSED_PARAMETER(settings);
return scene;
fail:
bfree(scene);
return NULL;
}
#define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
#define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
#define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
#define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
static inline void full_lock(struct obs_scene *scene)
{
video_lock(scene);
audio_lock(scene);
}
static inline void full_unlock(struct obs_scene *scene)
{
audio_unlock(scene);
video_unlock(scene);
}
static void set_visibility(struct obs_scene_item *item, bool vis);
static inline void detach_sceneitem(struct obs_scene_item *item);
static inline void remove_without_release(struct obs_scene_item *item)
{
item->removed = true;
set_visibility(item, false);
signal_item_remove(item);
detach_sceneitem(item);
}
static void remove_all_items(struct obs_scene *scene)
{
struct obs_scene_item *item;
DARRAY(struct obs_scene_item *) items;
da_init(items);
full_lock(scene);
item = scene->first_item;
while (item) {
struct obs_scene_item *del_item = item;
item = item->next;
remove_without_release(del_item);
da_push_back(items, &del_item);
}
full_unlock(scene);
for (size_t i = 0; i < items.num; i++)
obs_sceneitem_release(items.array[i]);
da_free(items);
}
static void scene_destroy(void *data)
{
struct obs_scene *scene = data;
remove_all_items(scene);
pthread_mutex_destroy(&scene->video_mutex);
pthread_mutex_destroy(&scene->audio_mutex);
bfree(scene);
}
static inline bool transition_active(obs_source_t *transition)
{
return transition && (transition->transitioning_audio ||
transition->transitioning_video);
}
static void scene_enum_sources(void *data, obs_source_enum_proc_t enum_callback,
void *param, bool active)
{
struct obs_scene *scene = data;
struct obs_scene_item *item;
struct obs_scene_item *next;
full_lock(scene);
item = scene->first_item;
while (item) {
next = item->next;
obs_sceneitem_addref(item);
if (active) {
if (item->visible &&
transition_active(item->show_transition))
enum_callback(scene->source,
item->show_transition, param);
else if (!item->visible &&
transition_active(item->hide_transition))
enum_callback(scene->source,
item->hide_transition, param);
else if (os_atomic_load_long(&item->active_refs) > 0)
enum_callback(scene->source, item->source,
param);
} else {
if (item->show_transition)
enum_callback(scene->source,
item->show_transition, param);
if (item->hide_transition)
enum_callback(scene->source,
item->hide_transition, param);
enum_callback(scene->source, item->source, param);
}
obs_sceneitem_release(item);
item = next;
}
full_unlock(scene);
}
static void scene_enum_active_sources(void *data,
obs_source_enum_proc_t enum_callback,
void *param)
{
scene_enum_sources(data, enum_callback, param, true);
}
static void scene_enum_all_sources(void *data,
obs_source_enum_proc_t enum_callback,
void *param)
{
scene_enum_sources(data, enum_callback, param, false);
}
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;
}
static inline void attach_sceneitem(struct obs_scene *parent,
struct obs_scene_item *item,
struct obs_scene_item *prev)
{
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 = parent->first_item;
if (parent->first_item)
parent->first_item->prev = item;
parent->first_item = item;
}
}
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)
{
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);
} else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
vec2_mulf(scale, scale, item->bounds.x / width);
} else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
vec2_mulf(scale, scale, item->bounds.y / height);
} 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 inline uint32_t calc_cx(const struct obs_scene_item *item,
uint32_t width)
{
uint32_t crop_cx = item->crop.left + item->crop.right;
return (crop_cx > width) ? 2 : (width - crop_cx);
}
static inline uint32_t calc_cy(const struct obs_scene_item *item,
uint32_t height)
{
uint32_t crop_cy = item->crop.top + item->crop.bottom;
return (crop_cy > height) ? 2 : (height - crop_cy);
}
static void update_item_transform(struct obs_scene_item *item, bool update_tex)
{
uint32_t width;
uint32_t height;
uint32_t cx;
uint32_t cy;
struct vec2 base_origin;
struct vec2 origin;
struct vec2 scale;
struct calldata params;
uint8_t stack[128];
if (os_atomic_load_long(&item->defer_update) > 0)
return;
width = obs_source_get_width(item->source);
height = obs_source_get_height(item->source);
cx = calc_cx(item, width);
cy = calc_cy(item, height);
scale = item->scale;
item->last_width = width;
item->last_height = height;
width = cx;
height = cy;
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);
item->output_scale = scale;
/* ----------------------- */
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;
}
item->box_scale = scale;
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);
/* ----------------------- */
calldata_init_fixed(¶ms, stack, sizeof(stack));
calldata_set_ptr(¶ms, "item", item);
signal_parent(item->parent, "item_transform", ¶ms);
if (!update_tex)
return;
if (item->item_render && !item_texture_enabled(item)) {
obs_enter_graphics();
gs_texrender_destroy(item->item_render);
item->item_render = NULL;
obs_leave_graphics();
} else if (!item->item_render && item_texture_enabled(item)) {
obs_enter_graphics();
item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
obs_leave_graphics();
}
os_atomic_set_bool(&item->update_transform, false);
}
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;
}
static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
{
return crop->left || crop->right || crop->top || crop->bottom;
}
static inline bool scale_filter_enabled(const struct obs_scene_item *item)
{
return item->scale_filter != OBS_SCALE_DISABLE;
}
static inline bool default_blending_enabled(const struct obs_scene_item *item)
{
return item->blend_type == OBS_BLEND_NORMAL;
}
static inline bool item_is_scene(const struct obs_scene_item *item)
{
return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
}
static inline bool item_texture_enabled(const struct obs_scene_item *item)
{
return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
!default_blending_enabled(item) ||
(item_is_scene(item) && !item->is_group);
}
static void render_item_texture(struct obs_scene_item *item)
{
gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
if (!tex) {
return;
}
GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_ITEM_TEXTURE,
"render_item_texture");
gs_effect_t *effect = obs->video.default_effect;
enum obs_scale_type type = item->scale_filter;
uint32_t cx = gs_texture_get_width(tex);
uint32_t cy = gs_texture_get_height(tex);
const char *tech = "Draw";
if (type != OBS_SCALE_DISABLE) {
if (type == OBS_SCALE_POINT) {
gs_eparam_t *image =
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_next_sampler(image,
obs->video.point_sampler);
} else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
!close_float(item->output_scale.y, 1.0f, EPSILON)) {
gs_eparam_t *scale_param;
gs_eparam_t *scale_i_param;
if (item->output_scale.x < 0.5f ||
item->output_scale.y < 0.5f) {
effect = obs->video.bilinear_lowres_effect;
} else if (type == OBS_SCALE_BICUBIC) {
effect = obs->video.bicubic_effect;
} else if (type == OBS_SCALE_LANCZOS) {
effect = obs->video.lanczos_effect;
} else if (type == OBS_SCALE_AREA) {
effect = obs->video.area_effect;
if ((item->output_scale.x >= 1.0f) &&
(item->output_scale.y >= 1.0f))
tech = "DrawUpscale";
}
scale_param = gs_effect_get_param_by_name(
effect, "base_dimension");
if (scale_param) {
struct vec2 base_res = {(float)cx, (float)cy};
gs_effect_set_vec2(scale_param, &base_res);
}
scale_i_param = gs_effect_get_param_by_name(
effect, "base_dimension_i");
if (scale_i_param) {
struct vec2 base_res_i = {1.0f / (float)cx,
1.0f / (float)cy};
gs_effect_set_vec2(scale_i_param, &base_res_i);
}
}
}
gs_blend_state_push();
gs_blend_function_separate(
obs_blend_mode_params[item->blend_type].src_color,
obs_blend_mode_params[item->blend_type].dst_color,
obs_blend_mode_params[item->blend_type].src_alpha,
obs_blend_mode_params[item->blend_type].dst_alpha);
gs_blend_op(obs_blend_mode_params[item->blend_type].op);
while (gs_effect_loop(effect, tech))
obs_source_draw(tex, 0, 0, 0, 0, 0);
gs_blend_state_pop();
GS_DEBUG_MARKER_END();
}
static bool are_texcoords_centered(struct matrix4 *m)
{
static const struct matrix4 identity = {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f},
};
struct matrix4 copy = identity;
copy.t.x = floorf(m->t.x);
copy.t.y = floorf(m->t.y);
return memcmp(m, ©, sizeof(*m)) == 0;
}
static inline void render_item(struct obs_scene_item *item)
{
GS_DEBUG_MARKER_BEGIN_FORMAT(GS_DEBUG_COLOR_ITEM, "Item: %s",
obs_source_get_name(item->source));
if (item->item_render) {
uint32_t width = obs_source_get_width(item->source);
uint32_t height = obs_source_get_height(item->source);
if (!width || !height) {
goto cleanup;
}
uint32_t cx = calc_cx(item, width);
uint32_t cy = calc_cy(item, height);
if (cx && cy && gs_texrender_begin(item->item_render, cx, cy)) {
float cx_scale = (float)width / (float)cx;
float cy_scale = (float)height / (float)cy;
struct vec4 clear_color;
vec4_zero(&clear_color);
gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
gs_ortho(0.0f, (float)width, 0.0f, (float)height,
-100.0f, 100.0f);
gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
gs_matrix_translate3f(-(float)item->crop.left,
-(float)item->crop.top, 0.0f);
if (item->user_visible &&
transition_active(item->show_transition)) {
const int cx =
obs_source_get_width(item->source);
const int cy =
obs_source_get_height(item->source);
obs_transition_set_size(item->show_transition,
cx, cy);
obs_source_video_render(item->show_transition);
} else if (!item->user_visible &&
transition_active(item->hide_transition)) {
const int cx =
obs_source_get_width(item->source);
const int cy =
obs_source_get_height(item->source);
obs_transition_set_size(item->hide_transition,
cx, cy);
obs_source_video_render(item->hide_transition);
} else {
obs_source_set_texcoords_centered(item->source,
true);
obs_source_video_render(item->source);
obs_source_set_texcoords_centered(item->source,
false);
}
gs_texrender_end(item->item_render);
}
}
const bool previous = gs_set_linear_srgb(true);
gs_matrix_push();
gs_matrix_mul(&item->draw_transform);
if (item->item_render) {
render_item_texture(item);
} else if (item->user_visible &&
transition_active(item->show_transition)) {
const int cx = obs_source_get_width(item->source);
const int cy = obs_source_get_height(item->source);
obs_transition_set_size(item->show_transition, cx, cy);
obs_source_video_render(item->show_transition);
} else if (!item->user_visible &&
transition_active(item->hide_transition)) {
const int cx = obs_source_get_width(item->source);
const int cy = obs_source_get_height(item->source);
obs_transition_set_size(item->hide_transition, cx, cy);
obs_source_video_render(item->hide_transition);
} else {
const bool centered =
are_texcoords_centered(&item->draw_transform);
obs_source_set_texcoords_centered(item->source, centered);
obs_source_video_render(item->source);
obs_source_set_texcoords_centered(item->source, false);
}
gs_matrix_pop();
gs_set_linear_srgb(previous);
cleanup:
GS_DEBUG_MARKER_END();
}
static void scene_video_tick(void *data, float seconds)
{
struct obs_scene *scene = data;
struct obs_scene_item *item;
video_lock(scene);
item = scene->first_item;
while (item) {
if (item->item_render)
gs_texrender_reset(item->item_render);
item = item->next;
}
video_unlock(scene);
UNUSED_PARAMETER(seconds);
}
/* assumes video lock */
static void
update_transforms_and_prune_sources(obs_scene_t *scene,
struct darray *remove_items,
obs_sceneitem_t *group_sceneitem)
{
struct obs_scene_item *item = scene->first_item;
bool rebuild_group =
group_sceneitem &&
os_atomic_load_bool(&group_sceneitem->update_group_resize);
while (item) {
if (obs_source_removed(item->source)) {
struct obs_scene_item *del_item = item;
item = item->next;
remove_without_release(del_item);
darray_push_back(sizeof(struct obs_scene_item *),
remove_items, &del_item);
rebuild_group = true;
continue;
}
if (item->is_group) {
obs_scene_t *group_scene = item->source->context.data;
video_lock(group_scene);
update_transforms_and_prune_sources(group_scene,
remove_items, item);
video_unlock(group_scene);
}
if (os_atomic_load_bool(&item->update_transform) ||
source_size_changed(item)) {
update_item_transform(item, true);
rebuild_group = true;
}
item = item->next;
}
if (rebuild_group && group_sceneitem)
resize_group(group_sceneitem);
}
static void scene_video_render(void *data, gs_effect_t *effect)
{
DARRAY(struct obs_scene_item *) remove_items;
struct obs_scene *scene = data;
struct obs_scene_item *item;
da_init(remove_items);
video_lock(scene);
if (!scene->is_group) {
update_transforms_and_prune_sources(scene, &remove_items.da,
NULL);
}
gs_blend_state_push();
gs_reset_blend_state();
item = scene->first_item;
while (item) {
if (item->user_visible ||
transition_active(item->hide_transition))
render_item(item);
item = item->next;
}
gs_blend_state_pop();
video_unlock(scene);
for (size_t i = 0; i < remove_items.num; i++)
obs_sceneitem_release(remove_items.array[i]);
da_free(remove_items);
UNUSED_PARAMETER(effect);
}
static void set_visibility(struct obs_scene_item *item, bool vis)
{
pthread_mutex_lock(&item->actions_mutex);
da_resize(item->audio_actions, 0);
if (os_atomic_load_long(&item->active_refs) > 0) {
if (!vis)
obs_source_remove_active_child(item->parent->source,
item->source);
} else if (vis) {
obs_source_add_active_child(item->parent->source, item->source);
}
os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
item->visible = vis;
item->user_visible = vis;
pthread_mutex_unlock(&item->actions_mutex);
}
static void scene_load(void *data, obs_data_t *settings);
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;
const char *scale_filter_str;
const char *blend_str;
struct obs_scene_item *item;
bool visible;
bool lock;
if (obs_data_get_bool(item_data, "group_item_backup"))
return;
source = obs_get_source_by_name(name);
if (!source) {
blog(LOG_WARNING,
"[scene_load_item] Source %s not "
"found!",
name);
return;
}
item = obs_scene_add(scene, source);
if (!item) {
blog(LOG_WARNING,
"[scene_load_item] Could not add source '%s' "
"to scene '%s'!",
name, obs_source_get_name(scene->source));
obs_source_release(source);
return;
}
item->is_group = strcmp(source->info.id, group_info.id) == 0;
obs_data_set_default_int(item_data, "align",
OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
if (obs_data_has_user_value(item_data, "id"))
item->id = obs_data_get_int(item_data, "id");
item->rot = (float)obs_data_get_double(item_data, "rot");
item->align = (uint32_t)obs_data_get_int(item_data, "align");
visible = obs_data_get_bool(item_data, "visible");
lock = obs_data_get_bool(item_data, "locked");
obs_data_get_vec2(item_data, "pos", &item->pos);
obs_data_get_vec2(item_data, "scale", &item->scale);
obs_data_release(item->private_settings);
item->private_settings =
obs_data_get_obj(item_data, "private_settings");
if (!item->private_settings)
item->private_settings = obs_data_create();
set_visibility(item, visible);
obs_sceneitem_set_locked(item, lock);
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);
item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
item->crop.bottom =
(uint32_t)obs_data_get_int(item_data, "crop_bottom");
scale_filter_str = obs_data_get_string(item_data, "scale_filter");
item->scale_filter = OBS_SCALE_DISABLE;
if (scale_filter_str) {
if (astrcmpi(scale_filter_str, "point") == 0)
item->scale_filter = OBS_SCALE_POINT;
else if (astrcmpi(scale_filter_str, "bilinear") == 0)
item->scale_filter = OBS_SCALE_BILINEAR;
else if (astrcmpi(scale_filter_str, "bicubic") == 0)
item->scale_filter = OBS_SCALE_BICUBIC;
else if (astrcmpi(scale_filter_str, "lanczos") == 0)
item->scale_filter = OBS_SCALE_LANCZOS;
else if (astrcmpi(scale_filter_str, "area") == 0)
item->scale_filter = OBS_SCALE_AREA;
}
blend_str = obs_data_get_string(item_data, "blend_type");
item->blend_type = OBS_BLEND_NORMAL;
if (blend_str) {
if (astrcmpi(blend_str, "normal") == 0)
item->blend_type = OBS_BLEND_NORMAL;
else if (astrcmpi(blend_str, "additive") == 0)
item->blend_type = OBS_BLEND_ADDITIVE;
else if (astrcmpi(blend_str, "subtract") == 0)
item->blend_type = OBS_BLEND_SUBTRACT;
else if (astrcmpi(blend_str, "screen") == 0)
item->blend_type = OBS_BLEND_SCREEN;
else if (astrcmpi(blend_str, "multiply") == 0)
item->blend_type = OBS_BLEND_MULTIPLY;
else if (astrcmpi(blend_str, "lighten") == 0)
item->blend_type = OBS_BLEND_LIGHTEN;
else if (astrcmpi(blend_str, "darken") == 0)
item->blend_type = OBS_BLEND_DARKEN;
}
obs_data_t *show_data = obs_data_get_obj(item_data, "show_transition");
if (show_data) {
obs_sceneitem_transition_load(item, show_data, true);
obs_data_release(show_data);
}
obs_data_t *hide_data = obs_data_get_obj(item_data, "hide_transition");
if (hide_data) {
obs_sceneitem_transition_load(item, hide_data, false);
obs_data_release(hide_data);
}
if (item->item_render && !item_texture_enabled(item)) {
obs_enter_graphics();
gs_texrender_destroy(item->item_render);
item->item_render = NULL;
obs_leave_graphics();
} else if (!item->item_render && item_texture_enabled(item)) {
obs_enter_graphics();
item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
obs_leave_graphics();
}
obs_source_release(source);
update_item_transform(item, false);
}
static void scene_load(void *data, obs_data_t *settings)