Try to make the GL code a little clearer.
[dcpomatic.git] / src / wx / gl_video_view.cc
index 877cd5125fec15cf1ee547e8cd315c38023dfd0b..ed8b8dbb2270fce19d4401b0765681f449aee708 100644 (file)
 
 #include "film_viewer.h"
 #include "wx_util.h"
-#include "lib/image.h"
+#include "lib/butler.h"
+#include "lib/cross.h"
 #include "lib/dcpomatic_assert.h"
+#include "lib/dcpomatic_log.h"
 #include "lib/exceptions.h"
-#include "lib/cross.h"
+#include "lib/image.h"
 #include "lib/player_video.h"
-#include "lib/butler.h"
 #include <boost/bind/bind.hpp>
 #include <iostream>
 
@@ -103,12 +104,15 @@ GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
 void
 GLVideoView::size_changed (wxSizeEvent const& ev)
 {
-       _canvas_size = ev.GetSize ();
+       auto const scale = _canvas->GetDPIScaleFactor();
+       int const width = std::round(ev.GetSize().GetWidth() * scale);
+       int const height = std::round(ev.GetSize().GetHeight() * scale);
+       _canvas_size = { width, height };
+       LOG_GENERAL("GLVideoView canvas size changed to %1x%2", width, height);
        Sized ();
 }
 
 
-
 GLVideoView::~GLVideoView ()
 {
        boost::this_thread::disable_interruption dis;
@@ -190,12 +194,13 @@ static constexpr char fragment_source[] =
 "in vec2 TexCoord;\n"
 "\n"
 "uniform sampler2D texture_sampler;\n"
-/* type = 0: draw border
+/* type = 0: draw outline content rectangle
  * type = 1: draw XYZ image
  * type = 2: draw RGB image
+ * See FragmentType enum below.
  */
 "uniform int type = 0;\n"
-"uniform vec4 border_colour;\n"
+"uniform vec4 outline_content_colour;\n"
 "uniform mat4 colour_conversion;\n"
 "\n"
 "out vec4 FragColor;\n"
@@ -254,7 +259,7 @@ static constexpr char fragment_source[] =
 "{\n"
 "      switch (type) {\n"
 "              case 0:\n"
-"                      FragColor = border_colour;\n"
+"                      FragColor = outline_content_colour;\n"
 "                      break;\n"
 "              case 1:\n"
 "                      FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
@@ -273,6 +278,14 @@ static constexpr char fragment_source[] =
 "}\n";
 
 
+enum class FragmentType
+{
+       OUTLINE_CONTENT = 0,
+       XYZ_IMAGE = 1,
+       RGB_IMAGE = 2,
+};
+
+
 void
 GLVideoView::ensure_context ()
 {
@@ -287,24 +300,30 @@ GLVideoView::ensure_context ()
 }
 
 
-/* Offset of video texture triangles in indices */
-static constexpr int indices_video_texture = 0;
-/* Offset of subtitle texture triangles in indices */
-static constexpr int indices_subtitle_texture = 6;
-/* Offset of border lines in indices */
-static constexpr int indices_border = 12;
+/* Offset and number of indices for the things in the indices array below */
+static constexpr int indices_video_texture_offset = 0;
+static constexpr int indices_video_texture_number = 6;
+static constexpr int indices_subtitle_texture_offset = indices_video_texture_offset + indices_video_texture_number;
+static constexpr int indices_subtitle_texture_number = 6;
+static constexpr int indices_outline_content_offset = indices_subtitle_texture_offset + indices_subtitle_texture_number;
+static constexpr int indices_outline_content_number = 8;
 
 static constexpr unsigned int indices[] = {
        0, 1, 3, // video texture triangle #1
        1, 2, 3, // video texture triangle #2
        4, 5, 7, // subtitle texture triangle #1
        5, 6, 7, // subtitle texture triangle #2
-       8, 9,    // border line #1
-       9, 10,   // border line #2
-       10, 11,  // border line #3
-       11, 8,   // border line #4
+       8, 9,    // outline content line #1
+       9, 10,   // outline content line #2
+       10, 11,  // outline content line #3
+       11, 8,   // outline content line #4
 };
 
+/* Offsets of things in the GL_ARRAY_BUFFER */
+static constexpr int array_buffer_video_offset = 0;
+static constexpr int array_buffer_subtitle_offset = array_buffer_video_offset + 4 * 5 * sizeof(float);
+static constexpr int array_buffer_outline_content_offset = array_buffer_subtitle_offset + 4 * 5 * sizeof(float);
+
 
 void
 GLVideoView::setup_shaders ()
@@ -420,7 +439,7 @@ GLVideoView::setup_shaders ()
 
        _fragment_type = glGetUniformLocation (program, "type");
        check_gl_error ("glGetUniformLocation");
-       set_border_colour (program);
+       set_outline_content_colour (program);
 
        auto conversion = dcp::ColourConversion::rec709_to_xyz();
        boost::numeric::ublas::matrix<double> matrix = conversion.xyz_to_rgb ();
@@ -449,9 +468,9 @@ GLVideoView::setup_shaders ()
 
 
 void
-GLVideoView::set_border_colour (GLuint program)
+GLVideoView::set_outline_content_colour (GLuint program)
 {
-       auto uniform = glGetUniformLocation (program, "border_colour");
+       auto uniform = glGetUniformLocation (program, "outline_content_colour");
        check_gl_error ("glGetUniformLocation");
        auto colour = outline_content_colour ();
        glUniform4f (uniform, colour.Red() / 255.0f, colour.Green() / 255.0f, colour.Blue() / 255.0f, 1.0f);
@@ -480,17 +499,17 @@ GLVideoView::draw ()
 
        glBindVertexArray(_vao);
        check_gl_error ("glBindVertexArray");
-       glUniform1i(_fragment_type, _optimise_for_j2k ? 1 : 2);
+       glUniform1i(_fragment_type, static_cast<GLint>(_optimise_for_j2k ? FragmentType::XYZ_IMAGE : FragmentType::RGB_IMAGE));
        _video_texture->bind();
-       glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_video_texture * sizeof(int)));
+       glDrawElements (GL_TRIANGLES, indices_video_texture_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_video_texture_offset * sizeof(int)));
        if (_have_subtitle_to_render) {
-               glUniform1i(_fragment_type, 2);
+               glUniform1i(_fragment_type, static_cast<GLint>(FragmentType::RGB_IMAGE));
                _subtitle_texture->bind();
-               glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_subtitle_texture * sizeof(int)));
+               glDrawElements (GL_TRIANGLES, indices_subtitle_texture_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_subtitle_texture_offset * sizeof(int)));
        }
        if (_viewer->outline_content()) {
-               glUniform1i(_fragment_type, 0);
-               glDrawElements (GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_border * sizeof(int)));
+               glUniform1i(_fragment_type, static_cast<GLint>(FragmentType::OUTLINE_CONTENT));
+               glDrawElements (GL_LINES, indices_outline_content_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_outline_content_offset * sizeof(int)));
                check_gl_error ("glDrawElements");
        }
 
