]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Re-run IWYU, again with lots of manual cleanup.
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <cstddef>
7
8 #include "util.h"
9
10 using namespace std;
11
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)
14 {
15         userdata.reset(new Userdata[num_queued_frames]);
16         for (size_t i = 0; i < num_queued_frames; ++i) {
17                 GLuint pbo;
18                 glGenBuffers(1, &pbo);
19                 check_error();
20                 glBindBuffer(buffer, pbo);
21                 check_error();
22                 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
23                 check_error();
24
25                 Frame frame;
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;
28                 check_error();
29                 frame.size = frame_size;
30                 frame.userdata = &userdata[i];
31                 userdata[i].pbo = pbo;
32                 frame.owner = this;
33                 frame.interleaved = true;
34
35                 // Create textures.
36                 glGenTextures(1, &userdata[i].tex_y);
37                 check_error();
38                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y);
39                 check_error();
40                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41                 check_error();
42                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
43                 check_error();
44                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
45                 check_error();
46                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
47                 check_error();
48
49                 glGenTextures(1, &userdata[i].tex_cbcr);
50                 check_error();
51                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr);
52                 check_error();
53                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54                 check_error();
55                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
56                 check_error();
57                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58                 check_error();
59                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
60                 check_error();
61
62                 freelist.push(frame);
63         }
64         glBindBuffer(buffer, 0);
65         check_error();
66         glBindTexture(GL_TEXTURE_2D, 0);
67         check_error();
68 }
69
70 PBOFrameAllocator::~PBOFrameAllocator()
71 {
72         while (!freelist.empty()) {
73                 Frame frame = freelist.front();
74                 freelist.pop();
75                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
76                 glBindBuffer(buffer, pbo);
77                 check_error();
78                 glUnmapBuffer(buffer);
79                 check_error();
80                 glBindBuffer(buffer, 0);
81                 check_error();
82                 glDeleteBuffers(1, &pbo);
83                 check_error();
84                 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
85                 glDeleteTextures(1, &tex_y);
86                 check_error();
87                 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
88                 glDeleteTextures(1, &tex_cbcr);
89                 check_error();
90         }
91 }
92 //static int sumsum = 0;
93
94 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
95 {
96         Frame vf;
97
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");
101         } else {
102                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
103                 vf = freelist.front();
104                 freelist.pop();  // Meh.
105         }
106         vf.len = 0;
107         vf.overflow = 0;
108         return vf;
109 }
110
111 void PBOFrameAllocator::release_frame(Frame frame)
112 {
113         if (frame.overflow > 0) {
114                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
115         }
116
117         std::unique_lock<std::mutex> lock(freelist_mutex);
118         freelist.push(frame);
119         //--sumsum;
120 }