From 85647937291886a18f812f1fd6ac6d04e1cb7695 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 13 Sep 2021 13:57:20 +0200 Subject: [PATCH] Move some stuff inside Texture. --- src/wx/gl_video_view.cc | 54 +++++++++++++++++++++++------------------ src/wx/gl_video_view.h | 7 +++--- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/wx/gl_video_view.cc b/src/wx/gl_video_view.cc index 7fb387f53..d646441b4 100644 --- a/src/wx/gl_video_view.cc +++ b/src/wx/gl_video_view.cc @@ -74,7 +74,6 @@ check_gl_error (char const * last) GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent) : VideoView (viewer) , _context (nullptr) - , _have_storage (false) , _vsync_enabled (false) , _playing (false) , _one_shot (false) @@ -494,29 +493,15 @@ GLVideoView::set_image (shared_ptr pv) /* XXX: video range conversion */ /* XXX: subs */ - if (image->size() != _video_size) { - _have_storage = false; - } - - _video_size = image->size (); - glPixelStorei (GL_UNPACK_ALIGNMENT, _optimise_for_j2k ? 2 : 1); - check_gl_error ("glPixelStorei"); - - auto const format = _optimise_for_j2k ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; - - if (_have_storage) { - glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _video_size->width, _video_size->height, GL_RGB, format, image->data()[0]); - check_gl_error ("glTexSubImage2D"); - } else { - glTexImage2D (GL_TEXTURE_2D, 0, _optimise_for_j2k ? GL_RGBA12 : GL_RGBA8, _video_size->width, _video_size->height, 0, GL_RGB, format, image->data()[0]); - check_gl_error ("glTexImage2D"); + auto const changed = _video_texture->set (image); + if (changed) { auto const canvas_size = _canvas_size.load(); int const canvas_width = canvas_size.GetWidth(); int const canvas_height = canvas_size.GetHeight(); - float const image_x = float(_video_size->width) / canvas_width; - float const image_y = float(_video_size->height) / canvas_height; + float const image_x = float(image->size().width) / canvas_width; + float const image_y = float(image->size().height) / canvas_height; auto x_pixels_to_gl = [canvas_width](int x) { return (x * 2.0f / canvas_width) - 1.0f; @@ -549,8 +534,6 @@ GLVideoView::set_image (shared_ptr pv) /* Set the vertex shader's input data (GL_ARRAY_BUFFER) */ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); check_gl_error ("glBufferData"); - - _have_storage = true; } glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); @@ -665,7 +648,7 @@ try _vsync_enabled = true; #endif - _video_texture.reset(new Texture()); + _video_texture.reset(new Texture(_optimise_for_j2k ? 2 : 1)); _video_texture->bind(); while (true) { @@ -714,7 +697,8 @@ GLVideoView::request_one_shot () } -Texture::Texture () +Texture::Texture (GLint unpack_alignment) + : _unpack_alignment (unpack_alignment) { glGenTextures (1, &_name); check_gl_error ("glGenTextures"); @@ -734,3 +718,27 @@ Texture::bind () check_gl_error ("glBindTexture"); } + +bool +Texture::set (shared_ptr image) +{ + auto const create = !_size || image->size() != _size; + _size = image->size(); + + glPixelStorei (GL_UNPACK_ALIGNMENT, _unpack_alignment); + check_gl_error ("glPixelStorei"); + + auto const format = image->pixel_format() == AV_PIX_FMT_RGB24 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT; + auto const internal_format = image->pixel_format() == AV_PIX_FMT_RGB24 ? GL_RGBA8 : GL_RGBA12; + + if (create) { + glTexImage2D (GL_TEXTURE_2D, 0, internal_format, _size->width, _size->height, 0, GL_RGB, format, image->data()[0]); + check_gl_error ("glTexImage2D"); + } else { + glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, format, image->data()[0]); + check_gl_error ("glTexSubImage2D"); + } + + return create; +} + diff --git a/src/wx/gl_video_view.h b/src/wx/gl_video_view.h index 272198591..4e61b5b4c 100644 --- a/src/wx/gl_video_view.h +++ b/src/wx/gl_video_view.h @@ -38,16 +38,19 @@ DCPOMATIC_ENABLE_WARNINGS class Texture { public: - Texture (); + Texture (GLint unpack_alignment); ~Texture (); Texture (Texture const&) = delete; Texture& operator= (Texture const&) = delete; void bind (); + bool set (std::shared_ptr image); private: GLuint _name; + GLint _unpack_alignment; + boost::optional _size; }; @@ -92,8 +95,6 @@ private: boost::atomic _canvas_size; std::unique_ptr _video_texture; - boost::optional _video_size; - bool _have_storage; bool _vsync_enabled; boost::thread _thread; -- 2.30.2