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);
64 glBindBuffer(buffer, 0);
66 glBindTexture(GL_TEXTURE_2D, 0);
70 PBOFrameAllocator::~PBOFrameAllocator()
72 while (!freelist.empty()) {
73 Frame frame = freelist.front();
75 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
76 glBindBuffer(buffer, pbo);
78 glUnmapBuffer(buffer);
80 glBindBuffer(buffer, 0);
82 glDeleteBuffers(1, &pbo);
84 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
85 glDeleteTextures(1, &tex_y);
87 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
88 glDeleteTextures(1, &tex_cbcr);
92 //static int sumsum = 0;
94 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
98 std::unique_lock<std::mutex> lock(freelist_mutex); // Meh.
99 if (freelist.empty()) {
100 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
102 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
103 vf = freelist.front();
104 freelist.pop(); // Meh.
111 void PBOFrameAllocator::release_frame(Frame frame)
113 if (frame.overflow > 0) {
114 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
117 std::unique_lock<std::mutex> lock(freelist_mutex);
118 freelist.push(frame);