]> git.sesse.net Git - nageru/commitdiff
Refresh ImageInputs from disk every second. Not the most efficient or elegant way...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 9 Apr 2016 11:59:46 +0000 (13:59 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 9 Apr 2016 11:59:46 +0000 (13:59 +0200)
image_input.cpp
image_input.h

index ad193ff008213f7c4b8112d1e735f4567ebeef6e..27c088d59c5b76f5ae8567fec58988bbba8882a1 100644 (file)
@@ -10,18 +10,59 @@ extern "C" {
 #include <libswscale/swscale.h>
 }
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <mutex>
+#include <thread>
+
 using namespace std;
 
 ImageInput::ImageInput(const string &filename)
        : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
-                          GL_UNSIGNED_BYTE, 1280, 720)  // FIXME
+                          GL_UNSIGNED_BYTE, 1280, 720),  // FIXME
+         filename(filename),
+         current_image(load_image(filename))
 {
-       const uint8_t *pixel_data = load_image(filename);
-       if (pixel_data == nullptr) {
+       if (current_image == nullptr) {
                fprintf(stderr, "Couldn't load image, exiting.\n");
                exit(1);
        }
-       set_pixel_data(pixel_data);
+       set_pixel_data(current_image->pixels.get());
+}
+
+void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
+{
+       // See if the background thread has given us a new version of our image.
+       // Note: The old version might still be lying around in other ImageInputs
+       // (in fact, it's likely), but at least the total amount of memory used
+       // is bounded. Currently we don't even share textures between them,
+       // so there's a fair amount of OpenGL memory waste anyway (the cache
+       // is mostly there to save startup time, not RAM).
+       {
+               unique_lock<mutex> lock(all_images_lock);
+               if (all_images[filename] != current_image) {
+                       current_image = all_images[filename];
+                       set_pixel_data(current_image->pixels.get());
+               }
+       }
+       movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num);
+}
+
+shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &filename)
+{
+       unique_lock<mutex> lock(all_images_lock);  // Held also during loading.
+       if (all_images.count(filename)) {
+               return all_images[filename];
+       }
+
+       all_images[filename] = load_image_raw(filename);
+       timespec first_modified = all_images[filename]->last_modified;
+       update_threads[filename] =
+               thread(bind(update_thread_func, filename, first_modified));
+
+       return all_images[filename];
 }
 
 // Some helpers to make RAII versions of FFmpeg objects.
@@ -63,11 +104,17 @@ av_frame_alloc_unique()
 
 }  // namespace
 
-const uint8_t *ImageInput::load_image(const string &filename)
+shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &filename)
 {
-       if (all_images.count(filename)) {
-               return all_images[filename].get();
+       // Note: Call before open, not after; otherwise, there's a race.
+       // (There is now, too, but it tips the correct way. We could use fstat()
+       // if we had the file descriptor.)
+       struct stat buf;
+       if (stat(filename.c_str(), &buf) != 0) {
+               fprintf(stderr, "%s: Error stat-ing file\n", filename.c_str());
+               return nullptr;
        }
+       timespec last_modified = buf.st_mtim;
 
        auto format_ctx = avformat_open_input_unique(filename.c_str(), nullptr, nullptr);
        if (format_ctx == nullptr) {
@@ -153,8 +200,41 @@ const uint8_t *ImageInput::load_image(const string &filename)
        unique_ptr<uint8_t[]> 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);
 
-       all_images[filename] = move(image_data);
-       return all_images[filename].get();
+       shared_ptr<Image> image(new Image{move(image_data), last_modified});
+       return image;
+}
+
+// Fire up a thread to update the image every second.
+// We could do inotify, but this is good enough for now.
+// TODO: These don't really quit, ever. Should they?
+void ImageInput::update_thread_func(const std::string &filename, const timespec &first_modified)
+{
+       timespec last_modified = first_modified;
+       struct stat buf;
+       for ( ;; ) {
+               sleep(1);
+
+               if (stat(filename.c_str(), &buf) != 0) {
+                       fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", filename.c_str());
+                       continue;
+               }
+               if (buf.st_mtim.tv_sec == last_modified.tv_sec &&
+                   buf.st_mtim.tv_nsec == last_modified.tv_nsec) {
+                       // Not changed.
+                       continue;
+               }
+               shared_ptr<const Image> image = load_image_raw(filename);
+               if (image == nullptr) {
+                       fprintf(stderr, "Couldn't load image, leaving the old in place.\n");
+                       continue;
+               }
+               fprintf(stderr, "Loaded new version of %s from disk.\n", filename.c_str());
+               unique_lock<mutex> lock(all_images_lock);
+               all_images[filename] = image;
+               last_modified = image->last_modified;
+       }
 }
 
-map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;
+mutex ImageInput::all_images_lock;
+map<string, shared_ptr<const ImageInput::Image>> ImageInput::all_images;
+map<string, thread> ImageInput::update_threads;
index 3fad845ea83351a43271fc1d357b4a7d7f162356..fbda72de6c36ddf69f660db77b78024afb5fc8cb 100644 (file)
@@ -3,21 +3,39 @@
 
 #include <map>
 #include <memory>
+#include <mutex>
 #include <string>
+#include <thread>
+
+#include <time.h>
 
 #include <movit/flat_input.h>
 
 // An output that takes its input from a static image, loaded with ffmpeg.
-// comes from a single 2D array with chunky pixels.
+// comes from a single 2D array with chunky pixels. The image is refreshed
+// from disk about every second.
 class ImageInput : public movit::FlatInput {
 public:
        ImageInput(const std::string &filename);
 
        std::string effect_type_id() const override { return "ImageInput"; }
+       void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num) override;
 
 private:
-       static const uint8_t *load_image(const std::string &filename);
-       static std::map<std::string, std::unique_ptr<uint8_t[]>> all_images;
+       struct Image {
+               std::unique_ptr<uint8_t[]> pixels;
+               timespec last_modified;
+       };
+
+       std::string filename;
+       std::shared_ptr<const Image> current_image;
+
+       static std::shared_ptr<const Image> load_image(const std::string &filename);
+       static std::shared_ptr<const Image> load_image_raw(const std::string &filename);
+       static void update_thread_func(const std::string &filename, const timespec &first_modified);
+       static std::mutex all_images_lock;
+       static std::map<std::string, std::shared_ptr<const Image>> all_images;
+       static std::map<std::string, std::thread> update_threads;
 };
 
 #endif // !defined(_IMAGE_INPUT_H)