blob: 8bb9155fda8e47b7c99bfc484b87eaf5ded3668d [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,
56 kDisableNVPR = 0x1,
Brian Osman2b23c4b2018-06-01 12:25:08 -040057 kAvoidStencilBuffers = 0x2,
csmartdaltone812d492017-02-21 12:36:05 -070058
Brian Osman2b23c4b2018-06-01 12:25:08 -040059 kRequireNVPRSupport = 0x4,
kkinnunen5219fd92015-12-10 06:28:13 -080060 };
61
bsalomon85b4b532016-04-05 11:06:27 -070062 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000063 switch (type) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040064 case kMock_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +000065 return false;
66 default:
67 return true;
68 }
69 }
70
Greg Danielbdf12ad2018-10-12 09:31:11 -040071 static GrBackendApi ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -070072 switch (type) {
73 case kVulkan_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040074 return GrBackendApi::kVulkan;
Greg Daniel2811aa22017-07-13 15:34:56 -040075 case kMetal_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040076 return GrBackendApi::kMetal;
Brian Salomoncfe910d2017-07-06 16:40:18 -040077 case kMock_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040078 return GrBackendApi::kMock;
bsalomondc0fcd42016-04-11 14:21:33 -070079 default:
Greg Danielbdf12ad2018-10-12 09:31:11 -040080 return GrBackendApi::kOpenGL;
bsalomondc0fcd42016-04-11 14:21:33 -070081 }
bsalomon758586c2016-04-06 14:02:39 -070082 }
83
Greg Daniela5cb7812017-06-16 09:45:32 -040084 static const char* ContextTypeName(ContextType contextType) {
85 switch (contextType) {
86 case kGL_ContextType:
87 return "OpenGL";
88 case kGLES_ContextType:
89 return "OpenGLES";
90 case kANGLE_D3D9_ES2_ContextType:
91 return "ANGLE D3D9 ES2";
92 case kANGLE_D3D11_ES2_ContextType:
93 return "ANGLE D3D11 ES2";
94 case kANGLE_D3D11_ES3_ContextType:
95 return "ANGLE D3D11 ES3";
96 case kANGLE_GL_ES2_ContextType:
97 return "ANGLE GL ES2";
98 case kANGLE_GL_ES3_ContextType:
99 return "ANGLE GL ES3";
100 case kCommandBuffer_ContextType:
101 return "Command Buffer";
Greg Daniela5cb7812017-06-16 09:45:32 -0400102 case kVulkan_ContextType:
103 return "Vulkan";
Greg Daniel2811aa22017-07-13 15:34:56 -0400104 case kMetal_ContextType:
105 return "Metal";
Brian Salomoncfe910d2017-07-06 16:40:18 -0400106 case kMock_ContextType:
107 return "Mock";
Greg Daniela5cb7812017-06-16 09:45:32 -0400108 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400109 SK_ABORT("Unreachable");
Greg Daniela5cb7812017-06-16 09:45:32 -0400110 return "Unknown";
111 }
112
kkinnunen34058002016-01-06 23:49:30 -0800113 explicit GrContextFactory(const GrContextOptions& opts);
114 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000115
kkinnunen34058002016-01-06 23:49:30 -0800116 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000117
kkinnunen34058002016-01-06 23:49:30 -0800118 void destroyContexts();
119 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700120 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700121
kkinnunen179a8f52015-11-20 13:32:24 -0800122 /**
123 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800124 */
bsalomon85b4b532016-04-05 11:06:27 -0700125 ContextInfo getContextInfo(ContextType type,
Brian Osman9eac2ea2017-02-24 14:51:44 -0500126 ContextOverrides overrides = ContextOverrides::kNone);
127
128 /**
129 * Get a context in the same share group as the passed in GrContext, with the same type and
130 * overrides. To get multiple contexts in a single share group, pass the same shareContext,
131 * with different values for shareIndex.
132 */
133 ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
134
bsalomon@google.com7361f542012-04-19 19:15:35 +0000135 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000136 * 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 +0000137 */
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400138 GrContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
bsalomon682c2692015-05-22 14:01:46 -0700139 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700140
bsalomon@google.com7361f542012-04-19 19:15:35 +0000141private:
Brian Osman9eac2ea2017-02-24 14:51:44 -0500142 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
143 GrContext* shareContext, uint32_t shareIndex);
144
kkinnunen34058002016-01-06 23:49:30 -0800145 struct Context {
Robert Phillips08ba0852019-05-22 20:23:43 +0000146 ContextType fType;
147 ContextOverrides fOverrides;
148 GrContextOptions fOptions;
Robert Phillips27eb5252019-06-03 12:59:40 -0400149 GrBackendApi fBackend;
Robert Phillips08ba0852019-05-22 20:23:43 +0000150 TestContext* fTestContext;
151 GrContext* fGrContext;
152 GrContext* fShareContext;
153 uint32_t fShareIndex;
Brian Osman60c774d2017-02-21 16:58:08 -0500154
Robert Phillips27eb5252019-06-03 12:59:40 -0400155 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800156 };
bsalomondc0fcd42016-04-11 14:21:33 -0700157 SkTArray<Context, true> fContexts;
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000158 std::unique_ptr<GLTestContext> fSentinelGLContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700159 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000160};
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400161
162class ContextInfo {
163public:
164 ContextInfo() = default;
165 ContextInfo& operator=(const ContextInfo&) = default;
166
167 GrContextFactory::ContextType type() const { return fType; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400168 GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); }
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400169
170 GrContext* grContext() const { return fGrContext; }
171
172 TestContext* testContext() const { return fTestContext; }
173
174 GLTestContext* glContext() const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400175 SkASSERT(GrBackendApi::kOpenGL == this->backend());
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400176 return static_cast<GLTestContext*>(fTestContext);
177 }
178
Brian Salomon43f8bf02017-10-18 08:33:29 -0400179 const GrContextOptions& options() const { return fOptions; }
180
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400181private:
Brian Salomon43f8bf02017-10-18 08:33:29 -0400182 ContextInfo(GrContextFactory::ContextType type, TestContext* testContext, GrContext* grContext,
183 const GrContextOptions& options)
184 : fType(type), fTestContext(testContext), fGrContext(grContext), fOptions(options) {}
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400185
186 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
187 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
Brian Salomon43f8bf02017-10-18 08:33:29 -0400188 TestContext* fTestContext = nullptr;
189 GrContext* fGrContext = nullptr;
190 GrContextOptions fOptions;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400191
192 friend class GrContextFactory;
193};
194
bsalomon3724e572016-03-30 18:56:19 -0700195} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700196
csmartdaltone812d492017-02-21 12:36:05 -0700197GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
csmartdalton6270e552016-09-13 10:41:49 -0700198
bsalomon@google.com7361f542012-04-19 19:15:35 +0000199#endif