2012-04-19 19:15:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrContextFactory_DEFINED
|
|
|
|
#define GrContextFactory_DEFINED
|
|
|
|
|
|
|
|
#if SK_ANGLE
|
|
|
|
#include "gl/SkANGLEGLContext.h"
|
|
|
|
#endif
|
|
|
|
#include "gl/SkDebugGLContext.h"
|
|
|
|
#if SK_MESA
|
|
|
|
#include "gl/SkMesaGLContext.h"
|
|
|
|
#endif
|
2014-10-09 12:24:15 +00:00
|
|
|
#include "gl/SkGLContext.h"
|
2012-04-19 19:15:35 +00:00
|
|
|
#include "gl/SkNullGLContext.h"
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
2012-08-01 20:08:47 +00:00
|
|
|
#include "SkTArray.h"
|
2012-04-19 19:15:35 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* This is a simple class that is useful in test apps that use different
|
2012-04-19 19:15:35 +00:00
|
|
|
* GrContexts backed by different types of GL contexts. It manages creating the
|
|
|
|
* GL context and a GrContext that uses it. The GL/Gr contexts persist until the
|
|
|
|
* factory is destroyed (though the caller can always grab a ref on the returned
|
2014-01-24 20:49:44 +00:00
|
|
|
* Gr and GL contexts to make them outlive the factory).
|
2012-04-19 19:15:35 +00:00
|
|
|
*/
|
2014-04-07 19:34:38 +00:00
|
|
|
class GrContextFactory : SkNoncopyable {
|
2012-04-19 19:15:35 +00:00
|
|
|
public:
|
|
|
|
/**
|
2014-01-24 20:49:44 +00:00
|
|
|
* Types of GL contexts supported. For historical and testing reasons the native GrContext will
|
|
|
|
* not use "GL_NV_path_rendering" even when the driver supports it. There is a separate context
|
|
|
|
* type that does not remove NVPR support and which will fail when the driver does not support
|
|
|
|
* the extension.
|
2012-04-19 19:15:35 +00:00
|
|
|
*/
|
|
|
|
enum GLContextType {
|
|
|
|
kNative_GLContextType,
|
|
|
|
#if SK_ANGLE
|
|
|
|
kANGLE_GLContextType,
|
|
|
|
#endif
|
|
|
|
#if SK_MESA
|
|
|
|
kMESA_GLContextType,
|
|
|
|
#endif
|
2014-01-24 20:49:44 +00:00
|
|
|
/** Similar to kNative but does not filter NVPR. It will fail if the GL driver does not
|
|
|
|
support NVPR */
|
|
|
|
kNVPR_GLContextType,
|
2012-04-19 19:15:35 +00:00
|
|
|
kNull_GLContextType,
|
|
|
|
kDebug_GLContextType,
|
2013-02-04 16:13:32 +00:00
|
|
|
|
|
|
|
kLastGLContextType = kDebug_GLContextType
|
2012-04-19 19:15:35 +00:00
|
|
|
};
|
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
static const int kGLContextTypeCnt = kLastGLContextType + 1;
|
|
|
|
|
|
|
|
static bool IsRenderingGLContext(GLContextType type) {
|
|
|
|
switch (type) {
|
|
|
|
case kNull_GLContextType:
|
|
|
|
case kDebug_GLContextType:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:13:16 +00:00
|
|
|
static const char* GLContextTypeName(GLContextType type) {
|
|
|
|
switch (type) {
|
|
|
|
case kNative_GLContextType:
|
|
|
|
return "native";
|
|
|
|
case kNull_GLContextType:
|
|
|
|
return "null";
|
|
|
|
#if SK_ANGLE
|
|
|
|
case kANGLE_GLContextType:
|
|
|
|
return "angle";
|
|
|
|
#endif
|
|
|
|
#if SK_MESA
|
|
|
|
case kMESA_GLContextType:
|
|
|
|
return "mesa";
|
|
|
|
#endif
|
2014-01-24 20:49:44 +00:00
|
|
|
case kNVPR_GLContextType:
|
|
|
|
return "nvpr";
|
2013-02-22 16:13:16 +00:00
|
|
|
case kDebug_GLContextType:
|
|
|
|
return "debug";
|
|
|
|
default:
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("Unknown GL Context type.");
|
2013-02-22 16:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 17:46:31 +00:00
|
|
|
explicit GrContextFactory(const GrContext::Options& opts) : fGlobalOptions(opts) { }
|
2014-06-30 13:36:31 +00:00
|
|
|
GrContextFactory() { }
|
2012-04-19 19:15:35 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
~GrContextFactory() { this->destroyContexts(); }
|
|
|
|
|
|
|
|
void destroyContexts() {
|
2012-04-19 19:15:35 +00:00
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fContexts[i].fGLContext) { // could be abandoned.
|
2014-07-28 20:48:36 +00:00
|
|
|
fContexts[i].fGLContext->makeCurrent();
|
|
|
|
}
|
2012-04-19 19:15:35 +00:00
|
|
|
fContexts[i].fGrContext->unref();
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fContexts[i].fGLContext) {
|
2014-07-28 20:48:36 +00:00
|
|
|
fContexts[i].fGLContext->unref();
|
|
|
|
}
|
2012-04-19 19:15:35 +00:00
|
|
|
}
|
2013-02-04 16:13:32 +00:00
|
|
|
fContexts.reset();
|
2012-04-19 19:15:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 20:48:36 +00:00
|
|
|
void abandonContexts() {
|
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fContexts[i].fGLContext) {
|
2014-07-29 15:01:52 +00:00
|
|
|
fContexts[i].fGLContext->testAbandon();
|
|
|
|
SkSafeSetNull(fContexts[i].fGLContext);
|
|
|
|
}
|
2014-07-28 20:48:36 +00:00
|
|
|
fContexts[i].fGrContext->abandonContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 19:15:35 +00:00
|
|
|
/**
|
2013-02-04 16:13:32 +00:00
|
|
|
* Get a GrContext initialized with a type of GL context. It also makes the GL context current.
|
2012-04-19 19:15:35 +00:00
|
|
|
*/
|
2014-06-30 13:36:31 +00:00
|
|
|
GrContext* get(GLContextType type, GrGLStandard forcedGpuAPI = kNone_GrGLStandard) {
|
2014-06-23 13:13:46 +00:00
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
2014-06-30 13:36:31 +00:00
|
|
|
if (forcedGpuAPI != kNone_GrGLStandard &&
|
|
|
|
forcedGpuAPI != fContexts[i].fGLContext->gl()->fStandard)
|
|
|
|
continue;
|
|
|
|
|
2012-04-19 19:15:35 +00:00
|
|
|
if (fContexts[i].fType == type) {
|
2013-02-04 16:13:32 +00:00
|
|
|
fContexts[i].fGLContext->makeCurrent();
|
2012-04-19 19:15:35 +00:00
|
|
|
return fContexts[i].fGrContext;
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 12:24:15 +00:00
|
|
|
SkAutoTUnref<SkGLContext> glCtx;
|
2012-04-19 19:15:35 +00:00
|
|
|
SkAutoTUnref<GrContext> grCtx;
|
|
|
|
switch (type) {
|
2014-01-24 20:49:44 +00:00
|
|
|
case kNVPR_GLContextType: // fallthru
|
2012-04-19 19:15:35 +00:00
|
|
|
case kNative_GLContextType:
|
2014-10-09 12:24:15 +00:00
|
|
|
glCtx.reset(SkCreatePlatformGLContext());
|
2012-04-19 19:15:35 +00:00
|
|
|
break;
|
|
|
|
#ifdef SK_ANGLE
|
|
|
|
case kANGLE_GLContextType:
|
2012-07-09 20:17:56 +00:00
|
|
|
glCtx.reset(SkNEW(SkANGLEGLContext));
|
2012-04-19 19:15:35 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef SK_MESA
|
|
|
|
case kMESA_GLContextType:
|
2012-07-09 20:17:56 +00:00
|
|
|
glCtx.reset(SkNEW(SkMesaGLContext));
|
2012-04-19 19:15:35 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case kNull_GLContextType:
|
2012-07-09 20:17:56 +00:00
|
|
|
glCtx.reset(SkNEW(SkNullGLContext));
|
2012-04-19 19:15:35 +00:00
|
|
|
break;
|
|
|
|
case kDebug_GLContextType:
|
2012-07-09 20:17:56 +00:00
|
|
|
glCtx.reset(SkNEW(SkDebugGLContext));
|
2012-04-19 19:15:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
static const int kBogusSize = 1;
|
|
|
|
if (!glCtx.get()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-30 13:36:31 +00:00
|
|
|
if (!glCtx.get()->init(forcedGpuAPI, kBogusSize, kBogusSize)) {
|
2012-04-19 19:15:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-24 20:49:44 +00:00
|
|
|
|
|
|
|
// Ensure NVPR is available for the NVPR type and block it from other types.
|
|
|
|
SkAutoTUnref<const GrGLInterface> glInterface(SkRef(glCtx.get()->gl()));
|
|
|
|
if (kNVPR_GLContextType == type) {
|
|
|
|
if (!glInterface->hasExtension("GL_NV_path_rendering")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface));
|
|
|
|
if (!glInterface) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glCtx->makeCurrent();
|
|
|
|
GrBackendContext p3dctx = reinterpret_cast<GrBackendContext>(glInterface.get());
|
2014-08-13 17:46:31 +00:00
|
|
|
grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx, &fGlobalOptions));
|
2012-04-19 19:15:35 +00:00
|
|
|
if (!grCtx.get()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
GPUContext& ctx = fContexts.push_back();
|
|
|
|
ctx.fGLContext = glCtx.get();
|
|
|
|
ctx.fGLContext->ref();
|
|
|
|
ctx.fGrContext = grCtx.get();
|
|
|
|
ctx.fGrContext->ref();
|
|
|
|
ctx.fType = type;
|
|
|
|
return ctx.fGrContext;
|
|
|
|
}
|
2012-08-14 22:02:48 +00:00
|
|
|
|
|
|
|
// Returns the GLContext of the given type. If it has not been created yet,
|
|
|
|
// NULL is returned instead.
|
2014-10-09 12:24:15 +00:00
|
|
|
SkGLContext* getGLContext(GLContextType type) {
|
2012-08-14 22:02:48 +00:00
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
|
|
|
if (fContexts[i].fType == type) {
|
|
|
|
return fContexts[i].fGLContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-18 14:52:17 +00:00
|
|
|
const GrContext::Options& getGlobalOptions() const { return fGlobalOptions; }
|
|
|
|
|
2012-04-19 19:15:35 +00:00
|
|
|
private:
|
|
|
|
struct GPUContext {
|
|
|
|
GLContextType fType;
|
2014-10-09 12:24:15 +00:00
|
|
|
SkGLContext* fGLContext;
|
2012-04-19 19:15:35 +00:00
|
|
|
GrContext* fGrContext;
|
|
|
|
};
|
2014-08-13 17:46:31 +00:00
|
|
|
SkTArray<GPUContext, true> fContexts;
|
|
|
|
const GrContext::Options fGlobalOptions;
|
2012-04-19 19:15:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|