Add sRGB mode toggle to Viewer.

Unlike SampleApp, this just switches out the format of the window
surface (and then adjusts the gamma-correct flag on the SkSurfaces).

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1950983007

Review-Url: https://codereview.chromium.org/1950983007
This commit is contained in:
brianosman 2016-05-06 13:28:57 -07:00 committed by Commit bot
parent 67a58dcd4a
commit 05de216ffb
12 changed files with 115 additions and 35 deletions

View File

@ -73,7 +73,7 @@ Viewer::Viewer(int argc, char** argv, void* platformData)
SkCommandLineFlags::Parse(argc, argv);
fWindow = Window::CreateNativeWindow(platformData);
fWindow->attach(Window::kVulkan_BackendType, 0);
fWindow->attach(Window::kVulkan_BackendType, DisplayParams());
// register callbacks
fWindow->registerKeyFunc(on_key_handler, this);
@ -149,14 +149,21 @@ Viewer::~Viewer() {
delete fWindow;
}
void Viewer::setupCurrentSlide(int previousSlide) {
void Viewer::updateTitle() {
SkString title("Viewer: ");
title.append(fSlides[fCurrentSlide]->getName());
if (kSRGB_SkColorProfileType == fWindow->getDisplayParams().fProfileType) {
title.append(" sRGB");
}
fWindow->setTitle(title.c_str());
}
void Viewer::setupCurrentSlide(int previousSlide) {
this->updateTitle();
fSlides[fCurrentSlide]->load();
if (previousSlide >= 0) {
fSlides[previousSlide]->unload();
}
fWindow->setTitle(title.c_str());
fWindow->inval();
}
@ -220,9 +227,6 @@ bool Viewer::onKey(Window::Key key, Window::InputState state, uint32_t modifiers
if (fCurrentSlide < 0) {
fCurrentSlide = fSlides.count() - 1;
}
SkString title("Viewer: ");
title.append(fSlides[fCurrentSlide]->getName());
fWindow->setTitle(title.c_str());
setupCurrentSlide(previousSlide);
return true;
}
@ -248,9 +252,18 @@ bool Viewer::onKey(Window::Key key, Window::InputState state, uint32_t modifiers
}
bool Viewer::onChar(SkUnichar c, uint32_t modifiers) {
if ('s' == c) {
switch (c) {
case 's':
fDisplayStats = !fDisplayStats;
return true;
case 'c':
DisplayParams params = fWindow->getDisplayParams();
params.fProfileType = (kLinear_SkColorProfileType == params.fProfileType)
? kSRGB_SkColorProfileType : kLinear_SkColorProfileType;
fWindow->setDisplayParams(params);
this->updateTitle();
fWindow->inval();
return true;
}
return false;

View File

@ -28,6 +28,7 @@ public:
private:
void initSlides();
void updateTitle();
void setupCurrentSlide(int previousSlide);
void drawStats(SkCanvas* canvas);

View File

@ -0,0 +1,27 @@
/*
* 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 DisplayParams_DEFINED
#define DisplayParams_DEFINED
#include "SkImageInfo.h"
namespace sk_app {
struct DisplayParams {
DisplayParams()
: fColorType(kN32_SkColorType)
, fProfileType(kLinear_SkColorProfileType)
, fMSAASampleCount(0) {}
SkColorType fColorType;
SkColorProfileType fProfileType;
int fMSAASampleCount;
};
} // namespace sk_app
#endif

View File

@ -24,7 +24,7 @@
namespace sk_app {
VulkanWindowContext::VulkanWindowContext(void* platformData, int msaaSampleCount)
VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams& params)
: fSurface(VK_NULL_HANDLE)
, fSwapchain(VK_NULL_HANDLE)
, fCommandPool(VK_NULL_HANDLE)
@ -32,10 +32,10 @@ VulkanWindowContext::VulkanWindowContext(void* platformData, int msaaSampleCount
// any config code here (particularly for msaa)?
this->initializeContext(platformData);
this->initializeContext(platformData, params);
}
void VulkanWindowContext::initializeContext(void* platformData) {
void VulkanWindowContext::initializeContext(void* platformData, const DisplayParams& params) {
fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
@ -74,7 +74,7 @@ void VulkanWindowContext::initializeContext(void* platformData) {
return;
}
if (!this->createSwapchain(-1, -1)) {
if (!this->createSwapchain(-1, -1, params)) {
this->destroyContext();
return;
}
@ -83,7 +83,8 @@ void VulkanWindowContext::initializeContext(void* platformData) {
vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
}
bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height) {
bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height,
const DisplayParams& params) {
// check for capabilities
VkSurfaceCapabilitiesKHR caps;
VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
@ -162,9 +163,24 @@ bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height) {
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
// Pick our surface format -- for now, the first one
VkFormat surfaceFormat = surfaceFormats[0].format;
VkColorSpaceKHR colorSpace = surfaceFormats[0].colorSpace;
// Pick our surface format. For now, just make sure it matches our sRGB request:
VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
bool wantSRGB = kSRGB_SkColorProfileType == params.fProfileType;
for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
GrPixelConfig config;
if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
GrPixelConfigIsSRGB(config) == wantSRGB) {
surfaceFormat = surfaceFormats[i].format;
colorSpace = surfaceFormats[i].colorSpace;
break;
}
}
fDisplayParams = params;
if (VK_FORMAT_UNDEFINED == surfaceFormat) {
return false;
}
// If mailbox mode is available, use it, as it is the lowest-latency non-
// tearing mode. If not, fall back to FIFO which is always available.
@ -252,7 +268,9 @@ void VulkanWindowContext::createBuffers(VkFormat format) {
desc.fSampleCnt = 0;
desc.fStencilBits = 0;
desc.fRenderTargetHandle = (GrBackendObject) &info;
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
SkSurfaceProps props(GrPixelConfigIsSRGB(fPixelConfig)
? SkSurfaceProps::kGammaCorrect_Flag : 0,
kUnknown_SkPixelGeometry);
fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &props);
}
@ -420,7 +438,7 @@ SkSurface* VulkanWindowContext::getBackbufferSurface() {
}
if (VK_ERROR_OUT_OF_DATE_KHR == res) {
// tear swapchain down and try again
if (!this->createSwapchain(0, 0)) {
if (!this->createSwapchain(0, 0, fDisplayParams)) {
return nullptr;
}

View File

@ -26,8 +26,8 @@ public:
static VkSurfaceKHR createVkSurface(VkInstance, void* platformData);
static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex);
static VulkanWindowContext* Create(void* platformData, int msaaSampleCount) {
VulkanWindowContext* ctx = new VulkanWindowContext(platformData, msaaSampleCount);
static VulkanWindowContext* Create(void* platformData, const DisplayParams& params) {
VulkanWindowContext* ctx = new VulkanWindowContext(platformData, params);
if (!ctx->isValid()) {
delete ctx;
return nullptr;
@ -43,7 +43,12 @@ public:
bool isValid() override { return SkToBool(fBackendContext.get()); }
void resize(uint32_t w, uint32_t h) override {
this->createSwapchain(w, h);
this->createSwapchain(w, h, fDisplayParams);
}
const DisplayParams& getDisplayParams() { return fDisplayParams; }
void setDisplayParams(const DisplayParams& params) {
this->createSwapchain(fWidth, fHeight, params);
}
GrBackendContext getBackendContext() override {
@ -51,9 +56,8 @@ public:
}
private:
VulkanWindowContext();
VulkanWindowContext(void*, int msaaSampleCount);
void initializeContext(void*);
VulkanWindowContext(void*, const DisplayParams&);
void initializeContext(void*, const DisplayParams&);
void destroyContext();
struct BackbufferInfo {
@ -65,7 +69,7 @@ private:
};
BackbufferInfo* getAvailableBackbuffer();
bool createSwapchain(uint32_t width, uint32_t height);
bool createSwapchain(uint32_t width, uint32_t height, const DisplayParams& params);
void createBuffers(VkFormat format);
void destroyBuffers();
@ -102,6 +106,7 @@ private:
VkQueue fPresentQueue;
int fWidth;
int fHeight;
DisplayParams fDisplayParams;
GrPixelConfig fPixelConfig;
uint32_t fImageCount;

View File

@ -75,4 +75,12 @@ void Window::onResize(uint32_t w, uint32_t h) {
fWindowContext->resize(w, h);
}
const DisplayParams& Window::getDisplayParams() {
return fWindowContext->getDisplayParams();
}
void Window::setDisplayParams(const DisplayParams& params) {
fWindowContext->setDisplayParams(params);
}
} // namespace sk_app

View File

@ -8,6 +8,7 @@
#ifndef Window_DEFINED
#define Window_DEFINED
#include "DisplayParams.h"
#include "SkTypes.h"
#include "SkRect.h"
@ -36,7 +37,7 @@ public:
kVulkan_BackendType
};
virtual bool attach(BackEndType attachType, int msaaSampleCount) = 0;
virtual bool attach(BackEndType attachType, const DisplayParams& params) = 0;
void detach();
// input handling
@ -129,6 +130,9 @@ public:
uint32_t width() { return fWidth; }
uint32_t height() { return fHeight; }
const DisplayParams& getDisplayParams();
void setDisplayParams(const DisplayParams& params);
protected:
Window();

View File

@ -7,6 +7,7 @@
#ifndef WindowContext_DEFINED
#define WindowContext_DEFINED
#include "DisplayParams.h"
#include "GrTypes.h"
class SkSurface;
@ -28,6 +29,9 @@ public:
virtual void resize(uint32_t w, uint32_t h) = 0;
virtual const DisplayParams& getDisplayParams() = 0;
virtual void setDisplayParams(const DisplayParams& params) = 0;
virtual GrBackendContext getBackendContext() = 0;
};

View File

@ -37,12 +37,12 @@ void Window_android::setTitle(const char* title) {
SkDebugf("Title: %s", title);
}
bool Window_android::attach(BackEndType attachType, int msaaSampleCount) {
bool Window_android::attach(BackEndType attachType, const DisplayParams& params) {
if (kVulkan_BackendType != attachType) {
return false;
}
mSampleCount = msaaSampleCount;
fDisplayParams = params;
// We delay the creation of fTestContext until Android informs us that
// the native window is ready to use.
@ -53,7 +53,7 @@ void Window_android::initDisplay(ANativeWindow* window) {
SkASSERT(window);
ContextPlatformData_android platformData;
platformData.fNativeWindow = window;
fWindowContext = VulkanWindowContext::Create((void*)&platformData, mSampleCount);
fWindowContext = VulkanWindowContext::Create((void*)&platformData, mSampleCount, fSRGB);
}
static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {

View File

@ -31,7 +31,7 @@ public:
void setTitle(const char*) override;
void show() override {}
bool attach(BackEndType attachType, int msaaSampleCount, bool deepColor) override;
bool attach(BackEndType attachType, const DisplayParams& params) override;
void inval() override;
void paintIfNeeded();
@ -44,7 +44,7 @@ public:
private:
android_app* mApp = nullptr;
SkRect mContentRect;
int mSampleCount = 0;
DisplayParams fDisplayParams;
};
} // namespace sk_app

View File

@ -264,7 +264,7 @@ void Window_win::show() {
}
bool Window_win::attach(BackEndType attachType, int msaaSampleCount) {
bool Window_win::attach(BackEndType attachType, const DisplayParams& params) {
if (kVulkan_BackendType != attachType) {
return false;
}
@ -273,7 +273,7 @@ bool Window_win::attach(BackEndType attachType, int msaaSampleCount) {
platformData.fHInstance = fHInstance;
platformData.fHWnd = fHWnd;
fWindowContext = VulkanWindowContext::Create((void*)&platformData, msaaSampleCount);
fWindowContext = VulkanWindowContext::Create((void*)&platformData, params);
return (SkToBool(fWindowContext));
}

View File

@ -23,7 +23,7 @@ public:
void setTitle(const char*) override;
void show() override;
bool attach(BackEndType attachType, int msaaSampleCount) override;
bool attach(BackEndType attachType, const DisplayParams& params) override;
void inval() override;