obs-display.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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. struct gs_init_data *graphics_data)
  19. {
  20. pthread_mutex_init_value(&display->draw_callbacks_mutex);
  21. if (graphics_data) {
  22. display->swap = gs_create_swapchain(graphics_data);
  23. if (!display->swap) {
  24. blog(LOG_ERROR, "obs_display_init: Failed to "
  25. "create swap chain");
  26. return false;
  27. }
  28. display->cx = graphics_data->cx;
  29. display->cy = graphics_data->cy;
  30. }
  31. if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
  32. blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
  33. return false;
  34. }
  35. return true;
  36. }
  37. obs_display_t obs_display_create(struct gs_init_data *graphics_data)
  38. {
  39. struct obs_display *display = bzalloc(sizeof(struct obs_display));
  40. gs_entercontext(obs_graphics());
  41. if (!graphics_data->num_backbuffers)
  42. graphics_data->num_backbuffers = 1;
  43. if (!obs_display_init(display, graphics_data)) {
  44. obs_display_destroy(display);
  45. display = NULL;
  46. } else {
  47. pthread_mutex_lock(&obs->data.displays_mutex);
  48. da_push_back(obs->data.displays, &display);
  49. pthread_mutex_unlock(&obs->data.displays_mutex);
  50. }
  51. gs_leavecontext();
  52. return display;
  53. }
  54. void obs_display_free(obs_display_t display)
  55. {
  56. pthread_mutex_destroy(&display->draw_callbacks_mutex);
  57. da_free(display->draw_callbacks);
  58. if (display->swap) {
  59. swapchain_destroy(display->swap);
  60. display->swap = NULL;
  61. }
  62. }
  63. void obs_display_destroy(obs_display_t display)
  64. {
  65. if (display) {
  66. pthread_mutex_lock(&obs->data.displays_mutex);
  67. da_erase_item(obs->data.displays, &display);
  68. pthread_mutex_unlock(&obs->data.displays_mutex);
  69. gs_entercontext(obs_graphics());
  70. obs_display_free(display);
  71. gs_leavecontext();
  72. bfree(display);
  73. }
  74. }
  75. void obs_display_resize(obs_display_t display, uint32_t cx, uint32_t cy)
  76. {
  77. if (!display) return;
  78. pthread_mutex_lock(&display->draw_callbacks_mutex);
  79. display->cx = cx;
  80. display->cy = cy;
  81. display->size_changed = true;
  82. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  83. }
  84. void obs_display_add_draw_callback(obs_display_t display,
  85. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  86. void *param)
  87. {
  88. if (!display) return;
  89. struct draw_callback data = {draw, param};
  90. pthread_mutex_lock(&display->draw_callbacks_mutex);
  91. da_push_back(display->draw_callbacks, &data);
  92. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  93. }
  94. void obs_display_remove_draw_callback(obs_display_t display,
  95. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  96. void *param)
  97. {
  98. if (!display) return;
  99. struct draw_callback data = {draw, param};
  100. pthread_mutex_lock(&display->draw_callbacks_mutex);
  101. da_erase_item(display->draw_callbacks, &data);
  102. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  103. }
  104. static inline void render_display_begin(struct obs_display *display)
  105. {
  106. struct vec4 clear_color;
  107. gs_load_swapchain(display ? display->swap : NULL);
  108. if (display->size_changed) {
  109. gs_resize(display->cx, display->cy);
  110. display->size_changed = false;
  111. }
  112. gs_beginscene();
  113. vec4_set(&clear_color, 0.3f, 0.3f, 0.3f, 1.0f);
  114. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  115. &clear_color, 1.0f, 0);
  116. gs_enable_depthtest(false);
  117. /* gs_enable_blending(false); */
  118. gs_setcullmode(GS_NEITHER);
  119. gs_ortho(0.0f, (float)display->cx,
  120. 0.0f, (float)display->cy, -100.0f, 100.0f);
  121. gs_setviewport(0, 0, display->cx, display->cy);
  122. }
  123. static inline void render_display_end()
  124. {
  125. gs_endscene();
  126. gs_present();
  127. }
  128. void render_display(struct obs_display *display)
  129. {
  130. if (!display) return;
  131. render_display_begin(display);
  132. pthread_mutex_lock(&display->draw_callbacks_mutex);
  133. for (size_t i = 0; i < display->draw_callbacks.num; i++) {
  134. struct draw_callback *callback;
  135. callback = display->draw_callbacks.array+i;
  136. callback->draw(callback->param, display->cx, display->cy);
  137. }
  138. pthread_mutex_unlock(&display->draw_callbacks_mutex);
  139. render_display_end();
  140. }