X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fimage_input.cpp;h=316292b15d7ffd01428eb2d7a165834278fe1fc7;hb=HEAD;hp=12789dc09ff45523c31eea5c67225d76998d8fe2;hpb=c660c05323237bf118e6844c71703bf96be70d32;p=nageru diff --git a/nageru/image_input.cpp b/nageru/image_input.cpp index 12789dc..316292b 100644 --- a/nageru/image_input.cpp +++ b/nageru/image_input.cpp @@ -1,14 +1,32 @@ #include "image_input.h" +#include +#include +#include +#include #include -#include +#include +#include +#include #include +#include +#include +#include +#include #include #include #include #include +#include +#include +#include +#include +#include +#include extern "C" { +#include +#include #include #include #include @@ -20,27 +38,24 @@ extern "C" { #include } -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "shared/ffmpeg_raii.h" #include "ffmpeg_util.h" -#include "flags.h" +#include "shared/context.h" +#include "shared/ffmpeg_raii.h" +#include "shared/ref_counted_texture.h" +#include "tweaked_inputs.h" 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 +65,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 +78,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 +95,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]; } @@ -123,7 +133,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str()); return nullptr; } - AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); + const AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); if (codec == nullptr) { fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str()); return nullptr; @@ -140,17 +150,14 @@ shared_ptr ImageInput::load_image_raw(const string &pat AVFrameWithDeleter frame = av_frame_alloc_unique(); bool eof = false; do { - AVPacket pkt; - unique_ptr pkt_cleanup( - &pkt, av_packet_unref); - av_init_packet(&pkt); - pkt.data = nullptr; - pkt.size = 0; - if (av_read_frame(format_ctx.get(), &pkt) == 0) { - if (pkt.stream_index != stream_index) { + AVPacketWithDeleter pkt = av_packet_alloc_unique(); + pkt->data = nullptr; + pkt->size = 0; + if (av_read_frame(format_ctx.get(), pkt.get()) == 0) { + if (pkt->stream_index != stream_index) { continue; } - if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) { + if (avcodec_send_packet(codec_ctx.get(), pkt.get()) < 0) { fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str()); return nullptr; } @@ -196,23 +203,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), UniqueTexture(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 +298,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;