blob: 38471ce09644ebe39e76f321e25acc4b95de580f [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/GrContextOptions.h"
Robert Phillips00f78de2020-07-01 16:09:43 -040012#include "include/gpu/GrDirectContext.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,
Brian Salomonb2b7f802020-08-26 15:27:03 -040059 kFakeGLESVersionAs2 = 0x2,
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;
Jim Van Verthb01e12b2020-02-18 14:34:38 -050077 case kDirect3D_ContextType:
78 return GrBackendApi::kDirect3D;
Stephen White985741a2019-07-18 11:43:45 -040079 case kDawn_ContextType:
80 return GrBackendApi::kDawn;
Brian Salomoncfe910d2017-07-06 16:40:18 -040081 case kMock_ContextType:
Greg Danielbdf12ad2018-10-12 09:31:11 -040082 return GrBackendApi::kMock;
bsalomondc0fcd42016-04-11 14:21:33 -070083 default:
Greg Danielbdf12ad2018-10-12 09:31:11 -040084 return GrBackendApi::kOpenGL;
bsalomondc0fcd42016-04-11 14:21:33 -070085 }
bsalomon758586c2016-04-06 14:02:39 -070086 }
87
Greg Daniela5cb7812017-06-16 09:45:32 -040088 static const char* ContextTypeName(ContextType contextType) {
89 switch (contextType) {
90 case kGL_ContextType:
91 return "OpenGL";
92 case kGLES_ContextType:
93 return "OpenGLES";
94 case kANGLE_D3D9_ES2_ContextType:
95 return "ANGLE D3D9 ES2";
96 case kANGLE_D3D11_ES2_ContextType:
97 return "ANGLE D3D11 ES2";
98 case kANGLE_D3D11_ES3_ContextType:
99 return "ANGLE D3D11 ES3";
100 case kANGLE_GL_ES2_ContextType:
101 return "ANGLE GL ES2";
102 case kANGLE_GL_ES3_ContextType:
103 return "ANGLE GL ES3";
104 case kCommandBuffer_ContextType:
105 return "Command Buffer";
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";
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500110 case kDirect3D_ContextType:
111 return "Direct3D";
Stephen White985741a2019-07-18 11:43:45 -0400112 case kDawn_ContextType:
113 return "Dawn";
Brian Salomoncfe910d2017-07-06 16:40:18 -0400114 case kMock_ContextType:
115 return "Mock";
Greg Daniela5cb7812017-06-16 09:45:32 -0400116 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400117 SK_ABORT("Unreachable");
Greg Daniela5cb7812017-06-16 09:45:32 -0400118 }
119
kkinnunen34058002016-01-06 23:49:30 -0800120 explicit GrContextFactory(const GrContextOptions& opts);
121 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000122
kkinnunen34058002016-01-06 23:49:30 -0800123 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000124
kkinnunen34058002016-01-06 23:49:30 -0800125 void destroyContexts();
126 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700127 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700128
kkinnunen179a8f52015-11-20 13:32:24 -0800129 /**
130 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800131 */
Chris Daltonb3c97452019-06-25 20:07:56 -0600132 ContextInfo getContextInfo(ContextType type, ContextOverrides = ContextOverrides::kNone);
Brian Osman9eac2ea2017-02-24 14:51:44 -0500133
134 /**
135 * Get a context in the same share group as the passed in GrContext, with the same type and
136 * overrides. To get multiple contexts in a single share group, pass the same shareContext,
137 * with different values for shareIndex.
138 */
Robert Phillips00f78de2020-07-01 16:09:43 -0400139 ContextInfo getSharedContextInfo(GrDirectContext* shareContext, uint32_t shareIndex = 0);
Brian Osman9eac2ea2017-02-24 14:51:44 -0500140
bsalomon@google.com7361f542012-04-19 19:15:35 +0000141 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000142 * 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 +0000143 */
Robert Phillips00f78de2020-07-01 16:09:43 -0400144 GrDirectContext* get(ContextType type, ContextOverrides overrides = ContextOverrides::kNone);
bsalomon682c2692015-05-22 14:01:46 -0700145 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700146
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147private:
Brian Osman9eac2ea2017-02-24 14:51:44 -0500148 ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
Robert Phillips00f78de2020-07-01 16:09:43 -0400149 GrDirectContext* shareContext, uint32_t shareIndex);
Brian Osman9eac2ea2017-02-24 14:51:44 -0500150
kkinnunen34058002016-01-06 23:49:30 -0800151 struct Context {
Robert Phillips08ba0852019-05-22 20:23:43 +0000152 ContextType fType;
153 ContextOverrides fOverrides;
154 GrContextOptions fOptions;
Robert Phillips27eb5252019-06-03 12:59:40 -0400155 GrBackendApi fBackend;
Robert Phillips08ba0852019-05-22 20:23:43 +0000156 TestContext* fTestContext;
Robert Phillips00f78de2020-07-01 16:09:43 -0400157 GrDirectContext* fGrContext;
158 GrDirectContext* fShareContext;
Robert Phillips08ba0852019-05-22 20:23:43 +0000159 uint32_t fShareIndex;
Brian Osman60c774d2017-02-21 16:58:08 -0500160
Robert Phillips27eb5252019-06-03 12:59:40 -0400161 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800162 };
bsalomondc0fcd42016-04-11 14:21:33 -0700163 SkTArray<Context, true> fContexts;
Brian Salomon7f9c29a2017-01-24 22:22:05 +0000164 std::unique_ptr<GLTestContext> fSentinelGLContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700165 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000166};
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400167
168class ContextInfo {
169public:
170 ContextInfo() = default;
171 ContextInfo& operator=(const ContextInfo&) = default;
172
173 GrContextFactory::ContextType type() const { return fType; }
Greg Danielbdf12ad2018-10-12 09:31:11 -0400174 GrBackendApi backend() const { return GrContextFactory::ContextTypeBackend(fType); }
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400175
Robert Phillips00f78de2020-07-01 16:09:43 -0400176 GrDirectContext* directContext() const { return fContext; }
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400177 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:
Robert Phillips00f78de2020-07-01 16:09:43 -0400189 ContextInfo(GrContextFactory::ContextType type,
190 TestContext* testContext,
191 GrDirectContext* context,
Brian Salomon43f8bf02017-10-18 08:33:29 -0400192 const GrContextOptions& options)
Robert Phillips00f78de2020-07-01 16:09:43 -0400193 : fType(type), fTestContext(testContext), fContext(context), fOptions(options) {}
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400194
195 GrContextFactory::ContextType fType = GrContextFactory::kGL_ContextType;
196 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
Brian Salomon43f8bf02017-10-18 08:33:29 -0400197 TestContext* fTestContext = nullptr;
Robert Phillips00f78de2020-07-01 16:09:43 -0400198 GrDirectContext* fContext = nullptr;
Brian Salomon43f8bf02017-10-18 08:33:29 -0400199 GrContextOptions fOptions;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400200
201 friend class GrContextFactory;
202};
203
bsalomon3724e572016-03-30 18:56:19 -0700204} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700205
csmartdaltone812d492017-02-21 12:36:05 -0700206GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOverrides);
csmartdalton6270e552016-09-13 10:41:49 -0700207
bsalomon@google.com7361f542012-04-19 19:15:35 +0000208#endif