Merge pull request #1080 from cdavis5e/display-timing-use-after-free

MVKSwapchainImage: Retain image and swapchain until presentation is done.
This commit is contained in:
Bill Hollings 2020-09-28 13:27:25 -04:00 committed by GitHub
commit ec7189b3f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -402,6 +402,8 @@ public:
MVKSwapchain* swapchain,
uint32_t swapchainIndex);
~MVKSwapchainImage() override;
protected:
friend class MVKPeerSwapchainImage;

View File

@ -1115,6 +1115,12 @@ MVKSwapchainImage::MVKSwapchainImage(MVKDevice* device,
uint32_t swapchainIndex) : MVKImage(device, pCreateInfo) {
_swapchain = swapchain;
_swapchainIndex = swapchainIndex;
_swapchain->retain();
}
MVKSwapchainImage::~MVKSwapchainImage() {
if (_swapchain) { _swapchain->release(); }
}
@ -1275,11 +1281,13 @@ void MVKPresentableSwapchainImage::presentCAMetalDrawable(id<MTLCommandBuffer> m
_swapchain->recordPresentTime(presentID, desiredPresentTime, desiredPresentTime);
#else
if ([_mtlDrawable respondsToSelector: @selector(addPresentedHandler:)]) {
retain(); // Ensure this image is not destroyed while awaiting presentation
[_mtlDrawable addPresentedHandler: ^(id<MTLDrawable> drawable) {
// Record the presentation time
CFTimeInterval presentedTimeSeconds = drawable.presentedTime;
uint64_t presentedTimeNanoseconds = (uint64_t)(presentedTimeSeconds * 1.0e9);
_swapchain->recordPresentTime(presentID, desiredPresentTime, presentedTimeNanoseconds);
release();
}];
} else {
// If MTLDrawable.presentedTime/addPresentedHandler isn't supported, just treat it as if the