1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
11 #include "v210_converter.h"
15 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
18 userdata.reset(new Userdata[num_queued_frames]);
19 for (size_t i = 0; i < num_queued_frames; ++i) {
21 glGenBuffers(1, &pbo);
23 glBindBuffer(buffer, pbo);
25 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
29 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
30 frame.data2 = frame.data + frame_size / 2;
32 frame.size = frame_size;
33 frame.userdata = &userdata[i];
34 userdata[i].pbo = pbo;
37 // For 8-bit Y'CbCr, we ask the driver to split Y' and Cb/Cr
38 // into separate textures. For 10-bit, the input format (v210)
39 // is complicated enough that we need to interpolate up to 4:4:4,
40 // which we do in a compute shader ourselves.
41 frame.interleaved = !global_flags.ten_bit_input;
43 // Create textures. We don't allocate any data for the second field at this point
44 // (just create the texture state with the samplers), since our default assumed
45 // resolution is progressive.
46 if (global_flags.ten_bit_input) {
47 glGenTextures(2, userdata[i].tex_v210);
49 glGenTextures(2, userdata[i].tex_444);
52 glGenTextures(2, userdata[i].tex_y);
54 glGenTextures(2, userdata[i].tex_cbcr);
57 userdata[i].last_width[0] = width;
58 userdata[i].last_height[0] = height;
59 userdata[i].last_width[1] = 0;
60 userdata[i].last_height[1] = 0;
61 userdata[i].last_interlaced = false;
62 userdata[i].last_has_signal = false;
63 userdata[i].last_is_connected = false;
64 for (unsigned field = 0; field < 2; ++field) {
65 if (global_flags.ten_bit_input) {
66 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
68 // Seemingly we need to set the minification filter even though
69 // shader image loads don't use them, or NVIDIA will just give us
71 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
80 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
89 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
93 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
102 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
106 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
115 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
121 freelist.push(frame);
123 glBindBuffer(buffer, 0);
125 glBindTexture(GL_TEXTURE_2D, 0);
129 PBOFrameAllocator::~PBOFrameAllocator()
131 while (!freelist.empty()) {
132 Frame frame = freelist.front();
134 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
135 glBindBuffer(buffer, pbo);
137 glUnmapBuffer(buffer);
139 glBindBuffer(buffer, 0);
141 glDeleteBuffers(1, &pbo);
143 if (global_flags.ten_bit_input) {
144 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
146 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
149 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
151 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
156 //static int sumsum = 0;
158 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
162 unique_lock<mutex> lock(freelist_mutex); // Meh.
163 if (freelist.empty()) {
164 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
166 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
167 vf = freelist.front();
168 freelist.pop(); // Meh.
175 void PBOFrameAllocator::release_frame(Frame frame)
177 if (frame.overflow > 0) {
178 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
182 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
183 memset(frame.data, 0, frame.size);
184 Userdata *userdata = (Userdata *)frame.userdata;
185 for (unsigned field = 0; field < 2; ++field) {
186 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
192 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
194 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
197 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
210 unique_lock<mutex> lock(freelist_mutex);
211 freelist.push(frame);