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.
8 #include <QSurfaceFormat>
12 #include <movit/resource_pool.h>
17 #include "ref_counted_gl_sync.h"
23 #include <movit/util.h>
26 GLWidget::GLWidget(QWidget *parent)
27 : QGLWidget(parent, global_share_widget),
28 resource_pool(new movit::ResourcePool)
36 void GLWidget::initializeGL()
38 printf("egl context=%p\n", eglGetCurrentContext());
39 //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
42 set_frame_ready_fallback([t]{
43 QMetaObject::invokeMethod(t, "update", Qt::AutoConnection);
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 start_mixer(surface, surface2, surface3, surface4);
53 // Prepare the shaders to actually get the texture shown (ick).
55 glDisable(GL_DEPTH_TEST);
56 glDepthMask(GL_FALSE);
58 std::string vert_shader =
60 "in vec2 position; \n"
61 "in vec2 texcoord; \n"
66 " gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
68 " tc.y = 1.0 - tc.y; \n"
70 std::string frag_shader =
73 "uniform sampler2D tex; \n"
75 " gl_FragColor = texture2D(tex, tc); \n"
77 program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
79 static const float vertices[] = {
84 glGenVertexArrays(1, &vao);
85 glBindVertexArray(vao);
86 position_vbo = movit::fill_vertex_attribute(program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
87 texcoord_vbo = movit::fill_vertex_attribute(program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices); // Same as vertices.
91 cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
92 cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
94 glDeleteVertexArrays(1, &vao);
99 void GLWidget::resizeGL(int width, int height)
101 glViewport(0, 0, width, height);
104 void GLWidget::paintGL()
107 if (!mixer_get_display_frame(&frame)) {
108 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
109 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
113 glUseProgram(program_num);
114 glActiveTexture(GL_TEXTURE0);
115 glBindTexture(GL_TEXTURE_2D, frame.texnum);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
120 glBindVertexArray(vao);
121 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
122 glDrawArrays(GL_TRIANGLES, 0, 3);