]> git.sesse.net Git - nageru/blob - glwidget.cpp
Refcount the input frames directly instead of trying to free them after-the-fact...
[nageru] / glwidget.cpp
1 #include <qmetatype.h>  // Needs to come before egl.h.
2 #include <qdatastream.h>  // Needs to come before egl.h.
3 #include <qtextstream.h>  // Needs to come before egl.h.
4 #include <qcursor.h>  // Needs to come before egl.h.
5 #include <qcoreevent.h>  // Needs to come before egl.h.
6 #include <epoxy/gl.h>
7 #include <epoxy/egl.h>
8 #include <QSurfaceFormat>
9
10 #include "glwidget.h"
11
12 #include <movit/resource_pool.h>
13 #include <stdio.h>
14 #include <mutex>
15
16 #include "context.h"
17 #include "mixer.h"
18 #include "ref_counted_gl_sync.h"
19
20 class QSurface;
21 class QWidget;
22
23 #undef Success
24 #include <movit/util.h>
25 #include <string>
26
27 GLWidget::GLWidget(QWidget *parent)
28     : QGLWidget(parent, global_share_widget),
29       resource_pool(new movit::ResourcePool)
30 {
31 }
32
33 GLWidget::~GLWidget()
34 {
35 }
36
37 void GLWidget::initializeGL()
38 {
39         printf("egl context=%p\n", eglGetCurrentContext());
40         //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
41
42         static std::once_flag flag;
43         std::call_once(flag, [this]{
44                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()));
45                 global_mixer->start();
46         });
47         global_mixer->set_frame_ready_callback(output, [this]{
48                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
49         });
50
51         // Prepare the shaders to actually get the texture shown (ick).
52         glDisable(GL_BLEND);
53         glDisable(GL_DEPTH_TEST);
54         glDepthMask(GL_FALSE);
55
56         std::string vert_shader =
57                 "#version 130 \n"
58                 "in vec2 position; \n"
59                 "in vec2 texcoord; \n"
60                 "out vec2 tc; \n"
61                 " \n"
62                 "void main() \n"
63                 "{ \n"
64                 "    gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
65                 "    tc = texcoord; \n"
66                 "    tc.y = 1.0 - tc.y; \n"
67                 "} \n";
68         std::string frag_shader =
69                 "#version 130 \n"
70                 "in vec2 tc; \n"
71                 "uniform sampler2D tex; \n"
72                 "void main() { \n"
73                 "    gl_FragColor = texture2D(tex, tc); \n"
74                 "} \n";
75         program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
76
77         static const float vertices[] = {
78                 0.0f, 2.0f,
79                 0.0f, 0.0f,
80                 2.0f, 0.0f
81         };
82         glGenVertexArrays(1, &vao);
83         glBindVertexArray(vao);
84         position_vbo = movit::fill_vertex_attribute(program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
85         texcoord_vbo = movit::fill_vertex_attribute(program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
86
87 #if 0
88         // Cleanup.
89         cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
90         cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
91
92         glDeleteVertexArrays(1, &vao);
93         nheck_error();
94 #endif
95 }
96
97 void GLWidget::resizeGL(int width, int height)
98 {
99         glViewport(0, 0, width, height);
100         global_mixer->set_preview_size(output, width, height);
101 }
102
103 void GLWidget::paintGL()
104 {
105         Mixer::DisplayFrame frame;
106         if (!global_mixer->get_display_frame(output, &frame)) {
107                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
108                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109                 return;
110         }
111
112         glUseProgram(program_num);
113         glActiveTexture(GL_TEXTURE0);
114         glBindTexture(GL_TEXTURE_2D, frame.texnum);
115         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
117         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
118
119         glBindVertexArray(vao);
120         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
121         glDrawArrays(GL_TRIANGLES, 0, 3);
122 }