More refactoring for Viewer

* Move support files into sk_app and main files up to top directory
* Rename VulkanTestContext and create WindowContext parent class
* Place VulkanWindowContext et al. in sk_app namespace.
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1944413005

Review-Url: https://codereview.chromium.org/1944413005
This commit is contained in:
jvanverth 2016-05-05 12:32:03 -07:00 committed by Commit bot
parent 2d1ee7936e
commit a8d0d6c8ba
23 changed files with 146 additions and 85 deletions

View File

@ -8,8 +8,8 @@
#ifndef Viewer_DEFINED
#define Viewer_DEFINED
#include "../Application.h"
#include "../Window.h"
#include "sk_app/Application.h"
#include "sk_app/Window.h"
#include "gm.h"
#include "SkAnimTimer.h"
#include "Slide.h"

View File

@ -8,7 +8,7 @@
#include "GrContext.h"
#include "SkSurface.h"
#include "VulkanTestContext.h"
#include "VulkanWindowContext.h"
#include "vk/GrVkInterface.h"
#include "vk/GrVkUtil.h"
@ -22,18 +22,20 @@
#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
VulkanTestContext::VulkanTestContext(void* platformData, int msaaSampleCount)
: fSurface(VK_NULL_HANDLE)
, fSwapchain(VK_NULL_HANDLE)
, fCommandPool(VK_NULL_HANDLE)
, fBackbuffers(nullptr) {
namespace sk_app {
VulkanWindowContext::VulkanWindowContext(void* platformData, int msaaSampleCount)
: fSurface(VK_NULL_HANDLE)
, fSwapchain(VK_NULL_HANDLE)
, fCommandPool(VK_NULL_HANDLE)
, fBackbuffers(nullptr) {
// any config code here (particularly for msaa)?
this->initializeContext(platformData);
}
void VulkanTestContext::initializeContext(void* platformData) {
void VulkanWindowContext::initializeContext(void* platformData) {
fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
@ -55,7 +57,7 @@ void VulkanTestContext::initializeContext(void* platformData) {
GET_DEV_PROC(AcquireNextImageKHR);
GET_DEV_PROC(QueuePresentKHR);
fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext)fBackendContext.get());
fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
fSurface = createVkSurface(instance, platformData);
if (VK_NULL_HANDLE == fSurface) {
@ -81,8 +83,7 @@ void VulkanTestContext::initializeContext(void* platformData) {
vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
}
bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height)
{
bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height) {
// check for capabilities
VkSurfaceCapabilitiesKHR caps;
VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
@ -116,7 +117,7 @@ bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height)
SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
&presentModeCount, presentModes);
&presentModeCount, presentModes);
if (VK_SUCCESS != res) {
return false;
}
@ -223,7 +224,7 @@ bool VulkanTestContext::createSwapchain(uint32_t width, uint32_t height)
return true;
}
void VulkanTestContext::createBuffers(VkFormat format) {
void VulkanWindowContext::createBuffers(VkFormat format) {
GrVkFormatToPixelConfig(format, &fPixelConfig);
fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
@ -311,14 +312,14 @@ void VulkanTestContext::createBuffers(VkFormat format) {
fCurrentBackbufferIndex = fImageCount;
}
void VulkanTestContext::destroyBuffers() {
void VulkanWindowContext::destroyBuffers() {
if (fBackbuffers) {
for (uint32_t i = 0; i < fImageCount + 1; ++i) {
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
WaitForFences(fBackendContext->fDevice, 2,
WaitForFences(fBackendContext->fDevice, 2,
fBackbuffers[i].fUsageFences,
true, UINT64_MAX));
true, UINT64_MAX));
fBackbuffers[i].fImageIndex = -1;
GR_VK_CALL(fBackendContext->fInterface,
DestroySemaphore(fBackendContext->fDevice,
@ -349,11 +350,11 @@ void VulkanTestContext::destroyBuffers() {
fImages = nullptr;
}
VulkanTestContext::~VulkanTestContext() {
VulkanWindowContext::~VulkanWindowContext() {
this->destroyContext();
}
void VulkanTestContext::destroyContext() {
void VulkanWindowContext::destroyContext() {
if (!fBackendContext.get()) {
return;
}
@ -383,7 +384,7 @@ void VulkanTestContext::destroyContext() {
fBackendContext.reset(nullptr);
}
VulkanTestContext::BackbufferInfo* VulkanTestContext::getAvailableBackbuffer() {
VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
SkASSERT(fBackbuffers);
++fCurrentBackbufferIndex;
@ -399,7 +400,7 @@ VulkanTestContext::BackbufferInfo* VulkanTestContext::getAvailableBackbuffer() {
return backbuffer;
}
SkSurface* VulkanTestContext::getBackbufferSurface() {
SkSurface* VulkanWindowContext::getBackbufferSurface() {
BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
SkASSERT(backbuffer);
@ -424,7 +425,7 @@ SkSurface* VulkanTestContext::getBackbufferSurface() {
}
// acquire the image
res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
&backbuffer->fImageIndex);
@ -439,7 +440,7 @@ SkSurface* VulkanTestContext::getBackbufferSurface() {
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
0 : VK_ACCESS_MEMORY_READ_BIT;
VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
@ -464,15 +465,15 @@ SkSurface* VulkanTestContext::getBackbufferSurface() {
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
GR_VK_CALL(fBackendContext->fInterface,
CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
srcStageMask, dstStageMask, 0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier));
GR_VK_CALL(fBackendContext->fInterface,
CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
srcStageMask, dstStageMask, 0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier));
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
// insert the layout transfer into the queue and wait on the acquire
@ -485,16 +486,16 @@ SkSurface* VulkanTestContext::getBackbufferSurface() {
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
submitInfo.signalSemaphoreCount = 0;
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
backbuffer->fUsageFences[0]));
return fSurfaces[backbuffer->fImageIndex].get();
}
void VulkanTestContext::swapBuffers() {
void VulkanWindowContext::swapBuffers() {
BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
@ -525,11 +526,11 @@ void VulkanTestContext::swapBuffers() {
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
GR_VK_CALL(fBackendContext->fInterface,
CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
srcStageMask, dstStageMask, 0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier));
CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
srcStageMask, dstStageMask, 0,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier));
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
@ -547,7 +548,7 @@ void VulkanTestContext::swapBuffers() {
submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
backbuffer->fUsageFences[1]));
// Submit present operation to present queue
@ -566,3 +567,5 @@ void VulkanTestContext::swapBuffers() {
fQueuePresentKHR(fPresentQueue, &presentInfo);
}
} //namespace sk_app

View File

@ -5,27 +5,29 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef VulkanTestContext_DEFINED
#define VulkanTestContext_DEFINED
#ifndef VulkanWindowContext_DEFINED
#define VulkanWindowContext_DEFINED
#ifdef SK_VULKAN
#include "GrTypes.h"
#include "vk/GrVkBackendContext.h"
#include "WindowContext.h"
class SkSurface;
class GrContext;
class VulkanTestContext {
namespace sk_app {
class VulkanWindowContext : public WindowContext {
public:
~VulkanTestContext();
~VulkanWindowContext() override;
// each platform will have to implement these in its CPP file
static VkSurfaceKHR createVkSurface(VkInstance, void* platformData);
static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex);
static VulkanTestContext* Create(void* platformData, int msaaSampleCount) {
VulkanTestContext* ctx = new VulkanTestContext(platformData, msaaSampleCount);
static VulkanWindowContext* Create(void* platformData, int msaaSampleCount) {
VulkanWindowContext* ctx = new VulkanWindowContext(platformData, msaaSampleCount);
if (!ctx->isValid()) {
delete ctx;
return nullptr;
@ -33,22 +35,24 @@ public:
return ctx;
}
SkSurface* getBackbufferSurface();
void swapBuffers();
SkSurface* getBackbufferSurface() override;
void swapBuffers() override;
bool makeCurrent() { return true; }
bool makeCurrent() override { return true; }
bool isValid() { return SkToBool(fBackendContext.get()); }
bool isValid() override { return SkToBool(fBackendContext.get()); }
void resize(uint32_t w, uint32_t h) {
this->createSwapchain(w, h);
void resize(uint32_t w, uint32_t h) override {
this->createSwapchain(w, h);
}
GrBackendContext getBackendContext() { return (GrBackendContext)fBackendContext.get(); }
GrBackendContext getBackendContext() override {
return (GrBackendContext) fBackendContext.get();
}
private:
VulkanTestContext();
VulkanTestContext(void*, int msaaSampleCount);
VulkanWindowContext();
VulkanWindowContext(void*, int msaaSampleCount);
void initializeContext(void*);
void destroyContext();
@ -76,7 +80,7 @@ private:
private:
FNPTR_TYPE fPtr;
};
// WSI interface functions
VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
@ -109,6 +113,8 @@ private:
uint32_t fCurrentBackbufferIndex;
};
} // namespace sk_app
#endif // SK_VULKAN
#endif

View File

@ -9,7 +9,7 @@
#include "SkSurface.h"
#include "SkCanvas.h"
#include "VulkanTestContext.h"
#include "VulkanWindowContext.h"
namespace sk_app {
@ -36,8 +36,8 @@ Window::Window() : fCharFunc(default_char_func)
}
void Window::detach() {
delete fTestContext;
fTestContext = nullptr;
delete fWindowContext;
fWindowContext = nullptr;
}
bool Window::onChar(SkUnichar c, uint32_t modifiers) {
@ -53,7 +53,7 @@ bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) {
}
void Window::onPaint() {
SkSurface* backbuffer = fTestContext->getBackbufferSurface();
SkSurface* backbuffer = fWindowContext->getBackbufferSurface();
if (backbuffer) {
// draw into the canvas of this surface
SkCanvas* canvas = backbuffer->getCanvas();
@ -62,7 +62,7 @@ void Window::onPaint() {
canvas->flush();
fTestContext->swapBuffers();
fWindowContext->swapBuffers();
} else {
// try recreating testcontext
}
@ -72,7 +72,7 @@ void Window::onPaint() {
void Window::onResize(uint32_t w, uint32_t h) {
fWidth = w;
fHeight = h;
fTestContext->resize(w, h);
fWindowContext->resize(w, h);
}
} // namespace sk_app

View File

@ -12,10 +12,11 @@
#include "SkRect.h"
class SkCanvas;
class VulkanTestContext;
namespace sk_app {
class WindowContext;
class Window {
public:
static Window* CreateNativeWindow(void* platformData);
@ -143,7 +144,7 @@ protected:
OnPaintFunc fPaintFunc;
void* fPaintUserData;
VulkanTestContext* fTestContext;
WindowContext* fWindowContext;
};
} // namespace sk_app

View File

@ -0,0 +1,36 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef WindowContext_DEFINED
#define WindowContext_DEFINED
#include "GrTypes.h"
class SkSurface;
namespace sk_app {
// TODO: fill this out with an interface
class WindowContext {
public:
virtual ~WindowContext() {}
virtual SkSurface* getBackbufferSurface() = 0;
virtual void swapBuffers() = 0;
virtual bool makeCurrent() = 0;
virtual bool isValid() = 0;
virtual void resize(uint32_t w, uint32_t h) = 0;
virtual GrBackendContext getBackendContext() = 0;
};
} // namespace sk_app
#endif

View File

@ -6,12 +6,14 @@
* found in the LICENSE file.
*/
#include "VulkanTestContext_android.h"
#include "VulkanWindowContext_android.h"
#include "vk/GrVkInterface.h"
#include "vk/GrVkUtil.h"
VkSurfaceKHR VulkanTestContext::createVkSurface(VkInstance instance, void* platformData) {
namespace sk_app {
VkSurfaceKHR VulkanWindowContext::createVkSurface(VkInstance instance, void* platformData) {
static PFN_vkCreateAndroidSurfaceKHR createAndroidSurfaceKHR = nullptr;
if (!createAndroidSurfaceKHR) {
createAndroidSurfaceKHR = (PFN_vkCreateAndroidSurfaceKHR)vkGetInstanceProcAddr(instance,
@ -37,7 +39,9 @@ VkSurfaceKHR VulkanTestContext::createVkSurface(VkInstance instance, void* platf
return (VK_SUCCESS == res) ? surface : VK_NULL_HANDLE;
}
bool VulkanTestContext::canPresent(VkInstance instance, VkPhysicalDevice physDev,
bool VulkanWindowContext::canPresent(VkInstance instance, VkPhysicalDevice physDev,
uint32_t queueFamilyIndex) {
return true;
}
} // namespace sk_app

View File

@ -10,14 +10,17 @@
#ifdef SK_VULKAN
#include "../VulkanTestContext.h"
#include "../VulkanWindowContext.h"
struct ANativeWindow;
namespace sk_app {
struct ContextPlatformData_android {
ANativeWindow* fNativeWindow;
};
}
#endif // SK_VULKAN
#endif

View File

@ -7,7 +7,7 @@
#include "Window_android.h"
#include "VulkanTestContext_android.h"
#include "VulkanWindowContext_android.h"
namespace sk_app {
@ -53,7 +53,7 @@ void Window_android::initDisplay(ANativeWindow* window) {
SkASSERT(window);
ContextPlatformData_android platformData;
platformData.fNativeWindow = window;
fTestContext = VulkanTestContext::Create((void*)&platformData, mSampleCount);
fWindowContext = VulkanWindowContext::Create((void*)&platformData, mSampleCount);
}
static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {

View File

@ -6,24 +6,26 @@
* found in the LICENSE file.
*/
#include "VulkanTestContext_win.h"
#include "VulkanWindowContext_win.h"
#include "vk/GrVkInterface.h"
#include "vk/GrVkUtil.h"
namespace sk_app {
// Platform dependant call
VkSurfaceKHR VulkanTestContext::createVkSurface(VkInstance instance, void* platformData) {
VkSurfaceKHR VulkanWindowContext::createVkSurface(VkInstance instance, void* platformData) {
static PFN_vkCreateWin32SurfaceKHR createWin32SurfaceKHR = nullptr;
if (!createWin32SurfaceKHR) {
createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)vkGetInstanceProcAddr(instance,
"vkCreateWin32SurfaceKHR");
createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR) vkGetInstanceProcAddr(instance,
"vkCreateWin32SurfaceKHR");
}
if (!platformData) {
return VK_NULL_HANDLE;
}
ContextPlatformData_win* winPlatformData =
reinterpret_cast<ContextPlatformData_win*>(platformData);
ContextPlatformData_win* winPlatformData =
reinterpret_cast<ContextPlatformData_win*>(platformData);
VkSurfaceKHR surface;
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
@ -43,16 +45,18 @@ VkSurfaceKHR VulkanTestContext::createVkSurface(VkInstance instance, void* platf
}
// Platform dependant call
bool VulkanTestContext::canPresent(VkInstance instance, VkPhysicalDevice physDev,
bool VulkanWindowContext::canPresent(VkInstance instance, VkPhysicalDevice physDev,
uint32_t queueFamilyIndex) {
static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR
getPhysicalDeviceWin32PresentationSupportKHR = nullptr;
static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR
getPhysicalDeviceWin32PresentationSupportKHR = nullptr;
if (!getPhysicalDeviceWin32PresentationSupportKHR) {
getPhysicalDeviceWin32PresentationSupportKHR =
getPhysicalDeviceWin32PresentationSupportKHR =
(PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR) vkGetInstanceProcAddr(instance,
"vkGetPhysicalDeviceWin32PresentationSupportKHR");
"vkGetPhysicalDeviceWin32PresentationSupportKHR");
}
VkBool32 check = getPhysicalDeviceWin32PresentationSupportKHR(physDev, queueFamilyIndex);
return (VK_FALSE != check);
}
} // namespace sk_app

View File

@ -11,7 +11,9 @@
#ifdef SK_VULKAN
#include <windows.h>
#include "../VulkanTestContext.h"
#include "../VulkanWindowContext.h"
namespace sk_app {
// for Windows
struct ContextPlatformData_win {
@ -19,6 +21,8 @@ struct ContextPlatformData_win {
HWND fHWnd;
};
}
#endif // SK_VULKAN
#endif

View File

@ -12,7 +12,7 @@
#include <windowsx.h>
#include "SkUtils.h"
#include "VulkanTestContext_win.h"
#include "VulkanWindowContext_win.h"
namespace sk_app {
@ -273,9 +273,9 @@ bool Window_win::attach(BackEndType attachType, int msaaSampleCount) {
platformData.fHInstance = fHInstance;
platformData.fHWnd = fHWnd;
fTestContext = VulkanTestContext::Create((void*)&platformData, msaaSampleCount);
fWindowContext = VulkanWindowContext::Create((void*)&platformData, msaaSampleCount);
return (SkToBool(fTestContext));
return (SkToBool(fWindowContext));
}
void Window_win::inval() {