blob: 7134c5c95968c1cd03800148cac8b232d308b197 [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"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000015#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000016
bsalomondc0fcd42016-04-11 14:21:33 -070017struct GrVkBackendContext;
18
bsalomon3724e572016-03-30 18:56:19 -070019namespace sk_gpu_test {
Robert Phillipscdabbcc2017-06-08 16:03:17 -040020class ContextInfo;
bsalomonf2f1c172016-04-05 12:59:06 -070021
bsalomon@google.com7361f542012-04-19 19:15:35 +000022/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000023 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000024 * GrContexts backed by different types of GL contexts. It manages creating the
25 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
26 * factory is destroyed (though the caller can always grab a ref on the returned
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000027 * Gr and GL contexts to make them outlive the factory).
bsalomon@google.com7361f542012-04-19 19:15:35 +000028 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000029class GrContextFactory : SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000030public:
bsalomondc0fcd42016-04-11 14:21:33 -070031 // The availability of context types is subject to platform and build configuration
32 // restrictions.
bsalomon85b4b532016-04-05 11:06:27 -070033 enum ContextType {
bsalomon11abd8d2016-10-14 08:13:48 -070034 kGL_ContextType, //! OpenGL context.
35 kGLES_ContextType, //! OpenGL ES context.
36 kANGLE_D3D9_ES2_ContextType, //! ANGLE on Direct3D9 OpenGL ES 2 context.
37 kANGLE_D3D11_ES2_ContextType,//! ANGLE on Direct3D11 OpenGL ES 2 context.
38 kANGLE_D3D11_ES3_ContextType,//! ANGLE on Direct3D11 OpenGL ES 3 context.
39 kANGLE_GL_ES2_ContextType, //! ANGLE on OpenGL OpenGL ES 2 context.
40 kANGLE_GL_ES3_ContextType, //! ANGLE on OpenGL OpenGL ES 3 context.
41 kCommandBuffer_ContextType, //! Chromium command buffer OpenGL ES context.
bsalomon11abd8d2016-10-14 08:13:48 -070042 kNullGL_ContextType, //! Non-rendering OpenGL mock context.
bsalomon11abd8d2016-10-14 08:13:48 -070043 kVulkan_ContextType, //! Vulkan
Greg Daniel2811aa22017-07-13 15:34:56 -040044 kMetal_ContextType, //! Metal
Brian Salomoncfe910d2017-07-06 16:40:18 -040045 kMock_ContextType, //! Mock context that does not draw.
46 kLastContextType = kMock_ContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000047 };
48
bsalomon85b4b532016-04-05 11:06:27 -070049 static const int kContextTypeCnt = kLastContextType + 1;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000050
kkinnunen5219fd92015-12-10 06:28:13 -080051 /**
csmartdaltone812d492017-02-21 12:36:05 -070052 * Overrides for the initial GrContextOptions provided at construction time, and required
53 * features that will cause context creation to fail if not present.
kkinnunen5219fd92015-12-10 06:28:13 -080054 */
csmartdaltone812d492017-02-21 12:36:05 -070055 enum class ContextOverrides {
56 kNone = 0x0,
57 kDisableNVPR = 0x1,
Brian Osman2b23c4b2018-06-01 12:25:08 -040058 kAvoidStencilBuffers = 0x2,
csmartdaltone812d492017-02-21 12:36:05 -070059
Brian Osman2b23c4b2018-06-01 12:25:08 -040060 kRequireNVPRSupport = 0x4,
kkinnunen5219fd92015-12-10 06:28:13 -080061 };
62
bsalomon85b4b532016-04-05 11:06:27 -070063 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000064 switch (type) {
bsalomon85b4b532016-04-05 11:06:27 -070065 case kNullGL_ContextType:
Brian Salomoncfe910d2017-07-06 16:40:18 -040066 case kMock_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +000067 return false;
68 default:
69 return true;
70 }
71 }
72
bsalomon758586c2016-04-06 14:02:39 -070073 static GrBackend ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -070074 switch (type) {
75 case kVulkan_ContextType:
76 return kVulkan_GrBackend;
Greg Daniel2811aa22017-07-13 15:34:56 -040077 case kMetal_ContextType:
78 return kMetal_GrBackend;
Brian Salomoncfe910d2017-07-06 16:40:18 -040079 case kMock_ContextType:
80 return kMock_GrBackend;
bsalomondc0fcd42016-04-11 14:21:33 -070081 default:
82 return kOpenGL_GrBackend;
83 }
bsalomon758586c2016-04-06 14:02:39 -070084 }
85
Greg Daniela5cb7812017-06-16 09:45:32 -040086 static const char* ContextTypeName(ContextType contextType) {
87 switch (contextType) {
88 case kGL_ContextType:
89 return "OpenGL";
90 case kGLES_ContextType:
91 return "OpenGLES";
92 case kANGLE_D3D9_ES2_ContextType:
93 return "ANGLE D3D9 ES2";
94 case kANGLE_D3D11_ES2_ContextType:
95 return "ANGLE D3D11 ES2";
96 case kANGLE_D3D11_ES3_ContextType:
97 return "ANGLE D3D11 ES3";
98 case kANGLE_GL_ES2_ContextType:
99 return "ANGLE GL ES2";
100 case kANGLE_GL_ES3_ContextType:
101 return "ANGLE GL ES3";
102 case kCommandBuffer_ContextType:
103 return "Command Buffer";
Greg Daniela5cb7812017-06-16 09:45:32 -0400104 case kNullGL_ContextType:
105 return "Null GL";
Greg Daniela5cb7812017-06-16 09:45:32 -0400106 case kVulkan_ContextType:
107 return "Vulkan";
Greg Daniel2811aa22017-07-13 15:34:56 -0400108 case kMetal_ContextType:
109 return "Metal";
Brian Salomoncfe910d2017-07-06 16:40:18 -0400110 case kMock_ContextType:
111 return "Mock";
Greg Daniela5cb7812017-06-16 09:45:32 -0400112 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400113 SK_ABORT("Unreachable");
Greg Daniela5cb7812017-06-16 09:45:32 -0400114 return "Unknown";
115 }
116
kkinnunen34058002016-01-06 23:49:30 -0800117 explicit GrContextFactory(const GrContextOptions& opts);
118 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000119
kkinnunen34058002016-01-06 23:49:30 -0800120 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000121
kkinnunen34058002016-01-06 23:49:30 -0800122 void destroyContexts();
123 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700124 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700125
kkinnunen179a8f52015-11-20 13:32:24 -0800126 /**
127 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800128 */
bsalomon85b4b532016-04-05 11:06:27 -0700129 ContextInfo getContextInfo(ContextType type,
Brian Osman9eac2ea2017-02-24 14:51:44 -0500130 ContextOverrides overrides = ContextOverrides::kNone);
131
132 /**
133 * Get a context in the same share group as the passed in GrContext, with the same type and
134 * overrides. To get multiple contexts in a single share group, pass the same shareContext,
135 * with different values for shareIndex.
136 */
137 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
138
bsalomon@google.com7361f542012-04-19 19:15:35 +0000139 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000140 * 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 +0000141 */
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400142 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
bsalomon682c2692015-05-22 14:01:46 -0700143 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700144
bsalomon@google.com7361f542012-04-19 19:15:35 +0000145private:
Brian Osman9eac2ea2017-02-24 14:51:44 -0500146 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
147 GrContext* shareContext, uint32_t shareIndex);
148
kkinnunen34058002016-01-06 23:49:30 -0800149 struct Context {
csmartdaltone812d492017-02-21 12:36:05 -0700150 ContextType fType;
151 ContextOverrides fOverrides;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400152 GrContextOptions fOptions;
csmartdaltone812d492017-02-21 12:36:05 -0700153 GrBackend fBackend;
154 TestContext* fTestContext;
155 GrContext* fGrContext;
Brian Osman60c774d2017-02-21 16:58:08 -0500156 GrContext* fShareContext;
157 uint32_t fShareIndex;
158
159 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800160 };
bsalomondc0fcd42016-04-11 14:21:33 -0700161 SkTArray<Context, true> fContexts;
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000162 std::unique_ptr<GLTestContext> fSentinelGLContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700163 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000164};
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400165
166class ContextInfo {
167public:
168 ContextInfo() = default;
169 ContextInfo& operator=(const ContextInfo&) = default;
170
171 GrContextFactory::ContextType type() const { return fType; }
172 GrBackend backend() const { return GrContextFactory::ContextTypeBackend(fType); }
173
174 GrContext* grContext() const { return fGrContext; }
175
176 TestContext* testContext() const { return fTestContext; }
177
178 GLTestContext* glContext() const {
179 SkASSERT(kOpenGL_GrBackend == this->backend());
180 return static_cast<GLTestContext*>(fTestContext);
181 }
182
Brian Salomon43f8bf02017-10-18 08:33:29 -0400183 const GrContextOptions& options() const { return fOptions; }
184
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400185private:
Brian Salomon43f8bf02017-10-18 08:33:29 -0400186 ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext,
187 const GrContextOptions& options)
188 : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {}
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400189
190 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
191 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
Brian Salomon43f8bf02017-10-18 08:33:29 -0400192 TestContext* fTestContext = nullptr;
193 GrContext* fGrContext = nullptr;
194 GrContextOptions fOptions;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400195
196 friend class GrContextFactory;
197};
198
bsalomon3724e572016-03-30 18:56:19 -0700199} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700200
csmartdaltone812d492017-02-21 12:36:05 -0700201GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
csmartdalton6270e552016-09-13 10:41:49 -0700202
bsalomon@google.com7361f542012-04-19 19:15:35 +0000203#endif