@@ -504,7 +523,7 @@ GLVideoView::draw ()
 void
 GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
 {
-       shared_ptr<const Image> video = _optimise_for_j2k ? pv->raw_image() : pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
+       shared_ptr<const Image> video = _optimise_for_j2k ? pv->raw_image() : pv->image(boost::bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
 
        /* Only the player's black frames should be aligned at this stage, so this should
         * almost always have no work to do.
@@ -513,7 +532,7 @@ GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
 
        /** If _optimise_for_j2k is true we render a XYZ image, doing the colourspace
         *  conversion, scaling and video range conversion in the GL shader.
-        *  Othewise we render a RGB image without any shader-side processing.
+        *  Otherwise we render a RGB image without any shader-side processing.
         */
 
        /* XXX: video range conversion */
@@ -619,17 +638,17 @@ GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
                        Rectangle(canvas_size, inter_position.x + x_offset, inter_position.y + y_offset, inter_size)
                        : Rectangle(canvas_size, x_offset, y_offset, out_size);
 
-               glBufferSubData (GL_ARRAY_BUFFER, 0, video.size(), video.vertices());
+               glBufferSubData (GL_ARRAY_BUFFER, array_buffer_video_offset, video.size(), video.vertices());
                check_gl_error ("glBufferSubData (video)");
 
-               const auto border = Rectangle(canvas_size, inter_position.x + x_offset, inter_position.y + y_offset, inter_size);
-               glBufferSubData (GL_ARRAY_BUFFER, 8 * 5 * sizeof(float), border.size(), border.vertices());
-               check_gl_error ("glBufferSubData (border)");
+               const auto outline_content = Rectangle(canvas_size, inter_position.x + x_offset, inter_position.y + y_offset, inter_size);
+               glBufferSubData (GL_ARRAY_BUFFER, array_buffer_outline_content_offset, outline_content.size(), outline_content.vertices());
+               check_gl_error ("glBufferSubData (outline_content)");
        }
 
        if (_have_subtitle_to_render) {
                const auto subtitle = Rectangle(canvas_size, inter_position.x + x_offset + text->position.x, inter_position.y + y_offset + text->position.y, text->image->size());
-               glBufferSubData (GL_ARRAY_BUFFER, 4 * 5 * sizeof(float), subtitle.size(), subtitle.vertices());
+               glBufferSubData (GL_ARRAY_BUFFER, array_buffer_subtitle_offset, subtitle.size(), subtitle.vertices());
                check_gl_error ("glBufferSubData (subtitle)");
        }