2016-03-31 01:56:19 +00:00
|
|
|
|
2014-11-13 19:12:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
2019-11-08 21:18:15 +00:00
|
|
|
#ifdef SK_GL
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/gl/GLTestContext.h"
|
2019-11-08 21:18:15 +00:00
|
|
|
#endif
|
2014-11-13 19:12:41 +00:00
|
|
|
|
2016-11-14 14:53:13 +00:00
|
|
|
#if SK_ANGLE
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/gl/angle/GLTestContext_angle.h"
|
2016-11-14 14:53:13 +00:00
|
|
|
#endif
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
|
2016-06-09 20:07:13 +00:00
|
|
|
#ifdef SK_VULKAN
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/vk/VkTestContext.h"
|
2016-04-01 14:42:05 +00:00
|
|
|
#endif
|
2017-07-13 19:07:54 +00:00
|
|
|
#ifdef SK_METAL
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/mtl/MtlTestContext.h"
|
2017-07-13 19:07:54 +00:00
|
|
|
#endif
|
2019-07-18 15:43:45 +00:00
|
|
|
#ifdef SK_DAWN
|
|
|
|
#include "tools/gpu/dawn/DawnTestContext.h"
|
|
|
|
#endif
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrCaps.h"
|
2019-05-22 20:23:43 +00:00
|
|
|
#include "src/gpu/gl/GrGLGpu.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/mock/MockTestContext.h"
|
2014-11-13 19:12:41 +00:00
|
|
|
|
2018-01-24 17:42:55 +00:00
|
|
|
#if defined(SK_BUILD_FOR_WIN) && defined(SK_ENABLE_DISCRETE_GPU)
|
2016-12-28 16:19:22 +00:00
|
|
|
extern "C" {
|
|
|
|
// NVIDIA documents that the presence and value of this symbol programmatically enable the high
|
|
|
|
// performance GPU in laptops with switchable graphics.
|
|
|
|
// https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm
|
|
|
|
// From testing, including this symbol, even if it is set to 0, we still get the NVIDIA GPU.
|
|
|
|
_declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
|
|
|
|
|
|
|
|
// AMD has a similar mechanism, although I don't have an AMD laptop, so this is untested.
|
|
|
|
// https://community.amd.com/thread/169965
|
|
|
|
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
namespace sk_gpu_test {
|
2016-01-07 07:49:30 +00:00
|
|
|
GrContextFactory::GrContextFactory() { }
|
|
|
|
|
|
|
|
GrContextFactory::GrContextFactory(const GrContextOptions& opts)
|
|
|
|
: fGlobalOptions(opts) {
|
|
|
|
}
|
|
|
|
|
|
|
|
GrContextFactory::~GrContextFactory() {
|
|
|
|
this->destroyContexts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GrContextFactory::destroyContexts() {
|
2018-01-16 16:20:41 +00:00
|
|
|
// We must delete the test contexts in reverse order so that any child context is finished and
|
|
|
|
// deleted before a parent context. This relies on the fact that when we make a new context we
|
|
|
|
// append it to the end of fContexts array.
|
|
|
|
// TODO: Look into keeping a dependency dag for contexts and deletion order
|
|
|
|
for (int i = fContexts.count() - 1; i >= 0; --i) {
|
|
|
|
Context& context = fContexts[i];
|
2017-11-17 14:25:23 +00:00
|
|
|
SkScopeExit restore(nullptr);
|
2016-05-11 17:09:18 +00:00
|
|
|
if (context.fTestContext) {
|
2017-11-17 14:25:23 +00:00
|
|
|
restore = context.fTestContext->makeCurrentAndAutoRestore();
|
2016-01-07 07:49:30 +00:00
|
|
|
}
|
|
|
|
if (!context.fGrContext->unique()) {
|
2016-04-11 21:21:33 +00:00
|
|
|
context.fGrContext->releaseResourcesAndAbandonContext();
|
|
|
|
context.fAbandoned = true;
|
2016-01-07 07:49:30 +00:00
|
|
|
}
|
|
|
|
context.fGrContext->unref();
|
2016-05-11 17:09:18 +00:00
|
|
|
delete context.fTestContext;
|
2016-01-07 07:49:30 +00:00
|
|
|
}
|
|
|
|
fContexts.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GrContextFactory::abandonContexts() {
|
2018-01-16 16:20:41 +00:00
|
|
|
// We must abandon the test contexts in reverse order so that any child context is finished and
|
|
|
|
// abandoned before a parent context. This relies on the fact that when we make a new context we
|
|
|
|
// append it to the end of fContexts array.
|
|
|
|
// TODO: Look into keeping a dependency dag for contexts and deletion order
|
|
|
|
for (int i = fContexts.count() - 1; i >= 0; --i) {
|
|
|
|
Context& context = fContexts[i];
|
2016-04-11 21:21:33 +00:00
|
|
|
if (!context.fAbandoned) {
|
2016-05-11 17:09:18 +00:00
|
|
|
if (context.fTestContext) {
|
2017-11-17 14:25:23 +00:00
|
|
|
auto restore = context.fTestContext->makeCurrentAndAutoRestore();
|
2016-05-11 17:09:18 +00:00
|
|
|
context.fTestContext->testAbandon();
|
2019-12-04 20:17:54 +00:00
|
|
|
}
|
|
|
|
bool requiresEarlyAbandon = (context.fGrContext->backend() == GrBackendApi::kVulkan);
|
|
|
|
if (requiresEarlyAbandon) {
|
|
|
|
context.fGrContext->abandonContext();
|
|
|
|
}
|
|
|
|
if (context.fTestContext) {
|
2016-05-11 17:09:18 +00:00
|
|
|
delete(context.fTestContext);
|
|
|
|
context.fTestContext = nullptr;
|
2016-04-11 21:21:33 +00:00
|
|
|
}
|
2019-12-04 20:17:54 +00:00
|
|
|
if (!requiresEarlyAbandon) {
|
|
|
|
context.fGrContext->abandonContext();
|
|
|
|
}
|
2016-04-11 21:21:33 +00:00
|
|
|
context.fAbandoned = true;
|
2016-01-07 07:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 18:54:31 +00:00
|
|
|
void GrContextFactory::releaseResourcesAndAbandonContexts() {
|
2018-01-16 16:20:41 +00:00
|
|
|
// We must abandon the test contexts in reverse order so that any child context is finished and
|
|
|
|
// abandoned before a parent context. This relies on the fact that when we make a new context we
|
|
|
|
// append it to the end of fContexts array.
|
|
|
|
// TODO: Look into keeping a dependency dag for contexts and deletion order
|
|
|
|
for (int i = fContexts.count() - 1; i >= 0; --i) {
|
|
|
|
Context& context = fContexts[i];
|
2017-11-17 14:25:23 +00:00
|
|
|
SkScopeExit restore(nullptr);
|
2016-04-11 21:21:33 +00:00
|
|
|
if (!context.fAbandoned) {
|
2016-05-11 17:09:18 +00:00
|
|
|
if (context.fTestContext) {
|
2017-11-17 14:25:23 +00:00
|
|
|
restore = context.fTestContext->makeCurrentAndAutoRestore();
|
2016-04-11 21:21:33 +00:00
|
|
|
}
|
2016-04-01 18:54:31 +00:00
|
|
|
context.fGrContext->releaseResourcesAndAbandonContext();
|
2016-05-11 17:09:18 +00:00
|
|
|
if (context.fTestContext) {
|
|
|
|
delete context.fTestContext;
|
|
|
|
context.fTestContext = nullptr;
|
2016-04-11 21:21:33 +00:00
|
|
|
}
|
2019-12-04 20:17:54 +00:00
|
|
|
context.fAbandoned = true;
|
2016-04-01 18:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:03:17 +00:00
|
|
|
GrContext* GrContextFactory::get(ContextType type, ContextOverrides overrides) {
|
|
|
|
return this->getContextInfo(type, overrides).grContext();
|
|
|
|
}
|
|
|
|
|
2017-02-24 19:51:44 +00:00
|
|
|
ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
|
|
|
|
GrContext* shareContext, uint32_t shareIndex) {
|
2017-02-21 21:58:08 +00:00
|
|
|
// (shareIndex != 0) -> (shareContext != nullptr)
|
|
|
|
SkASSERT((shareIndex == 0) || (shareContext != nullptr));
|
|
|
|
|
2014-11-13 19:12:41 +00:00
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
2016-01-07 07:49:30 +00:00
|
|
|
Context& context = fContexts[i];
|
|
|
|
if (context.fType == type &&
|
2017-02-21 19:36:05 +00:00
|
|
|
context.fOverrides == overrides &&
|
2017-02-21 21:58:08 +00:00
|
|
|
context.fShareContext == shareContext &&
|
|
|
|
context.fShareIndex == shareIndex &&
|
2016-04-11 21:21:33 +00:00
|
|
|
!context.fAbandoned) {
|
2016-05-11 17:09:18 +00:00
|
|
|
context.fTestContext->makeCurrent();
|
2017-10-18 12:33:29 +00:00
|
|
|
return ContextInfo(context.fType, context.fTestContext, context.fGrContext,
|
|
|
|
context.fOptions);
|
2014-11-13 19:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 21:58:08 +00:00
|
|
|
|
|
|
|
// If we're trying to create a context in a share group, find the master context
|
|
|
|
Context* masterContext = nullptr;
|
|
|
|
if (shareContext) {
|
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
|
|
|
if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
|
|
|
|
masterContext = &fContexts[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 19:51:44 +00:00
|
|
|
SkASSERT(masterContext && masterContext->fType == type);
|
2017-02-21 21:58:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<TestContext> testCtx;
|
2018-10-12 13:31:11 +00:00
|
|
|
GrBackendApi backend = ContextTypeBackend(type);
|
2016-04-11 21:21:33 +00:00
|
|
|
switch (backend) {
|
2019-11-08 21:18:15 +00:00
|
|
|
#ifdef SK_GL
|
2018-10-12 13:31:11 +00:00
|
|
|
case GrBackendApi::kOpenGL: {
|
2017-02-21 21:58:08 +00:00
|
|
|
GLTestContext* glShareContext = masterContext
|
|
|
|
? static_cast<GLTestContext*>(masterContext->fTestContext) : nullptr;
|
2016-05-11 17:09:18 +00:00
|
|
|
GLTestContext* glCtx;
|
2016-04-11 21:21:33 +00:00
|
|
|
switch (type) {
|
|
|
|
case kGL_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = CreatePlatformGLTestContext(kGL_GrGLStandard, glShareContext);
|
2016-04-11 21:21:33 +00:00
|
|
|
break;
|
|
|
|
case kGLES_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = CreatePlatformGLTestContext(kGLES_GrGLStandard, glShareContext);
|
2016-04-11 21:21:33 +00:00
|
|
|
break;
|
2016-11-14 14:53:13 +00:00
|
|
|
#if SK_ANGLE
|
2016-10-14 15:13:48 +00:00
|
|
|
case kANGLE_D3D9_ES2_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = MakeANGLETestContext(ANGLEBackend::kD3D9, ANGLEContextVersion::kES2,
|
|
|
|
glShareContext).release();
|
2016-04-11 21:21:33 +00:00
|
|
|
break;
|
2016-10-14 15:13:48 +00:00
|
|
|
case kANGLE_D3D11_ES2_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES2,
|
|
|
|
glShareContext).release();
|
2016-10-14 15:13:48 +00:00
|
|
|
break;
|
|
|
|
case kANGLE_D3D11_ES3_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = MakeANGLETestContext(ANGLEBackend::kD3D11, ANGLEContextVersion::kES3,
|
|
|
|
glShareContext).release();
|
2016-10-14 15:13:48 +00:00
|
|
|
break;
|
|
|
|
case kANGLE_GL_ES2_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES2,
|
|
|
|
glShareContext).release();
|
2016-10-14 15:13:48 +00:00
|
|
|
break;
|
|
|
|
case kANGLE_GL_ES3_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = MakeANGLETestContext(ANGLEBackend::kOpenGL, ANGLEContextVersion::kES3,
|
|
|
|
glShareContext).release();
|
2016-04-11 21:21:33 +00:00
|
|
|
break;
|
2016-11-14 14:53:13 +00:00
|
|
|
#endif
|
2017-05-24 19:30:35 +00:00
|
|
|
#ifndef SK_NO_COMMAND_BUFFER
|
2016-04-11 21:21:33 +00:00
|
|
|
case kCommandBuffer_ContextType:
|
2017-02-21 21:58:08 +00:00
|
|
|
glCtx = CommandBufferGLTestContext::Create(glShareContext);
|
2016-04-11 21:21:33 +00:00
|
|
|
break;
|
2014-11-13 19:12:41 +00:00
|
|
|
#endif
|
2016-04-11 21:21:33 +00:00
|
|
|
default:
|
|
|
|
return ContextInfo();
|
|
|
|
}
|
2016-05-11 17:09:18 +00:00
|
|
|
if (!glCtx) {
|
2016-04-11 21:21:33 +00:00
|
|
|
return ContextInfo();
|
|
|
|
}
|
2016-05-11 17:09:18 +00:00
|
|
|
testCtx.reset(glCtx);
|
2014-11-13 19:12:41 +00:00
|
|
|
break;
|
2016-05-11 17:09:18 +00:00
|
|
|
}
|
2019-11-08 21:18:15 +00:00
|
|
|
#endif // SK_GL
|
2016-04-11 21:21:33 +00:00
|
|
|
#ifdef SK_VULKAN
|
2018-10-12 13:31:11 +00:00
|
|
|
case GrBackendApi::kVulkan: {
|
2017-05-15 17:50:35 +00:00
|
|
|
VkTestContext* vkSharedContext = masterContext
|
|
|
|
? static_cast<VkTestContext*>(masterContext->fTestContext) : nullptr;
|
2016-04-11 21:21:33 +00:00
|
|
|
SkASSERT(kVulkan_ContextType == type);
|
2017-05-15 17:50:35 +00:00
|
|
|
testCtx.reset(CreatePlatformVkTestContext(vkSharedContext));
|
2016-05-11 17:09:18 +00:00
|
|
|
if (!testCtx) {
|
2016-04-11 21:21:33 +00:00
|
|
|
return ContextInfo();
|
|
|
|
}
|
2016-05-11 17:09:18 +00:00
|
|
|
|
2019-11-08 21:18:15 +00:00
|
|
|
#ifdef SK_GL
|
2017-01-24 22:22:05 +00:00
|
|
|
// There is some bug (either in Skia or the NV Vulkan driver) where VkDevice
|
|
|
|
// destruction will hang occaisonally. For some reason having an existing GL
|
|
|
|
// context fixes this.
|
|
|
|
if (!fSentinelGLContext) {
|
|
|
|
fSentinelGLContext.reset(CreatePlatformGLTestContext(kGL_GrGLStandard));
|
|
|
|
if (!fSentinelGLContext) {
|
|
|
|
fSentinelGLContext.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 21:18:15 +00:00
|
|
|
#endif
|
2014-11-13 19:12:41 +00:00
|
|
|
break;
|
2017-05-15 17:50:35 +00:00
|
|
|
}
|
2017-07-13 19:07:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SK_METAL
|
2018-10-12 13:31:11 +00:00
|
|
|
case GrBackendApi::kMetal: {
|
2019-03-15 19:22:39 +00:00
|
|
|
MtlTestContext* mtlSharedContext = masterContext
|
|
|
|
? static_cast<MtlTestContext*>(masterContext->fTestContext) : nullptr;
|
|
|
|
SkASSERT(kMetal_ContextType == type);
|
|
|
|
testCtx.reset(CreatePlatformMtlTestContext(mtlSharedContext));
|
2017-07-13 19:07:54 +00:00
|
|
|
if (!testCtx) {
|
|
|
|
return ContextInfo();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-07-18 15:43:45 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SK_DAWN
|
|
|
|
case GrBackendApi::kDawn: {
|
|
|
|
DawnTestContext* dawnSharedContext = masterContext
|
|
|
|
? static_cast<DawnTestContext*>(masterContext->fTestContext) : nullptr;
|
|
|
|
testCtx.reset(CreatePlatformDawnTestContext(dawnSharedContext));
|
|
|
|
if (!testCtx) {
|
|
|
|
return ContextInfo();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-04-11 21:21:33 +00:00
|
|
|
#endif
|
2018-10-12 13:31:11 +00:00
|
|
|
case GrBackendApi::kMock: {
|
2017-07-06 20:40:18 +00:00
|
|
|
TestContext* sharedContext = masterContext ? masterContext->fTestContext : nullptr;
|
|
|
|
SkASSERT(kMock_ContextType == type);
|
|
|
|
testCtx.reset(CreateMockTestContext(sharedContext));
|
|
|
|
if (!testCtx) {
|
|
|
|
return ContextInfo();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-04-11 21:21:33 +00:00
|
|
|
default:
|
2016-01-07 07:49:30 +00:00
|
|
|
return ContextInfo();
|
2014-11-13 19:12:41 +00:00
|
|
|
}
|
2017-11-17 14:25:23 +00:00
|
|
|
|
2016-05-11 17:09:18 +00:00
|
|
|
SkASSERT(testCtx && testCtx->backend() == backend);
|
2016-07-29 15:14:20 +00:00
|
|
|
GrContextOptions grOptions = fGlobalOptions;
|
2017-05-08 19:02:07 +00:00
|
|
|
if (ContextOverrides::kAvoidStencilBuffers & overrides) {
|
|
|
|
grOptions.fAvoidStencilBuffers = true;
|
|
|
|
}
|
2017-11-17 14:25:23 +00:00
|
|
|
sk_sp<GrContext> grCtx;
|
|
|
|
{
|
|
|
|
auto restore = testCtx->makeCurrentAndAutoRestore();
|
|
|
|
grCtx = testCtx->makeGrContext(grOptions);
|
|
|
|
}
|
2014-11-13 19:12:41 +00:00
|
|
|
if (!grCtx.get()) {
|
2016-01-07 07:49:30 +00:00
|
|
|
return ContextInfo();
|
2014-11-13 19:12:41 +00:00
|
|
|
}
|
2015-07-01 09:58:50 +00:00
|
|
|
|
2018-01-16 16:20:41 +00:00
|
|
|
// We must always add new contexts by pushing to the back so that when we delete them we delete
|
|
|
|
// them in reverse order in which they were made.
|
2016-01-07 07:49:30 +00:00
|
|
|
Context& context = fContexts.push_back();
|
2016-05-11 17:09:18 +00:00
|
|
|
context.fBackend = backend;
|
|
|
|
context.fTestContext = testCtx.release();
|
2016-01-07 07:49:30 +00:00
|
|
|
context.fGrContext = SkRef(grCtx.get());
|
|
|
|
context.fType = type;
|
2017-02-21 19:36:05 +00:00
|
|
|
context.fOverrides = overrides;
|
2016-04-11 21:21:33 +00:00
|
|
|
context.fAbandoned = false;
|
2017-02-21 21:58:08 +00:00
|
|
|
context.fShareContext = shareContext;
|
|
|
|
context.fShareIndex = shareIndex;
|
2017-10-18 12:33:29 +00:00
|
|
|
context.fOptions = grOptions;
|
2017-11-17 14:25:23 +00:00
|
|
|
context.fTestContext->makeCurrent();
|
2017-10-18 12:33:29 +00:00
|
|
|
return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
|
2014-11-13 19:12:41 +00:00
|
|
|
}
|
2017-02-21 21:58:08 +00:00
|
|
|
|
2017-02-24 19:51:44 +00:00
|
|
|
ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
|
|
|
|
return this->getContextInfoInternal(type, overrides, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
|
|
|
|
SkASSERT(shareContext);
|
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
|
|
|
if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
|
|
|
|
return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
|
|
|
|
shareContext, shareIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ContextInfo();
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
} // namespace sk_gpu_test
|