2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-05-05 19:32:03 +00:00
|
|
|
#ifndef VulkanWindowContext_DEFINED
|
|
|
|
#define VulkanWindowContext_DEFINED
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-06-10 14:50:00 +00:00
|
|
|
#include "SkTypes.h" // required to pull in any SkUserConfig defines
|
|
|
|
|
2016-04-06 13:08:59 +00:00
|
|
|
#ifdef SK_VULKAN
|
|
|
|
|
|
|
|
#include "vk/GrVkBackendContext.h"
|
2016-05-05 19:32:03 +00:00
|
|
|
#include "WindowContext.h"
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-05-20 13:01:06 +00:00
|
|
|
class GrRenderTarget;
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
namespace sk_app {
|
|
|
|
|
|
|
|
class VulkanWindowContext : public WindowContext {
|
2016-04-06 13:08:59 +00:00
|
|
|
public:
|
2016-05-05 19:32:03 +00:00
|
|
|
~VulkanWindowContext() override;
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
// each platform will have to implement these in its CPP file
|
2016-04-21 18:46:23 +00:00
|
|
|
static VkSurfaceKHR createVkSurface(VkInstance, void* platformData);
|
2016-05-23 20:13:36 +00:00
|
|
|
static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex,
|
|
|
|
void* platformData);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-05-06 20:28:57 +00:00
|
|
|
static VulkanWindowContext* Create(void* platformData, const DisplayParams& params) {
|
|
|
|
VulkanWindowContext* ctx = new VulkanWindowContext(platformData, params);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (!ctx->isValid()) {
|
|
|
|
delete ctx;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2016-05-20 13:01:06 +00:00
|
|
|
sk_sp<SkSurface> getBackbufferSurface() override;
|
2016-05-05 19:32:03 +00:00
|
|
|
void swapBuffers() override;
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
bool isValid() override { return SkToBool(fBackendContext.get()); }
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
void resize(uint32_t w, uint32_t h) override {
|
2016-05-06 20:28:57 +00:00
|
|
|
this->createSwapchain(w, h, fDisplayParams);
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:49:29 +00:00
|
|
|
void setDisplayParams(const DisplayParams& params) override {
|
2016-05-06 20:28:57 +00:00
|
|
|
this->createSwapchain(fWidth, fHeight, params);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
GrBackendContext getBackendContext() override {
|
|
|
|
return (GrBackendContext) fBackendContext.get();
|
|
|
|
}
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
private:
|
2016-05-06 20:28:57 +00:00
|
|
|
VulkanWindowContext(void*, const DisplayParams&);
|
|
|
|
void initializeContext(void*, const DisplayParams&);
|
2016-04-06 13:08:59 +00:00
|
|
|
void destroyContext();
|
|
|
|
|
|
|
|
struct BackbufferInfo {
|
|
|
|
uint32_t fImageIndex; // image this is associated with
|
|
|
|
VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image
|
|
|
|
VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
|
|
|
|
VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
|
|
|
|
VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU
|
|
|
|
};
|
|
|
|
|
|
|
|
BackbufferInfo* getAvailableBackbuffer();
|
2016-05-06 20:28:57 +00:00
|
|
|
bool createSwapchain(uint32_t width, uint32_t height, const DisplayParams& params);
|
2016-04-21 15:03:10 +00:00
|
|
|
void createBuffers(VkFormat format);
|
2016-04-06 13:08:59 +00:00
|
|
|
void destroyBuffers();
|
|
|
|
|
|
|
|
SkAutoTUnref<const GrVkBackendContext> fBackendContext;
|
|
|
|
|
2016-04-21 18:46:23 +00:00
|
|
|
// simple wrapper class that exists only to initialize a pointer to NULL
|
|
|
|
template <typename FNPTR_TYPE> class VkPtr {
|
|
|
|
public:
|
|
|
|
VkPtr() : fPtr(NULL) {}
|
|
|
|
VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
|
|
|
|
operator FNPTR_TYPE() const { return fPtr; }
|
|
|
|
private:
|
|
|
|
FNPTR_TYPE fPtr;
|
|
|
|
};
|
2016-05-05 19:32:03 +00:00
|
|
|
|
2016-04-21 18:46:23 +00:00
|
|
|
// WSI interface functions
|
|
|
|
VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
|
|
|
|
VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
|
|
|
|
VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR;
|
|
|
|
VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR;
|
|
|
|
VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR;
|
|
|
|
|
|
|
|
VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR;
|
|
|
|
VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR;
|
|
|
|
VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR;
|
|
|
|
VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR;
|
|
|
|
VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR;
|
|
|
|
VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR;
|
|
|
|
|
2016-04-06 13:08:59 +00:00
|
|
|
VkSurfaceKHR fSurface;
|
|
|
|
VkSwapchainKHR fSwapchain;
|
|
|
|
uint32_t fPresentQueueIndex;
|
|
|
|
VkQueue fPresentQueue;
|
2016-05-20 13:01:06 +00:00
|
|
|
|
|
|
|
uint32_t fImageCount;
|
|
|
|
VkImage* fImages; // images in the swapchain
|
|
|
|
VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
|
|
|
|
sk_sp<GrRenderTarget>* fRenderTargets; // wrapped rendertargets for those images
|
|
|
|
sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts)
|
|
|
|
VkCommandPool fCommandPool;
|
|
|
|
BackbufferInfo* fBackbuffers;
|
|
|
|
uint32_t fCurrentBackbufferIndex;
|
2016-04-06 13:08:59 +00:00
|
|
|
};
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
} // namespace sk_app
|
|
|
|
|
2016-04-06 13:08:59 +00:00
|
|
|
#endif // SK_VULKAN
|
|
|
|
|
|
|
|
#endif
|