1 #include "pbo_frame_allocator.h"
12 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
13 : frame_size(frame_size), buffer(buffer)
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;
36 glGenTextures(1, &userdata[i].tex_y);
38 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y);
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
46 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
49 glGenTextures(1, &userdata[i].tex_cbcr);
51 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr);
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
59 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
62 userdata[i].last_width = width;
63 userdata[i].last_height = height;
67 glBindBuffer(buffer, 0);
69 glBindTexture(GL_TEXTURE_2D, 0);
73 PBOFrameAllocator::~PBOFrameAllocator()
75 while (!freelist.empty()) {
76 Frame frame = freelist.front();
78 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
79 glBindBuffer(buffer, pbo);
81 glUnmapBuffer(buffer);
83 glBindBuffer(buffer, 0);
85 glDeleteBuffers(1, &pbo);
87 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
88 glDeleteTextures(1, &tex_y);
90 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
91 glDeleteTextures(1, &tex_cbcr);
95 //static int sumsum = 0;
97 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
101 std::unique_lock<std::mutex> lock(freelist_mutex); // Meh.
102 if (freelist.empty()) {
103 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
105 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
106 vf = freelist.front();
107 freelist.pop(); // Meh.
114 void PBOFrameAllocator::release_frame(Frame frame)
116 if (frame.overflow > 0) {
117 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
120 std::unique_lock<std::mutex> lock(freelist_mutex);
121 freelist.push(frame);