]> git.sesse.net Git - nageru/blob - pbo_frame_allocator.cpp
Use the new Movit functionality to save a texture upload for the preview.
[nageru] / pbo_frame_allocator.cpp
1 #include "pbo_frame_allocator.h"
2
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <cstddef>
6
7 #include "util.h"
8
9 using namespace std;
10
11 PBOFrameAllocator::PBOFrameAllocator(size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
12         : frame_size(frame_size), buffer(buffer)
13 {
14         userdata.reset(new Userdata[num_queued_frames]);
15         for (size_t i = 0; i < num_queued_frames; ++i) {
16                 GLuint pbo;
17                 glGenBuffers(1, &pbo);
18                 check_error();
19                 glBindBuffer(buffer, pbo);
20                 check_error();
21                 glBufferStorage(buffer, frame_size, NULL, permissions | GL_MAP_PERSISTENT_BIT);
22                 check_error();
23
24                 Frame frame;
25                 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
26                 frame.data2 = frame.data + frame_size / 2;
27                 check_error();
28                 frame.size = frame_size;
29                 frame.userdata = &userdata[i];
30                 userdata[i].pbo = pbo;
31                 frame.owner = this;
32                 frame.interleaved = true;
33
34                 // Create textures.
35                 glGenTextures(1, &userdata[i].tex_y);
36                 check_error();
37                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_y);
38                 check_error();
39                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
40                 check_error();
41                 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
42                 check_error();
43
44                 glGenTextures(1, &userdata[i].tex_cbcr);
45                 check_error();
46                 glBindTexture(GL_TEXTURE_2D, userdata[i].tex_cbcr);
47                 check_error();
48                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
49                 check_error();
50                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, NULL);
51                 check_error();
52
53                 freelist.push(frame);
54         }
55         glBindBuffer(buffer, 0);
56         check_error();
57         glBindTexture(GL_TEXTURE_2D, 0);
58         check_error();
59 }
60
61 PBOFrameAllocator::~PBOFrameAllocator()
62 {
63         while (!freelist.empty()) {
64                 Frame frame = freelist.front();
65                 freelist.pop();
66                 GLuint pbo = ((Userdata *)frame.userdata)->pbo;
67                 glBindBuffer(buffer, pbo);
68                 check_error();
69                 glUnmapBuffer(buffer);
70                 check_error();
71                 glBindBuffer(buffer, 0);
72                 check_error();
73                 glDeleteBuffers(1, &pbo);
74                 check_error();
75                 GLuint tex_y = ((Userdata *)frame.userdata)->tex_y;
76                 glDeleteTextures(1, &tex_y);
77                 check_error();
78                 GLuint tex_cbcr = ((Userdata *)frame.userdata)->tex_cbcr;
79                 glDeleteTextures(1, &tex_cbcr);
80                 check_error();
81         }
82 }
83 //static int sumsum = 0;
84
85 FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
86 {
87         Frame vf;
88
89         std::unique_lock<std::mutex> lock(freelist_mutex);  // Meh.
90         if (freelist.empty()) {
91                 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
92         } else {
93                 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
94                 vf = freelist.front();
95                 freelist.pop();  // Meh.
96         }
97         vf.len = 0;
98         return vf;
99 }
100
101 void PBOFrameAllocator::release_frame(Frame frame)
102 {
103         std::unique_lock<std::mutex> lock(freelist_mutex);
104         freelist.push(frame);
105         //--sumsum;
106 }