1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
10 #include "v210_converter.h"
14 PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
15 : pixel_format(pixel_format), buffer(buffer)
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;
34 userdata[i].pixel_format = pixel_format;
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. For RGBA, the data
41 // is already 4:4:4:4.
42 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
44 // Create textures. We don't allocate any data for the second field at this point
45 // (just create the texture state with the samplers), since our default assumed
46 // resolution is progressive.
47 switch (pixel_format) {
48 case bmusb::PixelFormat_8BitYCbCr:
49 glGenTextures(2, userdata[i].tex_y);
51 glGenTextures(2, userdata[i].tex_cbcr);
54 case bmusb::PixelFormat_10BitYCbCr:
55 glGenTextures(2, userdata[i].tex_v210);
57 glGenTextures(2, userdata[i].tex_444);
60 case bmusb::PixelFormat_8BitRGBA:
61 glGenTextures(2, userdata[i].tex_rgba);
66 userdata[i].last_width[0] = width;
67 userdata[i].last_height[0] = height;
68 userdata[i].last_v210_width[0] = 0;
70 userdata[i].last_width[1] = 0;
71 userdata[i].last_height[1] = 0;
72 userdata[i].last_v210_width[1] = 0;
74 userdata[i].last_interlaced = false;
75 userdata[i].last_has_signal = false;
76 userdata[i].last_is_connected = false;
77 for (unsigned field = 0; field < 2; ++field) {
78 switch (pixel_format) {
79 case bmusb::PixelFormat_10BitYCbCr: {
80 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
82 // Seemingly we need to set the minification filter even though
83 // shader image loads don't use them, or NVIDIA will just give us
85 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90 userdata[i].last_v210_width[0] = v210_width;
91 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
95 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
104 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
109 case bmusb::PixelFormat_8BitYCbCr:
110 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
119 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
123 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
136 case bmusb::PixelFormat_8BitRGBA:
137 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_rgba[field]);
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
155 freelist.push(frame);
157 glBindBuffer(buffer, 0);
159 glBindTexture(GL_TEXTURE_2D, 0);
163 PBOFrameAllocator::~PBOFrameAllocator()
165 while (!freelist.empty()) {
166 Frame frame = freelist.front();
168 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
169 glBindBuffer(buffer, pbo);
171 glUnmapBuffer(buffer);
173 glBindBuffer(buffer, 0);
175 glDeleteBuffers(1, &pbo);
177 switch (pixel_format) {
178 case bmusb::PixelFormat_10BitYCbCr:
179 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
181 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
184 case bmusb::PixelFormat_8BitYCbCr:
185 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
187 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
190 case bmusb::PixelFormat_8BitRGBA:
191 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_rgba);
199 //static int sumsum = 0;
201 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
205 unique_lock<mutex> lock(freelist_mutex); // Meh.
206 if (freelist.empty()) {
207 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
209 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
210 vf = freelist.front();
211 freelist.pop(); // Meh.
218 void PBOFrameAllocator::release_frame(Frame frame)
220 if (frame.overflow > 0) {
221 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
225 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
226 memset(frame.data, 0, frame.size);
227 Userdata *userdata = (Userdata *)frame.userdata;
228 for (unsigned field = 0; field < 2; ++field) {
229 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
237 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
240 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
248 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
253 unique_lock<mutex> lock(freelist_mutex);
254 freelist.push(frame);