1 #include "pbo_frame_allocator.h"
11 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
12 : frame_size(frame_size), buffer(buffer)
14 userdata.reset(new Userdata[num_queued_frames]);
15 for (size_t i = 0; i < num_queued_frames; ++i) {
17 glGenBuffers(1, &pbo);
19 glBindBuffer(buffer, pbo);
21 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
25 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
26 frame.data2 = frame.data + frame_size / 2;
28 frame.size = frame_size;
29 frame.userdata = &userdata[i];
30 userdata[i].pbo = pbo;
32 frame.interleaved = true;
35 glGenTextures(1, &userdata[i].tex_y);
37 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
45 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
48 glGenTextures(1, &userdata[i].tex_cbcr);
50 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
63 glBindBuffer(buffer, 0);
65 glBindTexture(GL_TEXTURE_2D, 0);
69 PBOFrameAllocator::~PBOFrameAllocator()
71 while (!freelist.empty()) {
72 Frame frame = freelist.front();
74 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
75 glBindBuffer(buffer, pbo);
77 glUnmapBuffer(buffer);
79 glBindBuffer(buffer, 0);
81 glDeleteBuffers(1, &pbo);
83 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
84 glDeleteTextures(1, &tex_y);
86 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
87 glDeleteTextures(1, &tex_cbcr);
91 //static int sumsum = 0;
93 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
97 std::unique_lock<std::mutex> lock(freelist_mutex); // Meh.
98 if (freelist.empty()) {
99 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
101 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
102 vf = freelist.front();
103 freelist.pop(); // Meh.
110 void PBOFrameAllocator::release_frame(Frame frame)
112 if (frame.overflow > 0) {
113 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
116 std::unique_lock<std::mutex> lock(freelist_mutex);
117 freelist.push(frame);