]> git.sesse.net Git - nageru/blob - nageru/image_input.h
IWYU-fix nageru/*.h.
[nageru] / nageru / image_input.h
1 #ifndef _IMAGE_INPUT_H
2 #define _IMAGE_INPUT_H 1
3
4 #include <epoxy/gl.h>
5 #include <stdbool.h>
6 #include <time.h>
7 #include <condition_variable>
8 #include <map>
9 #include <memory>
10 #include <mutex>
11 #include <string>
12 #include <thread>
13
14 #include "shared/ref_counted_texture.h"
15 #include "tweaked_inputs.h"
16
17 class QSurface;
18
19 // An output that takes its input from a static image, loaded with ffmpeg.
20 // comes from a single 2D array with chunky pixels. The image is refreshed
21 // from disk about every second.
22 class ImageInput : public sRGBSwitchingFlatInput {
23 public:
24         // For loading images.
25         // NOTE: You will need to call start_update_thread() yourself, once per program.
26         struct Image {
27                 unsigned width, height;
28                 UniqueTexture tex;
29                 timespec last_modified;
30         };
31         static std::shared_ptr<const Image> load_image(const std::string &filename, const std::string &pathname);
32
33         // Actual members.
34
35         ImageInput();  // Construct an empty input, which can't be used until you call switch_image().
36         ImageInput(const std::string &filename);
37
38         std::string effect_type_id() const override { return "ImageInput"; }
39         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num) override;
40
41         // Switch to a different image. The image must be previously loaded using load_image().
42         void switch_image(const std::string &pathname);
43
44         std::string get_pathname() const { return pathname; }
45
46         static void start_update_thread(QSurface *surface);
47         static void end_update_thread();
48         
49 private:
50         std::string pathname;
51         std::shared_ptr<const Image> current_image;
52
53         static std::shared_ptr<const Image> load_image_raw(const std::string &pathname);
54         static void update_thread_func(QSurface *surface);
55         static std::mutex all_images_lock;
56         static std::map<std::string, std::shared_ptr<const Image>> all_images;  // Under all_images_lock.
57
58         static std::thread update_thread;
59         static std::mutex update_thread_should_quit_mu;
60         static bool update_thread_should_quit;  // Under thread_should_quit_mu.
61         static std::condition_variable update_thread_should_quit_modified;  // Signals when threads_should_quit is set.
62 };
63
64 #endif // !defined(_IMAGE_INPUT_H)