]> git.sesse.net Git - nageru/commitdiff
When reconfiguring a PBO frame allocator, don't reuse userdata.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 10 May 2020 17:33:11 +0000 (19:33 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 10 May 2020 17:34:55 +0000 (19:34 +0200)
This would cause odd stuttering, as old and new frames would share
userdata briefly, causing chaos.

nageru/pbo_frame_allocator.cpp
nageru/pbo_frame_allocator.h

index f9b4601ec3653da0e23fd349a54ad2475291d085..70675b1a630dfbdf1c30730f5134e9f014820c41 100644 (file)
@@ -264,6 +264,14 @@ void PBOFrameAllocator::destroy_frame(Frame *frame)
        default:
                assert(false);
        }
+
+       if (ud->generation != generation) {
+               auto it = lingering_generations.find(ud->generation);
+               assert(it != lingering_generations.end());
+               if (--it->second.num_frames_left == 0) {
+                       lingering_generations.erase(it);  // Deallocates the userdata block.
+               }
+       }
 }
 //static int sumsum = 0;
 
@@ -429,6 +437,8 @@ void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format,
                return;
        }
 
+       size_t old_num_queued_frames = this->num_queued_frames;
+
        this->pixel_format = pixel_format;
        this->frame_size = frame_size;
        this->width = width;
@@ -441,12 +451,16 @@ void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format,
        this->map_bits = map_bits;
 
        lock_guard<mutex> lock(freelist_mutex);
+       lingering_generations[generation] = LingeringGeneration{ move(userdata), old_num_queued_frames };
+       ++generation;
+
        while (!freelist.empty()) {
                Frame frame = freelist.front();
                freelist.pop();
                destroy_frame(&frame);
        }
-       ++generation;
+
+       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, generation);
        }
index 5ea0ffee8f43f72a0abc4b0dcf3be01d8e998112..eee25ce906d443f6656935c33e3f9cfda476af9b 100644 (file)
@@ -114,6 +114,12 @@ private:
        GLenum permissions;
        GLenum map_bits;
        int generation = 0;  // Under freelist_mutex.
+
+       struct LingeringGeneration {
+               std::unique_ptr<Userdata[]> userdata;
+               size_t num_frames_left;
+       };
+       std::map<int, LingeringGeneration> lingering_generations;
 };
 
 #endif  // !defined(_PBO_FRAME_ALLOCATOR)