Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 671a9f66 authored by Alec Mouri's avatar Alec Mouri
Browse files

Tighten up race condition risk in VulkanManager.

Checking for mDevice != null risks using inconsistent state if the
HardwareBufferUpload thread and the render thread race in setting up
vulkan. Instead, use an atomic bool and std::call_once to manage
initiaizing VulkanManager instances.

Bug: 280178674
Test: builds, boots
Change-Id: Ic0a1c3ae1939ece536eb57de369232b213236d11
parent 025af291
Loading
Loading
Loading
Loading
+13 −15
Original line number Diff line number Diff line
@@ -384,12 +384,7 @@ void VulkanManager::setupDevice(GrVkExtensions& grExtensions, VkPhysicalDeviceFe
}

void VulkanManager::initialize() {
    std::lock_guard _lock{mInitializeLock};

    if (mDevice != VK_NULL_HANDLE) {
        return;
    }

    std::call_once(mInitFlag, [&] {
        GET_PROC(EnumerateInstanceVersion);
        uint32_t instanceVersion;
        LOG_ALWAYS_FATAL_IF(mEnumerateInstanceVersion(&instanceVersion));
@@ -403,6 +398,9 @@ void VulkanManager::initialize() {
        if (Properties::enablePartialUpdates && Properties::useBufferAge) {
            mSwapBehavior = SwapBehavior::BufferAge;
        }

        mInitialized = true;
    });
}

static void onGrContextReleased(void* context) {
+3 −2
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ public:
    void initialize();

    // Quick check to see if the VulkanManager has been initialized.
    bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
    bool hasVkContext() { return mInitialized; }

    // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface
    VulkanSurface* createSurface(ANativeWindow* window,
@@ -204,7 +204,8 @@ private:
    VkSemaphore mSwapSemaphore = VK_NULL_HANDLE;
    void* mDestroySemaphoreContext = nullptr;

    std::mutex mInitializeLock;
    std::once_flag mInitFlag;
    std::atomic_bool mInitialized = false;
};

} /* namespace renderthread */