]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Write 1.4.0 changelog.
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <cstddef>
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                 userdata[i].last_interlaced = false;
47                 userdata[i].last_has_signal = false;
48                 userdata[i].last_is_connected = false;
49                 for (unsigned field = 0; field < 2; ++field) {
50                         glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y[field]);
51                         check_error();
52                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53                         check_error();
54                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
55                         check_error();
56                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
57                         check_error();
58                         if (field == 0) {
59                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
60                                 check_error();
61                         }
62
63                         glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr[field]);
64                         check_error();
65                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
66                         check_error();
67                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
68                         check_error();
69                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
70                         check_error();
71                         if (field == 0) {
72                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
73                                 check_error();
74                         }
75                 }
76
77                 freelist.push(frame);
78         }
79         glBindBuffer(buffer, 0);
80         check_error();
81         glBindTexture(GL_TEXTURE_2D, 0);
82         check_error();
83 }
84
85 PBOFrameAllocator::~PBOFrameAllocator()
86 {
87         while (!freelist.empty()) {
88                 Frame frame = freelist.front();
89                 freelist.pop();
90                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
91                 glBindBuffer(buffer, pbo);
92                 check_error();
93                 glUnmapBuffer(buffer);
94                 check_error();
95                 glBindBuffer(buffer, 0);
96                 check_error();
97                 glDeleteBuffers(1, &pbo);
98                 check_error();
99                 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_y);
100                 check_error();
101                 glDeleteTextures(2, ((Userdata *)frame.userdata)->tex_cbcr);
102                 check_error();
103         }
104 }
105 //static int sumsum = 0;
106
107 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
108 {
109         Frame vf;
110
111         unique_lock<mutex> lock(freelist_mutex);  // Meh.
112         if (freelist.empty()) {
113                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
114         } else {
115                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
116                 vf = freelist.front();
117                 freelist.pop();  // Meh.
118         }
119         vf.len = 0;
120         vf.overflow = 0;
121         return vf;
122 }
123
124 void PBOFrameAllocator::release_frame(Frame frame)
125 {
126         if (frame.overflow > 0) {
127                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
128         }
129
130 #if 0
131         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
132         memset(frame.data, 0, frame.size);
133         Userdata *userdata = (Userdata *)frame.userdata;
134         for (unsigned field = 0; field < 2; ++field) {
135                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
136                 check_error();
137                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138                 check_error();
139                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
140                 check_error();
141                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142                 check_error();
143                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
144                 check_error();
145
146                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
147                 check_error();
148                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
149                 check_error();
150                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
151                 check_error();
152                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
153                 check_error();
154                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
155                 check_error();
156         }
157 #endif
158
159         unique_lock<mutex> lock(freelist_mutex);
160         freelist.push(frame);
161         //--sumsum;
162 }