]> git.sesse.net Git - nageru/blob - nageru/image_input.h
Make the ImageInput cache store textures, not images.
[nageru] / nageru / image_input.h
1 #ifndef _IMAGE_INPUT_H
2 #define _IMAGE_INPUT_H 1
3
4 #include <epoxy/gl.h>
5 #include <movit/flat_input.h>
6 #include <stdbool.h>
7 #include <time.h>
8 #include <condition_variable>
9 #include <cstdint>
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <string>
14 #include <thread>
15
16 #include "ref_counted_texture.h"
17
18 class QSurface;
19
20 // An output that takes its input from a static image, loaded with ffmpeg.
21 // comes from a single 2D array with chunky pixels. The image is refreshed
22 // from disk about every second.
23 class ImageInput : public movit::FlatInput {
24 public:
25         // NOTE: You will need to call start_update_thread() yourself, once per program.
26         ImageInput(const std::string &filename);
27
28         std::string effect_type_id() const override { return "ImageInput"; }
29         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num) override;
30
31         static void start_update_thread(QSurface *surface);
32         static void end_update_thread();
33         
34 private:
35         struct Image {
36                 unsigned width, height;
37                 RefCountedTexture tex;
38                 timespec last_modified;
39         };
40
41         std::string pathname;
42         std::shared_ptr<const Image> current_image;
43
44         static std::shared_ptr<const Image> load_image(const std::string &filename, const std::string &pathname);
45         static std::shared_ptr<const Image> load_image_raw(const std::string &pathname);
46         static void update_thread_func(QSurface *surface);
47         static std::mutex all_images_lock;
48         static std::map<std::string, std::shared_ptr<const Image>> all_images;  // Under all_images_lock.
49
50         static std::thread update_thread;
51         static std::mutex update_thread_should_quit_mu;
52         static bool update_thread_should_quit;  // Under thread_should_quit_mu.
53         static std::condition_variable update_thread_should_quit_modified;  // Signals when threads_should_quit is set.
54 };
55
56 #endif // !defined(_IMAGE_INPUT_H)