]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.h
Release Nageru 1.7.2.
[nageru] / pbo_frame_allocator.h
1 #ifndef _PBO_FRAME_ALLOCATOR 
2 #define _PBO_FRAME_ALLOCATOR 1
3
4 #include <epoxy/gl.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <memory>
8 #include <mutex>
9 #include <queue>
10
11 #include <movit/ycbcr.h>
12
13 #include "bmusb/bmusb.h"
14
15 // An allocator that allocates straight into OpenGL pinned memory.
16 // Meant for video frames only. We use a queue rather than a stack,
17 // since we want to maximize pipelineability.
18 class PBOFrameAllocator : public bmusb::FrameAllocator {
19 public:
20         // Note: You need to have an OpenGL context when calling
21         // the constructor.
22         PBOFrameAllocator(bmusb::PixelFormat pixel_format,
23                           size_t frame_size,
24                           GLuint width, GLuint height,
25                           size_t num_queued_frames = 16,
26                           GLenum buffer = GL_PIXEL_UNPACK_BUFFER_ARB,
27                           GLenum permissions = GL_MAP_WRITE_BIT,
28                           GLenum map_bits = GL_MAP_FLUSH_EXPLICIT_BIT);
29         ~PBOFrameAllocator() override;
30         Frame alloc_frame() override;
31         void release_frame(Frame frame) override;
32
33         struct Userdata {
34                 GLuint pbo;
35
36                 // NOTE: These frames typically go into LiveInputWrapper, which is
37                 // configured to accept one type of frame only. In other words,
38                 // the existence of a format field doesn't mean you can set it
39                 // freely at runtime.
40                 bmusb::PixelFormat pixel_format;
41
42                 // Used only for PixelFormat_8BitYCbCrPlanar.
43                 movit::YCbCrFormat ycbcr_format;
44
45                 // The second set is only used for the second field of interlaced inputs.
46                 GLuint tex_y[2], tex_cbcr[2];  // For PixelFormat_8BitYCbCr.
47                 GLuint tex_cb[2], tex_cr[2];  // For PixelFormat_8BitYCbCrPlanar (which also uses tex_y).
48                 GLuint tex_v210[2], tex_444[2];  // For PixelFormat_10BitYCbCr.
49                 GLuint tex_rgba[2];  // For PixelFormat_8BitBGRA.
50                 GLuint last_width[2], last_height[2];
51                 GLuint last_cbcr_width[2], last_cbcr_height[2];
52                 GLuint last_v210_width[2];  // PixelFormat_10BitYCbCr.
53                 bool last_interlaced, last_has_signal, last_is_connected;
54                 unsigned last_frame_rate_nom, last_frame_rate_den;
55         };
56
57 private:
58         void init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits);
59         void destroy_frame(Frame *frame);
60
61         bmusb::PixelFormat pixel_format;
62         std::mutex freelist_mutex;
63         std::queue<Frame> freelist;
64         GLenum buffer;
65         std::unique_ptr<Userdata[]> userdata;
66 };
67
68 #endif  // !defined(_PBO_FRAME_ALLOCATOR)