X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fimage_input.cpp;h=6a2c5abc846b913123ef00647373bd6267ee1d4b;hb=d3f363c0ac414e696844d277b34a79dd414c19e2;hp=12789dc09ff45523c31eea5c67225d76998d8fe2;hpb=c660c05323237bf118e6844c71703bf96be70d32;p=nageru diff --git a/nageru/image_input.cpp b/nageru/image_input.cpp index 12789dc..6a2c5ab 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,9 +41,14 @@ struct SwsContext; using namespace std; +ImageInput::ImageInput() + : sRGBSwitchingFlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, + GL_UNSIGNED_BYTE, 1280, 720) // Resolution will be overwritten. +{} + 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. + : sRGBSwitchingFlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, + GL_UNSIGNED_BYTE, 1280, 720), // Resolution will be overwritten. pathname(search_for_file_or_die(filename)), current_image(load_image(filename, pathname)) { @@ -50,7 +58,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) @@ -63,12 +71,13 @@ void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, uns // is mostly there to save startup time, not RAM). { lock_guard lock(all_images_lock); + assert(all_images.count(pathname)); 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); + sRGBSwitchingFlatInput::set_gl_state(glsl_program_num, prefix, sampler_num); } shared_ptr ImageInput::load_image(const string &filename, const string &pathname) @@ -79,12 +88,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 +199,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_SRGB8_ALPHA8, frame->width, frame->height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 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 +294,34 @@ void ImageInput::update_thread_func() } } -void ImageInput::shutdown_updaters() +void ImageInput::switch_image(const string &pathname) { - { - lock_guard lock(threads_should_quit_mu); - threads_should_quit = true; - threads_should_quit_modified.notify_all(); - } - +#ifndef NDEBUG lock_guard lock(all_images_lock); - if (update_thread_started) { - update_thread.join(); + assert(all_images.count(pathname)); +#endif + this->pathname = pathname; +} + +void ImageInput::start_update_thread(QSurface *surface) +{ + update_thread = thread(update_thread_func, surface); +} + +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;