Repository navigation
Expand file tree
/
Copy pathpano_gl.cpp
More file actions
402 lines (349 loc) · 14.3 KB
/
Copy pathpano_gl.cpp
File metadata and controls
402 lines (349 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* @file video_display/pano_gl.cpp
* @author Martin Piatka <piatka@cesnet.cz>
*/
/*
* Copyright (c) 2010-2026 CESNET, zájmové sdružení právnických osob
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cassert>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <queue>
#include "debug.h"
#include "host.h"
#include "keyboard_control.h" // K_*
#include "lib_common.h"
#include "messaging.h"
#include "module.h"
#include "video_display.h"
#include "video_frame.h"
#include "opengl_utils.hpp"
#include "opengl_panorama.hpp"
#include "sdl_window.hpp"
#include "utils/profile_timer.hpp"
#define MAX_BUFFER_SIZE 1
#define MOD_NAME "[pano gl] "
struct state_vr{
Sdl_window window = Sdl_window(true);
bool running = false;
bool fs = false;
unsigned sdl_frame_event{};
unsigned sdl_redraw_event{};
video_desc current_desc{};
int buffered_frames_count{};
PanoramaScene scene;
int threshold_fps = 60; //Target fps for redraws caused by interaction
SDL_TimerID redraw_timer = 0;
bool redraw_needed = false;
std::chrono::steady_clock::time_point last_frame;
std::chrono::steady_clock::time_point last_manual_movement;
std::mutex lock;
std::condition_variable frame_consumed_cv;
std::queue<video_frame *> free_frame_queue;
bool autorotate = false;
};
static void * display_panogl_init(module */*parent*/, const char *cfg, unsigned int /*flags*/) {
auto *s = new state_vr();
using namespace std::string_view_literals;
if(cfg && "autorotate"sv == cfg){
s->autorotate = true;
}
s->sdl_frame_event = SDL_RegisterEvents(1);
s->sdl_redraw_event = SDL_RegisterEvents(1);
return s;
}
static void draw(state_vr *s){
PROFILE_FUNC;
auto now = std::chrono::steady_clock::now();
s->last_frame = now;
glClear(GL_COLOR_BUFFER_BIT);
s->scene.render(s->window.width, s->window.height);
SDL_GL_SwapWindow(s->window.sdl_window);
}
static Uint32 redraw_callback(Uint32 interval, void *param){
int event_id = *static_cast<int *>(param);
SDL_Event event;
event.type = event_id;
SDL_PushEvent(&event);
return interval;
}
static void redraw(state_vr *s,
bool video_framerate_over_threshold = false,
bool triggered_by_frame = false)
{
if(video_framerate_over_threshold && triggered_by_frame){
/* If video framerate is over the threshold we draw the
* incoming frames immediately. Since this draw already
* contains changes caused by interaction (e.g. moving the
* camera) we cancel the redraw timer
*/
if(s->redraw_timer){
SDL_RemoveTimer(s->redraw_timer);
s->redraw_timer = 0;
s->redraw_needed = false;
}
draw(s);
} else {
if(!s->redraw_timer){
/* Redrawing due to interaction is delayed, so that if
* a fresh video frame arrives very soon after the
* interaction it can be drawn together with the
* changes caused by interaction
*/
s->redraw_timer = SDL_AddTimer(1000 / s->threshold_fps,
redraw_callback, &s->sdl_redraw_event);
if(triggered_by_frame)
draw(s);
}
s->redraw_needed = !triggered_by_frame;
}
}
static void handle_window_event(state_vr *s, SDL_Event *event){
if(event->window.event == SDL_WINDOWEVENT_RESIZED){
glViewport(0, 0, event->window.data1, event->window.data2);
s->window.width = event->window.data1;
s->window.height = event->window.data2;
redraw(s);
}
}
static void handle_keyboard_event(state_vr *s, SDL_Event *event){
if(event->key.type == SDL_KEYDOWN){
switch(event->key.keysym.sym){
case SDLK_f:
s->fs = !s->fs;
SDL_SetWindowFullscreen(s->window.sdl_window, s->fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
redraw(s);
break;
case SDLK_q:
exit_uv(0);
break;
case SDLK_LEFT:
s->scene.rotate(4.f, 0);
s->last_manual_movement = std::chrono::steady_clock::now();
break;
case SDLK_RIGHT:
s->scene.rotate(-4.f, 0);
s->last_manual_movement = std::chrono::steady_clock::now();
break;
case SDLK_UP:
s->scene.fov -= 2.f;
redraw(s);
break;
case SDLK_DOWN:
s->scene.fov += 2.f;
redraw(s);
break;
default:
break;
}
}
}
static void handle_user_event(state_vr *s, SDL_Event *event){
if(event->type == s->sdl_frame_event){
std::unique_lock<std::mutex> lk(s->lock);
s->buffered_frames_count -= 1;
lk.unlock();
s->frame_consumed_cv.notify_one();
auto *frame = static_cast<video_frame *>(event->user.data1);
if(frame){
s->scene.put_frame(frame);
lk.lock();
vf_recycle(frame);
s->free_frame_queue.push(frame);
lk.unlock();
} else {
//poison
s->running = false;
return;
}
redraw(s, frame->fps >= s->threshold_fps, true);
} else if(event->type == s->sdl_redraw_event){
if(s->redraw_needed){
draw(s);
s->redraw_needed = false;
} else {
SDL_RemoveTimer(s->redraw_timer);
s->redraw_timer = 0;
}
}
}
static void display_panogl_run(void *state) {
auto *s = static_cast<state_vr *>(state);
draw(s);
s->running = true;
while(s->running){
SDL_Event event;
if (!SDL_WaitEvent(&event)) {
continue;
}
switch(event.type){
case SDL_MOUSEMOTION:
if(event.motion.state & SDL_BUTTON_LMASK){
s->scene.rotate(event.motion.xrel / 8.f,
event.motion.yrel / 8.f);
s->last_manual_movement = std::chrono::steady_clock::now();
redraw(s);
}
break;
case SDL_MOUSEWHEEL:
s->scene.fov -= event.wheel.y;
redraw(s);
break;
case SDL_WINDOWEVENT:
handle_window_event(s, &event);
break;
case SDL_KEYDOWN:
handle_keyboard_event(s, &event);
break;
case SDL_QUIT:
exit_uv(0);
break;
default:
if(event.type >= SDL_USEREVENT)
handle_user_event(s, &event);
break;
}
if(s->autorotate && std::chrono::steady_clock::now() - s->last_manual_movement > std::chrono::seconds(10)){
s->scene.rotate(-0.05f, 0);
}
}
}
static void display_panogl_done(void *state) {
auto *s = static_cast<state_vr *>(state);
delete s;
}
static struct video_frame * display_panogl_getf(void *state) {
auto *s = static_cast<state_vr *>(state);
std::lock_guard<std::mutex> lock(s->lock);
while (!s->free_frame_queue.empty()) {
video_frame *buffer = s->free_frame_queue.front();
s->free_frame_queue.pop();
if (video_desc_eq(video_desc_from_frame(buffer), s->current_desc)) {
return buffer;
} else {
vf_free(buffer);
}
}
return vf_alloc_desc_data(s->current_desc);
}
static bool display_panogl_putf(void *state, struct video_frame *frame, long long nonblock) {
PROFILE_FUNC;
auto *s = static_cast<state_vr *>(state);
if (nonblock == PUTF_DISCARD) {
vf_free(frame);
return true;
}
std::unique_lock<std::mutex> lk(s->lock);
if (s->buffered_frames_count >= MAX_BUFFER_SIZE && nonblock != PUTF_BLOCKING
&& frame != nullptr) {
vf_free(frame);
printf("1 frame(s) dropped!\n");
return false;
}
s->frame_consumed_cv.wait(lk, [s]{return s->buffered_frames_count < MAX_BUFFER_SIZE;});
s->buffered_frames_count += 1;
lk.unlock();
SDL_Event event;
event.type = s->sdl_frame_event;
event.user.data1 = frame;
PROFILE_DETAIL("push frame event");
SDL_PushEvent(&event);
return true;
}
static bool display_panogl_reconfigure(void *state, struct video_desc desc) {
auto *s = static_cast<state_vr *>(state);
s->current_desc = desc;
return true;
}
static bool display_panogl_get_property(void *state, int property, void *val, size_t *len) {
auto *s = static_cast<state_vr *>(state);
interlacing_t supported_il_modes[] = {PROGRESSIVE};
int rgb_shift[] = {0, 8, 16};
switch (property) {
case DISPLAY_PROPERTY_CODECS: {
auto codecs = s->scene.get_codecs();
size_t codecs_size = codecs.size() * sizeof(codec_t);
if(codecs_size <= *len) {
memcpy(val, codecs.data(), codecs_size);
} else {
return false;
}
*len = codecs_size;
break;
}
case DISPLAY_PROPERTY_RGB_SHIFT:
if(sizeof(rgb_shift) > *len) {
return false;
}
memcpy(val, rgb_shift, sizeof(rgb_shift));
*len = sizeof(rgb_shift);
break;
case DISPLAY_PROPERTY_BUF_PITCH:
*(int *) val = PITCH_DEFAULT;
*len = sizeof(int);
break;
case DISPLAY_PROPERTY_SUPPORTED_IL_MODES:
if(sizeof(supported_il_modes) <= *len) {
memcpy(val, supported_il_modes, sizeof(supported_il_modes));
} else {
return false;
}
*len = sizeof(supported_il_modes);
break;
default:
return false;
}
return true;
}
static void display_panogl_probe(struct device_info **available_cards, int *count, void (**deleter)(void *)) {
UNUSED(deleter);
*count = 1;
*available_cards = static_cast<device_info *>(calloc(1, sizeof(device_info)));
strcpy((*available_cards)[0].dev, "");
strcpy((*available_cards)[0].name, "Panorama Gl SW display");
(*available_cards)[0].repeatable = true;
}
static const struct video_display_info display_panogl_info = {
display_panogl_probe,
display_panogl_init,
display_panogl_run,
display_panogl_done,
display_panogl_getf,
display_panogl_putf,
display_panogl_reconfigure,
display_panogl_get_property,
nullptr,
nullptr,
MOD_NAME,
};
REGISTER_MODULE(pano_gl, &display_panogl_info, LIBRARY_CLASS_VIDEO_DISPLAY, VIDEO_DISPLAY_ABI_VERSION);