blob: 2accd7329f9f85290748e3802c051be49fb8b236 [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,
36#endif
hendrikw885bf092015-08-27 10:38:39 -070037#if SK_COMMAND_BUFFER
38 kCommandBuffer_GLContextType,
39#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +000040#if SK_MESA
41 kMESA_GLContextType,
42#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000043 /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
44 support NVPR */
45 kNVPR_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000046 kNull_GLContextType,
47 kDebug_GLContextType,
bsalomon@google.com67b915d2013-02-04 16:13:32 +000048
49 kLastGLContextType = kDebug_GLContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000050 };
51
bsalomon@google.com67b915d2013-02-04 16:13:32 +000052 static const int kGLContextTypeCnt = kLastGLContextType + 1;
53
54 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";
68 case kNull_GLContextType:
69 return "null";
70#if SK_ANGLE
71 case kANGLE_GLContextType:
72 return "angle";
73#endif
hendrikw885bf092015-08-27 10:38:39 -070074#if SK_COMMAND_BUFFER
75 case kCommandBuffer_GLContextType:
76 return "commandbuffer";
77#endif
bsalomon@google.comcb265352013-02-22 16:13:16 +000078#if SK_MESA
79 case kMESA_GLContextType:
80 return "mesa";
81#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000082 case kNVPR_GLContextType:
83 return "nvpr";
bsalomon@google.comcb265352013-02-22 16:13:16 +000084 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) {
bsalomon49f085d2014-09-05 13:34:00 -070098 if (fContexts[i].fGLContext) { // could be abandoned.
bsalomon2354f842014-07-28 13:48:36 -070099 fContexts[i].fGLContext->makeCurrent();
100 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000101 fContexts[i].fGrContext->unref();
bsalomon49f085d2014-09-05 13:34:00 -0700102 if (fContexts[i].fGLContext) {
bsalomon2354f842014-07-28 13:48:36 -0700103 fContexts[i].fGLContext->unref();
104 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000105 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000106 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000107 }
108
bsalomon2354f842014-07-28 13:48:36 -0700109 void abandonContexts() {
110 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700111 if (fContexts[i].fGLContext) {
bsalomon944bcf02014-07-29 08:01:52 -0700112 fContexts[i].fGLContext->testAbandon();
113 SkSafeSetNull(fContexts[i].fGLContext);
114 }
bsalomon2354f842014-07-28 13:48:36 -0700115 fContexts[i].fGrContext->abandonContext();
116 }
117 }
118
bsalomon@google.com7361f542012-04-19 19:15:35 +0000119 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000120 * 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 +0000121 */
djsollene4545212014-11-13 11:12:41 -0800122 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard);
kkinnunen80549fc2014-06-30 06:36:31 -0700123
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000124
125 // Returns the GLContext of the given type. If it has not been created yet,
halcanary96fcdcc2015-08-27 07:41:13 -0700126 // nullptr is returned instead.
kkinnunen9e61bb72014-10-09 05:24:15 -0700127 SkGLContext* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000128 for (int i = 0; i < fContexts.count(); ++i) {
129 if (fContexts[i].fType == type) {
130 return fContexts[i].fGLContext;
131 }
132 }
133
halcanary96fcdcc2015-08-27 07:41:13 -0700134 return nullptr;
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000135 }
136
bsalomon682c2692015-05-22 14:01:46 -0700137 const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
krajcevskib1aded82014-08-18 07:52:17 -0700138
bsalomon@google.com7361f542012-04-19 19:15:35 +0000139private:
140 struct GPUContext {
141 GLContextType fType;
kkinnunen9e61bb72014-10-09 05:24:15 -0700142 SkGLContext* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000143 GrContext* fGrContext;
144 };
krajcevski69a55602014-08-13 10:46:31 -0700145 SkTArray<GPUContext, true> fContexts;
bsalomon682c2692015-05-22 14:01:46 -0700146 const GrContextOptions fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147};
148
149#endif