obs-display.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "graphics/vec4.h"
  15. #include "obs.h"
  16. #include "obs-internal.h"
  17. bool obs_display_init(struct obs_display *display,
  18. const struct gs_init_data *graphics_data)
  19. {
  20. pthread_mutex_init_value(&display->draw_callbacks_mutex);
  21. pthread_mutex_init_value(&display->draw_info_mutex);
  22. #if defined(_WIN32)
  23. /* Conservative test for NVIDIA flickering in multi-GPU setups */
  24. display->use_clear_workaround = gs_get_adapter_count() > 1 &&
  25. !gs_can_adapter_fast_clear();
  26. #elif defined(__APPLE__)
  27. /* Apple Silicon GL driver doesn't seem to track SRGB clears correctly */
  28. display->use_clear_workaround = true;
  29. #else
  30. display->use_clear_workaround = false;
  31. #endif
  32. if (graphics_data) {
  33. display->swap = gs_swapchain_create(graphics_data);
  34. if (!display->swap) {
  35. blog(LOG_ERROR, "obs_display_init: Failed to "
  36. "create swap chain");
  37. return false;
  38. }
  39. const uint32_t cx = graphics_data->cx;
  40. const uint32_t cy = graphics_data->cy;
  41. display->cx = cx;
  42. display->cy = cy;
  43. display->next_cx = cx;
  44. display->next_cy = cy;
  45. }
  46. if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
  47. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  48. return false;
  49. }
  50. if (pthread_mutex_init(&display->draw_info_mutex, NULL) != 0) {
  51. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  52. return false;
  53. }
  54. display->enabled = true;
  55. return true;
  56. }
  57. obs_display_t *obs_display_create(const struct gs_init_data *graphics_data,
  58. uint32_t background_color)
  59. {
  60. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  61. gs_enter_context(obs->video.graphics);
  62. display->background_color = background_color;
  63. if (!obs_display_init(display, graphics_data)) {
  64. obs_display_destroy(display);
  65. display = NULL;
  66. } else {
  67. pthread_mutex_lock(&obs->data.displays_mutex);
  68. display->prev_next = &obs->data.first_display;
  69. display->next = obs->data.first_display;
  70. obs->data.first_display = display;
  71. if (display->next)
  72. display->next->prev_next = &display->next;
  73. pthread_mutex_unlock(&obs->data.displays_mutex);
  74. }
  75. gs_leave_context();
  76. return display;
  77. }
  78. void obs_display_free(obs_display_t *display)
  79. {
  80. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  81. pthread_mutex_destroy(&display->draw_info_mutex);
  82. da_free(display->draw_callbacks);
  83. if (display->swap) {
  84. gs_swapchain_destroy(display->swap);
  85. display->swap = NULL;
  86. }
  87. }
  88. void obs_display_destroy(obs_display_t *display)
  89. {
  90. if (display) {
  91. pthread_mutex_lock(&obs->data.displays_mutex);
  92. if (display->prev_next)
  93. *display->prev_next = display->next;
  94. if (display->next)
  95. display->next->prev_next = display->prev_next;
  96. pthread_mutex_unlock(&obs->data.displays_mutex);
  97. obs_enter_graphics();
  98. obs_display_free(display);
  99. obs_leave_graphics();
  100. bfree(display);
  101. }
  102. }
  103. void obs_display_resize(obs_display_t *display, uint32_t cx, uint32_t cy)
  104. {
  105. if (!display)
  106. return;
  107. pthread_mutex_lock(&display->draw_info_mutex);
  108. display->next_cx = cx;
  109. display->next_cy = cy;
  110. pthread_mutex_unlock(&display->draw_info_mutex);
  111. }
  112. void obs_display_update_color_space(obs_display_t *display)
  113. {
  114. if (!display)
  115. return;
  116. pthread_mutex_lock(&display->draw_info_mutex);
  117. display->update_color_space = true;
  118. pthread_mutex_unlock(&display->draw_info_mutex);
  119. }
  120. void obs_display_add_draw_callback(obs_display_t *display,
  121. void (*draw)(void *param, uint32_t cx,
  122. uint32_t cy),
  123. void *param)
  124. {
  125. if (!display)
  126. return;
  127. struct draw_callback data = {draw, param};
  128. pthread_mutex_lock(&display->draw_callbacks_mutex);
  129. da_push_back(display->draw_callbacks, &data);
  130. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  131. }
  132. void obs_display_remove_draw_callback(obs_display_t *display,
  133. void (*draw)(void *param, uint32_t cx,
  134. uint32_t cy),
  135. void *param)
  136. {
  137. if (!display)
  138. return;
  139. struct draw_callback data = {draw, param};
  140. pthread_mutex_lock(&display->draw_callbacks_mutex);
  141. da_erase_item(display->draw_callbacks, &data);
  142. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  143. }
  144. static inline bool render_display_begin(struct obs_display *display,
  145. uint32_t cx, uint32_t cy,
  146. bool update_color_space)
  147. {
  148. struct vec4 clear_color;
  149. gs_load_swapchain(display->swap);
  150. if ((display->cx != cx) || (display->cy != cy)) {
  151. gs_resize(cx, cy);
  152. display->cx = cx;
  153. display->cy = cy;
  154. } else if (update_color_space) {
  155. gs_update_color_space();
  156. }
  157. const bool success = gs_is_present_ready();
  158. if (success) {
  159. gs_begin_scene();
  160. if (gs_get_color_space() == GS_CS_SRGB)
  161. vec4_from_rgba(&clear_color, display->background_color);
  162. else
  163. vec4_from_rgba_srgb(&clear_color,
  164. display->background_color);
  165. clear_color.w = 1.0f;
  166. const bool use_clear_workaround = display->use_clear_workaround;
  167. uint32_t clear_flags = GS_CLEAR_DEPTH | GS_CLEAR_STENCIL;
  168. if (!use_clear_workaround)
  169. clear_flags |= GS_CLEAR_COLOR;
  170. gs_clear(clear_flags, &clear_color, 1.0f, 0);
  171. gs_enable_depth_test(false);
  172. /* gs_enable_blending(false); */
  173. gs_set_cull_mode(GS_NEITHER);
  174. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  175. gs_set_viewport(0, 0, cx, cy);
  176. if (use_clear_workaround) {
  177. gs_effect_t *const solid_effect =
  178. obs->video.solid_effect;
  179. gs_effect_set_vec4(gs_effect_get_param_by_name(
  180. solid_effect, "color"),
  181. &clear_color);
  182. while (gs_effect_loop(solid_effect, "Solid"))
  183. gs_draw_sprite(NULL, 0, cx, cy);
  184. }
  185. }
  186. return success;
  187. }
  188. static inline void render_display_end()
  189. {
  190. gs_end_scene();
  191. }
  192. void render_display(struct obs_display *display)
  193. {
  194. uint32_t cx, cy;
  195. bool update_color_space;
  196. if (!display || !display->enabled)
  197. return;
  198. /* -------------------------------------------- */
  199. pthread_mutex_lock(&display->draw_info_mutex);
  200. cx = display->next_cx;
  201. cy = display->next_cy;
  202. update_color_space = display->update_color_space;
  203. display->update_color_space = false;
  204. pthread_mutex_unlock(&display->draw_info_mutex);
  205. /* -------------------------------------------- */
  206. if (render_display_begin(display, cx, cy, update_color_space)) {
  207. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DISPLAY, "obs_display");
  208. pthread_mutex_lock(&display->draw_callbacks_mutex);
  209. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  210. struct draw_callback *callback;
  211. callback = display->draw_callbacks.array + i;
  212. callback->draw(callback->param, cx, cy);
  213. }
  214. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  215. render_display_end();
  216. GS_DEBUG_MARKER_END();
  217. gs_present();
  218. }
  219. }
  220. void obs_display_set_enabled(obs_display_t *display, bool enable)
  221. {
  222. if (display)
  223. display->enabled = enable;
  224. }
  225. bool obs_display_enabled(obs_display_t *display)
  226. {
  227. return display ? display->enabled : false;
  228. }
  229. void obs_display_set_background_color(obs_display_t *display, uint32_t color)
  230. {
  231. if (display)
  232. display->background_color = color;
  233. }
  234. void obs_display_size(obs_display_t *display, uint32_t *width, uint32_t *height)
  235. {
  236. *width = 0;
  237. *height = 0;
  238. if (display) {
  239. pthread_mutex_lock(&display->draw_info_mutex);
  240. *width = display->cx;
  241. *height = display->cy;
  242. pthread_mutex_unlock(&display->draw_info_mutex);
  243. }
  244. }