blob: d6baffc0ead7e93c6282cadde1a42882f6420706 [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"
bsalomon18a2f9d2016-05-11 10:09:18 -070015#include "vk/VkTestContext.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000016#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000017
bsalomondc0fcd42016-04-11 14:21:33 -070018struct GrVkBackendContext;
19
bsalomon3724e572016-03-30 18:56:19 -070020namespace sk_gpu_test {
bsalomonf2f1c172016-04-05 12:59:06 -070021
bsalomon8b7451a2016-05-11 06:33:06 -070022class ContextInfo {
23public:
bsalomon18a2f9d2016-05-11 10:09:18 -070024 ContextInfo() = default;
25 ContextInfo& operator=(const ContextInfo&) = default;
26
Mike Kleinfc6c37b2016-09-27 09:34:10 -040027 GrBackend backend() const { return fBackend; }
bsalomon18a2f9d2016-05-11 10:09:18 -070028
bsalomon8b7451a2016-05-11 06:33:06 -070029 GrContext* grContext() const { return fGrContext; }
bsalomon18a2f9d2016-05-11 10:09:18 -070030
bsalomon0fd3c812016-05-11 10:38:05 -070031 TestContext* testContext() const { return fTestContext; }
32
bsalomon18a2f9d2016-05-11 10:09:18 -070033 GLTestContext* glContext() const {
34 SkASSERT(kOpenGL_GrBackend == fBackend);
35 return static_cast<GLTestContext*>(fTestContext);
36 }
37
38#ifdef SK_VULKAN
39 VkTestContext* vkContext() const {
40 SkASSERT(kVulkan_GrBackend == fBackend);
41 return static_cast<VkTestContext*>(fTestContext);
42 }
43#endif
bsalomon8b7451a2016-05-11 06:33:06 -070044
45private:
bsalomon18a2f9d2016-05-11 10:09:18 -070046 ContextInfo(GrBackend backend, TestContext* testContext, GrContext* grContext)
47 : fBackend(backend)
48 , fTestContext(testContext)
49 , fGrContext(grContext) {}
50
51 GrBackend fBackend = kOpenGL_GrBackend;
52 // Valid until the factory destroys it via abandonContexts() or destroyContexts().
53 TestContext* fTestContext = nullptr;
54 GrContext* fGrContext = nullptr;
bsalomon8b7451a2016-05-11 06:33:06 -070055
56 friend class GrContextFactory;
bsalomonf2f1c172016-04-05 12:59:06 -070057};
58
bsalomon@google.com7361f542012-04-19 19:15:35 +000059/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000060 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000061 * GrContexts backed by different types of GL contexts. It manages creating the
62 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
63 * factory is destroyed (though the caller can always grab a ref on the returned
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000064 * Gr and GL contexts to make them outlive the factory).
bsalomon@google.com7361f542012-04-19 19:15:35 +000065 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000066class GrContextFactory : SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000067public:
bsalomondc0fcd42016-04-11 14:21:33 -070068 // The availability of context types is subject to platform and build configuration
69 // restrictions.
bsalomon85b4b532016-04-05 11:06:27 -070070 enum ContextType {
bsalomon11abd8d2016-10-14 08:13:48 -070071 kGL_ContextType, //! OpenGL context.
72 kGLES_ContextType, //! OpenGL ES context.
73 kANGLE_D3D9_ES2_ContextType, //! ANGLE on Direct3D9 OpenGL ES 2 context.
74 kANGLE_D3D11_ES2_ContextType,//! ANGLE on Direct3D11 OpenGL ES 2 context.
75 kANGLE_D3D11_ES3_ContextType,//! ANGLE on Direct3D11 OpenGL ES 3 context.
76 kANGLE_GL_ES2_ContextType, //! ANGLE on OpenGL OpenGL ES 2 context.
77 kANGLE_GL_ES3_ContextType, //! ANGLE on OpenGL OpenGL ES 3 context.
78 kCommandBuffer_ContextType, //! Chromium command buffer OpenGL ES context.
79 kMESA_ContextType, //! MESA OpenGL context
80 kNullGL_ContextType, //! Non-rendering OpenGL mock context.
81 kDebugGL_ContextType, //! Non-rendering, state verifying OpenGL context.
82 kVulkan_ContextType, //! Vulkan
bsalomondc0fcd42016-04-11 14:21:33 -070083 kLastContextType = kVulkan_ContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000084 };
85
bsalomon85b4b532016-04-05 11:06:27 -070086 //! OpenGL or OpenGL ES context depending on the platform. To be removed.
87 static const ContextType kNativeGL_ContextType;
88
89 static const int kContextTypeCnt = kLastContextType + 1;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000090
kkinnunen5219fd92015-12-10 06:28:13 -080091 /**
92 * Options for GL context creation. For historical and testing reasons the options will default
93 * to not using GL_NV_path_rendering extension even when the driver supports it.
94 */
csmartdalton6270e552016-09-13 10:41:49 -070095 enum class ContextOptions {
96 kNone = 0x0,
97 kEnableNVPR = 0x1,
98 kUseInstanced = 0x2,
99 kRequireSRGBSupport = 0x4,
kkinnunen5219fd92015-12-10 06:28:13 -0800100 };
101
bsalomon0fd3c812016-05-11 10:38:05 -0700102 static ContextType NativeContextTypeForBackend(GrBackend backend) {
103 switch (backend) {
104 case kOpenGL_GrBackend:
105 return kNativeGL_ContextType;
106 case kVulkan_GrBackend:
107 return kVulkan_ContextType;
108 default:
109 SkFAIL("Unknown backend");
110 return kNullGL_ContextType;
111 }
112 }
113
bsalomon85b4b532016-04-05 11:06:27 -0700114 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000115 switch (type) {
bsalomon85b4b532016-04-05 11:06:27 -0700116 case kNullGL_ContextType:
117 case kDebugGL_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000118 return false;
119 default:
120 return true;
121 }
122 }
123
bsalomon758586c2016-04-06 14:02:39 -0700124 static GrBackend ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -0700125 switch (type) {
126 case kVulkan_ContextType:
127 return kVulkan_GrBackend;
128 default:
129 return kOpenGL_GrBackend;
130 }
bsalomon758586c2016-04-06 14:02:39 -0700131 }
132
kkinnunen34058002016-01-06 23:49:30 -0800133 explicit GrContextFactory(const GrContextOptions& opts);
134 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000135
kkinnunen34058002016-01-06 23:49:30 -0800136 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000137
kkinnunen34058002016-01-06 23:49:30 -0800138 void destroyContexts();
139 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700140 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700141
kkinnunen179a8f52015-11-20 13:32:24 -0800142 /**
143 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800144 */
bsalomon85b4b532016-04-05 11:06:27 -0700145 ContextInfo getContextInfo(ContextType type,
csmartdalton6270e552016-09-13 10:41:49 -0700146 ContextOptions options = ContextOptions::kNone);
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000148 * 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 +0000149 */
csmartdalton6270e552016-09-13 10:41:49 -0700150 GrContext* get(ContextType type, ContextOptions options = ContextOptions::kNone) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700151 return this->getContextInfo(type, options).grContext();
kkinnunen179a8f52015-11-20 13:32:24 -0800152 }
bsalomon682c2692015-05-22 14:01:46 -0700153 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700154
bsalomon@google.com7361f542012-04-19 19:15:35 +0000155private:
kkinnunen34058002016-01-06 23:49:30 -0800156 struct Context {
bsalomondc0fcd42016-04-11 14:21:33 -0700157 ContextType fType;
158 ContextOptions fOptions;
bsalomon18a2f9d2016-05-11 10:09:18 -0700159 GrBackend fBackend;
160 TestContext* fTestContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700161 GrContext* fGrContext;
162 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800163 };
bsalomondc0fcd42016-04-11 14:21:33 -0700164 SkTArray<Context, true> fContexts;
165 SkAutoTDelete<GLTestContext> fSentinelGLContext;
166 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000167};
bsalomon3724e572016-03-30 18:56:19 -0700168} // namespace sk_gpu_test
csmartdalton6270e552016-09-13 10:41:49 -0700169
170GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOptions);
171
bsalomon@google.com7361f542012-04-19 19:15:35 +0000172#endif