skia2/tools/gpu/GrContextFactory.h

175 lines
5.9 KiB
C
Raw Normal View History

/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrContextFactory_DEFINED
#define GrContextFactory_DEFINED
#include "GrContext.h"
#include "GrContextOptions.h"
#include "gl/GLTestContext.h"
#include "vk/VkTestContext.h"
#include "SkTArray.h"
struct GrVkBackendContext;
namespace sk_gpu_test {
class ContextInfo {
public:
ContextInfo() = default;
ContextInfo& operator=(const ContextInfo&) = default;
GrBackend backend() const { return fBackend; }
GrContext* grContext() const { return fGrContext; }
TestContext* testContext() const { return fTestContext; }
GLTestContext* glContext() const {
SkASSERT(kOpenGL_GrBackend == fBackend);
return static_cast<GLTestContext*>(fTestContext);
}
#ifdef SK_VULKAN
VkTestContext* vkContext() const {
SkASSERT(kVulkan_GrBackend == fBackend);
return static_cast<VkTestContext*>(fTestContext);
}
#endif
private:
ContextInfo(GrBackend backend, TestContext* testContext, GrContext* grContext)
: fBackend(backend)
, fTestContext(testContext)
, fGrContext(grContext) {}
GrBackend fBackend = kOpenGL_GrBackend;
// Valid until the factory destroys it via abandonContexts() or destroyContexts().
TestContext* fTestContext = nullptr;
GrContext* fGrContext = nullptr;
friend class GrContextFactory;
};
/**
* This is a simple class that is useful in test apps that use different
* GrContexts backed by different types of GL contexts. It manages creating the
* GL context and a GrContext that uses it. The GL/Gr contexts persist until the
* factory is destroyed (though the caller can always grab a ref on the returned
* Gr and GL contexts to make them outlive the factory).
*/
class GrContextFactory : SkNoncopyable {
public:
// The availability of context types is subject to platform and build configuration
// restrictions.
enum ContextType {
kGL_ContextType, //! OpenGL context.
kGLES_ContextType, //! OpenGL ES context.
kANGLE_D3D9_ES2_ContextType, //! ANGLE on Direct3D9 OpenGL ES 2 context.
kANGLE_D3D11_ES2_ContextType,//! ANGLE on Direct3D11 OpenGL ES 2 context.
kANGLE_D3D11_ES3_ContextType,//! ANGLE on Direct3D11 OpenGL ES 3 context.
kANGLE_GL_ES2_ContextType, //! ANGLE on OpenGL OpenGL ES 2 context.
kANGLE_GL_ES3_ContextType, //! ANGLE on OpenGL OpenGL ES 3 context.
kCommandBuffer_ContextType, //! Chromium command buffer OpenGL ES context.
kMESA_ContextType, //! MESA OpenGL context
kNullGL_ContextType, //! Non-rendering OpenGL mock context.
kDebugGL_ContextType, //! Non-rendering, state verifying OpenGL context.
kVulkan_ContextType, //! Vulkan
kLastContextType = kVulkan_ContextType
};
static const int kContextTypeCnt = kLastContextType + 1;
/**
* Overrides for the initial GrContextOptions provided at construction time, and required
* features that will cause context creation to fail if not present.
*/
enum class ContextOverrides {
kNone = 0x0,
kDisableNVPR = 0x1,
kUseInstanced = 0x2,
kAllowSRGBWithoutDecodeControl = 0x4,
kRequireNVPRSupport = 0x8,
kRequireSRGBSupport = 0x10
};
static bool IsRenderingContext(ContextType type) {
switch (type) {
case kNullGL_ContextType:
case kDebugGL_ContextType:
return false;
default:
return true;
}
}
static GrBackend ContextTypeBackend(ContextType type) {
switch (type) {
case kVulkan_ContextType:
return kVulkan_GrBackend;
default:
return kOpenGL_GrBackend;
}
}
explicit GrContextFactory(const GrContextOptions& opts);
GrContextFactory();
~GrContextFactory();
void destroyContexts();
void abandonContexts();
void releaseResourcesAndAbandonContexts();
/**
Generate list of GPU contexts outside SurfaceTest tests Add support for feeding the tests with contexts directly to the unit test framework. This fixes the problem where tests are more complex than needed just in order to run the test code with multiple backends. Also makes it possible to change the logic how contexts are created. Instead of direct numbering, the different testable contexts may be generated from filtered cross-product of context options. For example: currently NVPR is a type of context. However, it could be also an on/off feature of any context. In order to test this kind of context, the enumeration can not be just of context type. It's simpler to move the enumeration out of the tests. A test targeting both normal and GPU backends would look like: static void test_obj_behavior(skiatest::Reporter* reporter, SkObj* obj, [other params] ) { ... test with obj and param .. } DEF_TEST(ObjBehavior, reporter) { for (auto& object : generate_object) { for (auto& other_param : generate_other_variant) { test_obj_behavior(reporter, object, other_param); } } } #if SK_SUPPORT_GPU DEF_GPUTEST_FOR_ALL_CONTEXTS(ObjBehavior_Gpu, reporter, context) { for (auto& object : generate_gpu_object) { for (auto& other_param : generate_other_variant) { test_obj_behavior(reporter, object, other_param); } } } #endif Uses the feature in SurfaceTests as an example. Moves SkSurface -related tests from ImageTest to SurfaceTest. BUG=skia:2992 Review URL: https://codereview.chromium.org/1446453003
2015-11-20 21:32:24 +00:00
* Get a context initialized with a type of GL context. It also makes the GL context current.
*/
ContextInfo getContextInfo(ContextType type,
ContextOverrides overrides = ContextOverrides::kNone);
/**
* Get a context in the same share group as the passed in GrContext, with the same type and
* overrides. To get multiple contexts in a single share group, pass the same shareContext,
* with different values for shareIndex.
*/
ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
Generate list of GPU contexts outside SurfaceTest tests Add support for feeding the tests with contexts directly to the unit test framework. This fixes the problem where tests are more complex than needed just in order to run the test code with multiple backends. Also makes it possible to change the logic how contexts are created. Instead of direct numbering, the different testable contexts may be generated from filtered cross-product of context options. For example: currently NVPR is a type of context. However, it could be also an on/off feature of any context. In order to test this kind of context, the enumeration can not be just of context type. It's simpler to move the enumeration out of the tests. A test targeting both normal and GPU backends would look like: static void test_obj_behavior(skiatest::Reporter* reporter, SkObj* obj, [other params] ) { ... test with obj and param .. } DEF_TEST(ObjBehavior, reporter) { for (auto& object : generate_object) { for (auto& other_param : generate_other_variant) { test_obj_behavior(reporter, object, other_param); } } } #if SK_SUPPORT_GPU DEF_GPUTEST_FOR_ALL_CONTEXTS(ObjBehavior_Gpu, reporter, context) { for (auto& object : generate_gpu_object) { for (auto& other_param : generate_other_variant) { test_obj_behavior(reporter, object, other_param); } } } #endif Uses the feature in SurfaceTests as an example. Moves SkSurface -related tests from ImageTest to SurfaceTest. BUG=skia:2992 Review URL: https://codereview.chromium.org/1446453003
2015-11-20 21:32:24 +00:00
/**
* Get a GrContext initialized with a type of GL context. It also makes the GL context current.
*/
GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone) {
return this->getContextInfo(type, overrides).grContext();
Generate list of GPU contexts outside SurfaceTest tests Add support for feeding the tests with contexts directly to the unit test framework. This fixes the problem where tests are more complex than needed just in order to run the test code with multiple backends. Also makes it possible to change the logic how contexts are created. Instead of direct numbering, the different testable contexts may be generated from filtered cross-product of context options. For example: currently NVPR is a type of context. However, it could be also an on/off feature of any context. In order to test this kind of context, the enumeration can not be just of context type. It's simpler to move the enumeration out of the tests. A test targeting both normal and GPU backends would look like: static void test_obj_behavior(skiatest::Reporter* reporter, SkObj* obj, [other params] ) { ... test with obj and param .. } DEF_TEST(ObjBehavior, reporter) { for (auto& object : generate_object) { for (auto& other_param : generate_other_variant) { test_obj_behavior(reporter, object, other_param); } } } #if SK_SUPPORT_GPU DEF_GPUTEST_FOR_ALL_CONTEXTS(ObjBehavior_Gpu, reporter, context) { for (auto& object : generate_gpu_object) { for (auto& other_param : generate_other_variant) { test_obj_behavior(reporter, object, other_param); } } } #endif Uses the feature in SurfaceTests as an example. Moves SkSurface -related tests from ImageTest to SurfaceTest. BUG=skia:2992 Review URL: https://codereview.chromium.org/1446453003
2015-11-20 21:32:24 +00:00
}
const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
private:
ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
GrContext* shareContext, uint32_t shareIndex);
struct Context {
ContextType fType;
ContextOverrides fOverrides;
GrBackend fBackend;
TestContext* fTestContext;
GrContext* fGrContext;
GrContext* fShareContext;
uint32_t fShareIndex;
bool fAbandoned;
};
SkTArray<Context, true> fContexts;
std::unique_ptr<GLTestContext> fSentinelGLContext;
const GrContextOptions fGlobalOptions;
};
} // namespace sk_gpu_test
GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
#endif