]> git.sesse.net Git - nageru/blobdiff - glwidget.cpp
Refcount the input frames directly instead of trying to free them after-the-fact...
[nageru] / glwidget.cpp
index 4a6eafe10bfd3f77cee02743694d68bbb0848657..3fb4889dc314c7ce163d389477926dfe110d51bc 100644 (file)
@@ -1,16 +1,32 @@
-#include "context.h"
+#include <qmetatype.h>  // Needs to come before egl.h.
+#include <qdatastream.h>  // Needs to come before egl.h.
+#include <qtextstream.h>  // Needs to come before egl.h.
+#include <qcursor.h>  // Needs to come before egl.h.
+#include <qcoreevent.h>  // Needs to come before egl.h.
+#include <epoxy/gl.h>
+#include <epoxy/egl.h>
+#include <QSurfaceFormat>
+
 #include "glwidget.h"
+
+#include <movit/resource_pool.h>
+#include <stdio.h>
+#include <mutex>
+
+#include "context.h"
 #include "mixer.h"
-#include <QCoreApplication>
-#include <QGuiApplication>
-#include <QThread>
-#include <math.h>
-#include <EGL/egl.h>
-#include <GL/glx.h>
-#include <thread>
+#include "ref_counted_gl_sync.h"
+
+class QSurface;
+class QWidget;
+
+#undef Success
+#include <movit/util.h>
+#include <string>
 
 GLWidget::GLWidget(QWidget *parent)
-    : QOpenGLWidget(parent)
+    : QGLWidget(parent, global_share_widget),
+      resource_pool(new movit::ResourcePool)
 {
 }
 
@@ -18,30 +34,89 @@ GLWidget::~GLWidget()
 {
 }
 
-QSize GLWidget::minimumSizeHint() const
+void GLWidget::initializeGL()
 {
-       return QSize(50, 50);
-}
+       printf("egl context=%p\n", eglGetCurrentContext());
+       //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
 
-QSize GLWidget::sizeHint() const
-{
-       return QSize(400, 400);
+       static std::once_flag flag;
+       std::call_once(flag, [this]{
+               global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()));
+               global_mixer->start();
+       });
+       global_mixer->set_frame_ready_callback(output, [this]{
+               QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
+       });
+
+       // Prepare the shaders to actually get the texture shown (ick).
+       glDisable(GL_BLEND);
+       glDisable(GL_DEPTH_TEST);
+       glDepthMask(GL_FALSE);
+
+       std::string vert_shader =
+               "#version 130 \n"
+               "in vec2 position; \n"
+               "in vec2 texcoord; \n"
+               "out vec2 tc; \n"
+               " \n"
+               "void main() \n"
+               "{ \n"
+               "    gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); \n"
+               "    tc = texcoord; \n"
+               "    tc.y = 1.0 - tc.y; \n"
+               "} \n";
+       std::string frag_shader =
+               "#version 130 \n"
+               "in vec2 tc; \n"
+               "uniform sampler2D tex; \n"
+               "void main() { \n"
+               "    gl_FragColor = texture2D(tex, tc); \n"
+               "} \n";
+       program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
+
+       static const float vertices[] = {
+               0.0f, 2.0f,
+               0.0f, 0.0f,
+               2.0f, 0.0f
+       };
+       glGenVertexArrays(1, &vao);
+       glBindVertexArray(vao);
+       position_vbo = movit::fill_vertex_attribute(program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
+       texcoord_vbo = movit::fill_vertex_attribute(program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
+
+#if 0
+       // Cleanup.
+       cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
+       cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
+
+       glDeleteVertexArrays(1, &vao);
+       nheck_error();
+#endif
 }
 
-void GLWidget::initializeGL()
+void GLWidget::resizeGL(int width, int height)
 {
-       printf("egl=%p glx=%p\n", eglGetCurrentContext(), glXGetCurrentContext());
-       //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
-
-       QSurface *surface = create_surface(format());
-       QSurface *surface2 = create_surface(format());
-       QSurface *surface3 = create_surface(format());
-       QSurface *surface4 = create_surface(format());
-       start_mixer(surface, surface2, surface3, surface4);
+       glViewport(0, 0, width, height);
+       global_mixer->set_preview_size(output, width, height);
 }
 
 void GLWidget::paintGL()
 {
-       glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       Mixer::DisplayFrame frame;
+       if (!global_mixer->get_display_frame(output, &frame)) {
+               glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+               return;
+       }
+
+       glUseProgram(program_num);
+       glActiveTexture(GL_TEXTURE0);
+       glBindTexture(GL_TEXTURE_2D, frame.texnum);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+       glBindVertexArray(vao);
+       glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
+       glDrawArrays(GL_TRIANGLES, 0, 3);
 }