2 #include <qdatastream.h>
3 #include <qtextstream.h>
5 #include <qcoreevent.h>
11 #include <QCoreApplication>
12 #include <QGuiApplication>
16 #include <movit/resource_pool.h>
18 #include <movit/util.h>
20 GLWidget::GLWidget(QWidget *parent)
21 : QGLWidget(parent, global_share_widget),
22 resource_pool(new movit::ResourcePool)
30 void GLWidget::initializeGL()
32 printf("egl context=%p\n", eglGetCurrentContext());
33 //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
36 set_frame_ready_fallback([t]{
37 QMetaObject::invokeMethod(t, "update", Qt::AutoConnection);
40 QSurfaceFormat fmt = QGLFormat::toSurfaceFormat(format());
41 QSurface *surface = create_surface(fmt);
42 QSurface *surface2 = create_surface(fmt);
43 QSurface *surface3 = create_surface(fmt);
44 QSurface *surface4 = create_surface(fmt);
45 start_mixer(surface, surface2, surface3, surface4);
47 // Prepare the shaders to actually get the texture shown (ick).
49 glDisable(GL_DEPTH_TEST);
50 glDepthMask(GL_FALSE);
52 std::string vert_shader =
54 "in vec2 position; \n"
55 "in vec2 texcoord; \n"
60 " gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
62 " tc.y = 1.0 - tc.y; \n"
64 std::string frag_shader =
67 "uniform sampler2D tex; \n"
69 " gl_FragColor = texture2D(tex, tc); \n"
71 program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
73 static const float vertices[] = {
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.
85 cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
86 cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
88 glDeleteVertexArrays(1, &vao);
93 void GLWidget::resizeGL(int width, int height)
95 glViewport(0, 0, width, height);
98 void GLWidget::paintGL()
101 if (!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);
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);
114 glBindVertexArray(vao);
115 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
116 glDrawArrays(GL_TRIANGLES, 0, 3);