blob: 130c333dd0f066c0e9fce39fca1e680944c4f94b [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
14#include "gl/SkGLContext.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000015#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000016
17/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000018 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000019 * GrContexts backed by different types of GL contexts. It manages creating the
20 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
21 * factory is destroyed (though the caller can always grab a ref on the returned
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000022 * Gr and GL contexts to make them outlive the factory).
bsalomon@google.com7361f542012-04-19 19:15:35 +000023 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000024class GrContextFactory : SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000025public:
26 /**
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000027 * Types of GL contexts supported. For historical and testing reasons the native GrContext will
28 * not use "GL_NV_path_rendering" even when the driver supports it. There is a separate context
29 * type that does not remove NVPR support and which will fail when the driver does not support
30 * the extension.
bsalomon@google.com7361f542012-04-19 19:15:35 +000031 */
32 enum GLContextType {
33 kNative_GLContextType,
34#if SK_ANGLE
35 kANGLE_GLContextType,
hendrikweddbefb2015-09-11 13:07:29 -070036 kANGLE_GL_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000037#endif
hendrikw885bf092015-08-27 10:38:39 -070038#if SK_COMMAND_BUFFER
39 kCommandBuffer_GLContextType,
40#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +000041#if SK_MESA
42 kMESA_GLContextType,
43#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000044 /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
45 support NVPR */
46 kNVPR_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000047 kNull_GLContextType,
48 kDebug_GLContextType,
bsalomon@google.com67b915d2013-02-04 16:13:32 +000049
50 kLastGLContextType = kDebug_GLContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000051 };
52
bsalomon@google.com67b915d2013-02-04 16:13:32 +000053 static const int kGLContextTypeCnt = kLastGLContextType + 1;
54
55 static bool IsRenderingGLContext(GLContextType type) {
56 switch (type) {
57 case kNull_GLContextType:
58 case kDebug_GLContextType:
59 return false;
60 default:
61 return true;
62 }
63 }
64
bsalomon@google.comcb265352013-02-22 16:13:16 +000065 static const char* GLContextTypeName(GLContextType type) {
66 switch (type) {
67 case kNative_GLContextType:
68 return "native";
69 case kNull_GLContextType:
70 return "null";
71#if SK_ANGLE
72 case kANGLE_GLContextType:
73 return "angle";
hendrikweddbefb2015-09-11 13:07:29 -070074 case kANGLE_GL_GLContextType:
75 return "angle-gl";
bsalomon@google.comcb265352013-02-22 16:13:16 +000076#endif
hendrikw885bf092015-08-27 10:38:39 -070077#if SK_COMMAND_BUFFER
78 case kCommandBuffer_GLContextType:
79 return "commandbuffer";
80#endif
bsalomon@google.comcb265352013-02-22 16:13:16 +000081#if SK_MESA
82 case kMESA_GLContextType:
83 return "mesa";
84#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000085 case kNVPR_GLContextType:
86 return "nvpr";
bsalomon@google.comcb265352013-02-22 16:13:16 +000087 case kDebug_GLContextType:
88 return "debug";
89 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000090 SkFAIL("Unknown GL Context type.");
bsalomon@google.comcb265352013-02-22 16:13:16 +000091 }
92 }
93
bsalomon682c2692015-05-22 14:01:46 -070094 explicit GrContextFactory(const GrContextOptions& opts) : fGlobalOptions(opts) { }
kkinnunen80549fc2014-06-30 06:36:31 -070095 GrContextFactory() { }
bsalomon@google.com7361f542012-04-19 19:15:35 +000096
bsalomon@google.com67b915d2013-02-04 16:13:32 +000097 ~GrContextFactory() { this->destroyContexts(); }
98
99 void destroyContexts() {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000100 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700101 if (fContexts[i].fGLContext) { // could be abandoned.
bsalomon2354f842014-07-28 13:48:36 -0700102 fContexts[i].fGLContext->makeCurrent();
103 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000104 fContexts[i].fGrContext->unref();
bsalomon49f085d2014-09-05 13:34:00 -0700105 if (fContexts[i].fGLContext) {
bsalomon2354f842014-07-28 13:48:36 -0700106 fContexts[i].fGLContext->unref();
107 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000108 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000109 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000110 }
111
bsalomon2354f842014-07-28 13:48:36 -0700112 void abandonContexts() {
113 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700114 if (fContexts[i].fGLContext) {
bsalomon944bcf02014-07-29 08:01:52 -0700115 fContexts[i].fGLContext->testAbandon();
116 SkSafeSetNull(fContexts[i].fGLContext);
117 }
bsalomon2354f842014-07-28 13:48:36 -0700118 fContexts[i].fGrContext->abandonContext();
119 }
120 }
121
bsalomon@google.com7361f542012-04-19 19:15:35 +0000122 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000123 * 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 +0000124 */
djsollene4545212014-11-13 11:12:41 -0800125 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard);
kkinnunen80549fc2014-06-30 06:36:31 -0700126
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000127
128 // Returns the GLContext of the given type. If it has not been created yet,
halcanary96fcdcc2015-08-27 07:41:13 -0700129 // nullptr is returned instead.
kkinnunen9e61bb72014-10-09 05:24:15 -0700130 SkGLContext* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000131 for (int i = 0; i < fContexts.count(); ++i) {
132 if (fContexts[i].fType == type) {
133 return fContexts[i].fGLContext;
134 }
135 }
136
halcanary96fcdcc2015-08-27 07:41:13 -0700137 return nullptr;
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000138 }
139
bsalomon682c2692015-05-22 14:01:46 -0700140 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700141
bsalomon@google.com7361f542012-04-19 19:15:35 +0000142private:
143 struct GPUContext {
144 GLContextType fType;
kkinnunen9e61bb72014-10-09 05:24:15 -0700145 SkGLContext* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000146 GrContext* fGrContext;
147 };
krajcevski69a55602014-08-13 10:46:31 -0700148 SkTArray<GPUContext, true> fContexts;
bsalomon682c2692015-05-22 14:01:46 -0700149 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000150};
151
152#endif