obs-video.c 4.0 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5
/******************************************************************************
    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
6
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17 18
    (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/>.
******************************************************************************/

#include "obs.h"
J
jp9000 已提交
19
#include "obs-internal.h"
J
jp9000 已提交
20 21
#include "graphics/vec4.h"

22
static void tick_sources(uint64_t cur_time, uint64_t *last_time)
J
jp9000 已提交
23 24 25 26 27 28
{
	size_t i;
	uint64_t delta_time;
	float seconds;

	if (!last_time)
29
		*last_time = cur_time - video_getframetime(obs->video.video);
J
jp9000 已提交
30 31 32
	delta_time = cur_time - *last_time;
	seconds = (float)((double)delta_time / 1000000000.0);

33 34
	for (i = 0; i < obs->data.sources.num; i++)
		obs_source_video_tick(obs->data.sources.array[i], seconds);
J
jp9000 已提交
35 36 37 38

	*last_time = cur_time;
}

39
static inline void render_begin(struct obs_display *display)
J
jp9000 已提交
40 41
{
	struct vec4 clear_color;
42
	uint32_t width, height;
J
jp9000 已提交
43

44
	gs_load_swapchain(display ? display->swap : NULL);
J
jp9000 已提交
45

46
	gs_getsize(&width, &height);
J
jp9000 已提交
47 48

	gs_beginscene();
49
	vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
50
	gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
J
jp9000 已提交
51 52
			&clear_color, 1.0f, 0);

53
	gs_enable_depthtest(false);
54
	/* gs_enable_blending(false); */
55 56
	gs_setcullmode(GS_NEITHER);

57 58
	gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
	gs_setviewport(0, 0, width, height);
59 60 61 62 63 64 65 66 67 68 69 70 71
}

static inline void render_end(struct obs_display *display)
{
	gs_endscene();
	gs_present();
}

static void render_display(struct obs_display *display)
{
	size_t i;
	render_begin(display);

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	for (i = 0; i < MAX_CHANNELS; i++) {
		struct obs_source **p_source;

		p_source = (display) ? display->channels+i :
		                       obs->data.channels+i;

		if (*p_source) {
			if ((*p_source)->removed) {
				obs_source_release(*p_source);
				*p_source = NULL;
			} else {
				obs_source_video_render(*p_source);
			}
		}
	}
J
jp9000 已提交
87

88
	render_end(display);
J
jp9000 已提交
89 90
}

91 92 93 94
static inline void render_displays(void)
{
	size_t i;

95 96 97
	if (!obs->data.valid)
		return;

98 99
	/* render extra displays/swaps */
	pthread_mutex_lock(&obs->data.displays_mutex);
100

101
	for (i = 0; i < obs->data.displays.num; i++) {
102
		render_display(obs->data.displays.array[i]);
103
	}
104

105
	pthread_mutex_unlock(&obs->data.displays_mutex);
106

107
	/* render main display */
108 109 110
	render_display(NULL);
}

111
static bool swap_frame(uint64_t timestamp)
J
jp9000 已提交
112
{
113 114
	struct obs_video *video = &obs->video;
	stagesurf_t last_surface = video->copy_surfaces[video->cur_texture];
J
jp9000 已提交
115 116 117
	stagesurf_t surface;
	struct video_frame frame;

118 119
	/* the last frame stays mapped until rendering starts with the next */
	if (video->copy_mapped) {
J
jp9000 已提交
120
		stagesurface_unmap(last_surface);
121
		video->copy_mapped = false;
J
jp9000 已提交
122 123
	}

124 125 126
	video->textures_copied[video->cur_texture] = true;
	/* TODO: texture staging */
	//gs_stage_texture(last_surface, );
J
jp9000 已提交
127

128 129
	if (++video->cur_texture == NUM_TEXTURES)
		video->cur_texture = 0;
J
jp9000 已提交
130

131 132 133
	if (video->textures_copied[video->cur_texture]) {
		surface = video->copy_surfaces[video->cur_texture];
		video->copy_mapped = stagesurface_map(surface, &frame.data,
J
jp9000 已提交
134 135
				&frame.row_size);

136
		if (video->copy_mapped) {
J
jp9000 已提交
137
			frame.timestamp = timestamp;
138
			video_output_frame(video->video, &frame);
J
jp9000 已提交
139 140 141
		}
	}

142
	return video->copy_mapped;
J
jp9000 已提交
143 144 145 146 147 148
}

void *obs_video_thread(void *param)
{
	uint64_t last_time = 0;

149 150
	while (video_output_wait(obs->video.video)) {
		uint64_t cur_time = video_gettime(obs->video.video);
J
jp9000 已提交
151

152 153 154 155 156 157 158
		gs_entercontext(obs_graphics());

		tick_sources(cur_time, &last_time);
		render_displays();
		swap_frame(cur_time);

		gs_leavecontext();
J
jp9000 已提交
159 160 161 162
	}

	return NULL;
}