gfx/skia/trunk/include/gpu/GrContextFactory.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef GrContextFactory_DEFINED
michael@0 9 #define GrContextFactory_DEFINED
michael@0 10
michael@0 11 #if SK_ANGLE
michael@0 12 #include "gl/SkANGLEGLContext.h"
michael@0 13 #endif
michael@0 14 #include "gl/SkDebugGLContext.h"
michael@0 15 #if SK_MESA
michael@0 16 #include "gl/SkMesaGLContext.h"
michael@0 17 #endif
michael@0 18 #include "gl/SkNativeGLContext.h"
michael@0 19 #include "gl/SkNullGLContext.h"
michael@0 20
michael@0 21 #include "GrContext.h"
michael@0 22 #include "SkTArray.h"
michael@0 23
michael@0 24 /**
michael@0 25 * This is a simple class that is useful in test apps that use different
michael@0 26 * GrContexts backed by different types of GL contexts. It manages creating the
michael@0 27 * GL context and a GrContext that uses it. The GL/Gr contexts persist until the
michael@0 28 * factory is destroyed (though the caller can always grab a ref on the returned
michael@0 29 * Gr and GL contexts to make them outlive the factory).
michael@0 30 */
michael@0 31 class GrContextFactory : public SkNoncopyable {
michael@0 32 public:
michael@0 33 /**
michael@0 34 * Types of GL contexts supported. For historical and testing reasons the native GrContext will
michael@0 35 * not use "GL_NV_path_rendering" even when the driver supports it. There is a separate context
michael@0 36 * type that does not remove NVPR support and which will fail when the driver does not support
michael@0 37 * the extension.
michael@0 38 */
michael@0 39 enum GLContextType {
michael@0 40 kNative_GLContextType,
michael@0 41 #if SK_ANGLE
michael@0 42 kANGLE_GLContextType,
michael@0 43 #endif
michael@0 44 #if SK_MESA
michael@0 45 kMESA_GLContextType,
michael@0 46 #endif
michael@0 47 /** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
michael@0 48 support NVPR */
michael@0 49 kNVPR_GLContextType,
michael@0 50 kNull_GLContextType,
michael@0 51 kDebug_GLContextType,
michael@0 52
michael@0 53 kLastGLContextType = kDebug_GLContextType
michael@0 54 };
michael@0 55
michael@0 56 static const int kGLContextTypeCnt = kLastGLContextType + 1;
michael@0 57
michael@0 58 static bool IsRenderingGLContext(GLContextType type) {
michael@0 59 switch (type) {
michael@0 60 case kNull_GLContextType:
michael@0 61 case kDebug_GLContextType:
michael@0 62 return false;
michael@0 63 default:
michael@0 64 return true;
michael@0 65 }
michael@0 66 }
michael@0 67
michael@0 68 static const char* GLContextTypeName(GLContextType type) {
michael@0 69 switch (type) {
michael@0 70 case kNative_GLContextType:
michael@0 71 return "native";
michael@0 72 case kNull_GLContextType:
michael@0 73 return "null";
michael@0 74 #if SK_ANGLE
michael@0 75 case kANGLE_GLContextType:
michael@0 76 return "angle";
michael@0 77 #endif
michael@0 78 #if SK_MESA
michael@0 79 case kMESA_GLContextType:
michael@0 80 return "mesa";
michael@0 81 #endif
michael@0 82 case kNVPR_GLContextType:
michael@0 83 return "nvpr";
michael@0 84 case kDebug_GLContextType:
michael@0 85 return "debug";
michael@0 86 default:
michael@0 87 GrCrash("Unknown GL Context type.");
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 GrContextFactory() {
michael@0 92 }
michael@0 93
michael@0 94 ~GrContextFactory() { this->destroyContexts(); }
michael@0 95
michael@0 96 void destroyContexts() {
michael@0 97 for (int i = 0; i < fContexts.count(); ++i) {
michael@0 98 fContexts[i].fGLContext->makeCurrent();
michael@0 99 fContexts[i].fGrContext->unref();
michael@0 100 fContexts[i].fGLContext->unref();
michael@0 101 }
michael@0 102 fContexts.reset();
michael@0 103 }
michael@0 104
michael@0 105 /**
michael@0 106 * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
michael@0 107 */
michael@0 108 GrContext* get(GLContextType type) {
michael@0 109
michael@0 110 for (int i = 0; i < fContexts.count(); ++i) {
michael@0 111 if (fContexts[i].fType == type) {
michael@0 112 fContexts[i].fGLContext->makeCurrent();
michael@0 113 return fContexts[i].fGrContext;
michael@0 114 }
michael@0 115 }
michael@0 116 SkAutoTUnref<SkGLContextHelper> glCtx;
michael@0 117 SkAutoTUnref<GrContext> grCtx;
michael@0 118 switch (type) {
michael@0 119 case kNVPR_GLContextType: // fallthru
michael@0 120 case kNative_GLContextType:
michael@0 121 glCtx.reset(SkNEW(SkNativeGLContext));
michael@0 122 break;
michael@0 123 #ifdef SK_ANGLE
michael@0 124 case kANGLE_GLContextType:
michael@0 125 glCtx.reset(SkNEW(SkANGLEGLContext));
michael@0 126 break;
michael@0 127 #endif
michael@0 128 #ifdef SK_MESA
michael@0 129 case kMESA_GLContextType:
michael@0 130 glCtx.reset(SkNEW(SkMesaGLContext));
michael@0 131 break;
michael@0 132 #endif
michael@0 133 case kNull_GLContextType:
michael@0 134 glCtx.reset(SkNEW(SkNullGLContext));
michael@0 135 break;
michael@0 136 case kDebug_GLContextType:
michael@0 137 glCtx.reset(SkNEW(SkDebugGLContext));
michael@0 138 break;
michael@0 139 }
michael@0 140 static const int kBogusSize = 1;
michael@0 141 if (!glCtx.get()) {
michael@0 142 return NULL;
michael@0 143 }
michael@0 144 if (!glCtx.get()->init(kBogusSize, kBogusSize)) {
michael@0 145 return NULL;
michael@0 146 }
michael@0 147
michael@0 148 // Ensure NVPR is available for the NVPR type and block it from other types.
michael@0 149 SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl()));
michael@0 150 if (kNVPR_GLContextType == type) {
michael@0 151 if (!glInterface->hasExtension("GL_NV_path_rendering")) {
michael@0 152 return NULL;
michael@0 153 }
michael@0 154 } else {
michael@0 155 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
michael@0 156 if (!glInterface) {
michael@0 157 return NULL;
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 glCtx->makeCurrent();
michael@0 162 GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
michael@0 163 grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx));
michael@0 164 if (!grCtx.get()) {
michael@0 165 return NULL;
michael@0 166 }
michael@0 167 GPUContext& ctx = fContexts.push_back();
michael@0 168 ctx.fGLContext = glCtx.get();
michael@0 169 ctx.fGLContext->ref();
michael@0 170 ctx.fGrContext = grCtx.get();
michael@0 171 ctx.fGrContext->ref();
michael@0 172 ctx.fType = type;
michael@0 173 return ctx.fGrContext;
michael@0 174 }
michael@0 175
michael@0 176 // Returns the GLContext of the given type. If it has not been created yet,
michael@0 177 // NULL is returned instead.
michael@0 178 SkGLContextHelper* getGLContext(GLContextType type) {
michael@0 179 for (int i = 0; i < fContexts.count(); ++i) {
michael@0 180 if (fContexts[i].fType == type) {
michael@0 181 return fContexts[i].fGLContext;
michael@0 182 }
michael@0 183 }
michael@0 184
michael@0 185 return NULL;
michael@0 186 }
michael@0 187
michael@0 188 private:
michael@0 189 struct GPUContext {
michael@0 190 GLContextType fType;
michael@0 191 SkGLContextHelper* fGLContext;
michael@0 192 GrContext* fGrContext;
michael@0 193 };
michael@0 194 SkTArray<GPUContext, true> fContexts;
michael@0 195 };
michael@0 196
michael@0 197 #endif

mercurial