]> git.sesse.net Git - nageru/blobdiff - nageru/pbo_frame_allocator.cpp
Fix some issues with cards changing pixel format on-the-fly.
[nageru] / nageru / pbo_frame_allocator.cpp
index 133b65fe3310c2b0c69b27330098286e9f6cb994..f9b4601ec3653da0e23fd349a54ad2475291d085 100644 (file)
@@ -29,11 +29,20 @@ void set_clamp_to_edge()
 }  // namespace
 
 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)
-        : card_index(card_index), mjpeg_encoder(mjpeg_encoder), pixel_format(pixel_format), buffer(buffer)
+        : card_index(card_index),
+         mjpeg_encoder(mjpeg_encoder),
+         pixel_format(pixel_format),
+         buffer(buffer),
+         frame_size(frame_size),
+         num_queued_frames(num_queued_frames),
+         width(width),
+         height(height),
+         permissions(permissions),
+         map_bits(map_bits)
 {
        userdata.reset(new Userdata[num_queued_frames]);
        for (size_t i = 0; i < num_queued_frames; ++i) {
-               init_frame(i, frame_size, width, height, permissions, map_bits);
+               init_frame(i, frame_size, width, height, permissions, map_bits, generation);
        }
        glBindBuffer(buffer, 0);
        check_error();
@@ -41,7 +50,7 @@ PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t fra
        check_error();
 }
 
-void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits)
+void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation)
 {
        GLuint pbo;
        glGenBuffers(1, &pbo);
@@ -58,6 +67,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
        frame.size = frame_size;
        Userdata *ud = &userdata[frame_idx];
        frame.userdata = ud;
+       ud->generation = generation;
        ud->pbo = pbo;
        ud->pixel_format = pixel_format;
        ud->data_copy_malloc = new uint8_t[frame_size];
@@ -388,6 +398,60 @@ void PBOFrameAllocator::release_frame(Frame frame)
        }
 
        lock_guard<mutex> lock(freelist_mutex);
-       freelist.push(frame);
+       Userdata *userdata = (Userdata *)frame.userdata;
+       if (userdata->generation == generation) {
+               freelist.push(frame);
+       } else {
+               destroy_frame(&frame);
+       }
        //--sumsum;
 }
+
+void PBOFrameAllocator::reconfigure(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)
+{
+       if (pixel_format == this->pixel_format &&
+           frame_size == this->frame_size &&
+           width == this->width && height == this->height &&
+           card_index == this->card_index &&
+           mjpeg_encoder == this->mjpeg_encoder &&
+           num_queued_frames == this->num_queued_frames &&
+           buffer == this->buffer &&
+           permissions == this->permissions &&
+           map_bits == this->map_bits) {
+               return;
+       }
+
+       this->pixel_format = pixel_format;
+       this->frame_size = frame_size;
+       this->width = width;
+       this->height = height;
+       this->card_index = card_index;
+       this->mjpeg_encoder = mjpeg_encoder;
+       this->num_queued_frames = num_queued_frames;
+       this->buffer = buffer;
+       this->permissions = permissions;
+       this->map_bits = map_bits;
+
+       lock_guard<mutex> lock(freelist_mutex);
+       while (!freelist.empty()) {
+               Frame frame = freelist.front();
+               freelist.pop();
+               destroy_frame(&frame);
+       }
+       ++generation;
+       for (size_t i = 0; i < num_queued_frames; ++i) {
+               init_frame(i, frame_size, width, height, permissions, map_bits, generation);
+       }
+
+       // There may still be frames out with the old configuration
+       // (for instance, living in GLWidget); they will be destroyed
+       // when they come back in release_frame().
+}