blob: eb54b50bbbfb80cc84a9f0a163cc79a3ec499e2e [file] [log] [blame]
bsalomon@google.com7361f542012-04-19 19:15:35 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrContextFactory_DEFINED
9#define GrContextFactory_DEFINED
10
bsalomon@google.com7361f542012-04-19 19:15:35 +000011#include "GrContext.h"
bsalomon682c2692015-05-22 14:01:46 -070012#include "GrContextOptions.h"
djsollene4545212014-11-13 11:12:41 -080013
bsalomon273c0f52016-03-31 10:59:06 -070014#include "gl/GLTestContext.h"
bsalomon18a2f9d2016-05-11 10:09:18 -070015#include "vk/VkTestContext.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000016#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000017
bsalomondc0fcd42016-04-11 14:21:33 -070018struct GrVkBackendContext;
19
bsalomon3724e572016-03-30 18:56:19 -070020namespace sk_gpu_test {
bsalomonf2f1c172016-04-05 12:59:06 -070021
bsalomon8b7451a2016-05-11 06:33:06 -070022class ContextInfo {
23public:
bsalomon18a2f9d2016-05-11 10:09:18 -070024 ContextInfo() = default;
25 ContextInfo& operator=(const ContextInfo&) = default;
26
Mike Kleinfc6c37b2016-09-27 09:34:10 -040027 GrBackend backend() const { return fBackend; }
bsalomon18a2f9d2016-05-11 10:09:18 -070028
bsalomon8b7451a2016-05-11 06:33:06 -070029 GrContext* grContext() const { return fGrContext; }
bsalomon18a2f9d2016-05-11 10:09:18 -070030
bsalomon0fd3c812016-05-11 10:38:05 -070031 TestContext* testContext() const { return fTestContext; }
32
bsalomon18a2f9d2016-05-11 10:09:18 -070033 GLTestContext* glContext() const {
34 SkASSERT(kOpenGL_GrBackend == fBackend);
35 return static_cast<GLTestContext*>(fTestContext);
36 }
37
38#ifdef SK_VULKAN
39 VkTestContext* vkContext() const {
40 SkASSERT(kVulkan_GrBackend == fBackend);
41 return static_cast<VkTestContext*>(fTestContext);
42 }
43#endif
bsalomon8b7451a2016-05-11 06:33:06 -070044
45private:
bsalomon18a2f9d2016-05-11 10:09:18 -070046 ContextInfo(GrBackend backend, TestContext* testContext, GrContext* grContext)
47 : fBackend(backend)
48 , fTestContext(testContext)
49 , fGrContext(grContext) {}
50
51 GrBackend fBackend = kOpenGL_GrBackend;
52 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
53 TestContext* fTestContext = nullptr;
54 GrContext* fGrContext = nullptr;
bsalomon8b7451a2016-05-11 06:33:06 -070055
56 friend class GrContextFactory;
bsalomonf2f1c172016-04-05 12:59:06 -070057};
58
bsalomon@google.com7361f542012-04-19 19:15:35 +000059/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000060 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000061 * GrContexts backed by different types of GL contexts. It manages creating the
62 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
63 * factory is destroyed (though the caller can always grab a ref on the returned
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000064 * Gr and GL contexts to make them outlive the factory).
bsalomon@google.com7361f542012-04-19 19:15:35 +000065 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000066class GrContextFactory : SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000067public:
bsalomondc0fcd42016-04-11 14:21:33 -070068 // The availability of context types is subject to platform and build configuration
69 // restrictions.
bsalomon85b4b532016-04-05 11:06:27 -070070 enum ContextType {
bsalomon11abd8d2016-10-14 08:13:48 -070071 kGL_ContextType, //! OpenGL context.
72 kGLES_ContextType, //! OpenGL ES context.
73 kANGLE_D3D9_ES2_ContextType, //! ANGLE on Direct3D9 OpenGL ES 2 context.
74 kANGLE_D3D11_ES2_ContextType,//! ANGLE on Direct3D11 OpenGL ES 2 context.
75 kANGLE_D3D11_ES3_ContextType,//! ANGLE on Direct3D11 OpenGL ES 3 context.
76 kANGLE_GL_ES2_ContextType, //! ANGLE on OpenGL OpenGL ES 2 context.
77 kANGLE_GL_ES3_ContextType, //! ANGLE on OpenGL OpenGL ES 3 context.
78 kCommandBuffer_ContextType, //! Chromium command buffer OpenGL ES context.
79 kMESA_ContextType, //! MESA OpenGL context
80 kNullGL_ContextType, //! Non-rendering OpenGL mock context.
81 kDebugGL_ContextType, //! Non-rendering, state verifying OpenGL context.
82 kVulkan_ContextType, //! Vulkan
bsalomondc0fcd42016-04-11 14:21:33 -070083 kLastContextType = kVulkan_ContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000084 };
85
bsalomon85b4b532016-04-05 11:06:27 -070086 static const int kContextTypeCnt = kLastContextType + 1;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000087
kkinnunen5219fd92015-12-10 06:28:13 -080088 /**
csmartdaltone812d492017-02-21 12:36:05 -070089 * Overrides for the initial GrContextOptions provided at construction time, and required
90 * features that will cause context creation to fail if not present.
kkinnunen5219fd92015-12-10 06:28:13 -080091 */
csmartdaltone812d492017-02-21 12:36:05 -070092 enum class ContextOverrides {
93 kNone = 0x0,
94 kDisableNVPR = 0x1,
95 kUseInstanced = 0x2,
96 kAllowSRGBWithoutDecodeControl = 0x4,
Eric Karl5c779752017-05-08 12:02:07 -070097 kAvoidStencilBuffers = 0x8,
csmartdaltone812d492017-02-21 12:36:05 -070098
Eric Karl5c779752017-05-08 12:02:07 -070099 kRequireNVPRSupport = 0x10,
100 kRequireSRGBSupport = 0x20,
kkinnunen5219fd92015-12-10 06:28:13 -0800101 };
102
bsalomon85b4b532016-04-05 11:06:27 -0700103 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000104 switch (type) {
bsalomon85b4b532016-04-05 11:06:27 -0700105 case kNullGL_ContextType:
106 case kDebugGL_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000107 return false;
108 default:
109 return true;
110 }
111 }
112
bsalomon758586c2016-04-06 14:02:39 -0700113 static GrBackend ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -0700114 switch (type) {
115 case kVulkan_ContextType:
116 return kVulkan_GrBackend;
117 default:
118 return kOpenGL_GrBackend;
119 }
bsalomon758586c2016-04-06 14:02:39 -0700120 }
121
kkinnunen34058002016-01-06 23:49:30 -0800122 explicit GrContextFactory(const GrContextOptions& opts);
123 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000124
kkinnunen34058002016-01-06 23:49:30 -0800125 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000126
kkinnunen34058002016-01-06 23:49:30 -0800127 void destroyContexts();
128 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700129 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700130
kkinnunen179a8f52015-11-20 13:32:24 -0800131 /**
132 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800133 */
bsalomon85b4b532016-04-05 11:06:27 -0700134 ContextInfo getContextInfo(ContextType type,
Brian Osman9eac2ea2017-02-24 14:51:44 -0500135 ContextOverrides overrides = ContextOverrides::kNone);
136
137 /**
138 * Get a context in the same share group as the passed in GrContext, with the same type and
139 * overrides. To get multiple contexts in a single share group, pass the same shareContext,
140 * with different values for shareIndex.
141 */
142 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
143
bsalomon@google.com7361f542012-04-19 19:15:35 +0000144 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000145 * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
bsalomon@google.com7361f542012-04-19 19:15:35 +0000146 */
csmartdaltone812d492017-02-21 12:36:05 -0700147 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone) {
148 return this->getContextInfo(type, overrides).grContext();
kkinnunen179a8f52015-11-20 13:32:24 -0800149 }
bsalomon682c2692015-05-22 14:01:46 -0700150 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700151
bsalomon@google.com7361f542012-04-19 19:15:35 +0000152private:
Brian Osman9eac2ea2017-02-24 14:51:44 -0500153 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
154 GrContext* shareContext, uint32_t shareIndex);
155
kkinnunen34058002016-01-06 23:49:30 -0800156 struct Context {
csmartdaltone812d492017-02-21 12:36:05 -0700157 ContextType fType;
158 ContextOverrides fOverrides;
159 GrBackend fBackend;
160 TestContext* fTestContext;
161 GrContext* fGrContext;
Brian Osman60c774d2017-02-21 16:58:08 -0500162 GrContext* fShareContext;
163 uint32_t fShareIndex;
164
165 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800166 };
bsalomondc0fcd42016-04-11 14:21:33 -0700167 SkTArray<Context, true> fContexts;
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000168 std::unique_ptr<GLTestContext> fSentinelGLContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700169 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000170};
bsalomon3724e572016-03-30 18:56:19 -0700171} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700172
csmartdaltone812d492017-02-21 12:36:05 -0700173GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
csmartdalton6270e552016-09-13 10:41:49 -0700174
bsalomon@google.com7361f542012-04-19 19:15:35 +0000175#endif