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
|
|
|
|
#include "gl/SkNativeGLContext.h"
|
|
|
|
#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
|
|
|
*/
|
2013-09-18 13:00:55 +00:00
|
|
|
class GrContextFactory : public 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:
|
|
|
|
GrCrash("Unknown GL Context type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 19:15:35 +00:00
|
|
|
GrContextFactory() {
|
|
|
|
}
|
|
|
|
|
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-01-24 20:49:44 +00:00
|
|
|
fContexts[i].fGLContext->makeCurrent();
|
2012-04-19 19:15:35 +00:00
|
|
|
fContexts[i].fGrContext->unref();
|
|
|
|
fContexts[i].fGLContext->unref();
|
|
|
|
}
|
2013-02-04 16:13:32 +00:00
|
|
|
fContexts.reset();
|
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
|
|
|
*/
|
|
|
|
GrContext* get(GLContextType type) {
|
|
|
|
|
|
|
|
for (int i = 0; i < fContexts.count(); ++i) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-02-28 20:16:25 +00:00
|
|
|
SkAutoTUnref<SkGLContextHelper> 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:
|
2012-07-09 20:17:56 +00:00
|
|
|
glCtx.reset(SkNEW(SkNativeGLContext));
|
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;
|
|
|
|
}
|
|
|
|
if (!glCtx.get()->init(kBogusSize, kBogusSize)) {
|
|
|
|
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());
|
2012-10-25 18:43:28 +00:00
|
|
|
grCtx.reset(GrContext::Create(kOpenGL_GrBackend, p3dctx));
|
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.
|
2013-02-28 20:16:25 +00:00
|
|
|
SkGLContextHelper* 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;
|
|
|
|
}
|
|
|
|
|
2012-04-19 19:15:35 +00:00
|
|
|
private:
|
|
|
|
struct GPUContext {
|
|
|
|
GLContextType fType;
|
2013-02-28 20:16:25 +00:00
|
|
|
SkGLContextHelper* fGLContext;
|
2012-04-19 19:15:35 +00:00
|
|
|
GrContext* fGrContext;
|
|
|
|
};
|
|
|
|
SkTArray<GPUContext, true> fContexts;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|