1
0

obs-display.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #else
  25. display->use_clear_workaround = false;
  26. #endif
  27. if (graphics_data) {
  28. display->swap = gs_swapchain_create(graphics_data);
  29. if (!display->swap) {
  30. blog(LOG_ERROR, "obs_display_init: Failed to "
  31. "create swap chain");
  32. return false;
  33. }
  34. const uint32_t cx = graphics_data->cx;
  35. const uint32_t cy = graphics_data->cy;
  36. display->cx = cx;
  37. display->cy = cy;
  38. display->next_cx = cx;
  39. display->next_cy = cy;
  40. }
  41. if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
  42. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  43. return false;
  44. }
  45. if (pthread_mutex_init(&display->draw_info_mutex, NULL) != 0) {
  46. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  47. return false;
  48. }
  49. display->enabled = true;
  50. return true;
  51. }
  52. obs_display_t *obs_display_create(const struct gs_init_data *graphics_data, uint32_t background_color)
  53. {
  54. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  55. gs_enter_context(obs->video.graphics);
  56. display->background_color = background_color;
  57. if (!obs_display_init(display, graphics_data)) {
  58. obs_display_destroy(display);
  59. display = NULL;
  60. } else {
  61. pthread_mutex_lock(&obs->data.displays_mutex);
  62. display->prev_next = &obs->data.first_display;
  63. display->next = obs->data.first_display;
  64. obs->data.first_display = display;
  65. if (display->next)
  66. display->next->prev_next = &display->next;
  67. pthread_mutex_unlock(&obs->data.displays_mutex);
  68. }
  69. gs_leave_context();
  70. return display;
  71. }
  72. void obs_display_free(obs_display_t *display)
  73. {
  74. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  75. pthread_mutex_destroy(&display->draw_info_mutex);
  76. da_free(display->draw_callbacks);
  77. if (display->swap) {
  78. gs_swapchain_destroy(display->swap);
  79. display->swap = NULL;
  80. }
  81. }
  82. void obs_display_destroy(obs_display_t *display)
  83. {
  84. if (display) {
  85. pthread_mutex_lock(&obs->data.displays_mutex);
  86. if (display->prev_next)
  87. *display->prev_next = display->next;
  88. if (display->next)
  89. display->next->prev_next = display->prev_next;
  90. pthread_mutex_unlock(&obs->data.displays_mutex);
  91. obs_enter_graphics();
  92. obs_display_free(display);
  93. obs_leave_graphics();
  94. bfree(display);
  95. }
  96. }
  97. void obs_display_resize(obs_display_t *display, uint32_t cx, uint32_t cy)
  98. {
  99. if (!display)
  100. return;
  101. pthread_mutex_lock(&display->draw_info_mutex);
  102. display->next_cx = cx;
  103. display->next_cy = cy;
  104. pthread_mutex_unlock(&display->draw_info_mutex);
  105. }
  106. void obs_display_update_color_space(obs_display_t *display)
  107. {
  108. if (!display)
  109. return;
  110. pthread_mutex_lock(&display->draw_info_mutex);
  111. display->update_color_space = true;
  112. pthread_mutex_unlock(&display->draw_info_mutex);
  113. }
  114. void obs_display_add_draw_callback(obs_display_t *display, void (*draw)(void *param, uint32_t cx, uint32_t cy),
  115. void *param)
  116. {
  117. if (!display)
  118. return;
  119. struct draw_callback data = {draw, param};
  120. pthread_mutex_lock(&display->draw_callbacks_mutex);
  121. da_push_back(display->draw_callbacks, &data);
  122. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  123. }
  124. void obs_display_remove_draw_callback(obs_display_t *display, void (*draw)(void *param, uint32_t cx, uint32_t cy),
  125. void *param)
  126. {
  127. if (!display)
  128. return;
  129. struct draw_callback data = {draw, param};
  130. pthread_mutex_lock(&display->draw_callbacks_mutex);
  131. da_erase_item(display->draw_callbacks, &data);
  132. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  133. }
  134. static inline bool render_display_begin(struct obs_display *display, uint32_t cx, uint32_t cy, bool update_color_space)
  135. {
  136. struct vec4 clear_color;
  137. gs_load_swapchain(display->swap);
  138. if ((display->cx != cx) || (display->cy != cy)) {
  139. gs_resize(cx, cy);
  140. display->cx = cx;
  141. display->cy = cy;
  142. } else if (update_color_space) {
  143. gs_update_color_space();
  144. }
  145. const bool success = gs_is_present_ready();
  146. if (success) {
  147. gs_begin_scene();
  148. /*
  149. * In contrast to OpenGL or Direct3D 11, Metal and Direct3D 12 require the clear color to use linear gamma
  150. * as either the load command to clear the render target (Metal) or the explicit clear command seem to operate
  151. * on the render target in linear space.
  152. *
  153. * As OpenGL is implemented via Metal on Apple Silicon Macs and "glClear" has to be emulated via an explicit
  154. * render pass that returns the clear color for every fragment, the color becomes subject to automatic sRGB
  155. * gamma encoding if the render target uses an sRGB color format.
  156. */
  157. #if defined(__APPLE__) && defined(__aarch64__)
  158. vec4_from_rgba_srgb(&clear_color, display->background_color);
  159. #else
  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, display->background_color);
  164. #endif
  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 = obs->video.solid_effect;
  178. gs_effect_set_vec4(gs_effect_get_param_by_name(solid_effect, "color"), &clear_color);
  179. while (gs_effect_loop(solid_effect, "Solid"))
  180. gs_draw_sprite(NULL, 0, cx, cy);
  181. }
  182. }
  183. return success;
  184. }
  185. static inline void render_display_end()
  186. {
  187. gs_end_scene();
  188. }
  189. void render_display(struct obs_display *display)
  190. {
  191. uint32_t cx, cy;
  192. bool update_color_space;
  193. if (!display || !display->enabled)
  194. return;
  195. /* -------------------------------------------- */
  196. pthread_mutex_lock(&display->draw_info_mutex);
  197. cx = display->next_cx;
  198. cy = display->next_cy;
  199. update_color_space = display->update_color_space;
  200. display->update_color_space = false;
  201. pthread_mutex_unlock(&display->draw_info_mutex);
  202. /* -------------------------------------------- */
  203. if (render_display_begin(display, cx, cy, update_color_space)) {
  204. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_DISPLAY, "obs_display");
  205. pthread_mutex_lock(&display->draw_callbacks_mutex);
  206. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  207. struct draw_callback *callback;
  208. callback = display->draw_callbacks.array + i;
  209. callback->draw(callback->param, cx, cy);
  210. }
  211. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  212. render_display_end();
  213. GS_DEBUG_MARKER_END();
  214. gs_present();
  215. }
  216. }
  217. void obs_display_set_enabled(obs_display_t *display, bool enable)
  218. {
  219. if (display)
  220. display->enabled = enable;
  221. }
  222. bool obs_display_enabled(obs_display_t *display)
  223. {
  224. return display ? display->enabled : false;
  225. }
  226. void obs_display_set_background_color(obs_display_t *display, uint32_t color)
  227. {
  228. if (display)
  229. display->background_color = color;
  230. }
  231. void obs_display_size(obs_display_t *display, uint32_t *width, uint32_t *height)
  232. {
  233. *width = 0;
  234. *height = 0;
  235. if (display) {
  236. pthread_mutex_lock(&display->draw_info_mutex);
  237. *width = display->cx;
  238. *height = display->cy;
  239. pthread_mutex_unlock(&display->draw_info_mutex);
  240. }
  241. }