]> git.sesse.net Git - nageru/blob - nageru/pbo_frame_allocator.cpp
Set CEF autoplay policy to be more lenient.
[nageru] / nageru / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <bmusb/bmusb.h>
4 #include <assert.h>
5 #include <epoxy/gl.h>
6 #include <movit/util.h>
7 #include <mutex>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <cstddef>
12 #include <utility>
13 #include <va/va.h>
14
15 #include "mjpeg_encoder.h"
16 #include "shared/va_resource_pool.h"
17 #include "v210_converter.h"
18 #include "shared/va_display.h"
19
20 using namespace std;
21
22 namespace {
23
24 void set_clamp_to_edge()
25 {
26         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
27         check_error();
28         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
29         check_error();
30         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
31         check_error();
32 }
33
34 }  // namespace
35
36 PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, unsigned card_index, MJPEGEncoder *mjpeg_encoder, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
37         : card_index(card_index),
38           mjpeg_encoder(mjpeg_encoder),
39           pixel_format(pixel_format),
40           buffer(buffer),
41           frame_size(frame_size),
42           num_queued_frames(num_queued_frames),
43           width(width),
44           height(height),
45           permissions(permissions),
46           map_bits(map_bits)
47 {
48         userdata.reset(new Userdata[num_queued_frames]);
49         for (size_t i = 0; i < num_queued_frames; ++i) {
50                 init_frame(i, frame_size, width, height, permissions, map_bits, generation);
51         }
52         glBindBuffer(buffer, 0);
53         check_error();
54         glBindTexture(GL_TEXTURE_2D, 0);
55         check_error();
56 }
57
58 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation)
59 {
60         GLuint pbo;
61         glGenBuffers(1, &pbo);
62         check_error();
63         glBindBuffer(buffer, pbo);
64         check_error();
65         glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
66         check_error();
67
68         Frame frame;
69         frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
70         frame.data2 = frame.data + frame_size / 2;
71         check_error();
72         frame.size = frame_size;
73         Userdata *ud = &userdata[frame_idx];
74         frame.userdata = ud;
75         ud->generation = generation;
76         ud->pbo = pbo;
77         ud->pixel_format = pixel_format;
78         ud->data_copy_malloc = new uint8_t[frame_size];
79         frame.owner = this;
80
81         // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
82         // into separate textures. For 10-bit, the input format (v210)
83         // is complicated enough that we need to interpolate up to 4:4:4,
84         // which we do in a compute shader ourselves. For BGRA, the data
85         // is already 4:4:4:4.
86         frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
87
88         // Create textures. We don't allocate any data for the second field at this point
89         // (just create the texture state with the samplers), since our default assumed
90         // resolution is progressive.
91         switch (pixel_format) {
92         case bmusb::PixelFormat_8BitYCbCr:
93                 glGenTextures(2, ud->tex_y);
94                 check_error();
95                 glGenTextures(2, ud->tex_cbcr);
96                 check_error();
97                 break;
98         case bmusb::PixelFormat_10BitYCbCr:
99                 glGenTextures(2, ud->tex_v210);
100                 check_error();
101                 glGenTextures(2, ud->tex_444);
102                 check_error();
103                 break;
104         case bmusb::PixelFormat_8BitBGRA:
105                 glGenTextures(2, ud->tex_rgba);
106                 check_error();
107                 break;
108         case bmusb::PixelFormat_8BitYCbCrPlanar:
109                 glGenTextures(2, ud->tex_y);
110                 check_error();
111                 glGenTextures(2, ud->tex_cb);
112                 check_error();
113                 glGenTextures(2, ud->tex_cr);
114                 check_error();
115                 break;
116         default:
117                 assert(false);
118         }
119
120         ud->last_width[0] = width;
121         ud->last_height[0] = height;
122         ud->last_cbcr_width[0] = width / 2;
123         ud->last_cbcr_height[0] = height;
124         ud->last_v210_width[0] = 0;
125
126         ud->last_width[1] = 0;
127         ud->last_height[1] = 0;
128         ud->last_cbcr_width[1] = 0;
129         ud->last_cbcr_height[1] = 0;
130         ud->last_v210_width[1] = 0;
131
132         ud->last_interlaced = false;
133         ud->last_has_signal = false;
134         ud->last_is_connected = false;
135         for (unsigned field = 0; field < 2; ++field) {
136                 switch (pixel_format) {
137                 case bmusb::PixelFormat_10BitYCbCr: {
138                         const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
139
140                         // Seemingly we need to set the minification filter even though
141                         // shader image loads don't use them, or NVIDIA will just give us
142                         // zero back.
143                         glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
144                         check_error();
145                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146                         check_error();
147                         if (field == 0) {
148                                 ud->last_v210_width[0] = v210_width;
149                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
150                                 check_error();
151                         }
152
153                         glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
154                         check_error();
155                         set_clamp_to_edge();
156                         if (field == 0) {
157                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
158                                 check_error();
159                         }
160                         break;
161                 }
162                 case bmusb::PixelFormat_8BitYCbCr:
163                         glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
164                         check_error();
165                         set_clamp_to_edge();
166                         if (field == 0) {
167                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
168                                 check_error();
169                         }
170
171                         glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
172                         check_error();
173                         set_clamp_to_edge();
174                         if (field == 0) {
175                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
176                                 check_error();
177                         }
178                         break;
179                 case bmusb::PixelFormat_8BitBGRA:
180                         glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
181                         check_error();
182                         set_clamp_to_edge();
183                         if (field == 0) {
184                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
185                                 check_error();
186                         }
187                         break;
188                 case bmusb::PixelFormat_8BitYCbCrPlanar:
189                         glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
190                         check_error();
191                         set_clamp_to_edge();
192                         if (field == 0) {
193                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
194                                 check_error();
195                         }
196
197                         glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
198                         check_error();
199                         set_clamp_to_edge();
200                         if (field == 0) {
201                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
202                                 check_error();
203                         }
204
205                         glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
206                         check_error();
207                         set_clamp_to_edge();
208                         if (field == 0) {
209                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
210                                 check_error();
211                         }
212                         break;
213                 default:
214                         assert(false);
215                 }
216         }
217
218         freelist.push(frame);
219 }
220
221 PBOFrameAllocator::~PBOFrameAllocator()
222 {
223         while (!freelist.empty()) {
224                 Frame frame = freelist.front();
225                 freelist.pop();
226                 destroy_frame(&frame);
227         }
228 }
229
230 void PBOFrameAllocator::destroy_frame(Frame *frame)
231 {
232         Userdata *ud = (Userdata *)frame->userdata;
233         delete[] ud->data_copy_malloc;
234
235         GLuint pbo = ud->pbo;
236         glBindBuffer(buffer, pbo);
237         check_error();
238         glUnmapBuffer(buffer);
239         check_error();
240         glBindBuffer(buffer, 0);
241         check_error();
242         glDeleteBuffers(1, &pbo);
243         check_error();
244         switch (ud->pixel_format) {
245         case bmusb::PixelFormat_10BitYCbCr:
246                 glDeleteTextures(2, ud->tex_v210);
247                 check_error();
248                 glDeleteTextures(2, ud->tex_444);
249                 check_error();
250                 break;
251         case bmusb::PixelFormat_8BitYCbCr:
252                 glDeleteTextures(2, ud->tex_y);
253                 check_error();
254                 glDeleteTextures(2, ud->tex_cbcr);
255                 check_error();
256                 break;
257         case bmusb::PixelFormat_8BitBGRA:
258                 glDeleteTextures(2, ud->tex_rgba);
259                 check_error();
260                 break;
261         case bmusb::PixelFormat_8BitYCbCrPlanar:
262                 glDeleteTextures(2, ud->tex_y);
263                 check_error();
264                 glDeleteTextures(2, ud->tex_cb);
265                 check_error();
266                 glDeleteTextures(2, ud->tex_cr);
267                 check_error();
268                 break;
269         default:
270                 assert(false);
271         }
272
273         if (ud->generation != generation) {
274                 auto it = lingering_generations.find(ud->generation);
275                 assert(it != lingering_generations.end());
276                 if (--it->second.num_frames_left == 0) {
277                         lingering_generations.erase(it);  // Deallocates the userdata block.
278                 }
279         }
280 }
281 //static int sumsum = 0;
282
283 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
284 {
285         Frame vf;
286
287         lock_guard<mutex> lock(freelist_mutex);  // Meh.
288         if (freelist.empty()) {
289                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
290         } else {
291                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
292                 vf = freelist.front();
293                 freelist.pop();  // Meh.
294         }
295         vf.len = 0;
296         vf.overflow = 0;
297
298         if (mjpeg_encoder != nullptr &&
299             mjpeg_encoder->should_encode_mjpeg_for_card(card_index) &&
300             vf.userdata != nullptr) {
301                 Userdata *ud = (Userdata *)vf.userdata;
302                 vf.data_copy = ud->data_copy_malloc;
303                 ud->data_copy_current_src = Userdata::FROM_MALLOC;
304         } else {
305                 vf.data_copy = nullptr;
306         }
307
308         return vf;
309 }
310
311 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
312 {
313         Frame vf;
314
315         {
316                 lock_guard<mutex> lock(freelist_mutex);
317                 if (freelist.empty()) {
318                         printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
319                         vf.len = 0;
320                         vf.overflow = 0;
321                         return vf;
322                 } else {
323                         vf = freelist.front();
324                         freelist.pop();
325                 }
326         }
327         vf.len = 0;
328         vf.overflow = 0;
329
330         Userdata *userdata = (Userdata *)vf.userdata;
331
332         if (mjpeg_encoder != nullptr &&
333             mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {
334                 if (mjpeg_encoder->using_vaapi()) {
335                         VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
336                         VAResourcePool::VAResources resources = mjpeg_encoder->get_va_pool()->get_va_resources(width, height, VA_FOURCC_UYVY);  // Only used by DeckLinkCapture, so always 4:2:2.
337                         ReleaseVAResources release(mjpeg_encoder->get_va_pool(), resources);
338
339                         if (resources.image.pitches[0] == stride) {
340                                 userdata->va_resources = move(resources);
341                                 userdata->va_resources_release = move(release);
342
343                                 VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
344                                 CHECK_VASTATUS(va_status, "vaMapBuffer");
345                                 vf.data_copy += resources.image.offsets[0];
346                                 userdata->data_copy_current_src = Userdata::FROM_VA_API;
347                         } else {
348                                 printf("WARNING: Could not copy directly into VA-API MJPEG buffer for %zu x %zu, since producer and consumer disagreed on stride (%zu != %d).\n", width, height, stride, resources.image.pitches[0]);
349                                 vf.data_copy = userdata->data_copy_malloc;
350                                 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
351                         }
352                 } else {
353                         vf.data_copy = userdata->data_copy_malloc;
354                         userdata->data_copy_current_src = Userdata::FROM_MALLOC;
355                 }
356         } else {
357                 vf.data_copy = nullptr;
358         }
359
360         return vf;
361 }
362
363 void PBOFrameAllocator::release_frame(Frame frame)
364 {
365         if (frame.overflow > 0) {
366                 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
367         }
368
369 #if 0
370         // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
371         memset(frame.data, 0, frame.size);
372         Userdata *userdata = (Userdata *)frame.userdata;
373         for (unsigned field = 0; field < 2; ++field) {
374                 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
375                 check_error();
376                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
377                 check_error();
378                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
379                 check_error();
380                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
381                 check_error();
382                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
383                 check_error();
384
385                 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
386                 check_error();
387                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
388                 check_error();
389                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
390                 check_error();
391                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
392                 check_error();
393                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
394                 check_error();
395         }
396 #endif
397
398         {
399                 // In case we never got to upload the frame to MJPEGEncoder.
400                 Userdata *userdata = (Userdata *)frame.userdata;
401                 VAResourcePool::VAResources resources __attribute__((unused)) = move(userdata->va_resources);
402                 ReleaseVAResources release = move(userdata->va_resources_release);
403
404                 if (frame.data_copy != nullptr && userdata->data_copy_current_src == Userdata::FROM_VA_API) {
405                         VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
406                         VAStatus va_status = vaUnmapBuffer(va_dpy, resources.image.buf);
407                         CHECK_VASTATUS(va_status, "vaUnmapBuffer");
408
409                         frame.data_copy = nullptr;
410                 }
411         }
412
413         lock_guard<mutex> lock(freelist_mutex);
414         Userdata *userdata = (Userdata *)frame.userdata;
415         if (userdata->generation == generation) {
416                 freelist.push(frame);
417         } else {
418                 destroy_frame(&frame);
419         }
420         //--sumsum;
421 }
422
423 void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format,
424                          size_t frame_size,
425                          GLuint width, GLuint height,
426                          unsigned card_index,
427                          MJPEGEncoder *mjpeg_encoder,
428                          size_t num_queued_frames,
429                          GLenum buffer,
430                          GLenum permissions,
431                          GLenum map_bits)
432 {
433         if (pixel_format == this->pixel_format &&
434             frame_size == this->frame_size &&
435             width == this->width && height == this->height &&
436             card_index == this->card_index &&
437             mjpeg_encoder == this->mjpeg_encoder &&
438             num_queued_frames == this->num_queued_frames &&
439             buffer == this->buffer &&
440             permissions == this->permissions &&
441             map_bits == this->map_bits) {
442                 return;
443         }
444
445         lock_guard<mutex> lock(freelist_mutex);
446         lingering_generations[generation] = LingeringGeneration{ move(userdata), this->num_queued_frames };
447         ++generation;
448
449         while (!freelist.empty()) {
450                 Frame frame = freelist.front();
451                 freelist.pop();
452                 destroy_frame(&frame);
453         }
454
455         this->pixel_format = pixel_format;
456         this->frame_size = frame_size;
457         this->width = width;
458         this->height = height;
459         this->card_index = card_index;
460         this->mjpeg_encoder = mjpeg_encoder;
461         this->num_queued_frames = num_queued_frames;
462         this->buffer = buffer;
463         this->permissions = permissions;
464         this->map_bits = map_bits;
465
466         userdata.reset(new Userdata[num_queued_frames]);
467         for (size_t i = 0; i < num_queued_frames; ++i) {
468                 init_frame(i, frame_size, width, height, permissions, map_bits, generation);
469         }
470
471         // There may still be frames out with the old configuration
472         // (for instance, living in GLWidget); they will be destroyed
473         // when they come back in release_frame().
474 }