summaryrefslogtreecommitdiff
path: root/stream-servers/DisplayVk.h
diff options
context:
space:
mode:
authorKaiyi Li <kaiyili@google.com>2021-02-07 16:57:42 -0800
committerKaiyi Li <kaiyili@google.com>2021-02-10 09:48:00 -0800
commitb52acdc73ff266da83d6f8dfb8c50118cbec8adb (patch)
tree0e29fb785f9869571969c6676c5e6e08a6f63b19 /stream-servers/DisplayVk.h
parent1f6758c4fa07fee8ece736eadd59cc311c435df4 (diff)
downloadvulkan-cereal-b52acdc73ff266da83d6f8dfb8c50118cbec8adb.tar.gz
Native VK Swapchain: Fix the lifetime of DisplayVk::ColorBufferInfo
The release of ColorBuffer itself and the release of the id of the ColorBuffer doesn't happen at the same time. More than one ColorBuffers may share the same ID at the same time - one is in use, and others are wait to be freed. Therefore, if we try to use ColorBuffer id to reference a ColorBuffer inside DisplayVk we may encounter a collision. Therefore, instead of the id, the client of DisplayVk now creates a DisplayVk::DisplayBufferInfo object and have the lifetime of that object via shared_ptr. Inside DisplayVk, when post is called a weak_ptr is used an compared if current composition is the same as the previous one. * Rename DisplayVk::ColorBufferInfo to DisplayVk::DisplayBufferInfo * Remove the releaseDisplayImport interface and its present in ColorBuffer destructor. Now the lifetime of a DisplayBufferInfo is entirely controlled by the object created via DisplayVk::createDisplayBuffer, and is released automatically when ColorBufferRef is released. Test: run the emulator with host vulkan native swapchain, and it won't crash now Change-Id: If3da93b059bcccfb1bbce40134d4d0789676a00b
Diffstat (limited to 'stream-servers/DisplayVk.h')
-rw-r--r--stream-servers/DisplayVk.h48
1 files changed, 29 insertions, 19 deletions
diff --git a/stream-servers/DisplayVk.h b/stream-servers/DisplayVk.h
index 05dae479..88e86c20 100644
--- a/stream-servers/DisplayVk.h
+++ b/stream-servers/DisplayVk.h
@@ -3,10 +3,8 @@
#include <functional>
#include <memory>
-#include <optional>
#include <unordered_map>
-#include "ColorBuffer.h"
#include "CompositorVk.h"
#include "RenderContext.h"
#include "SwapChainStateVk.h"
@@ -17,17 +15,39 @@
class DisplayVk {
public:
+ class DisplayBufferInfo {
+ public:
+ ~DisplayBufferInfo();
+
+ private:
+ DisplayBufferInfo(const goldfish_vk::VulkanDispatch &, VkDevice,
+ uint32_t width, uint32_t height, VkFormat, VkImage);
+
+ const goldfish_vk::VulkanDispatch &m_vk;
+ VkDevice m_vkDevice;
+ uint32_t m_width;
+ uint32_t m_height;
+ VkFormat m_vkFormat;
+
+ VkImageView m_vkImageView;
+
+ friend class DisplayVk;
+ };
DisplayVk(const goldfish_vk::VulkanDispatch &, VkPhysicalDevice,
uint32_t swapChainQueueFamilyIndex,
uint32_t compositorQueueFamilyIndex, VkDevice,
VkQueue compositorVkQueue, VkQueue swapChainVkQueue);
~DisplayVk();
void bindToSurface(VkSurfaceKHR, uint32_t width, uint32_t height);
- void importVkImage(HandleType id, VkImage, VkFormat, uint32_t width,
- uint32_t height);
- void releaseImport(HandleType id);
- // To display a colorbuffer, first import it by calling importVkImage
- void post(HandleType id);
+ // The caller is responsible to make sure the VkImage lives longer than the
+ // DisplayBufferInfo created here. However, given that DisplayBufferInfo
+ // lives in a shared_ptr, the potential lifetime of DisplayBufferInfo is
+ // aligned to DisplayVk when DisplayVk::m_surfaceState::m_prevDisplayBuffer
+ // is locked and upgraded to a shared_ptr in DisplayVk::post.
+ std::shared_ptr<DisplayBufferInfo> createDisplayBuffer(VkImage, VkFormat,
+ uint32_t width,
+ uint32_t height);
+ void post(const std::shared_ptr<DisplayBufferInfo> &);
private:
bool canComposite(VkFormat);
@@ -45,23 +65,13 @@ class DisplayVk {
VkSemaphore m_imageReadySem;
VkSemaphore m_frameDrawCompleteSem;
- struct ColorBufferInfo {
- uint32_t m_width;
- uint32_t m_height;
- VkFormat m_vkFormat;
- VkImageView m_vkImageView;
- };
- // This map won't automatically relaase all the created VkImageView. The
- // client is responsible for calling releaseImport on all imported ids
- // before this object is released.
- std::unordered_map<HandleType, ColorBufferInfo> m_colorBuffers;
-
std::unique_ptr<SwapChainStateVk> m_swapChainStateVk;
std::unique_ptr<CompositorVk> m_compositorVk;
struct SurfaceState {
uint32_t m_width = 0;
uint32_t m_height = 0;
- std::optional<HandleType> m_prevColorBuffer = std::nullopt;
+ std::weak_ptr<DisplayBufferInfo> m_prevDisplayBuffer =
+ std::weak_ptr<DisplayBufferInfo>();
};
std::unique_ptr<SurfaceState> m_surfaceState;
std::unordered_map<VkFormat, bool> m_canComposite;