]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Remove an unused member variable.
[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         : 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. 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);
39                 check_error();
40                 glGenTextures(2, userdata[i].tex_cbcr);
41                 check_error();
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                 for (unsigned field = 0; field < 2; ++field) {
47                         glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
48                         check_error();
49                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
50                         check_error();
51                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
52                         check_error();
53                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
54                         check_error();
55                         if (field == 0) {
56                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
57                                 check_error();
58                         }
59
60                         glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
61                         check_error();
62                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
63                         check_error();
64                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
65                         check_error();
66                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
67                         check_error();
68                         if (field == 0) {
69                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
70                                 check_error();
71                         }
72                 }
73
74                 freelist.push(frame);
75         }
76         glBindBuffer(buffer, 0);
77         check_error();
78         glBindTexture(GL_TEXTURE_2D, 0);
79         check_error();
80 }
81
82 PBOFrameAllocator::~PBOFrameAllocator()
83 {
84         while (!freelist.empty()) {
85                 Frame frame = freelist.front();
86                 freelist.pop();
87                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
88                 glBindBuffer(buffer, pbo);
89                 check_error();
90                 glUnmapBuffer(buffer);
91                 check_error();
92                 glBindBuffer(buffer, 0);
93                 check_error();
94                 glDeleteBuffers(1, &pbo);
95                 check_error();
96                 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
97                 check_error();
98                 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
99                 check_error();
100         }
101 }
102 //static int sumsum = 0;
103
104 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
105 {
106         Frame vf;
107
108         std::unique_lock<std::mutex> lock(freelist_mutex);  // Meh.
109         if (freelist.empty()) {
110                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
111         } else {
112                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
113                 vf = freelist.front();
114                 freelist.pop();  // Meh.
115         }
116         vf.len = 0;
117         vf.overflow = 0;
118         return vf;
119 }
120
121 void PBOFrameAllocator::release_frame(Frame frame)
122 {
123         if (frame.overflow > 0) {
124                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
125         }
126
127         std::unique_lock<std::mutex> lock(freelist_mutex);
128         freelist.push(frame);
129         //--sumsum;
130 }