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