blob: 353e3d9c96ada7ec5dff59d7487008b63219187c [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"
djsollene4545212014-11-13 11:12:41 -080012
13#include "gl/SkGLContext.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000014#include "SkTArray.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000015
16/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000017 * This is a simple class that is useful in test apps that use different
bsalomon@google.com7361f542012-04-19 19:15:35 +000018 * GrContexts backed by different types of GL contexts. It manages creating the
19 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
20 * factory is destroyed (though the caller can always grab a ref on the returned
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000021 * Gr and GL contexts to make them outlive the factory).
bsalomon@google.com7361f542012-04-19 19:15:35 +000022 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000023class GrContextFactory : SkNoncopyable {
bsalomon@google.com7361f542012-04-19 19:15:35 +000024public:
25 /**
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000026 * Types of GL contexts supported. For historical and testing reasons the native GrContext will
27 * not use "GL_NV_path_rendering" even when the driver supports it. There is a separate context
28 * type that does not remove NVPR support and which will fail when the driver does not support
29 * the extension.
bsalomon@google.com7361f542012-04-19 19:15:35 +000030 */
31 enum GLContextType {
32 kNative_GLContextType,
33#if SK_ANGLE
34 kANGLE_GLContextType,
35#endif
36#if SK_MESA
37 kMESA_GLContextType,
38#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000039 /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
40 support NVPR */
41 kNVPR_GLContextType,
bsalomon@google.com7361f542012-04-19 19:15:35 +000042 kNull_GLContextType,
43 kDebug_GLContextType,
bsalomon@google.com67b915d2013-02-04 16:13:32 +000044
45 kLastGLContextType = kDebug_GLContextType
bsalomon@google.com7361f542012-04-19 19:15:35 +000046 };
47
bsalomon@google.com67b915d2013-02-04 16:13:32 +000048 static const int kGLContextTypeCnt = kLastGLContextType + 1;
49
50 static bool IsRenderingGLContext(GLContextType type) {
51 switch (type) {
52 case kNull_GLContextType:
53 case kDebug_GLContextType:
54 return false;
55 default:
56 return true;
57 }
58 }
59
bsalomon@google.comcb265352013-02-22 16:13:16 +000060 static const char* GLContextTypeName(GLContextType type) {
61 switch (type) {
62 case kNative_GLContextType:
63 return "native";
64 case kNull_GLContextType:
65 return "null";
66#if SK_ANGLE
67 case kANGLE_GLContextType:
68 return "angle";
69#endif
70#if SK_MESA
71 case kMESA_GLContextType:
72 return "mesa";
73#endif
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +000074 case kNVPR_GLContextType:
75 return "nvpr";
bsalomon@google.comcb265352013-02-22 16:13:16 +000076 case kDebug_GLContextType:
77 return "debug";
78 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000079 SkFAIL("Unknown GL Context type.");
bsalomon@google.comcb265352013-02-22 16:13:16 +000080 }
81 }
82
krajcevski69a55602014-08-13 10:46:31 -070083 explicit GrContextFactory(const GrContext::Options& opts) : fGlobalOptions(opts) { }
kkinnunen80549fc2014-06-30 06:36:31 -070084 GrContextFactory() { }
bsalomon@google.com7361f542012-04-19 19:15:35 +000085
bsalomon@google.com67b915d2013-02-04 16:13:32 +000086 ~GrContextFactory() { this->destroyContexts(); }
87
88 void destroyContexts() {
bsalomon@google.com7361f542012-04-19 19:15:35 +000089 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -070090 if (fContexts[i].fGLContext) { // could be abandoned.
bsalomon2354f842014-07-28 13:48:36 -070091 fContexts[i].fGLContext->makeCurrent();
92 }
bsalomon@google.com7361f542012-04-19 19:15:35 +000093 fContexts[i].fGrContext->unref();
bsalomon49f085d2014-09-05 13:34:00 -070094 if (fContexts[i].fGLContext) {
bsalomon2354f842014-07-28 13:48:36 -070095 fContexts[i].fGLContext->unref();
96 }
bsalomon@google.com7361f542012-04-19 19:15:35 +000097 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000098 fContexts.reset();
bsalomon@google.com7361f542012-04-19 19:15:35 +000099 }
100
bsalomon2354f842014-07-28 13:48:36 -0700101 void abandonContexts() {
102 for (int i = 0; i < fContexts.count(); ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700103 if (fContexts[i].fGLContext) {
bsalomon944bcf02014-07-29 08:01:52 -0700104 fContexts[i].fGLContext->testAbandon();
105 SkSafeSetNull(fContexts[i].fGLContext);
106 }
bsalomon2354f842014-07-28 13:48:36 -0700107 fContexts[i].fGrContext->abandonContext();
108 }
109 }
110
bsalomon@google.com7361f542012-04-19 19:15:35 +0000111 /**
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000112 * 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 +0000113 */
djsollene4545212014-11-13 11:12:41 -0800114 GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard);
kkinnunen80549fc2014-06-30 06:36:31 -0700115
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000116
117 // Returns the GLContext of the given type. If it has not been created yet,
118 // NULL is returned instead.
kkinnunen9e61bb72014-10-09 05:24:15 -0700119 SkGLContext* getGLContext(GLContextType type) {
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000120 for (int i = 0; i < fContexts.count(); ++i) {
121 if (fContexts[i].fType == type) {
122 return fContexts[i].fGLContext;
123 }
124 }
125
126 return NULL;
127 }
128
krajcevskib1aded82014-08-18 07:52:17 -0700129 const GrContext::Options& getGlobalOptions() const { return fGlobalOptions; }
130
bsalomon@google.com7361f542012-04-19 19:15:35 +0000131private:
132 struct GPUContext {
133 GLContextType fType;
kkinnunen9e61bb72014-10-09 05:24:15 -0700134 SkGLContext* fGLContext;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000135 GrContext* fGrContext;
136 };
krajcevski69a55602014-08-13 10:46:31 -0700137 SkTArray<GPUContext, true> fContexts;
138 const GrContext::Options fGlobalOptions;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000139};
140
141#endif