X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fimage_input.cpp;fp=nageru%2Fimage_input.cpp;h=d2d6611162fae0d88ab06dd382d026f5997337a2;hb=533f00a9b992d06767737f9db236b4cf76b9c124;hp=12789dc09ff45523c31eea5c67225d76998d8fe2;hpb=7163b3183566e477057943dd2f325d4984a76eef;p=nageru diff --git a/nageru/image_input.cpp b/nageru/image_input.cpp index 12789dc..d2d6611 100644 --- a/nageru/image_input.cpp +++ b/nageru/image_input.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,7 @@ extern "C" { #include } +#include #include #include #include @@ -30,6 +32,7 @@ extern "C" { #include #include +#include "shared/context.h" #include "shared/ffmpeg_raii.h" #include "ffmpeg_util.h" #include "flags.h" @@ -38,6 +41,7 @@ struct SwsContext; using namespace std; +// FIXME gl context ImageInput::ImageInput(const string &filename) : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, 1280, 720), // Resolution will be overwritten. @@ -50,7 +54,7 @@ ImageInput::ImageInput(const string &filename) } set_width(current_image->width); set_height(current_image->height); - set_pixel_data(current_image->pixels.get()); + set_texture_num(*current_image->tex); } void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num) @@ -65,7 +69,7 @@ void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, uns lock_guard lock(all_images_lock); if (all_images[pathname] != current_image) { current_image = all_images[pathname]; - set_pixel_data(current_image->pixels.get()); + set_texture_num(*current_image->tex); } } movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num); @@ -79,12 +83,6 @@ shared_ptr ImageInput::load_image(const string &filenam } all_images[pathname] = load_image_raw(pathname); - - if (!update_thread_started) { - update_thread = thread(update_thread_func); - update_thread_started = true; - } - return all_images[pathname]; } @@ -196,23 +194,59 @@ shared_ptr ImageInput::load_image_raw(const string &pat unique_ptr image_data(new uint8_t[len]); av_image_copy_to_buffer(image_data.get(), len, pic_data, linesizes, AV_PIX_FMT_RGBA, frame->width, frame->height, 1); - shared_ptr image(new Image{unsigned(frame->width), unsigned(frame->height), move(image_data), last_modified}); + // Create and upload the texture. We always make mipmaps, since we have + // generally no idea of all the different chains that might crop up. + GLuint tex; + glGenTextures(1, &tex); + check_error(); + glBindTexture(GL_TEXTURE_2D, tex); + check_error(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + check_error(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + check_error(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + check_error(); + + // Actual upload. + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + check_error(); + glPixelStorei(GL_UNPACK_ROW_LENGTH, linesizes[0] / 4); + check_error(); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, frame->width, frame->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data.get()); + check_error(); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + check_error(); + + glGenerateMipmap(GL_TEXTURE_2D); + check_error(); + glBindTexture(GL_TEXTURE_2D, 0); + check_error(); + + shared_ptr image(new Image{unsigned(frame->width), unsigned(frame->height), RefCountedTexture(new GLuint(tex)), last_modified}); return image; } // Fire up a thread to update all images every second. // We could do inotify, but this is good enough for now. -void ImageInput::update_thread_func() +void ImageInput::update_thread_func(QSurface *surface) { pthread_setname_np(pthread_self(), "Update_Images"); + eglBindAPI(EGL_OPENGL_API); + QOpenGLContext *context = create_context(surface); + if (!make_current(context, surface)) { + printf("Couldn't initialize OpenGL context!\n"); + abort(); + } + struct stat buf; for ( ;; ) { { - unique_lock lock(threads_should_quit_mu); - threads_should_quit_modified.wait_for(lock, chrono::seconds(1), []() { return threads_should_quit; }); + unique_lock lock(update_thread_should_quit_mu); + update_thread_should_quit_modified.wait_for(lock, chrono::seconds(1), [] { return update_thread_should_quit; }); } - if (threads_should_quit) { + if (update_thread_should_quit) { return; } @@ -255,24 +289,25 @@ void ImageInput::update_thread_func() } } -void ImageInput::shutdown_updaters() +void ImageInput::start_update_thread(QSurface *surface) { - { - lock_guard lock(threads_should_quit_mu); - threads_should_quit = true; - threads_should_quit_modified.notify_all(); - } + update_thread = thread(update_thread_func, surface); +} - lock_guard lock(all_images_lock); - if (update_thread_started) { - update_thread.join(); +void ImageInput::end_update_thread() + +{ + { + lock_guard lock(update_thread_should_quit_mu); + update_thread_should_quit = true; + update_thread_should_quit_modified.notify_all(); } + update_thread.join(); } mutex ImageInput::all_images_lock; map> ImageInput::all_images; -bool ImageInput::update_thread_started = false; thread ImageInput::update_thread; -mutex ImageInput::threads_should_quit_mu; -bool ImageInput::threads_should_quit = false; -condition_variable ImageInput::threads_should_quit_modified; +mutex ImageInput::update_thread_should_quit_mu; +bool ImageInput::update_thread_should_quit = false; +condition_variable ImageInput::update_thread_should_quit_modified;