1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
14 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
17 userdata.reset(new Userdata[num_queued_frames]);
18 for (size_t i = 0; i < num_queued_frames; ++i) {
20 glGenBuffers(1, &pbo);
22 glBindBuffer(buffer, pbo);
24 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
28 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
29 frame.data2 = frame.data + frame_size / 2;
31 frame.size = frame_size;
32 frame.userdata = &userdata[i];
33 userdata[i].pbo = pbo;
36 // For 8-bit Y'CbCr, we ask the driver to split Y' and Cb/Cr
37 // into separate textures. For 10-bit, the input format (v210)
38 // is complicated enough that we need to interpolate up to 4:4:4,
39 // which we do in a compute shader ourselves.
40 frame.interleaved = !global_flags.ten_bit_input;
42 // Create textures. We don't allocate any data for the second field at this point
43 // (just create the texture state with the samplers), since our default assumed
44 // resolution is progressive.
45 if (global_flags.ten_bit_input) {
46 glGenTextures(2, userdata[i].tex_v210);
48 glGenTextures(2, userdata[i].tex_444);
51 glGenTextures(2, userdata[i].tex_y);
53 glGenTextures(2, userdata[i].tex_cbcr);
56 userdata[i].last_width[0] = width;
57 userdata[i].last_height[0] = height;
58 userdata[i].last_width[1] = 0;
59 userdata[i].last_height[1] = 0;
60 userdata[i].last_interlaced = false;
61 userdata[i].last_has_signal = false;
62 userdata[i].last_is_connected = false;
63 for (unsigned field = 0; field < 2; ++field) {
64 if (global_flags.ten_bit_input) {
65 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
67 // Don't care about texture parameters, we're only going to read it
68 // from the compute shader anyway.
70 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
74 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
83 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
87 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
96 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
100 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
109 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
115 freelist.push(frame);
117 glBindBuffer(buffer, 0);
119 glBindTexture(GL_TEXTURE_2D, 0);
123 PBOFrameAllocator::~PBOFrameAllocator()
125 while (!freelist.empty()) {
126 Frame frame = freelist.front();
128 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
129 glBindBuffer(buffer, pbo);
131 glUnmapBuffer(buffer);
133 glBindBuffer(buffer, 0);
135 glDeleteBuffers(1, &pbo);
137 if (global_flags.ten_bit_input) {
138 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
140 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
143 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
145 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
150 //static int sumsum = 0;
152 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
156 unique_lock<mutex> lock(freelist_mutex); // Meh.
157 if (freelist.empty()) {
158 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
160 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
161 vf = freelist.front();
162 freelist.pop(); // Meh.
169 void PBOFrameAllocator::release_frame(Frame frame)
171 if (frame.overflow > 0) {
172 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
176 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
177 memset(frame.data, 0, frame.size);
178 Userdata *userdata = (Userdata *)frame.userdata;
179 for (unsigned field = 0; field < 2; ++field) {
180 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
191 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
193 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
199 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
204 unique_lock<mutex> lock(freelist_mutex);
205 freelist.push(frame);