blob: ed8e6451999514aa001fb0870754a24b36282cd8 [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
27 GrBackend backend() const { return fBackend; };
28
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 {
71 kGL_ContextType, //! OpenGL context.
72 kGLES_ContextType, //! OpenGL ES context.
bsalomon85b4b532016-04-05 11:06:27 -070073 kANGLE_ContextType, //! ANGLE on DirectX OpenGL ES context.
bsalomon85b4b532016-04-05 11:06:27 -070074 kANGLE_GL_ContextType, //! ANGLE on OpenGL OpenGL ES context.
bsalomon85b4b532016-04-05 11:06:27 -070075 kCommandBuffer_ContextType, //! Chromium command buffer OpenGL ES context.
bsalomon85b4b532016-04-05 11:06:27 -070076 kMESA_ContextType, //! MESA OpenGL context
bsalomon85b4b532016-04-05 11:06:27 -070077 kNullGL_ContextType, //! Non-rendering OpenGL mock context.
78 kDebugGL_ContextType, //! Non-rendering, state verifying OpenGL context.
bsalomondc0fcd42016-04-11 14:21:33 -070079 kVulkan_ContextType, //! Vulkan
80 kLastContextType = kVulkan_ContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000081 };
82
bsalomon85b4b532016-04-05 11:06:27 -070083 //! OpenGL or OpenGL ES context depending on the platform. To be removed.
84 static const ContextType kNativeGL_ContextType;
85
86 static const int kContextTypeCnt = kLastContextType + 1;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000087
kkinnunen5219fd92015-12-10 06:28:13 -080088 /**
89 * Options for GL context creation. For historical and testing reasons the options will default
90 * to not using GL_NV_path_rendering extension even when the driver supports it.
91 */
bsalomon85b4b532016-04-05 11:06:27 -070092 enum ContextOptions {
93 kNone_ContextOptions = 0x0,
94 kEnableNVPR_ContextOptions = 0x1,
95 kRequireSRGBSupport_ContextOptions = 0x2,
kkinnunen5219fd92015-12-10 06:28:13 -080096 };
97
bsalomon0fd3c812016-05-11 10:38:05 -070098 static ContextType NativeContextTypeForBackend(GrBackend backend) {
99 switch (backend) {
100 case kOpenGL_GrBackend:
101 return kNativeGL_ContextType;
102 case kVulkan_GrBackend:
103 return kVulkan_ContextType;
104 default:
105 SkFAIL("Unknown backend");
106 return kNullGL_ContextType;
107 }
108 }
109
bsalomon85b4b532016-04-05 11:06:27 -0700110 static bool IsRenderingContext(ContextType type) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000111 switch (type) {
bsalomon85b4b532016-04-05 11:06:27 -0700112 case kNullGL_ContextType:
113 case kDebugGL_ContextType:
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000114 return false;
115 default:
116 return true;
117 }
118 }
119
bsalomon758586c2016-04-06 14:02:39 -0700120 static GrBackend ContextTypeBackend(ContextType type) {
bsalomondc0fcd42016-04-11 14:21:33 -0700121 switch (type) {
122 case kVulkan_ContextType:
123 return kVulkan_GrBackend;
124 default:
125 return kOpenGL_GrBackend;
126 }
bsalomon758586c2016-04-06 14:02:39 -0700127 }
128
bsalomon85b4b532016-04-05 11:06:27 -0700129 static const char* ContextTypeName(ContextType type) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000130 switch (type) {
bsalomon85b4b532016-04-05 11:06:27 -0700131 case kGL_ContextType:
kkinnunen3e980c32015-12-23 01:33:00 -0800132 return "gl";
bsalomon85b4b532016-04-05 11:06:27 -0700133 case kGLES_ContextType:
kkinnunen3e980c32015-12-23 01:33:00 -0800134 return "gles";
bsalomon85b4b532016-04-05 11:06:27 -0700135 case kANGLE_ContextType:
bsalomon@google.comcb265352013-02-22 16:13:16 +0000136 return "angle";
bsalomon85b4b532016-04-05 11:06:27 -0700137 case kANGLE_GL_ContextType:
hendrikweddbefb2015-09-11 13:07:29 -0700138 return "angle-gl";
bsalomon85b4b532016-04-05 11:06:27 -0700139 case kCommandBuffer_ContextType:
hendrikw885bf092015-08-27 10:38:39 -0700140 return "commandbuffer";
bsalomon85b4b532016-04-05 11:06:27 -0700141 case kMESA_ContextType:
bsalomon@google.comcb265352013-02-22 16:13:16 +0000142 return "mesa";
bsalomon85b4b532016-04-05 11:06:27 -0700143 case kNullGL_ContextType:
bsalomondc0fcd42016-04-11 14:21:33 -0700144 return "nullgl";
bsalomon85b4b532016-04-05 11:06:27 -0700145 case kDebugGL_ContextType:
bsalomondc0fcd42016-04-11 14:21:33 -0700146 return "debuggl";
147 case kVulkan_ContextType:
148 return "vulkan";
bsalomon@google.comcb265352013-02-22 16:13:16 +0000149 }
150 }
151
kkinnunen34058002016-01-06 23:49:30 -0800152 explicit GrContextFactory(const GrContextOptions& opts);
153 GrContextFactory();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000154
kkinnunen34058002016-01-06 23:49:30 -0800155 ~GrContextFactory();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000156
kkinnunen34058002016-01-06 23:49:30 -0800157 void destroyContexts();
158 void abandonContexts();
bsalomon6e2aad42016-04-01 11:54:31 -0700159 void releaseResourcesAndAbandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700160
kkinnunen179a8f52015-11-20 13:32:24 -0800161 /**
162 * Get a context initialized with a type of GL context. It also makes the GL context current.
kkinnunen179a8f52015-11-20 13:32:24 -0800163 */
bsalomon85b4b532016-04-05 11:06:27 -0700164 ContextInfo getContextInfo(ContextType type,
165 ContextOptions options = kNone_ContextOptions);
bsalomon@google.com7361f542012-04-19 19:15:35 +0000166 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000167 * 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 +0000168 */
bsalomon85b4b532016-04-05 11:06:27 -0700169 GrContext* get(ContextType type, ContextOptions options = kNone_ContextOptions) {
bsalomon18a2f9d2016-05-11 10:09:18 -0700170 return this->getContextInfo(type, options).grContext();
kkinnunen179a8f52015-11-20 13:32:24 -0800171 }
bsalomon682c2692015-05-22 14:01:46 -0700172 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700173
bsalomon@google.com7361f542012-04-19 19:15:35 +0000174private:
kkinnunen34058002016-01-06 23:49:30 -0800175 struct Context {
bsalomondc0fcd42016-04-11 14:21:33 -0700176 ContextType fType;
177 ContextOptions fOptions;
bsalomon18a2f9d2016-05-11 10:09:18 -0700178 GrBackend fBackend;
179 TestContext* fTestContext;
bsalomondc0fcd42016-04-11 14:21:33 -0700180 GrContext* fGrContext;
181 bool fAbandoned;
kkinnunen34058002016-01-06 23:49:30 -0800182 };
bsalomondc0fcd42016-04-11 14:21:33 -0700183 SkTArray<Context, true> fContexts;
184 SkAutoTDelete<GLTestContext> fSentinelGLContext;
185 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000186};
bsalomon3724e572016-03-30 18:56:19 -0700187} // namespace sk_gpu_test
bsalomon@google.com7361f542012-04-19 19:15:35 +0000188#endif