1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
11 #include "v210_converter.h"
17 void set_clamp_to_edge()
19 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
23 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
29 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)
30 : pixel_format(pixel_format), buffer(buffer)
32 userdata.reset(new Userdata[num_queued_frames]);
33 for (size_t i = 0; i < num_queued_frames; ++i) {
34 init_frame(i, frame_size, width, height, permissions, map_bits);
36 glBindBuffer(buffer, 0);
38 glBindTexture(GL_TEXTURE_2D, 0);
42 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits)
45 glGenBuffers(1, &pbo);
47 glBindBuffer(buffer, pbo);
49 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
53 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
54 frame.data2 = frame.data + frame_size / 2;
55 frame.data_copy = new uint8_t[frame_size];
57 frame.size = frame_size;
58 Userdata *ud = &userdata[frame_idx];
61 ud->pixel_format = pixel_format;
64 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
65 // into separate textures. For 10-bit, the input format (v210)
66 // is complicated enough that we need to interpolate up to 4:4:4,
67 // which we do in a compute shader ourselves. For BGRA, the data
68 // is already 4:4:4:4.
69 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
71 // Create textures. We don't allocate any data for the second field at this point
72 // (just create the texture state with the samplers), since our default assumed
73 // resolution is progressive.
74 switch (pixel_format) {
75 case bmusb::PixelFormat_8BitYCbCr:
76 glGenTextures(2, ud->tex_y);
78 glGenTextures(2, ud->tex_cbcr);
81 case bmusb::PixelFormat_10BitYCbCr:
82 glGenTextures(2, ud->tex_v210);
84 glGenTextures(2, ud->tex_444);
87 case bmusb::PixelFormat_8BitBGRA:
88 glGenTextures(2, ud->tex_rgba);
91 case bmusb::PixelFormat_8BitYCbCrPlanar:
92 glGenTextures(2, ud->tex_y);
94 glGenTextures(2, ud->tex_cb);
96 glGenTextures(2, ud->tex_cr);
103 ud->last_width[0] = width;
104 ud->last_height[0] = height;
105 ud->last_cbcr_width[0] = width / 2;
106 ud->last_cbcr_height[0] = height;
107 ud->last_v210_width[0] = 0;
109 ud->last_width[1] = 0;
110 ud->last_height[1] = 0;
111 ud->last_cbcr_width[1] = 0;
112 ud->last_cbcr_height[1] = 0;
113 ud->last_v210_width[1] = 0;
115 ud->last_interlaced = false;
116 ud->last_has_signal = false;
117 ud->last_is_connected = false;
118 for (unsigned field = 0; field < 2; ++field) {
119 switch (pixel_format) {
120 case bmusb::PixelFormat_10BitYCbCr: {
121 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
123 // Seemingly we need to set the minification filter even though
124 // shader image loads don't use them, or NVIDIA will just give us
126 glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
131 ud->last_v210_width[0] = v210_width;
132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
136 glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
145 case bmusb::PixelFormat_8BitYCbCr:
146 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
150 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
154 glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
158 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
162 case bmusb::PixelFormat_8BitBGRA:
163 glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
167 if (global_flags.can_disable_srgb_decoder) { // See the comments in tweaked_inputs.h.
168 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
175 case bmusb::PixelFormat_8BitYCbCrPlanar:
176 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
180 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
184 glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
192 glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
205 freelist.push(frame);
208 PBOFrameAllocator::~PBOFrameAllocator()
210 while (!freelist.empty()) {
211 Frame frame = freelist.front();
213 destroy_frame(&frame);
217 void PBOFrameAllocator::destroy_frame(Frame *frame)
219 delete[] frame->data_copy;
221 GLuint pbo = ((Userdata *)frame->userdata)->pbo;
222 glBindBuffer(buffer, pbo);
224 glUnmapBuffer(buffer);
226 glBindBuffer(buffer, 0);
228 glDeleteBuffers(1, &pbo);
230 switch (pixel_format) {
231 case bmusb::PixelFormat_10BitYCbCr:
232 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_v210);
234 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_444);
237 case bmusb::PixelFormat_8BitYCbCr:
238 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
240 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cbcr);
243 case bmusb::PixelFormat_8BitBGRA:
244 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_rgba);
247 case bmusb::PixelFormat_8BitYCbCrPlanar:
248 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_y);
250 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cb);
252 glDeleteTextures(2, ((Userdata *)frame->userdata)->tex_cr);
259 //static int sumsum = 0;
261 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
265 lock_guard<mutex> lock(freelist_mutex); // Meh.
266 if (freelist.empty()) {
267 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
269 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
270 vf = freelist.front();
271 freelist.pop(); // Meh.
278 void PBOFrameAllocator::release_frame(Frame frame)
280 if (frame.overflow > 0) {
281 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
285 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
286 memset(frame.data, 0, frame.size);
287 Userdata *userdata = (Userdata *)frame.userdata;
288 for (unsigned field = 0; field < 2; ++field) {
289 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
297 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
300 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
302 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
306 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
308 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
313 lock_guard<mutex> lock(freelist_mutex);
314 freelist.push(frame);