]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Open up for inputs that are different from the native resolution. No deinterlacing...
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <cstddef>
7
8 #include "util.h"
9
10 using namespace std;
11
12 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
13         : frame_size(frame_size), buffer(buffer)
14 {
15         userdata.reset(new Userdata[num_queued_frames]);
16         for (size_t i = 0; i < num_queued_frames; ++i) {
17                 GLuint pbo;
18                 glGenBuffers(1, &pbo);
19                 check_error();
20                 glBindBuffer(buffer, pbo);
21                 check_error();
22                 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
23                 check_error();
24
25                 Frame frame;
26                 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
27                 frame.data2 = frame.data + frame_size / 2;
28                 check_error();
29                 frame.size = frame_size;
30                 frame.userdata = &userdata[i];
31                 userdata[i].pbo = pbo;
32                 frame.owner = this;
33                 frame.interleaved = true;
34
35                 // Create textures.
36                 glGenTextures(1, &userdata[i].tex_y);
37                 check_error();
38                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y);
39                 check_error();
40                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41                 check_error();
42                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
43                 check_error();
44                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
45                 check_error();
46                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
47                 check_error();
48
49                 glGenTextures(1, &userdata[i].tex_cbcr);
50                 check_error();
51                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr);
52                 check_error();
53                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54                 check_error();
55                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
56                 check_error();
57                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58                 check_error();
59                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
60                 check_error();
61
62                 userdata[i].last_width = width;
63                 userdata[i].last_height = height;
64
65                 freelist.push(frame);
66         }
67         glBindBuffer(buffer, 0);
68         check_error();
69         glBindTexture(GL_TEXTURE_2D, 0);
70         check_error();
71 }
72
73 PBOFrameAllocator::~PBOFrameAllocator()
74 {
75         while (!freelist.empty()) {
76                 Frame frame = freelist.front();
77                 freelist.pop();
78                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
79                 glBindBuffer(buffer, pbo);
80                 check_error();
81                 glUnmapBuffer(buffer);
82                 check_error();
83                 glBindBuffer(buffer, 0);
84                 check_error();
85                 glDeleteBuffers(1, &pbo);
86                 check_error();
87                 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
88                 glDeleteTextures(1, &tex_y);
89                 check_error();
90                 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
91                 glDeleteTextures(1, &tex_cbcr);
92                 check_error();
93         }
94 }
95 //static int sumsum = 0;
96
97 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
98 {
99         Frame vf;
100
101         std::unique_lock<std::mutex> lock(freelist_mutex);  // Meh.
102         if (freelist.empty()) {
103                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
104         } else {
105                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
106                 vf = freelist.front();
107                 freelist.pop();  // Meh.
108         }
109         vf.len = 0;
110         vf.overflow = 0;
111         return vf;
112 }
113
114 void PBOFrameAllocator::release_frame(Frame frame)
115 {
116         if (frame.overflow > 0) {
117                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
118         }
119
120         std::unique_lock<std::mutex> lock(freelist_mutex);
121         freelist.push(frame);
122         //--sumsum;
123 }