2013-09-30 19:37:13 -07:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2013-12-02 21:24:38 -08:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2013-09-30 19:37:13 -07:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
#include "graphics/vec4.h"
|
2013-09-30 19:37:13 -07:00
|
|
|
#include "obs.h"
|
2014-01-26 17:48:14 -08:00
|
|
|
#include "obs-internal.h"
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
bool obs_display_init(struct obs_display *display,
|
|
|
|
struct gs_init_data *graphics_data)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2014-02-13 07:58:31 -08:00
|
|
|
pthread_mutex_init_value(&display->draw_callbacks_mutex);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
if (graphics_data) {
|
|
|
|
display->swap = gs_create_swapchain(graphics_data);
|
|
|
|
if (!display->swap) {
|
2014-02-13 07:58:31 -08:00
|
|
|
blog(LOG_ERROR, "obs_display_init: Failed to "
|
|
|
|
"create swap chain");
|
|
|
|
return false;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
2014-02-13 07:58:31 -08:00
|
|
|
|
|
|
|
display->cx = graphics_data->cx;
|
|
|
|
display->cy = graphics_data->cy;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&display->draw_callbacks_mutex, NULL) != 0) {
|
|
|
|
blog(LOG_ERROR, "obs_display_init: Failed to create mutex");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_display_t obs_display_create(struct gs_init_data *graphics_data)
|
|
|
|
{
|
|
|
|
struct obs_display *display = bzalloc(sizeof(struct obs_display));
|
|
|
|
|
2014-03-23 01:07:54 -07:00
|
|
|
gs_entercontext(obs_graphics());
|
|
|
|
|
|
|
|
if (!graphics_data->num_backbuffers)
|
|
|
|
graphics_data->num_backbuffers = 1;
|
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
if (!obs_display_init(display, graphics_data)) {
|
|
|
|
obs_display_destroy(display);
|
|
|
|
display = NULL;
|
2014-03-23 01:07:54 -07:00
|
|
|
} else {
|
|
|
|
pthread_mutex_lock(&obs->data.displays_mutex);
|
|
|
|
da_push_back(obs->data.displays, &display);
|
|
|
|
pthread_mutex_unlock(&obs->data.displays_mutex);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2014-03-23 01:07:54 -07:00
|
|
|
gs_leavecontext();
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
return display;
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
void obs_display_free(obs_display_t display)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&display->draw_callbacks_mutex);
|
|
|
|
da_free(display->draw_callbacks);
|
|
|
|
|
|
|
|
if (display->swap) {
|
|
|
|
swapchain_destroy(display->swap);
|
|
|
|
display->swap = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:37:52 -07:00
|
|
|
void obs_display_destroy(obs_display_t display)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
if (display) {
|
2013-11-20 14:00:16 -08:00
|
|
|
pthread_mutex_lock(&obs->data.displays_mutex);
|
|
|
|
da_erase_item(obs->data.displays, &display);
|
|
|
|
pthread_mutex_unlock(&obs->data.displays_mutex);
|
|
|
|
|
2014-03-23 01:07:54 -07:00
|
|
|
gs_entercontext(obs_graphics());
|
2014-02-13 07:58:31 -08:00
|
|
|
obs_display_free(display);
|
2014-03-23 01:07:54 -07:00
|
|
|
gs_leavecontext();
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
bfree(display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
void obs_display_resize(obs_display_t display, uint32_t cx, uint32_t cy)
|
|
|
|
{
|
|
|
|
if (!display) return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&display->draw_callbacks_mutex);
|
|
|
|
|
|
|
|
display->cx = cx;
|
|
|
|
display->cy = cy;
|
|
|
|
display->size_changed = true;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&display->draw_callbacks_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_display_add_draw_callback(obs_display_t display,
|
|
|
|
void (*draw)(void *param, uint32_t cx, uint32_t cy),
|
|
|
|
void *param)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2014-02-13 07:58:31 -08:00
|
|
|
if (!display) return;
|
2014-01-30 00:31:52 -08:00
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
struct draw_callback data = {draw, param};
|
2014-01-30 00:31:52 -08:00
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
pthread_mutex_lock(&display->draw_callbacks_mutex);
|
|
|
|
da_push_back(display->draw_callbacks, &data);
|
|
|
|
pthread_mutex_unlock(&display->draw_callbacks_mutex);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
void obs_display_remove_draw_callback(obs_display_t display,
|
|
|
|
void (*draw)(void *param, uint32_t cx, uint32_t cy),
|
|
|
|
void *param)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2014-02-13 07:58:31 -08:00
|
|
|
if (!display) return;
|
|
|
|
|
|
|
|
struct draw_callback data = {draw, param};
|
|
|
|
|
|
|
|
pthread_mutex_lock(&display->draw_callbacks_mutex);
|
|
|
|
da_erase_item(display->draw_callbacks, &data);
|
|
|
|
pthread_mutex_unlock(&display->draw_callbacks_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void render_display_begin(struct obs_display *display)
|
|
|
|
{
|
|
|
|
struct vec4 clear_color;
|
|
|
|
|
|
|
|
gs_load_swapchain(display ? display->swap : NULL);
|
|
|
|
|
|
|
|
if (display->size_changed) {
|
|
|
|
gs_resize(display->cx, display->cy);
|
|
|
|
display->size_changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_beginscene();
|
2014-03-07 09:19:03 -08:00
|
|
|
|
|
|
|
vec4_set(&clear_color, 0.3f, 0.3f, 0.3f, 1.0f);
|
2014-02-13 07:58:31 -08:00
|
|
|
gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
|
|
|
|
&clear_color, 1.0f, 0);
|
|
|
|
|
|
|
|
gs_enable_depthtest(false);
|
|
|
|
/* gs_enable_blending(false); */
|
|
|
|
gs_setcullmode(GS_NEITHER);
|
|
|
|
|
2014-03-07 09:19:03 -08:00
|
|
|
gs_ortho(0.0f, (float)display->cx,
|
|
|
|
0.0f, (float)display->cy, -100.0f, 100.0f);
|
2014-02-13 07:58:31 -08:00
|
|
|
gs_setviewport(0, 0, display->cx, display->cy);
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:13:36 -08:00
|
|
|
static inline void render_display_end()
|
2014-02-13 07:58:31 -08:00
|
|
|
{
|
|
|
|
gs_endscene();
|
|
|
|
gs_present();
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_display(struct obs_display *display)
|
|
|
|
{
|
|
|
|
if (!display) return;
|
|
|
|
|
|
|
|
render_display_begin(display);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&display->draw_callbacks_mutex);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < display->draw_callbacks.num; i++) {
|
|
|
|
struct draw_callback *callback;
|
|
|
|
callback = display->draw_callbacks.array+i;
|
|
|
|
|
|
|
|
callback->draw(callback->param, display->cx, display->cy);
|
|
|
|
}
|
2013-11-20 14:00:16 -08:00
|
|
|
|
2014-02-13 07:58:31 -08:00
|
|
|
pthread_mutex_unlock(&display->draw_callbacks_mutex);
|
2013-11-20 14:00:16 -08:00
|
|
|
|
2014-02-14 14:13:36 -08:00
|
|
|
render_display_end();
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|