obs-display.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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, const struct gs_init_data *graphics_data)
  18. {
  19. pthread_mutex_init_value(&display->draw_callbacks_mutex);
  20. pthread_mutex_init_value(&display->draw_info_mutex);
  21. #if defined(_WIN32)
  22. /* Conservative test for NVIDIA flickering in multi-GPU setups */
  23. display->use_clear_workaround = gs_get_adapter_count() > 1 && !gs_can_adapter_fast_clear();
  24. #elif defined(__APPLE__)
  25. /* Apple Silicon GL driver doesn't seem to track SRGB clears correctly */
  26. display->use_clear_workaround = true;
  27. #else
  28. display->use_clear_workaround = false;
  29. #endif
  30. if (graphics_data) {
  31. display->swap = gs_swapchain_create(graphics_data);
  32. if (!display->swap) {
  33. blog(LOG_ERROR, "obs_display_init: Failed to "
  34. "create swap chain");
  35. return false;
  36. }
  37. const uint32_t cx = graphics_data->cx;
  38. const uint32_t cy = graphics_data->cy;
  39. display->cx = cx;
  40. display->cy = cy;
  41. display->next_cx = cx;
  42. display->next_cy = cy;
  43. }
  44. if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
  45. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  46. return false;
  47. }
  48. if (pthread_mutex_init(&display->draw_info_mutex, NULL) != 0) {
  49. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  50. return false;
  51. }
  52. display->enabled = true;
  53. return true;
  54. }
  55. obs_display_t *obs_display_create(const struct gs_init_data *graphics_data, uint32_t background_color)
  56. {
  57. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  58. gs_enter_context(obs->video.graphics);
  59. display->background_color = background_color;
  60. if (!obs_display_init(display, graphics_data)) {
  61. obs_display_destroy(display);
  62. display = NULL;
  63. } else {
  64. pthread_mutex_lock(&obs->data.displays_mutex);
  65. display->prev_next = &obs->data.first_display;
  66. display->next = obs->data.first_display;
  67. obs->data.first_display = display;
  68. if (display->next)
  69. display->next->prev_next = &display->next;
  70. pthread_mutex_unlock(&obs->data.displays_mutex);
  71. }
  72. gs_leave_context();
  73. return display;
  74. }
  75. void obs_display_free(obs_display_t *display)
  76. {
  77. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  78. pthread_mutex_destroy(&display->draw_info_mutex);
  79. da_free(display->draw_callbacks);
  80. if (display->swap) {
  81. gs_swapchain_destroy(display->swap);
  82. display->swap = NULL;
  83. }
  84. }
  85. void obs_display_destroy(obs_display_t *display)
  86. {
  87. if (display) {
  88. pthread_mutex_lock(&obs->data.displays_mutex);
  89. if (display->prev_next)
  90. *display->prev_next = display->next;
  91. if (display->next)
  92. display->next->prev_next = display->prev_next;
  93. pthread_mutex_unlock(&obs->data.displays_mutex);
  94. obs_enter_graphics();
  95. obs_display_free(display);
  96. obs_leave_graphics();
  97. bfree(display);
  98. }
  99. }
  100. void obs_display_resize(obs_display_t *display, uint32_t cx, uint32_t cy)
  101. {
  102. if (!display)
  103. return;
  104. pthread_mutex_lock(&display->draw_info_mutex);
  105. display->next_cx = cx;
  106. display->next_cy = cy;
  107. pthread_mutex_unlock(&display->draw_info_mutex);
  108. }
  109. void obs_display_update_color_space(obs_display_t *display)
  110. {
  111. if (!display)
  112. return;
  113. pthread_mutex_lock(&display->draw_info_mutex);
  114. display->update_color_space = true;
  115. pthread_mutex_unlock(&display->draw_info_mutex);
  116. }
  117. void obs_display_add_draw_callback(obs_display_t *display, void (*draw)(void *param, uint32_t cx, uint32_t cy),
  118. void *param)
  119. {
  120. if (!display)
  121. return;
  122. struct draw_callback data = {draw, param};
  123. pthread_mutex_lock(&display->draw_callbacks_mutex);
  124. da_push_back(display->draw_callbacks, &data);
  125. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  126. }
  127. void obs_display_remove_draw_callback(obs_display_t *display, void (*draw)(void *param, uint32_t cx, uint32_t cy),
  128. void *param)
  129. {
  130. if (!display)
  131. return;
  132. struct draw_callback data = {draw, param};
  133. pthread_mutex_lock(&display->draw_callbacks_mutex);
  134. da_erase_item(display->draw_callbacks, &data);
  135. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  136. }
  137. static inline bool render_display_begin(struct obs_display *display, uint32_t cx, uint32_t cy, bool update_color_space)
  138. {
  139. struct vec4 clear_color;
  140. gs_load_swapchain(display->swap);
  141. if ((display->cx != cx) || (display->cy != cy)) {
  142. gs_resize(cx, cy);
  143. display->cx = cx;
  144. display->cy = cy;
  145. } else if (update_color_space) {
  146. gs_update_color_space();
  147. }
  148. const bool success = gs_is_present_ready();
  149. if (success) {
  150. gs_begin_scene();
  151. if (gs_get_color_space() == GS_CS_SRGB)
  152. vec4_from_rgba(&clear_color, display->background_color);
  153. else
  154. vec4_from_rgba_srgb(&clear_color, display->background_color);
  155. clear_color.w = 1.0f;
  156. const bool use_clear_workaround = display->use_clear_workaround;
  157. uint32_t clear_flags = GS_CLEAR_DEPTH | GS_CLEAR_STENCIL;
  158. if (!use_clear_workaround)
  159. clear_flags |= GS_CLEAR_COLOR;
  160. gs_clear(clear_flags, &clear_color, 1.0f, 0);
  161. gs_enable_depth_test(false);
  162. /* gs_enable_blending(false); */
  163. gs_set_cull_mode(GS_NEITHER);
  164. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  165. gs_set_viewport(0, 0, cx, cy);
  166. if (use_clear_workaround) {
  167. gs_effect_t *const solid_effect = obs->video.solid_effect;
  168. gs_effect_set_vec4(gs_effect_get_param_by_name(solid_effect, "color"), &clear_color);
  169. while (gs_effect_loop(solid_effect, "Solid"))
  170. gs_draw_sprite(NULL, 0, cx, cy);
  171. }
  172. }
  173. return success;
  174. }
  175. static inline void render_display_end()
  176. {
  177. gs_end_scene();
  178. }
  179. void render_display(struct obs_display *display)
  180. {
  181. uint32_t cx, cy;
  182. bool update_color_space;
  183. if (!display || !display->enabled)
  184. return;
  185. /* -------------------------------------------- */
  186. pthread_mutex_lock(&display->draw_info_mutex);
  187. cx = display->next_cx;
  188. cy = display->next_cy;
  189. update_color_space = display->update_color_space;
  190. display->update_color_space = false;
  191. pthread_mutex_unlock(&display->draw_info_mutex);
  192. /* -------------------------------------------- */
  193. if (render_display_begin(display, cx, cy, update_color_space)) {
  194. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DISPLAY, "obs_display");
  195. pthread_mutex_lock(&display->draw_callbacks_mutex);
  196. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  197. struct draw_callback *callback;
  198. callback = display->draw_callbacks.array + i;
  199. callback->draw(callback->param, cx, cy);
  200. }
  201. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  202. render_display_end();
  203. GS_DEBUG_MARKER_END();
  204. gs_present();
  205. }
  206. }
  207. void obs_display_set_enabled(obs_display_t *display, bool enable)
  208. {
  209. if (display)
  210. display->enabled = enable;
  211. }
  212. bool obs_display_enabled(obs_display_t *display)
  213. {
  214. return display ? display->enabled : false;
  215. }
  216. void obs_display_set_background_color(obs_display_t *display, uint32_t color)
  217. {
  218. if (display)
  219. display->background_color = color;
  220. }
  221. void obs_display_size(obs_display_t *display, uint32_t *width, uint32_t *height)
  222. {
  223. *width = 0;
  224. *height = 0;
  225. if (display) {
  226. pthread_mutex_lock(&display->draw_info_mutex);
  227. *width = display->cx;
  228. *height = display->cy;
  229. pthread_mutex_unlock(&display->draw_info_mutex);
  230. }
  231. }