UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
opengl_utils.hpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2010-2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37#ifndef OPENGL_UTILS_HPP
38#define OPENGL_UTILS_HPP
39
40#ifdef HAVE_CONFIG_H
41# include "config.h"
42#endif //HAVE_CONFIG_H
43
44#ifdef __APPLE__
45# include <OpenGL/OpenGL.h> // CGL
46# include <OpenGL/gl3.h>
47# include <OpenGL/glext.h>
48#elif defined __linux__
49# include <X11/Xlib.h>
50# include <GL/glew.h>
51# include <GL/glx.h>
52#else // _WIN32
53# include <GL/glew.h>
54#endif //__APPLE__
55
56#include <SDL2/SDL.h>
57//#include <SDL2/SDL_opengl.h>
58#include <mutex>
59#include <memory>
60#include <vector>
61#include <cassert>
62
63#include "types.h"
64
65bool check_gl_extension_present(const char *ext);
66
71public:
78 GlProgram() = default;
79 GlProgram(const char *vert_src, const char *frag_src);
80 ~GlProgram();
81
85 GLuint get() { return program; }
86
87 GlProgram(const GlProgram&) = delete;
89 GlProgram& operator=(const GlProgram&) = delete;
90 GlProgram& operator=(GlProgram&& o) { swap(o); return *this; }
91
92private:
93 void swap(GlProgram& o){
94 std::swap(program, o.program);
95 }
96 GLuint program = 0;
97};
98
102class Model{
103public:
104 Model() = default;
105 Model(const Model&) = delete;
106 Model(Model&& o) { swap(o); }
107 Model& operator=(const Model&) = delete;
108 Model& operator=(Model&& o) { swap(o); return *this; }
109 ~Model();
110
114 GLuint get_vao() const { return vao; }
115
119 void render();
120
124 static Model get_sphere();
125
129 static Model get_quad();
130
131private:
132 void swap(Model& o){
133 std::swap(vao, o.vao);
134 std::swap(vbo, o.vbo);
135 std::swap(elem_buf, o.elem_buf);
136 std::swap(indices_num, o.indices_num);
137 }
138 GLuint vao = 0;
139 GLuint vbo = 0;
140 GLuint elem_buf = 0;
141 GLsizei indices_num = 0;
142};
143
148public:
152 Texture() = default;
153 ~Texture();
154
159 GLuint get() const { assert(tex_id); return tex_id; }
160
169 void allocate(int w, int h, GLint internal_fmt);
170
171 void allocate();
172
173 void load_frame(int w, int h,
174 GLint internal_format,
175 GLenum src_format,
176 GLenum type,
177 video_frame *f,
178 bool pbo_frame);
179
180 Texture(const Texture&) = delete;
181 Texture(Texture&& o) { swap(o); }
182 Texture& operator=(const Texture&) = delete;
183 Texture& operator=(Texture&& o) { swap(o); return *this; }
184
185
189 void swap(Texture& o){
190 std::swap(tex_id, o.tex_id);
191 std::swap(width, o.width);
192 std::swap(height, o.height);
193 std::swap(internal_format, o.internal_format);
194 std::swap(pbo, o.pbo);
195 }
196private:
197 GLuint tex_id = 0;
198 int width = 0;
199 int height = 0;
201
202 GLuint pbo = 0;
203};
204
208class Framebuffer{
209public:
214 glGenFramebuffers(1, &fbo);
215 }
216
218 glDeleteFramebuffers(1, &fbo);
219 }
220
221 Framebuffer(const Framebuffer&) = delete;
224 Framebuffer& operator=(Framebuffer&& o) { swap(o); return *this; }
225
229 GLuint get() { return fbo; }
230
236 void attach_texture(GLuint tex);
237
243 void attach_texture(const Texture& tex){
244 attach_texture(tex.get());
245 }
246
247private:
249 std::swap(fbo, o.fbo);
250 }
251
252 GLuint fbo = 0;
253};
254
259public:
264 glGenBuffers(1, &buf_id);
265 }
266
268 glDeleteBuffers(1, &buf_id);
269 }
270
274 GLuint get() {
275 return buf_id;
276 }
277
278 GlBuffer(const GlBuffer&) = delete;
279 GlBuffer(GlBuffer&& o) { swap(o); }
280 GlBuffer& operator=(const GlBuffer&) = delete;
281 GlBuffer& operator=(GlBuffer&& o) { swap(o); return *this; }
282
283private:
284 void swap(GlBuffer& o){
285 std::swap(buf_id, o.buf_id);
286 }
287
288 GLuint buf_id = 0;
289};
290
292public:
293 virtual ~Frame_convertor() {};
294
295 template<class Derived>
296 static std::unique_ptr<Frame_convertor> construct_unique() {
297 return std::make_unique<Derived>();
298 }
299
307 virtual void put_frame(video_frame *f, bool pbo_frame = false) = 0;
308
314 virtual void attach_texture(Texture& tex) = 0;
315
316 void set_pbo(GlBuffer *pbo) { internal_pbo = pbo; }
317protected:
319};
320
325public:
326 void put_frame(video_frame *f, bool pbo_frame = false);
327 void attach_dst_texture(Texture *tex){ this->tex = tex; }
328
329 void enable_pbo(bool enable) {
330 if(enable)
331 upload_pbo = std::make_unique<GlBuffer>();
332 else
333 upload_pbo.reset();
334 }
335
336 std::vector<codec_t> get_supported_codecs();
337
338private:
339 std::unique_ptr<Frame_convertor> conv;
341 Texture *tex = nullptr;
342 std::unique_ptr<GlBuffer> upload_pbo;
343};
344
349public:
351 back.allocate();
352 mid.allocate();
353 front.allocate();
354 }
355
359 Texture& get_back(){ return back; }
360
364 Texture& get_front(){ return front; }
365
371 void swap_back(){
372 back.swap(mid);
373 new_front_available = true;
374 }
375
380 front.swap(mid);
381 new_front_available = false;
382 }
383
388
389private:
393
395};
396
397
399public:
400 FlatVideoScene() = default;
401
402 void init();
403 void put_frame(video_frame *f);
404 void render();
405 void resize(int width, int height);
406
407 void enableDeinterlacing(bool enable);
408
409 GLuint get_texture() { return texture.get(); }
410private:
415
419};
420
421#endif
FlatVideoScene()=default
FrameUploader uploader
void resize(int width, int height)
void enableDeinterlacing(bool enable)
video_desc current_desc
void put_frame(video_frame *f)
virtual void attach_texture(Texture &tex)=0
static std::unique_ptr< Frame_convertor > construct_unique()
virtual ~Frame_convertor()
virtual void put_frame(video_frame *f, bool pbo_frame=false)=0
void set_pbo(GlBuffer *pbo)
GlBuffer * internal_pbo
std::unique_ptr< GlBuffer > upload_pbo
std::vector< codec_t > get_supported_codecs()
void enable_pbo(bool enable)
void put_frame(video_frame *f, bool pbo_frame=false)
std::unique_ptr< Frame_convertor > conv
void attach_dst_texture(Texture *tex)
codec_t configured_codec
GLuint get()
GlBuffer & operator=(const GlBuffer &)=delete
GlBuffer & operator=(GlBuffer &&o)
void swap(GlBuffer &o)
GlBuffer(const GlBuffer &)=delete
GLuint buf_id
GlBuffer(GlBuffer &&o)
GlProgram()=default
GlProgram & operator=(GlProgram &&o)
GlProgram(GlProgram &&o)
void swap(GlProgram &o)
GLuint program
GlProgram & operator=(const GlProgram &)=delete
GLuint get()
GlProgram(const GlProgram &)=delete
Model(const Model &)=delete
GLuint vao
static Model get_quad()
Model & operator=(const Model &)=delete
GLsizei indices_num
void swap(Model &o)
GLuint get_vao() const
Model(Model &&o)
void render()
GLuint vbo
static Model get_sphere()
GLuint elem_buf
Model()=default
Model & operator=(Model &&o)
Texture & operator=(Texture &&o)
void swap(Texture &o)
GLuint tex_id
Texture & operator=(const Texture &)=delete
GLint internal_format
GLuint get() const
Texture()=default
Texture(const Texture &)=delete
Texture(Texture &&o)
void allocate()
void load_frame(int w, int h, GLint internal_format, GLenum src_format, GLenum type, video_frame *f, bool pbo_frame)
void allocate(int w, int h, GLint internal_fmt)
bool is_new_front_available() const
bool check_gl_extension_present(const char *ext)
static const char * vert_src
static const char * frag_src
Option::OptType type
Definition settings.cpp:194
void attach_texture(GLuint tex)
Framebuffer(Framebuffer &&o)
void swap(Framebuffer &o)
void attach_texture(const Texture &tex)
Framebuffer & operator=(const Framebuffer &)=delete
Framebuffer & operator=(Framebuffer &&o)
Framebuffer(const Framebuffer &)=delete
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
codec_t
Definition types.h:65
@ VIDEO_CODEC_NONE
dummy color spec
Definition types.h:66