obs-scene.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Philippe Groarke <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "util/threading.h"
  16. #include "graphics/math-defs.h"
  17. #include "obs-scene.h"
  18. static const char *obs_scene_signals[] = {
  19. "void item_add(ptr scene, ptr item)",
  20. "void item_remove(ptr scene, ptr item)",
  21. "void reorder(ptr scene)",
  22. "void item_select(ptr scene, ptr item)",
  23. "void item_deselect(ptr scene, ptr item)",
  24. "void item_transform(ptr scene, ptr item)",
  25. NULL
  26. };
  27. static inline void signal_item_remove(struct obs_scene_item *item)
  28. {
  29. struct calldata params = {0};
  30. calldata_set_ptr(&params, "scene", item->parent);
  31. calldata_set_ptr(&params, "item", item);
  32. signal_handler_signal(item->parent->source->context.signals,
  33. "item_remove", &params);
  34. calldata_free(&params);
  35. }
  36. static const char *scene_getname(void)
  37. {
  38. /* TODO: locale */
  39. return "Scene";
  40. }
  41. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  42. {
  43. pthread_mutexattr_t attr;
  44. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  45. scene->source = source;
  46. scene->first_item = NULL;
  47. signal_handler_add_array(obs_source_get_signal_handler(source),
  48. obs_scene_signals);
  49. if (pthread_mutexattr_init(&attr) != 0)
  50. goto fail;
  51. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  52. goto fail;
  53. if (pthread_mutex_init(&scene->mutex, &attr) != 0) {
  54. blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
  55. goto fail;
  56. }
  57. UNUSED_PARAMETER(settings);
  58. return scene;
  59. fail:
  60. pthread_mutexattr_destroy(&attr);
  61. bfree(scene);
  62. return NULL;
  63. }
  64. static void remove_all_items(struct obs_scene *scene)
  65. {
  66. struct obs_scene_item *item;
  67. pthread_mutex_lock(&scene->mutex);
  68. item = scene->first_item;
  69. while (item) {
  70. struct obs_scene_item *del_item = item;
  71. item = item->next;
  72. obs_sceneitem_remove(del_item);
  73. }
  74. pthread_mutex_unlock(&scene->mutex);
  75. }
  76. static void scene_destroy(void *data)
  77. {
  78. struct obs_scene *scene = data;
  79. remove_all_items(scene);
  80. pthread_mutex_destroy(&scene->mutex);
  81. bfree(scene);
  82. }
  83. static void scene_enum_sources(void *data,
  84. obs_source_enum_proc_t enum_callback,
  85. void *param)
  86. {
  87. struct obs_scene *scene = data;
  88. struct obs_scene_item *item;
  89. pthread_mutex_lock(&scene->mutex);
  90. item = scene->first_item;
  91. while (item) {
  92. struct obs_scene_item *next = item->next;
  93. obs_sceneitem_addref(item);
  94. enum_callback(scene->source, item->source, param);
  95. obs_sceneitem_release(item);
  96. item = next;
  97. }
  98. pthread_mutex_unlock(&scene->mutex);
  99. }
  100. static inline void detach_sceneitem(struct obs_scene_item *item)
  101. {
  102. if (item->prev)
  103. item->prev->next = item->next;
  104. else
  105. item->parent->first_item = item->next;
  106. if (item->next)
  107. item->next->prev = item->prev;
  108. item->parent = NULL;
  109. }
  110. static inline void attach_sceneitem(struct obs_scene *parent,
  111. struct obs_scene_item *item, struct obs_scene_item *prev)
  112. {
  113. item->prev = prev;
  114. item->parent = parent;
  115. if (prev) {
  116. item->next = prev->next;
  117. if (prev->next)
  118. prev->next->prev = item;
  119. prev->next = item;
  120. } else {
  121. item->next = parent->first_item;
  122. if (parent->first_item)
  123. parent->first_item->prev = item;
  124. parent->first_item = item;
  125. }
  126. }
  127. static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  128. {
  129. if (align & OBS_ALIGN_RIGHT)
  130. v->x += (float)cx;
  131. else if ((align & OBS_ALIGN_LEFT) == 0)
  132. v->x += (float)(cx / 2);
  133. if (align & OBS_ALIGN_BOTTOM)
  134. v->y += (float)cy;
  135. else if ((align & OBS_ALIGN_TOP) == 0)
  136. v->y += (float)(cy / 2);
  137. }
  138. static void calculate_bounds_data(struct obs_scene_item *item,
  139. struct vec2 *origin, struct vec2 *scale,
  140. uint32_t *cx, uint32_t *cy)
  141. {
  142. float width = (float)(*cx) * fabsf(scale->x);
  143. float height = (float)(*cy) * fabsf(scale->y);
  144. float item_aspect = width / height;
  145. float bounds_aspect = item->bounds.x / item->bounds.y;
  146. uint32_t bounds_type = item->bounds_type;
  147. float width_diff, height_diff;
  148. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  149. if (width > item->bounds.x || height > item->bounds.y)
  150. bounds_type = OBS_BOUNDS_SCALE_INNER;
  151. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  152. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  153. bool use_width = (bounds_aspect < item_aspect);
  154. float mul;
  155. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  156. use_width = !use_width;
  157. mul = use_width ?
  158. item->bounds.x / width :
  159. item->bounds.y / height;
  160. vec2_mulf(scale, scale, mul);
  161. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  162. vec2_mulf(scale, scale, item->bounds.x / width);
  163. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  164. vec2_mulf(scale, scale, item->bounds.y / height);
  165. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  166. scale->x = item->bounds.x / (float)(*cx);
  167. scale->y = item->bounds.y / (float)(*cy);
  168. }
  169. width = (float)(*cx) * scale->x;
  170. height = (float)(*cy) * scale->y;
  171. width_diff = item->bounds.x - width;
  172. height_diff = item->bounds.y - height;
  173. *cx = (uint32_t)item->bounds.x;
  174. *cy = (uint32_t)item->bounds.y;
  175. add_alignment(origin, item->bounds_align,
  176. (int)-width_diff, (int)-height_diff);
  177. }
  178. static void update_item_transform(struct obs_scene_item *item)
  179. {
  180. uint32_t width = obs_source_get_width(item->source);
  181. uint32_t height = obs_source_get_height(item->source);
  182. uint32_t cx = width;
  183. uint32_t cy = height;
  184. struct vec2 base_origin;
  185. struct vec2 origin;
  186. struct vec2 scale = item->scale;
  187. struct calldata params = {0};
  188. vec2_zero(&base_origin);
  189. vec2_zero(&origin);
  190. /* ----------------------- */
  191. if (item->bounds_type != OBS_BOUNDS_NONE) {
  192. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  193. } else {
  194. cx = (uint32_t)((float)cx * scale.x);
  195. cy = (uint32_t)((float)cy * scale.y);
  196. }
  197. add_alignment(&origin, item->align, (int)cx, (int)cy);
  198. matrix4_identity(&item->draw_transform);
  199. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  200. scale.x, scale.y, 1.0f);
  201. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  202. -origin.x, -origin.y, 0.0f);
  203. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  204. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  205. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  206. item->pos.x, item->pos.y, 0.0f);
  207. /* ----------------------- */
  208. if (item->bounds_type != OBS_BOUNDS_NONE) {
  209. vec2_copy(&scale, &item->bounds);
  210. } else {
  211. scale.x = (float)width * item->scale.x;
  212. scale.y = (float)height * item->scale.y;
  213. }
  214. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  215. matrix4_identity(&item->box_transform);
  216. matrix4_scale3f(&item->box_transform, &item->box_transform,
  217. scale.x, scale.y, 1.0f);
  218. matrix4_translate3f(&item->box_transform, &item->box_transform,
  219. -base_origin.x, -base_origin.y, 0.0f);
  220. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  221. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  222. matrix4_translate3f(&item->box_transform, &item->box_transform,
  223. item->pos.x, item->pos.y, 0.0f);
  224. /* ----------------------- */
  225. item->last_width = width;
  226. item->last_height = height;
  227. calldata_set_ptr(&params, "scene", item->parent);
  228. calldata_set_ptr(&params, "item", item);
  229. signal_handler_signal(item->parent->source->context.signals,
  230. "item_transform", &params);
  231. calldata_free(&params);
  232. }
  233. static inline bool source_size_changed(struct obs_scene_item *item)
  234. {
  235. uint32_t width = obs_source_get_width(item->source);
  236. uint32_t height = obs_source_get_height(item->source);
  237. return item->last_width != width || item->last_height != height;
  238. }
  239. static void scene_video_render(void *data, gs_effect_t *effect)
  240. {
  241. struct obs_scene *scene = data;
  242. struct obs_scene_item *item;
  243. pthread_mutex_lock(&scene->mutex);
  244. item = scene->first_item;
  245. gs_blend_state_push();
  246. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  247. while (item) {
  248. if (obs_source_removed(item->source)) {
  249. struct obs_scene_item *del_item = item;
  250. item = item->next;
  251. obs_sceneitem_remove(del_item);
  252. continue;
  253. }
  254. if (source_size_changed(item))
  255. update_item_transform(item);
  256. gs_matrix_push();
  257. gs_matrix_mul(&item->draw_transform);
  258. obs_source_video_render(item->source);
  259. gs_matrix_pop();
  260. item = item->next;
  261. }
  262. gs_blend_state_pop();
  263. pthread_mutex_unlock(&scene->mutex);
  264. UNUSED_PARAMETER(effect);
  265. }
  266. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  267. {
  268. const char *name = obs_data_get_string(item_data, "name");
  269. obs_source_t *source = obs_get_source_by_name(name);
  270. struct obs_scene_item *item;
  271. if (!source) {
  272. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  273. name);
  274. return;
  275. }
  276. item = obs_scene_add(scene, source);
  277. if (!item) {
  278. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  279. "to scene '%s'!",
  280. name, obs_source_get_name(scene->source));
  281. obs_source_release(source);
  282. return;
  283. }
  284. obs_data_set_default_int(item_data, "align",
  285. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  286. item->rot = (float)obs_data_get_double(item_data, "rot");
  287. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  288. item->visible = obs_data_get_bool(item_data, "visible");
  289. obs_data_get_vec2(item_data, "pos", &item->pos);
  290. obs_data_get_vec2(item_data, "scale", &item->scale);
  291. item->bounds_type =
  292. (enum obs_bounds_type)obs_data_get_int(item_data,
  293. "bounds_type");
  294. item->bounds_align =
  295. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  296. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  297. obs_source_release(source);
  298. update_item_transform(item);
  299. }
  300. static void scene_load(void *scene, obs_data_t *settings)
  301. {
  302. obs_data_array_t *items = obs_data_get_array(settings, "items");
  303. size_t count, i;
  304. remove_all_items(scene);
  305. if (!items) return;
  306. count = obs_data_array_count(items);
  307. for (i = 0; i < count; i++) {
  308. obs_data_t *item_data = obs_data_array_item(items, i);
  309. scene_load_item(scene, item_data);
  310. obs_data_release(item_data);
  311. }
  312. obs_data_array_release(items);
  313. }
  314. static void scene_save_item(obs_data_array_t *array,
  315. struct obs_scene_item *item)
  316. {
  317. obs_data_t *item_data = obs_data_create();
  318. const char *name = obs_source_get_name(item->source);
  319. obs_data_set_string(item_data, "name", name);
  320. obs_data_set_bool (item_data, "visible", item->visible);
  321. obs_data_set_double(item_data, "rot", item->rot);
  322. obs_data_set_vec2 (item_data, "pos", &item->pos);
  323. obs_data_set_vec2 (item_data, "scale", &item->scale);
  324. obs_data_set_int (item_data, "align", (int)item->align);
  325. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  326. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  327. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  328. obs_data_array_push_back(array, item_data);
  329. obs_data_release(item_data);
  330. }
  331. static void scene_save(void *data, obs_data_t *settings)
  332. {
  333. struct obs_scene *scene = data;
  334. obs_data_array_t *array = obs_data_array_create();
  335. struct obs_scene_item *item;
  336. pthread_mutex_lock(&scene->mutex);
  337. item = scene->first_item;
  338. while (item) {
  339. scene_save_item(array, item);
  340. item = item->next;
  341. }
  342. pthread_mutex_unlock(&scene->mutex);
  343. obs_data_set_array(settings, "items", array);
  344. obs_data_array_release(array);
  345. }
  346. static uint32_t scene_getwidth(void *data)
  347. {
  348. UNUSED_PARAMETER(data);
  349. return obs->video.base_width;
  350. }
  351. static uint32_t scene_getheight(void *data)
  352. {
  353. UNUSED_PARAMETER(data);
  354. return obs->video.base_height;
  355. }
  356. const struct obs_source_info scene_info =
  357. {
  358. .id = "scene",
  359. .type = OBS_SOURCE_TYPE_INPUT,
  360. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  361. .get_name = scene_getname,
  362. .create = scene_create,
  363. .destroy = scene_destroy,
  364. .video_render = scene_video_render,
  365. .get_width = scene_getwidth,
  366. .get_height = scene_getheight,
  367. .load = scene_load,
  368. .save = scene_save,
  369. .enum_sources = scene_enum_sources
  370. };
  371. obs_scene_t *obs_scene_create(const char *name)
  372. {
  373. struct obs_source *source =
  374. obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL);
  375. return source->context.data;
  376. }
  377. void obs_scene_addref(obs_scene_t *scene)
  378. {
  379. if (scene)
  380. obs_source_addref(scene->source);
  381. }
  382. void obs_scene_release(obs_scene_t *scene)
  383. {
  384. if (scene)
  385. obs_source_release(scene->source);
  386. }
  387. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  388. {
  389. return scene ? scene->source : NULL;
  390. }
  391. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  392. {
  393. if (!source || source->info.id != scene_info.id)
  394. return NULL;
  395. return source->context.data;
  396. }
  397. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  398. {
  399. struct obs_scene_item *item;
  400. if (!scene)
  401. return NULL;
  402. pthread_mutex_lock(&scene->mutex);
  403. item = scene->first_item;
  404. while (item) {
  405. if (strcmp(item->source->context.name, name) == 0)
  406. break;
  407. item = item->next;
  408. }
  409. pthread_mutex_unlock(&scene->mutex);
  410. return item;
  411. }
  412. void obs_scene_enum_items(obs_scene_t *scene,
  413. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  414. void *param)
  415. {
  416. struct obs_scene_item *item;
  417. if (!scene || !callback)
  418. return;
  419. pthread_mutex_lock(&scene->mutex);
  420. item = scene->first_item;
  421. while (item) {
  422. struct obs_scene_item *next = item->next;
  423. obs_sceneitem_addref(item);
  424. if (!callback(scene, item, param)) {
  425. obs_sceneitem_release(item);
  426. break;
  427. }
  428. obs_sceneitem_release(item);
  429. item = next;
  430. }
  431. pthread_mutex_unlock(&scene->mutex);
  432. }
  433. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  434. {
  435. struct obs_scene_item *last;
  436. struct obs_scene_item *item;
  437. struct calldata params = {0};
  438. if (!scene)
  439. return NULL;
  440. if (!source) {
  441. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  442. return NULL;
  443. }
  444. if (!obs_source_add_child(scene->source, source)) {
  445. blog(LOG_WARNING, "Failed to add source to scene due to "
  446. "infinite source recursion");
  447. return NULL;
  448. }
  449. item = bzalloc(sizeof(struct obs_scene_item));
  450. item->source = source;
  451. item->visible = true;
  452. item->parent = scene;
  453. item->ref = 1;
  454. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  455. vec2_set(&item->scale, 1.0f, 1.0f);
  456. matrix4_identity(&item->draw_transform);
  457. matrix4_identity(&item->box_transform);
  458. obs_source_addref(source);
  459. pthread_mutex_lock(&scene->mutex);
  460. last = scene->first_item;
  461. if (!last) {
  462. scene->first_item = item;
  463. } else {
  464. while (last->next)
  465. last = last->next;
  466. last->next = item;
  467. item->prev = last;
  468. }
  469. pthread_mutex_unlock(&scene->mutex);
  470. calldata_set_ptr(&params, "scene", scene);
  471. calldata_set_ptr(&params, "item", item);
  472. signal_handler_signal(scene->source->context.signals, "item_add",
  473. &params);
  474. calldata_free(&params);
  475. return item;
  476. }
  477. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  478. {
  479. if (item) {
  480. if (item->source)
  481. obs_source_release(item->source);
  482. bfree(item);
  483. }
  484. }
  485. void obs_sceneitem_addref(obs_sceneitem_t *item)
  486. {
  487. if (item)
  488. os_atomic_inc_long(&item->ref);
  489. }
  490. void obs_sceneitem_release(obs_sceneitem_t *item)
  491. {
  492. if (!item)
  493. return;
  494. if (os_atomic_dec_long(&item->ref) == 0)
  495. obs_sceneitem_destroy(item);
  496. }
  497. void obs_sceneitem_remove(obs_sceneitem_t *item)
  498. {
  499. obs_scene_t *scene;
  500. if (!item)
  501. return;
  502. scene = item->parent;
  503. if (scene)
  504. pthread_mutex_lock(&scene->mutex);
  505. if (item->removed) {
  506. if (scene)
  507. pthread_mutex_unlock(&scene->mutex);
  508. return;
  509. }
  510. item->removed = true;
  511. assert(scene != NULL);
  512. assert(scene->source != NULL);
  513. obs_source_remove_child(scene->source, item->source);
  514. signal_item_remove(item);
  515. detach_sceneitem(item);
  516. pthread_mutex_unlock(&scene->mutex);
  517. obs_sceneitem_release(item);
  518. }
  519. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  520. {
  521. return item ? item->parent : NULL;
  522. }
  523. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  524. {
  525. return item ? item->source : NULL;
  526. }
  527. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  528. {
  529. struct calldata params = {0};
  530. const char *command = select ? "item_select" : "item_deselect";
  531. if (!item || item->selected == select)
  532. return;
  533. item->selected = select;
  534. calldata_set_ptr(&params, "scene", item->parent);
  535. calldata_set_ptr(&params, "item", item);
  536. signal_handler_signal(item->parent->source->context.signals,
  537. command, &params);
  538. calldata_free(&params);
  539. }
  540. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  541. {
  542. return item ? item->selected : false;
  543. }
  544. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  545. {
  546. if (item) {
  547. vec2_copy(&item->pos, pos);
  548. update_item_transform(item);
  549. }
  550. }
  551. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  552. {
  553. if (item) {
  554. item->rot = rot;
  555. update_item_transform(item);
  556. }
  557. }
  558. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  559. {
  560. if (item) {
  561. vec2_copy(&item->scale, scale);
  562. update_item_transform(item);
  563. }
  564. }
  565. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  566. {
  567. if (item) {
  568. item->align = alignment;
  569. update_item_transform(item);
  570. }
  571. }
  572. static inline void signal_reorder(struct obs_scene_item *item)
  573. {
  574. const char *command = NULL;
  575. struct calldata params = {0};
  576. command = "reorder";
  577. calldata_set_ptr(&params, "scene", item->parent);
  578. signal_handler_signal(item->parent->source->context.signals,
  579. command, &params);
  580. calldata_free(&params);
  581. }
  582. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  583. enum obs_order_movement movement)
  584. {
  585. if (!item) return;
  586. struct obs_scene_item *next, *prev;
  587. struct obs_scene *scene = item->parent;
  588. obs_scene_addref(scene);
  589. pthread_mutex_lock(&scene->mutex);
  590. next = item->next;
  591. prev = item->prev;
  592. detach_sceneitem(item);
  593. if (movement == OBS_ORDER_MOVE_DOWN) {
  594. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  595. } else if (movement == OBS_ORDER_MOVE_UP) {
  596. attach_sceneitem(scene, item, next ? next : prev);
  597. } else if (movement == OBS_ORDER_MOVE_TOP) {
  598. struct obs_scene_item *last = next;
  599. if (!last) {
  600. last = prev;
  601. } else {
  602. while (last->next)
  603. last = last->next;
  604. }
  605. attach_sceneitem(scene, item, last);
  606. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  607. attach_sceneitem(scene, item, NULL);
  608. }
  609. signal_reorder(item);
  610. pthread_mutex_unlock(&scene->mutex);
  611. obs_scene_release(scene);
  612. }
  613. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  614. int position)
  615. {
  616. if (!item) return;
  617. struct obs_scene *scene = item->parent;
  618. struct obs_scene_item *next;
  619. obs_scene_addref(scene);
  620. pthread_mutex_lock(&scene->mutex);
  621. detach_sceneitem(item);
  622. next = scene->first_item;
  623. if (position == 0) {
  624. attach_sceneitem(scene, item, NULL);
  625. } else {
  626. for (int i = position; i > 1; --i) {
  627. if (next->next == NULL)
  628. break;
  629. next = next->next;
  630. }
  631. attach_sceneitem(scene, item, next);
  632. }
  633. signal_reorder(item);
  634. pthread_mutex_unlock(&scene->mutex);
  635. obs_scene_release(scene);
  636. }
  637. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  638. enum obs_bounds_type type)
  639. {
  640. if (item) {
  641. item->bounds_type = type;
  642. update_item_transform(item);
  643. }
  644. }
  645. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  646. uint32_t alignment)
  647. {
  648. if (item) {
  649. item->bounds_align = alignment;
  650. update_item_transform(item);
  651. }
  652. }
  653. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  654. {
  655. if (item) {
  656. item->bounds = *bounds;
  657. update_item_transform(item);
  658. }
  659. }
  660. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  661. {
  662. if (item)
  663. vec2_copy(pos, &item->pos);
  664. }
  665. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  666. {
  667. return item ? item->rot : 0.0f;
  668. }
  669. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  670. {
  671. if (item)
  672. vec2_copy(scale, &item->scale);
  673. }
  674. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  675. {
  676. return item ? item->align : 0;
  677. }
  678. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  679. {
  680. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  681. }
  682. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  683. {
  684. return item ? item->bounds_align : 0;
  685. }
  686. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  687. {
  688. if (item)
  689. *bounds = item->bounds;
  690. }
  691. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  692. struct obs_transform_info *info)
  693. {
  694. if (item && info) {
  695. info->pos = item->pos;
  696. info->rot = item->rot;
  697. info->scale = item->scale;
  698. info->alignment = item->align;
  699. info->bounds_type = item->bounds_type;
  700. info->bounds_alignment = item->bounds_align;
  701. info->bounds = item->bounds;
  702. }
  703. }
  704. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  705. const struct obs_transform_info *info)
  706. {
  707. if (item && info) {
  708. item->pos = info->pos;
  709. item->rot = info->rot;
  710. item->scale = info->scale;
  711. item->align = info->alignment;
  712. item->bounds_type = info->bounds_type;
  713. item->bounds_align = info->bounds_alignment;
  714. item->bounds = info->bounds;
  715. update_item_transform(item);
  716. }
  717. }
  718. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  719. struct matrix4 *transform)
  720. {
  721. if (item)
  722. matrix4_copy(transform, &item->draw_transform);
  723. }
  724. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  725. struct matrix4 *transform)
  726. {
  727. if (item)
  728. matrix4_copy(transform, &item->box_transform);
  729. }