1 #include "pbo_frame_allocator.h"
8 #include <movit/util.h>
12 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
15 userdata.reset(new Userdata[num_queued_frames]);
16 for (size_t i = 0; i < num_queued_frames; ++i) {
18 glGenBuffers(1, &pbo);
20 glBindBuffer(buffer, pbo);
22 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
26 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
27 frame.data2 = frame.data + frame_size / 2;
29 frame.size = frame_size;
30 frame.userdata = &userdata[i];
31 userdata[i].pbo = pbo;
33 frame.interleaved = true;
35 // Create textures. We don't allocate any data for the second field at this point
36 // (just create the texture state with the samplers), since our default assumed
37 // resolution is progressive.
38 glGenTextures(2, userdata[i].tex_y);
40 glGenTextures(2, userdata[i].tex_cbcr);
42 userdata[i].last_width[0] = width;
43 userdata[i].last_height[0] = height;
44 userdata[i].last_width[1] = 0;
45 userdata[i].last_height[1] = 0;
46 userdata[i].last_interlaced = false;
47 userdata[i].last_has_signal = false;
48 for (unsigned field = 0; field < 2; ++field) {
49 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
62 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
78 glBindBuffer(buffer, 0);
80 glBindTexture(GL_TEXTURE_2D, 0);
84 PBOFrameAllocator::~PBOFrameAllocator()
86 while (!freelist.empty()) {
87 Frame frame = freelist.front();
89 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
90 glBindBuffer(buffer, pbo);
92 glUnmapBuffer(buffer);
94 glBindBuffer(buffer, 0);
96 glDeleteBuffers(1, &pbo);
98 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
100 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
104 //static int sumsum = 0;
106 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
110 unique_lock<mutex> lock(freelist_mutex); // Meh.
111 if (freelist.empty()) {
112 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
114 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
115 vf = freelist.front();
116 freelist.pop(); // Meh.
123 void PBOFrameAllocator::release_frame(Frame frame)
125 if (frame.overflow > 0) {
126 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
130 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
131 memset(frame.data, 0, frame.size);
132 Userdata *userdata = (Userdata *)frame.userdata;
133 for (unsigned field = 0; field < 2; ++field) {
134 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
145 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
153 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
158 unique_lock<mutex> lock(freelist_mutex);
159 freelist.push(frame);