blob: 50978138174a22c22b68f45727b1e32a96aab8bf [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:
bsalomon@google.com7361f542012-04-19 19:15:35 +000026 enum GLContextType {
rmistry0f515bd2015-12-22 10:22:26 -080027 kNative_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000028#if SK_ANGLE
rmistry0f515bd2015-12-22 10:22:26 -080029 kANGLE_GLContextType,
30 kANGLE_GL_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000031#endif
hendrikw885bf092015-08-27 10:38:39 -070032#if SK_COMMAND_BUFFER
rmistry0f515bd2015-12-22 10:22:26 -080033 kCommandBuffer_GLContextType,
hendrikw885bf092015-08-27 10:38:39 -070034#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +000035#if SK_MESA
rmistry0f515bd2015-12-22 10:22:26 -080036 kMESA_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000037#endif
rmistry0f515bd2015-12-22 10:22:26 -080038 kNull_GLContextType,
39 kDebug_GLContextType,
kkinnunen5219fd92015-12-10 06:28:13 -080040 kLastGLContextType = kDebug_GLContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000041 };
42
bsalomon@google.com67b915d2013-02-04 16:13:32 +000043 static const int kGLContextTypeCnt = kLastGLContextType + 1;
44
kkinnunen5219fd92015-12-10 06:28:13 -080045 /**
46 * Options for GL context creation. For historical and testing reasons the options will default
47 * to not using GL_NV_path_rendering extension even when the driver supports it.
48 */
49 enum GLContextOptions {
50 kNone_GLContextOptions = 0,
51 kEnableNVPR_GLContextOptions = 0x1,
52 };
53
bsalomon@google.com67b915d2013-02-04 16:13:32 +000054 static bool IsRenderingGLContext(GLContextType type) {
55 switch (type) {
56 case kNull_GLContextType:
57 case kDebug_GLContextType:
58 return false;
59 default:
60 return true;
61 }
62 }
63
bsalomon@google.comcb265352013-02-22 16:13:16 +000064 static const char* GLContextTypeName(GLContextType type) {
65 switch (type) {
66 case kNative_GLContextType:
67 return "native";
rmistry0f515bd2015-12-22 10:22:26 -080068 case kNull_GLContextType:
69 return "null";
bsalomon@google.comcb265352013-02-22 16:13:16 +000070#if SK_ANGLE
71 case kANGLE_GLContextType:
72 return "angle";
hendrikweddbefb2015-09-11 13:07:29 -070073 case kANGLE_GL_GLContextType:
74 return "angle-gl";
bsalomon@google.comcb265352013-02-22 16:13:16 +000075#endif
hendrikw885bf092015-08-27 10:38:39 -070076#if SK_COMMAND_BUFFER
77 case kCommandBuffer_GLContextType:
78 return "commandbuffer";
79#endif
bsalomon@google.comcb265352013-02-22 16:13:16 +000080#if SK_MESA
81 case kMESA_GLContextType:
82 return "mesa";
83#endif
84 case kDebug_GLContextType:
85 return "debug";
86 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000087 SkFAIL("Unknown GL Context type.");
bsalomon@google.comcb265352013-02-22 16:13:16 +000088 }
89 }
90
bsalomon682c2692015-05-22 14:01:46 -070091 explicit GrContextFactory(const GrContextOptions& opts) : fGlobalOptions(opts) { }
kkinnunen80549fc2014-06-30 06:36:31 -070092 GrContextFactory() { }
bsalomon@google.com7361f542012-04-19 19:15:35 +000093
bsalomon@google.com67b915d2013-02-04 16:13:32 +000094 ~GrContextFactory() { this->destroyContexts(); }
95
96 void destroyContexts() {
bsalomon@google.com7361f542012-04-19 19:15:35 +000097 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen179a8f52015-11-20 13:32:24 -080098 if (fContexts[i]->fGLContext) { // could be abandoned.
99 fContexts[i]->fGLContext->makeCurrent();
bsalomon2354f842014-07-28 13:48:36 -0700100 }
kkinnunen179a8f52015-11-20 13:32:24 -0800101 fContexts[i]->fGrContext->unref();
102 SkSafeUnref(fContexts[i]->fGLContext);
bsalomon@google.com7361f542012-04-19 19:15:35 +0000103 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000104 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000105 }
106
bsalomon2354f842014-07-28 13:48:36 -0700107 void abandonContexts() {
108 for (int i = 0; i < fContexts.count(); ++i) {
kkinnunen179a8f52015-11-20 13:32:24 -0800109 if (fContexts[i]->fGLContext) {
110 fContexts[i]->fGLContext->testAbandon();
111 SkSafeSetNull(fContexts[i]->fGLContext);
bsalomon944bcf02014-07-29 08:01:52 -0700112 }
kkinnunen179a8f52015-11-20 13:32:24 -0800113 fContexts[i]->fGrContext->abandonContext();
bsalomon2354f842014-07-28 13:48:36 -0700114 }
115 }
116
kkinnunen179a8f52015-11-20 13:32:24 -0800117 struct ContextInfo {
118 GLContextType fType;
kkinnunen5219fd92015-12-10 06:28:13 -0800119 GLContextOptions fOptions;
kkinnunen179a8f52015-11-20 13:32:24 -0800120 SkGLContext* fGLContext;
121 GrContext* fGrContext;
122 };
123 /**
124 * Get a context initialized with a type of GL context. It also makes the GL context current.
125 * Pointer is valid until destroyContexts() is called.
126 */
rmistry0f515bd2015-12-22 10:22:26 -0800127 ContextInfo* getContextInfo(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard,
kkinnunen5219fd92015-12-10 06:28:13 -0800128 GLContextOptions options = kNone_GLContextOptions);
kkinnunen179a8f52015-11-20 13:32:24 -0800129
bsalomon@google.com7361f542012-04-19 19:15:35 +0000130 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000131 * 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 +0000132 */
rmistry0f515bd2015-12-22 10:22:26 -0800133 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard,
134 GLContextOptions options = kNone_GLContextOptions) {
135 if (ContextInfo* info = this->getContextInfo(type, forcedGpuAPI, options)) {
kkinnunen179a8f52015-11-20 13:32:24 -0800136 return info->fGrContext;
137 }
138 return nullptr;
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:
kkinnunen179a8f52015-11-20 13:32:24 -0800143 SkTArray<SkAutoTDelete<ContextInfo>, true> fContexts;
bsalomon682c2692015-05-22 14:01:46 -0700144 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000145};
146
147#endif