blob: e08dbbdb96b5f399badf75ec70c5f7b86ac6d063 [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
Brian Salomoncfe910d2017-07-06 16:40:18 -040044 kMock_ContextType, //! Mock context that does not draw.
45 kLastContextType = kMock_ContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000046 };
47
bsalomon85b4b532016-04-05 11:06:27 -070048 static const int kContextTypeCnt = kLastContextType + 1;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000049
kkinnunen5219fd92015-12-10 06:28:13 -080050 /**
csmartdaltone812d492017-02-21 12:36:05 -070051 * Overrides for the initial GrContextOptions provided at construction time, and required
52 * features that will cause context creation to fail if not present.
kkinnunen5219fd92015-12-10 06:28:13 -080053 */
csmartdaltone812d492017-02-21 12:36:05 -070054 enum class ContextOverrides {
55 kNone = 0x0,
Chris Daltonb3c97452019-06-25 20:07:56 -060056 kAvoidStencilBuffers = 0x1,
kkinnunen5219fd92015-12-10 06:28:13 -080057 };
58
bsalomon85b4b532016-04-05 11:06:27 -070059 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000060 switch (type) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040061 case kMock_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +000062 return false;
63 default:
64 return true;
65 }
66 }
67
Greg Danielbdf12ad2018-10-12 09:31:11 -040068 static GrBackendApi ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -070069 switch (type) {
70 case kVulkan_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040071 return GrBackendApi::kVulkan;
Greg Daniel2811aa22017-07-13 15:34:56 -040072 case kMetal_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040073 return GrBackendApi::kMetal;
Brian Salomoncfe910d2017-07-06 16:40:18 -040074 case kMock_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040075 return GrBackendApi::kMock;
bsalomondc0fcd42016-04-11 14:21:33 -070076 default:
Greg Danielbdf12ad2018-10-12 09:31:11 -040077 return GrBackendApi::kOpenGL;
bsalomondc0fcd42016-04-11 14:21:33 -070078 }
bsalomon758586c2016-04-06 14:02:39 -070079 }
80
Greg Daniela5cb7812017-06-16 09:45:32 -040081 static const char* ContextTypeName(ContextType contextType) {
82 switch (contextType) {
83 case kGL_ContextType:
84 return "OpenGL";
85 case kGLES_ContextType:
86 return "OpenGLES";
87 case kANGLE_D3D9_ES2_ContextType:
88 return "ANGLE D3D9 ES2";
89 case kANGLE_D3D11_ES2_ContextType:
90 return "ANGLE D3D11 ES2";
91 case kANGLE_D3D11_ES3_ContextType:
92 return "ANGLE D3D11 ES3";
93 case kANGLE_GL_ES2_ContextType:
94 return "ANGLE GL ES2";
95 case kANGLE_GL_ES3_ContextType:
96 return "ANGLE GL ES3";
97 case kCommandBuffer_ContextType:
98 return "Command Buffer";
Greg Daniela5cb7812017-06-16 09:45:32 -040099 case kVulkan_ContextType:
100 return "Vulkan";
Greg Daniel2811aa22017-07-13 15:34:56 -0400101 case kMetal_ContextType:
102 return "Metal";
Brian Salomoncfe910d2017-07-06 16:40:18 -0400103 case kMock_ContextType:
104 return "Mock";
Greg Daniela5cb7812017-06-16 09:45:32 -0400105 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400106 SK_ABORT("Unreachable");
Greg Daniela5cb7812017-06-16 09:45:32 -0400107 return "Unknown";
108 }
109
kkinnunen34058002016-01-06 23:49:30 -0800110 explicit GrContextFactory(const GrContextOptions& opts);
111 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000112
kkinnunen34058002016-01-06 23:49:30 -0800113 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000114
kkinnunen34058002016-01-06 23:49:30 -0800115 void destroyContexts();
116 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700117 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700118
kkinnunen179a8f52015-11-20 13:32:24 -0800119 /**
120 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800121 */
Chris Daltonb3c97452019-06-25 20:07:56 -0600122 ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone);
Brian Osman9eac2ea2017-02-24 14:51:44 -0500123
124 /**
125 * Get a context in the same share group as the passed in GrContext, with the same type and
126 * overrides. To get multiple contexts in a single share group, pass the same shareContext,
127 * with different values for shareIndex.
128 */
129 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
130
bsalomon@google.com7361f542012-04-19 19:15:35 +0000131 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000132 * 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 +0000133 */
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400134 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
bsalomon682c2692015-05-22 14:01:46 -0700135 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700136
bsalomon@google.com7361f542012-04-19 19:15:35 +0000137private:
Brian Osman9eac2ea2017-02-24 14:51:44 -0500138 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
139 GrContext* shareContext, uint32_t shareIndex);
140
kkinnunen34058002016-01-06 23:49:30 -0800141 struct Context {
Robert Phillips08ba0852019-05-22 20:23:43 +0000142 ContextType fType;
143 ContextOverrides fOverrides;
144 GrContextOptions fOptions;
Robert Phillips27eb5252019-06-03 12:59:40 -0400145 GrBackendApi fBackend;
Robert Phillips08ba0852019-05-22 20:23:43 +0000146 TestContext* fTestContext;
147 GrContext* fGrContext;
148 GrContext* fShareContext;
149 uint32_t fShareIndex;
Brian Osman60c774d2017-02-21 16:58:08 -0500150
Robert Phillips27eb5252019-06-03 12:59:40 -0400151 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800152 };
bsalomondc0fcd42016-04-11 14:21:33 -0700153 SkTArray<Context, true> fContexts;
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000154 std::unique_ptr<GLTestContext> fSentinelGLContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700155 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000156};
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400157
158class ContextInfo {
159public:
160 ContextInfo() = default;
161 ContextInfo& operator=(const ContextInfo&) = default;
162
163 GrContextFactory::ContextType type() const { return fType; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400164 GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); }
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400165
166 GrContext* grContext() const { return fGrContext; }
167
168 TestContext* testContext() const { return fTestContext; }
169
170 GLTestContext* glContext() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400171 SkASSERT(GrBackendApi::kOpenGL == this->backend());
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400172 return static_cast<GLTestContext*>(fTestContext);
173 }
174
Brian Salomon43f8bf02017-10-18 08:33:29 -0400175 const GrContextOptions& options() const { return fOptions; }
176
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400177private:
Brian Salomon43f8bf02017-10-18 08:33:29 -0400178 ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext,
179 const GrContextOptions& options)
180 : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {}
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400181
182 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
183 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
Brian Salomon43f8bf02017-10-18 08:33:29 -0400184 TestContext* fTestContext = nullptr;
185 GrContext* fGrContext = nullptr;
186 GrContextOptions fOptions;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400187
188 friend class GrContextFactory;
189};
190
bsalomon3724e572016-03-30 18:56:19 -0700191} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700192
csmartdaltone812d492017-02-21 12:36:05 -0700193GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
csmartdalton6270e552016-09-13 10:41:49 -0700194
bsalomon@google.com7361f542012-04-19 19:15:35 +0000195#endif