]> git.sesse.net Git - nageru/blob - futatabi/jpeg_frame_view.h
In Futatabi, make it possible to set custom source labels.
[nageru] / futatabi / jpeg_frame_view.h
1 #ifndef _JPEG_FRAME_VIEW_H
2 #define _JPEG_FRAME_VIEW_H 1
3
4 #include "frame_on_disk.h"
5 #include "jpeg_frame.h"
6 #include "ycbcr_converter.h"
7
8 #include <QGLWidget>
9 #include <epoxy/gl.h>
10 #include <memory>
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
13 #include <movit/mix_effect.h>
14 #include <movit/ycbcr_input.h>
15 #include <stdint.h>
16 #include <condition_variable>
17 #include <deque>
18 #include <thread>
19
20 enum CacheMissBehavior {
21         DECODE_IF_NOT_IN_CACHE,
22         RETURN_NULLPTR_IF_NOT_IN_CACHE
23 };
24
25 std::shared_ptr<Frame> decode_jpeg(const std::string &jpeg);
26 std::shared_ptr<Frame> decode_jpeg_with_cache(FrameOnDisk id, CacheMissBehavior cache_miss_behavior, FrameReader *frame_reader, bool *did_decode);
27 std::shared_ptr<Frame> get_black_frame();
28
29 class JPEGFrameView : public QGLWidget {
30         Q_OBJECT
31
32 public:
33         JPEGFrameView(QWidget *parent);
34         ~JPEGFrameView();
35
36         void setFrame(unsigned stream_idx, FrameOnDisk frame, FrameOnDisk secondary_frame = {}, float fade_alpha = 0.0f);
37         void setFrame(std::shared_ptr<Frame> frame);
38
39         void mousePressEvent(QMouseEvent *event) override;
40
41         unsigned get_stream_idx() const { return current_stream_idx; }
42
43         void setDecodedFrame(std::shared_ptr<Frame> frame, std::shared_ptr<Frame> secondary_frame, float fade_alpha);
44         void set_overlay(const std::string &text);  // Blank for none.
45
46 signals:
47         void clicked();
48
49 protected:
50         void initializeGL() override;
51         void resizeGL(int width, int height) override;
52         void paintGL() override;
53
54 private:
55         void jpeg_decoder_thread_func();
56
57         FrameReader frame_reader;
58
59         // The stream index of the latest frame we displayed.
60         unsigned current_stream_idx = 0;
61
62         std::unique_ptr<YCbCrConverter> ycbcr_converter;
63         movit::EffectChain *current_chain = nullptr;  // Owned by ycbcr_converter.
64
65         std::shared_ptr<Frame> current_frame;  // So that we hold on to the pixels.
66         std::shared_ptr<Frame> current_secondary_frame;  // Same.
67
68         int overlay_base_width = 16, overlay_base_height = 16;
69         int overlay_width = overlay_base_width, overlay_height = overlay_base_height;
70         std::unique_ptr<QImage> overlay_image;  // If nullptr, no overlay.
71         std::unique_ptr<movit::EffectChain> overlay_chain;  // Just to get the overlay on screen in the easiest way possible.
72         movit::FlatInput *overlay_input;
73         bool overlay_input_needs_refresh = false;
74
75         int gl_width, gl_height;
76
77         std::thread jpeg_decoder_thread;
78         movit::ResourcePool *resource_pool = nullptr;
79
80         struct PendingDecode {
81                 // For actual decodes (only if frame below is nullptr).
82                 FrameOnDisk primary, secondary;
83                 float fade_alpha;  // Irrelevant if secondary.stream_idx == -1.
84
85                 // Already-decoded frames are also sent through PendingDecode,
86                 // so that they get drawn in the right order. If frame is nullptr,
87                 // it's a real decode.
88                 std::shared_ptr<Frame> frame;
89         };
90
91         std::condition_variable any_pending_decodes;
92         std::deque<PendingDecode> pending_decodes;  // Under cache_mu.
93 };
94
95 #endif  // !defined(_JPEG_FRAME_VIEW_H)