]> git.sesse.net Git - nageru/blobdiff - nageru/pbo_frame_allocator.cpp
Fix some overly repetitive code in PBOFrameAllocator.
[nageru] / nageru / pbo_frame_allocator.cpp
index ea17f1259fde4076320c9ebdb37da6c714a6c5f3..1904cf17e26ea8ca3b7ea76ea56442a5acbb85bb 100644 (file)
@@ -52,11 +52,13 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
        Frame frame;
        frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
        frame.data2 = frame.data + frame_size / 2;
+       frame.data_copy = new uint8_t[frame_size];
        check_error();
        frame.size = frame_size;
-       frame.userdata = &userdata[frame_idx];
-       userdata[frame_idx].pbo = pbo;
-       userdata[frame_idx].pixel_format = pixel_format;
+       Userdata *ud = &userdata[frame_idx];
+       frame.userdata = ud;
+       ud->pbo = pbo;
+       ud->pixel_format = pixel_format;
        frame.owner = this;
 
        // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
@@ -71,48 +73,48 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
        // resolution is progressive.
        switch (pixel_format) {
        case bmusb::PixelFormat_8BitYCbCr:
-               glGenTextures(2, userdata[frame_idx].tex_y);
+               glGenTextures(2, ud->tex_y);
                check_error();
-               glGenTextures(2, userdata[frame_idx].tex_cbcr);
+               glGenTextures(2, ud->tex_cbcr);
                check_error();
                break;
        case bmusb::PixelFormat_10BitYCbCr:
-               glGenTextures(2, userdata[frame_idx].tex_v210);
+               glGenTextures(2, ud->tex_v210);
                check_error();
-               glGenTextures(2, userdata[frame_idx].tex_444);
+               glGenTextures(2, ud->tex_444);
                check_error();
                break;
        case bmusb::PixelFormat_8BitBGRA:
-               glGenTextures(2, userdata[frame_idx].tex_rgba);
+               glGenTextures(2, ud->tex_rgba);
                check_error();
                break;
        case bmusb::PixelFormat_8BitYCbCrPlanar:
-               glGenTextures(2, userdata[frame_idx].tex_y);
+               glGenTextures(2, ud->tex_y);
                check_error();
-               glGenTextures(2, userdata[frame_idx].tex_cb);
+               glGenTextures(2, ud->tex_cb);
                check_error();
-               glGenTextures(2, userdata[frame_idx].tex_cr);
+               glGenTextures(2, ud->tex_cr);
                check_error();
                break;
        default:
                assert(false);
        }
 
-       userdata[frame_idx].last_width[0] = width;
-       userdata[frame_idx].last_height[0] = height;
-       userdata[frame_idx].last_cbcr_width[0] = width / 2;
-       userdata[frame_idx].last_cbcr_height[0] = height;
-       userdata[frame_idx].last_v210_width[0] = 0;
+       ud->last_width[0] = width;
+       ud->last_height[0] = height;
+       ud->last_cbcr_width[0] = width / 2;
+       ud->last_cbcr_height[0] = height;
+       ud->last_v210_width[0] = 0;
 
-       userdata[frame_idx].last_width[1] = 0;
-       userdata[frame_idx].last_height[1] = 0;
-       userdata[frame_idx].last_cbcr_width[1] = 0;
-       userdata[frame_idx].last_cbcr_height[1] = 0;
-       userdata[frame_idx].last_v210_width[1] = 0;
+       ud->last_width[1] = 0;
+       ud->last_height[1] = 0;
+       ud->last_cbcr_width[1] = 0;
+       ud->last_cbcr_height[1] = 0;
+       ud->last_v210_width[1] = 0;
 
-       userdata[frame_idx].last_interlaced = false;
-       userdata[frame_idx].last_has_signal = false;
-       userdata[frame_idx].last_is_connected = false;
+       ud->last_interlaced = false;
+       ud->last_has_signal = false;
+       ud->last_is_connected = false;
        for (unsigned field = 0; field < 2; ++field) {
                switch (pixel_format) {
                case bmusb::PixelFormat_10BitYCbCr: {
@@ -121,17 +123,17 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                        // Seemingly we need to set the minification filter even though
                        // shader image loads don't use them, or NVIDIA will just give us
                        // zero back.
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_v210[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
                        check_error();
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                        check_error();
                        if (field == 0) {
-                               userdata[frame_idx].last_v210_width[0] = v210_width;
+                               ud->last_v210_width[0] = v210_width;
                                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
                                check_error();
                        }
 
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_444[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -141,7 +143,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                        break;
                }
                case bmusb::PixelFormat_8BitYCbCr:
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -149,7 +151,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                                check_error();
                        }
 
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cbcr[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -158,7 +160,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                        }
                        break;
                case bmusb::PixelFormat_8BitBGRA:
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_rgba[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -171,7 +173,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                        }
                        break;
                case bmusb::PixelFormat_8BitYCbCrPlanar:
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_y[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -179,7 +181,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                                check_error();
                        }
 
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cb[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -187,7 +189,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
                                check_error();
                        }
 
-                       glBindTexture(GL_TEXTURE_2D, userdata[frame_idx].tex_cr[field]);
+                       glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
                        check_error();
                        set_clamp_to_edge();
                        if (field == 0) {
@@ -214,6 +216,8 @@ PBOFrameAllocator::~PBOFrameAllocator()
 
 void PBOFrameAllocator::destroy_frame(Frame *frame)
 {
+       delete[] frame->data_copy;
+
        GLuint pbo = ((Userdata *)frame->userdata)->pbo;
        glBindBuffer(buffer, pbo);
        check_error();
@@ -258,7 +262,7 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
 {
         Frame vf;
 
-       unique_lock<mutex> lock(freelist_mutex);  // Meh.
+       lock_guard<mutex> lock(freelist_mutex);  // Meh.
        if (freelist.empty()) {
                printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
        } else {
@@ -306,7 +310,7 @@ void PBOFrameAllocator::release_frame(Frame frame)
        }
 #endif
 
-       unique_lock<mutex> lock(freelist_mutex);
+       lock_guard<mutex> lock(freelist_mutex);
        freelist.push(frame);
        //--sumsum;
 }