1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
11 #include "v210_converter.h"
15 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)
16 : pixel_format(pixel_format), buffer(buffer)
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;
35 userdata[i].pixel_format = pixel_format;
38 // For 8-bit Y'CbCr, we ask the driver to split Y' and Cb/Cr
39 // into separate textures. For 10-bit, the input format (v210)
40 // is complicated enough that we need to interpolate up to 4:4:4,
41 // which we do in a compute shader ourselves. For BGRA, the data
42 // is already 4:4:4:4.
43 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
45 // Create textures. We don't allocate any data for the second field at this point
46 // (just create the texture state with the samplers), since our default assumed
47 // resolution is progressive.
48 switch (pixel_format) {
49 case bmusb::PixelFormat_8BitYCbCr:
50 glGenTextures(2, userdata[i].tex_y);
52 glGenTextures(2, userdata[i].tex_cbcr);
55 case bmusb::PixelFormat_10BitYCbCr:
56 glGenTextures(2, userdata[i].tex_v210);
58 glGenTextures(2, userdata[i].tex_444);
61 case bmusb::PixelFormat_8BitBGRA:
62 glGenTextures(2, userdata[i].tex_rgba);
67 userdata[i].last_width[0] = width;
68 userdata[i].last_height[0] = height;
69 userdata[i].last_v210_width[0] = 0;
71 userdata[i].last_width[1] = 0;
72 userdata[i].last_height[1] = 0;
73 userdata[i].last_v210_width[1] = 0;
75 userdata[i].last_interlaced = false;
76 userdata[i].last_has_signal = false;
77 userdata[i].last_is_connected = false;
78 for (unsigned field = 0; field < 2; ++field) {
79 switch (pixel_format) {
80 case bmusb::PixelFormat_10BitYCbCr: {
81 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
83 // Seemingly we need to set the minification filter even though
84 // shader image loads don't use them, or NVIDIA will just give us
86 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_v210[field]);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 userdata[i].last_v210_width[0] = v210_width;
92 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
96 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_444[field]);
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
105 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, NULL);
110 case bmusb::PixelFormat_8BitYCbCr:
111 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
120 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
124 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
137 case bmusb::PixelFormat_8BitBGRA:
138 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_rgba[field]);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
147 if (global_flags.can_disable_srgb_decoder) { // See the comments in tweaked_inputs.h.
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
150 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
160 freelist.push(frame);
162 glBindBuffer(buffer, 0);
164 glBindTexture(GL_TEXTURE_2D, 0);
168 PBOFrameAllocator::~PBOFrameAllocator()
170 while (!freelist.empty()) {
171 Frame frame = freelist.front();
173 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
174 glBindBuffer(buffer, pbo);
176 glUnmapBuffer(buffer);
178 glBindBuffer(buffer, 0);
180 glDeleteBuffers(1, &pbo);
182 switch (pixel_format) {
183 case bmusb::PixelFormat_10BitYCbCr:
184 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_v210);
186 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_444);
189 case bmusb::PixelFormat_8BitYCbCr:
190 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
192 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
195 case bmusb::PixelFormat_8BitBGRA:
196 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_rgba);
204 //static int sumsum = 0;
206 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
210 unique_lock<mutex> lock(freelist_mutex); // Meh.
211 if (freelist.empty()) {
212 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
214 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
215 vf = freelist.front();
216 freelist.pop(); // Meh.
223 void PBOFrameAllocator::release_frame(Frame frame)
225 if (frame.overflow > 0) {
226 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
230 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
231 memset(frame.data, 0, frame.size);
232 Userdata *userdata = (Userdata *)frame.userdata;
233 for (unsigned field = 0; field < 2; ++field) {
234 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
242 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
245 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
258 unique_lock<mutex> lock(freelist_mutex);
259 freelist.push(frame);