]> git.sesse.net Git - nageru/blob - glwidget.cpp
Clean up the surface stuff in Mixer, now that we have Qt/epoxy interop working.
[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         global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()));
42         global_mixer->set_frame_ready_fallback([this]{
43                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
44         });
45         global_mixer->start();
46
47         // Prepare the shaders to actually get the texture shown (ick).
48         glDisable(GL_BLEND);
49         glDisable(GL_DEPTH_TEST);
50         glDepthMask(GL_FALSE);
51
52         std::string vert_shader =
53                 "#version 130 \n"
54                 "in vec2 position; \n"
55                 "in vec2 texcoord; \n"
56                 "out vec2 tc; \n"
57                 " \n"
58                 "void main() \n"
59                 "{ \n"
60                 "    gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
61                 "    tc = texcoord; \n"
62                 "    tc.y = 1.0 - tc.y; \n"
63                 "} \n";
64         std::string frag_shader =
65                 "#version 130 \n"
66                 "in vec2 tc; \n"
67                 "uniform sampler2D tex; \n"
68                 "void main() { \n"
69                 "    gl_FragColor = texture2D(tex, tc); \n"
70                 "} \n";
71         program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
72
73         static const float vertices[] = {
74                 0.0f, 2.0f,
75                 0.0f, 0.0f,
76                 2.0f, 0.0f
77         };
78         glGenVertexArrays(1, &vao);
79         glBindVertexArray(vao);
80         position_vbo = movit::fill_vertex_attribute(program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
81         texcoord_vbo = movit::fill_vertex_attribute(program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
82
83 #if 0
84         // Cleanup.
85         cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
86         cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
87
88         glDeleteVertexArrays(1, &vao);
89         nheck_error();
90 #endif
91 }
92
93 void GLWidget::resizeGL(int width, int height)
94 {
95         glViewport(0, 0, width, height);
96 }
97
98 void GLWidget::paintGL()
99 {
100         Mixer::DisplayFrame frame;
101         if (!global_mixer->get_display_frame(&frame)) {
102                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
103                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104                 return;
105         }
106
107         glUseProgram(program_num);
108         glActiveTexture(GL_TEXTURE0);
109         glBindTexture(GL_TEXTURE_2D, frame.texnum);
110         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
111         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
112         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
113
114         glBindVertexArray(vao);
115         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
116         glDrawArrays(GL_TRIANGLES, 0, 3);
117 }