blob: d0d76c34a75aae02ba53bcebec17595359fc5223 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContext.h"
12#include "include/gpu/GrContextOptions.h"
djsollene4545212014-11-13 11:12:41 -080013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTArray.h"
15#include "tools/gpu/gl/GLTestContext.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 kVulkan_ContextType, //! Vulkan
Greg Daniel2811aa22017-07-13 15:34:56 -040043 kMetal_ContextType, //! Metal
Jim Van Verthb01e12b2020-02-18 14:34:38 -050044 kDirect3D_ContextType, //! Direct3D 12
Stephen White985741a2019-07-18 11:43:45 -040045 kDawn_ContextType, //! Dawn
Brian Salomoncfe910d2017-07-06 16:40:18 -040046 kMock_ContextType, //! Mock context that does not draw.
47 kLastContextType = kMock_ContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000048 };
49
bsalomon85b4b532016-04-05 11:06:27 -070050 static const int kContextTypeCnt = kLastContextType + 1;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000051
kkinnunen5219fd92015-12-10 06:28:13 -080052 /**
csmartdaltone812d492017-02-21 12:36:05 -070053 * Overrides for the initial GrContextOptions provided at construction time, and required
54 * features that will cause context creation to fail if not present.
kkinnunen5219fd92015-12-10 06:28:13 -080055 */
csmartdaltone812d492017-02-21 12:36:05 -070056 enum class ContextOverrides {
57 kNone = 0x0,
Chris Daltonb3c97452019-06-25 20:07:56 -060058 kAvoidStencilBuffers = 0x1,
kkinnunen5219fd92015-12-10 06:28:13 -080059 };
60
bsalomon85b4b532016-04-05 11:06:27 -070061 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000062 switch (type) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040063 case kMock_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +000064 return false;
65 default:
66 return true;
67 }
68 }
69
Greg Danielbdf12ad2018-10-12 09:31:11 -040070 static GrBackendApi ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -070071 switch (type) {
72 case kVulkan_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040073 return GrBackendApi::kVulkan;
Greg Daniel2811aa22017-07-13 15:34:56 -040074 case kMetal_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040075 return GrBackendApi::kMetal;
Jim Van Verthb01e12b2020-02-18 14:34:38 -050076 case kDirect3D_ContextType:
77 return GrBackendApi::kDirect3D;
Stephen White985741a2019-07-18 11:43:45 -040078 case kDawn_ContextType:
79 return GrBackendApi::kDawn;
Brian Salomoncfe910d2017-07-06 16:40:18 -040080 case kMock_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040081 return GrBackendApi::kMock;
bsalomondc0fcd42016-04-11 14:21:33 -070082 default:
Greg Danielbdf12ad2018-10-12 09:31:11 -040083 return GrBackendApi::kOpenGL;
bsalomondc0fcd42016-04-11 14:21:33 -070084 }
bsalomon758586c2016-04-06 14:02:39 -070085 }
86
Greg Daniela5cb7812017-06-16 09:45:32 -040087 static const char* ContextTypeName(ContextType contextType) {
88 switch (contextType) {
89 case kGL_ContextType:
90 return "OpenGL";
91 case kGLES_ContextType:
92 return "OpenGLES";
93 case kANGLE_D3D9_ES2_ContextType:
94 return "ANGLE D3D9 ES2";
95 case kANGLE_D3D11_ES2_ContextType:
96 return "ANGLE D3D11 ES2";
97 case kANGLE_D3D11_ES3_ContextType:
98 return "ANGLE D3D11 ES3";
99 case kANGLE_GL_ES2_ContextType:
100 return "ANGLE GL ES2";
101 case kANGLE_GL_ES3_ContextType:
102 return "ANGLE GL ES3";
103 case kCommandBuffer_ContextType:
104 return "Command Buffer";
Greg Daniela5cb7812017-06-16 09:45:32 -0400105 case kVulkan_ContextType:
106 return "Vulkan";
Greg Daniel2811aa22017-07-13 15:34:56 -0400107 case kMetal_ContextType:
108 return "Metal";
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500109 case kDirect3D_ContextType:
110 return "Direct3D";
Stephen White985741a2019-07-18 11:43:45 -0400111 case kDawn_ContextType:
112 return "Dawn";
Brian Salomoncfe910d2017-07-06 16:40:18 -0400113 case kMock_ContextType:
114 return "Mock";
Greg Daniela5cb7812017-06-16 09:45:32 -0400115 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400116 SK_ABORT("Unreachable");
Greg Daniela5cb7812017-06-16 09:45:32 -0400117 }
118
kkinnunen34058002016-01-06 23:49:30 -0800119 explicit GrContextFactory(const GrContextOptions& opts);
120 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000121
kkinnunen34058002016-01-06 23:49:30 -0800122 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000123
kkinnunen34058002016-01-06 23:49:30 -0800124 void destroyContexts();
125 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700126 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700127
kkinnunen179a8f52015-11-20 13:32:24 -0800128 /**
129 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800130 */
Chris Daltonb3c97452019-06-25 20:07:56 -0600131 ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone);
Brian Osman9eac2ea2017-02-24 14:51:44 -0500132
133 /**
134 * Get a context in the same share group as the passed in GrContext, with the same type and
135 * overrides. To get multiple contexts in a single share group, pass the same shareContext,
136 * with different values for shareIndex.
137 */
138 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
139
bsalomon@google.com7361f542012-04-19 19:15:35 +0000140 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000141 * 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 +0000142 */
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400143 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
bsalomon682c2692015-05-22 14:01:46 -0700144 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700145
bsalomon@google.com7361f542012-04-19 19:15:35 +0000146private:
Brian Osman9eac2ea2017-02-24 14:51:44 -0500147 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
148 GrContext* shareContext, uint32_t shareIndex);
149
kkinnunen34058002016-01-06 23:49:30 -0800150 struct Context {
Robert Phillips08ba0852019-05-22 20:23:43 +0000151 ContextType fType;
152 ContextOverrides fOverrides;
153 GrContextOptions fOptions;
Robert Phillips27eb5252019-06-03 12:59:40 -0400154 GrBackendApi fBackend;
Robert Phillips08ba0852019-05-22 20:23:43 +0000155 TestContext* fTestContext;
156 GrContext* fGrContext;
157 GrContext* fShareContext;
158 uint32_t fShareIndex;
Brian Osman60c774d2017-02-21 16:58:08 -0500159
Robert Phillips27eb5252019-06-03 12:59:40 -0400160 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800161 };
bsalomondc0fcd42016-04-11 14:21:33 -0700162 SkTArray<Context, true> fContexts;
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000163 std::unique_ptr<GLTestContext> fSentinelGLContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700164 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000165};
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400166
167class ContextInfo {
168public:
169 ContextInfo() = default;
170 ContextInfo& operator=(const ContextInfo&) = default;
171
172 GrContextFactory::ContextType type() const { return fType; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400173 GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); }
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400174
175 GrContext* grContext() const { return fGrContext; }
176
177 TestContext* testContext() const { return fTestContext; }
178
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400179#ifdef SK_GL
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400180 GLTestContext* glContext() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400181 SkASSERT(GrBackendApi::kOpenGL == this->backend());
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400182 return static_cast<GLTestContext*>(fTestContext);
183 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400184#endif
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400185
Brian Salomon43f8bf02017-10-18 08:33:29 -0400186 const GrContextOptions& options() const { return fOptions; }
187
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400188private:
Brian Salomon43f8bf02017-10-18 08:33:29 -0400189 ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext,
190 const GrContextOptions& options)
191 : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {}
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400192
193 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
194 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
Brian Salomon43f8bf02017-10-18 08:33:29 -0400195 TestContext* fTestContext = nullptr;
196 GrContext* fGrContext = nullptr;
197 GrContextOptions fOptions;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400198
199 friend class GrContextFactory;
200};
201
bsalomon3724e572016-03-30 18:56:19 -0700202} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700203
csmartdaltone812d492017-02-21 12:36:05 -0700204GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
csmartdalton6270e552016-09-13 10:41:49 -0700205
bsalomon@google.com7361f542012-04-19 19:15:35 +0000206#endif