Make the Sk GL context class an abstract base class

Make the Sk GL context class, SkGLNativeContext, an abstract base class. Before,
it depended on ifdefs to implement the platform dependent polymorphism.  Move
the logic to subclasses of the various platform implementations.

This a step to enable Skia embedders to compile dm and bench_pictures. The
concrete goal is to support running these test apps with Chromium command buffer.

With this change, Chromium can implement its own version of SkGLNativeContext
that uses command buffer, and host the implementation in its own repository.

Implements the above by renaming the SkGLContextHelper to SkGLContext and
removing the unneeded SkGLNativeContext. Also removes
SkGLNativeContext::AutoRestoreContext functionality, it appeared to be unused:
no use in Skia code, and no tests.

BUG=skia:2992

Committed: https://skia.googlesource.com/skia/+/a90ed4e83897b45d6331ee4c54e1edd4054de9a8

Review URL: https://codereview.chromium.org/630843002
This commit is contained in:
kkinnunen 2014-10-09 05:24:15 -07:00 committed by Commit bot
parent 78c71272fb
commit 9e61bb7815
29 changed files with 312 additions and 351 deletions

View File

@ -84,7 +84,7 @@ static SkString humanize(double ms) {
} }
#define HUMANIZE(ms) humanize(ms).c_str() #define HUMANIZE(ms) humanize(ms).c_str()
static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) { static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContext* gl) {
if (canvas) { if (canvas) {
canvas->clear(SK_ColorWHITE); canvas->clear(SK_ColorWHITE);
} }
@ -203,7 +203,7 @@ static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas,
} }
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
static int gpu_bench(SkGLContextHelper* gl, static int gpu_bench(SkGLContext* gl,
Benchmark* bench, Benchmark* bench,
SkCanvas* canvas, SkCanvas* canvas,
double* samples) { double* samples) {
@ -274,7 +274,7 @@ struct Target {
const Config config; const Config config;
SkAutoTDelete<SkSurface> surface; SkAutoTDelete<SkSurface> surface;
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
SkGLContextHelper* gl; SkGLContext* gl;
#endif #endif
}; };
@ -389,7 +389,7 @@ static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
} }
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) { static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
const GrGLubyte* version; const GrGLubyte* version;
SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION)); SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
log->configOption("GL_VERSION", (const char*)(version)); log->configOption("GL_VERSION", (const char*)(version));

View File

@ -279,7 +279,7 @@
'<(skia_include_path)/gpu/SkGrPixelRef.h', '<(skia_include_path)/gpu/SkGrPixelRef.h',
'<(skia_include_path)/gpu/SkGrTexturePixelRef.h', '<(skia_include_path)/gpu/SkGrTexturePixelRef.h',
'<(skia_include_path)/gpu/gl/SkGLContextHelper.h', '<(skia_include_path)/gpu/gl/SkGLContext.h',
'<(skia_src_path)/gpu/SkGpuDevice.cpp', '<(skia_src_path)/gpu/SkGpuDevice.cpp',
'<(skia_src_path)/gpu/SkGpuDevice.h', '<(skia_src_path)/gpu/SkGpuDevice.h',
@ -290,7 +290,7 @@
'<(skia_src_path)/image/SkImage_Gpu.cpp', '<(skia_src_path)/image/SkImage_Gpu.cpp',
'<(skia_src_path)/image/SkSurface_Gpu.cpp', '<(skia_src_path)/image/SkSurface_Gpu.cpp',
'<(skia_src_path)/gpu/gl/SkGLContextHelper.cpp' '<(skia_src_path)/gpu/gl/SkGLContext.cpp'
], ],
'skgpu_native_gl_sources': [ 'skgpu_native_gl_sources': [
'<(skia_src_path)/gpu/gl/GrGLDefaultInterface_native.cpp', '<(skia_src_path)/gpu/gl/GrGLDefaultInterface_native.cpp',
@ -302,13 +302,12 @@
'<(skia_src_path)/gpu/gl/android/GrGLCreateNativeInterface_android.cpp', '<(skia_src_path)/gpu/gl/android/GrGLCreateNativeInterface_android.cpp',
# Sk files # Sk files
'<(skia_include_path)/gpu/gl/SkNativeGLContext.h', '<(skia_src_path)/gpu/gl/mac/SkCreatePlatformGLContext_mac.cpp',
'<(skia_src_path)/gpu/gl/mac/SkNativeGLContext_mac.cpp', '<(skia_src_path)/gpu/gl/nacl/SkCreatePlatformGLContext_nacl.cpp',
'<(skia_src_path)/gpu/gl/nacl/SkNativeGLContext_nacl.cpp', '<(skia_src_path)/gpu/gl/win/SkCreatePlatformGLContext_win.cpp',
'<(skia_src_path)/gpu/gl/win/SkNativeGLContext_win.cpp', '<(skia_src_path)/gpu/gl/glx/SkCreatePlatformGLContext_glx.cpp',
'<(skia_src_path)/gpu/gl/glx/SkNativeGLContext_glx.cpp', '<(skia_src_path)/gpu/gl/egl/SkCreatePlatformGLContext_egl.cpp',
'<(skia_src_path)/gpu/gl/egl/SkNativeGLContext_egl.cpp', '<(skia_src_path)/gpu/gl/iOS/SkCreatePlatformGLContext_iOS.mm',
'<(skia_src_path)/gpu/gl/iOS/SkNativeGLContext_iOS.mm',
], ],
'skgpu_mesa_gl_sources': [ 'skgpu_mesa_gl_sources': [
'<(skia_src_path)/gpu/gl/mesa/GrGLCreateMesaInterface.cpp', '<(skia_src_path)/gpu/gl/mesa/GrGLCreateMesaInterface.cpp',

View File

@ -183,9 +183,8 @@
'gpu/gl/GrGLInterface.h', 'gpu/gl/GrGLInterface.h',
'gpu/gl/SkANGLEGLContext.h', 'gpu/gl/SkANGLEGLContext.h',
'gpu/gl/SkDebugGLContext.h', 'gpu/gl/SkDebugGLContext.h',
'gpu/gl/SkGLContextHelper.h',
'gpu/gl/SkMesaGLContext.h', 'gpu/gl/SkMesaGLContext.h',
'gpu/gl/SkNativeGLContext.h', 'gpu/gl/SkGLContext.h',
'gpu/gl/SkNullGLContext.h', 'gpu/gl/SkNullGLContext.h',
'images/SkForceLinking.h', 'images/SkForceLinking.h',
'images/SkMovie.h', 'images/SkMovie.h',

View File

@ -15,7 +15,7 @@
#if SK_MESA #if SK_MESA
#include "gl/SkMesaGLContext.h" #include "gl/SkMesaGLContext.h"
#endif #endif
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
#include "gl/SkNullGLContext.h" #include "gl/SkNullGLContext.h"
#include "GrContext.h" #include "GrContext.h"
@ -130,12 +130,12 @@ public:
return fContexts[i].fGrContext; return fContexts[i].fGrContext;
} }
} }
SkAutoTUnref<SkGLContextHelper> glCtx; SkAutoTUnref<SkGLContext> glCtx;
SkAutoTUnref<GrContext> grCtx; SkAutoTUnref<GrContext> grCtx;
switch (type) { switch (type) {
case kNVPR_GLContextType: // fallthru case kNVPR_GLContextType: // fallthru
case kNative_GLContextType: case kNative_GLContextType:
glCtx.reset(SkNEW(SkNativeGLContext)); glCtx.reset(SkCreatePlatformGLContext());
break; break;
#ifdef SK_ANGLE #ifdef SK_ANGLE
case kANGLE_GLContextType: case kANGLE_GLContextType:
@ -192,7 +192,7 @@ public:
// Returns the GLContext of the given type. If it has not been created yet, // Returns the GLContext of the given type. If it has not been created yet,
// NULL is returned instead. // NULL is returned instead.
SkGLContextHelper* getGLContext(GLContextType type) { SkGLContext* getGLContext(GLContextType type) {
for (int i = 0; i < fContexts.count(); ++i) { for (int i = 0; i < fContexts.count(); ++i) {
if (fContexts[i].fType == type) { if (fContexts[i].fType == type) {
return fContexts[i].fGLContext; return fContexts[i].fGLContext;
@ -207,7 +207,7 @@ public:
private: private:
struct GPUContext { struct GPUContext {
GLContextType fType; GLContextType fType;
SkGLContextHelper* fGLContext; SkGLContext* fGLContext;
GrContext* fGrContext; GrContext* fGrContext;
}; };
SkTArray<GPUContext, true> fContexts; SkTArray<GPUContext, true> fContexts;

View File

@ -10,12 +10,12 @@
#if SK_ANGLE #if SK_ANGLE
#include "SkGLContextHelper.h" #include "SkGLContext.h"
#include <GLES2/gl2.h> #include <GLES2/gl2.h>
#include <EGL/egl.h> #include <EGL/egl.h>
class SkANGLEGLContext : public SkGLContextHelper { class SkANGLEGLContext : public SkGLContext {
public: public:
SkANGLEGLContext(); SkANGLEGLContext();
@ -24,17 +24,6 @@ public:
virtual void makeCurrent() const SK_OVERRIDE; virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE; virtual void swapBuffers() const SK_OVERRIDE;
class AutoContextRestore {
public:
AutoContextRestore();
~AutoContextRestore();
private:
EGLContext fOldEGLContext;
EGLDisplay fOldDisplay;
EGLSurface fOldSurface;
};
protected: protected:
virtual const GrGLInterface* createGLContext( virtual const GrGLInterface* createGLContext(
GrGLStandard forcedGpuAPI) SK_OVERRIDE; GrGLStandard forcedGpuAPI) SK_OVERRIDE;

View File

@ -8,9 +8,9 @@
#ifndef SkDebugGLContext_DEFINED #ifndef SkDebugGLContext_DEFINED
#define SkDebugGLContext_DEFINED #define SkDebugGLContext_DEFINED
#include "SkGLContextHelper.h" #include "SkGLContext.h"
class SkDebugGLContext : public SkGLContextHelper { class SkDebugGLContext : public SkGLContext {
public: public:
SkDebugGLContext() {} SkDebugGLContext() {}

View File

@ -5,8 +5,8 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#ifndef SkGLContextHelper_DEFINED #ifndef SkGLContext_DEFINED
#define SkGLContextHelper_DEFINED #define SkGLContext_DEFINED
#include "GrGLInterface.h" #include "GrGLInterface.h"
@ -15,12 +15,12 @@
* Provides a GrGLInterface struct of function pointers for the context. * Provides a GrGLInterface struct of function pointers for the context.
*/ */
class SK_API SkGLContextHelper : public SkRefCnt { class SK_API SkGLContext : public SkRefCnt {
public: public:
SK_DECLARE_INST_COUNT(SkGLContextHelper) SK_DECLARE_INST_COUNT(SkGLContext)
SkGLContextHelper(); SkGLContext();
virtual ~SkGLContextHelper(); virtual ~SkGLContext();
/** /**
* Initializes the context and makes it current. * Initializes the context and makes it current.
@ -81,6 +81,14 @@ private:
typedef SkRefCnt INHERITED; typedef SkRefCnt INHERITED;
}; };
/** Creates platform-dependent GL context object
* Note: If Skia embedder needs a custom GL context that sets up the GL
* interface, this function should be implemented by the embedder.
* Otherwise, the default implementation for the platform should be compiled in
* the library.
*/
SK_API SkGLContext* SkCreatePlatformGLContext();
/** /**
* Helper macros for using the GL context through the GrGLInterface. Example: * Helper macros for using the GL context through the GrGLInterface. Example:
* SK_GL(glCtx, GenTextures(1, &texID)); * SK_GL(glCtx, GenTextures(1, &texID));

View File

@ -8,11 +8,11 @@
#ifndef SkMesaGLContext_DEFINED #ifndef SkMesaGLContext_DEFINED
#define SkMesaGLContext_DEFINED #define SkMesaGLContext_DEFINED
#include "SkGLContextHelper.h" #include "SkGLContext.h"
#if SK_MESA #if SK_MESA
class SkMesaGLContext : public SkGLContextHelper { class SkMesaGLContext : public SkGLContext {
private: private:
typedef intptr_t Context; typedef intptr_t Context;
@ -24,19 +24,6 @@ public:
virtual void makeCurrent() const SK_OVERRIDE; virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE; virtual void swapBuffers() const SK_OVERRIDE;
class AutoContextRestore {
public:
AutoContextRestore();
~AutoContextRestore();
private:
Context fOldContext;
GrGLint fOldWidth;
GrGLint fOldHeight;
GrGLint fOldFormat;
void* fOldImage;
};
protected: protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE; virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE; virtual void destroyGLContext() SK_OVERRIDE;

View File

@ -1,111 +0,0 @@
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkNativeGLContext_DEFINED
#define SkNativeGLContext_DEFINED
#include "SkGLContextHelper.h"
/* This struct is taken from a mesa demo. Please update as required */
static const struct { int major, minor; } gl_versions[] = {
{1, 0},
{1, 1},
{1, 2},
{1, 3},
{1, 4},
{1, 5},
{2, 0},
{2, 1},
{3, 0},
{3, 1},
{3, 2},
{3, 3},
{4, 0},
{4, 1},
{4, 2},
{4, 3},
{4, 4},
{0, 0} /* end of list */
};
#define NUM_GL_VERSIONS SK_ARRAY_COUNT(gl_versions)
#if defined(SK_BUILD_FOR_MAC)
#include <OpenGL/OpenGL.h>
#elif SK_EGL
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#elif defined(SK_BUILD_FOR_UNIX)
#include <X11/Xlib.h>
#include <GL/glx.h>
#elif defined(SK_BUILD_FOR_WIN32)
#include <windows.h>
#include <GL/GL.h>
#include "SkWGL.h"
#endif
class SkNativeGLContext : public SkGLContextHelper {
public:
SkNativeGLContext();
virtual ~SkNativeGLContext();
virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
class AutoContextRestore {
public:
AutoContextRestore();
~AutoContextRestore();
private:
#if defined(SK_BUILD_FOR_MAC)
CGLContextObj fOldCGLContext;
#elif SK_EGL
EGLContext fOldEGLContext;
EGLDisplay fOldDisplay;
EGLSurface fOldSurface;
#elif defined(SK_BUILD_FOR_UNIX)
GLXContext fOldGLXContext;
Display* fOldDisplay;
GLXDrawable fOldDrawable;
#elif defined(SK_BUILD_FOR_WIN32)
HDC fOldHDC;
HGLRC fOldHGLRC;
#elif defined(SK_BUILD_FOR_IOS)
void* fEAGLContext;
#endif
};
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
private:
#if defined(SK_BUILD_FOR_MAC)
CGLContextObj fContext;
#elif SK_EGL
EGLContext fContext;
EGLDisplay fDisplay;
EGLSurface fSurface;
#elif defined(SK_BUILD_FOR_UNIX)
GLXContext fContext;
Display* fDisplay;
Pixmap fPixmap;
GLXPixmap fGlxPixmap;
#elif defined(SK_BUILD_FOR_WIN32)
HWND fWindow;
HDC fDeviceContext;
HGLRC fGlRenderContext;
static ATOM gWC;
SkWGLPbufferContext* fPbufferContext;
#elif defined(SK_BUILD_FOR_IOS)
void* fEAGLContext;
#endif
};
#endif

View File

@ -8,9 +8,9 @@
#ifndef SkNullGLContext_DEFINED #ifndef SkNullGLContext_DEFINED
#define SkNullGLContext_DEFINED #define SkNullGLContext_DEFINED
#include "SkGLContextHelper.h" #include "SkGLContext.h"
class SK_API SkNullGLContext : public SkGLContextHelper { class SK_API SkNullGLContext : public SkGLContext {
public: public:
SkNullGLContext() {}; SkNullGLContext() {};

View File

@ -5,17 +5,17 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "gl/SkGLContextHelper.h" #include "gl/SkGLContext.h"
#include "GrGLUtil.h" #include "GrGLUtil.h"
SkGLContextHelper::SkGLContextHelper() SkGLContext::SkGLContext()
: fFBO(0) : fFBO(0)
, fColorBufferID(0) , fColorBufferID(0)
, fDepthStencilBufferID(0) , fDepthStencilBufferID(0)
, fGL(NULL) { , fGL(NULL) {
} }
SkGLContextHelper::~SkGLContextHelper() { SkGLContext::~SkGLContext() {
if (fGL) { if (fGL) {
// TODO: determine why DeleteFramebuffers is generating a GL error in tests // TODO: determine why DeleteFramebuffers is generating a GL error in tests
@ -27,7 +27,7 @@ SkGLContextHelper::~SkGLContextHelper() {
SkSafeUnref(fGL); SkSafeUnref(fGL);
} }
bool SkGLContextHelper::init(GrGLStandard forcedGpuAPI, int width, bool SkGLContext::init(GrGLStandard forcedGpuAPI, int width,
int height) { int height) {
if (fGL) { if (fGL) {
fGL->unref(); fGL->unref();
@ -135,7 +135,7 @@ bool SkGLContextHelper::init(GrGLStandard forcedGpuAPI, int width,
return false; return false;
} }
void SkGLContextHelper::testAbandon() { void SkGLContext::testAbandon() {
if (fGL) { if (fGL) {
fGL->abandon(); fGL->abandon();
} }

View File

@ -8,21 +8,6 @@
#include "gl/SkANGLEGLContext.h" #include "gl/SkANGLEGLContext.h"
SkANGLEGLContext::AutoContextRestore::AutoContextRestore() {
fOldEGLContext = eglGetCurrentContext();
fOldDisplay = eglGetCurrentDisplay();
fOldSurface = eglGetCurrentSurface(EGL_DRAW);
}
SkANGLEGLContext::AutoContextRestore::~AutoContextRestore() {
if (fOldDisplay) {
eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
}
}
///////////////////////////////////////////////////////////////////////////////
SkANGLEGLContext::SkANGLEGLContext() SkANGLEGLContext::SkANGLEGLContext()
: fContext(EGL_NO_CONTEXT) : fContext(EGL_NO_CONTEXT)
, fDisplay(EGL_NO_DISPLAY) , fDisplay(EGL_NO_DISPLAY)

View File

@ -819,7 +819,7 @@ public:
// The solution to this is probably to alter SkDebugGlContext's // The solution to this is probably to alter SkDebugGlContext's
// "makeCurrent" method to make a call like "makeCurrent(this)" to // "makeCurrent" method to make a call like "makeCurrent(this)" to
// the debug GL interface (assuming that the application will create // the debug GL interface (assuming that the application will create
// multiple SkGLContextHelper's) to let it switch between the active // multiple SkGLContext's) to let it switch between the active
// context. Everything in the GrDebugGL object would then need to be // context. Everything in the GrDebugGL object would then need to be
// moved to a GrContextObj and the GrDebugGL object would just switch // moved to a GrContextObj and the GrDebugGL object would just switch
// between them. Note that this approach would also require that // between them. Note that this approach would also require that

View File

@ -5,34 +5,42 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
SkNativeGLContext::AutoContextRestore::AutoContextRestore() { #include <GLES2/gl2.h>
fOldEGLContext = eglGetCurrentContext(); #include <EGL/egl.h>
fOldDisplay = eglGetCurrentDisplay();
fOldSurface = eglGetCurrentSurface(EGL_DRAW);
} namespace {
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { class EGLGLContext : public SkGLContext {
if (fOldDisplay) { public:
eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext); EGLGLContext();
}
}
/////////////////////////////////////////////////////////////////////////////// virtual ~EGLGLContext();
SkNativeGLContext::SkNativeGLContext() virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
private:
EGLContext fContext;
EGLDisplay fDisplay;
EGLSurface fSurface;
};
EGLGLContext::EGLGLContext()
: fContext(EGL_NO_CONTEXT) : fContext(EGL_NO_CONTEXT)
, fDisplay(EGL_NO_DISPLAY) , fDisplay(EGL_NO_DISPLAY)
, fSurface(EGL_NO_SURFACE) { , fSurface(EGL_NO_SURFACE) {
} }
SkNativeGLContext::~SkNativeGLContext() { EGLGLContext::~EGLGLContext() {
this->destroyGLContext(); this->destroyGLContext();
} }
void SkNativeGLContext::destroyGLContext() { void EGLGLContext::destroyGLContext() {
if (fDisplay) { if (fDisplay) {
eglMakeCurrent(fDisplay, 0, 0, 0); eglMakeCurrent(fDisplay, 0, 0, 0);
@ -51,7 +59,7 @@ void SkNativeGLContext::destroyGLContext() {
} }
} }
const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { const GrGLInterface* EGLGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
static const EGLint kEGLContextAttribsForOpenGL[] = { static const EGLint kEGLContextAttribsForOpenGL[] = {
EGL_NONE EGL_NONE
}; };
@ -169,14 +177,21 @@ const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAP
return interface; return interface;
} }
void SkNativeGLContext::makeCurrent() const { void EGLGLContext::makeCurrent() const {
if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
SkDebugf("Could not set the context.\n"); SkDebugf("Could not set the context.\n");
} }
} }
void SkNativeGLContext::swapBuffers() const { void EGLGLContext::swapBuffers() const {
if (!eglSwapBuffers(fDisplay, fSurface)) { if (!eglSwapBuffers(fDisplay, fSurface)) {
SkDebugf("Could not complete eglSwapBuffers.\n"); SkDebugf("Could not complete eglSwapBuffers.\n");
} }
} }
} // anonymous namespace
SkGLContext* SkCreatePlatformGLContext() {
return SkNEW(EGLGLContext);
}

View File

@ -5,25 +5,38 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
#include <X11/Xlib.h>
#include <GL/glx.h>
#include <GL/glu.h> #include <GL/glu.h>
namespace {
/* Note: Skia requires glx 1.3 or newer */ /* Note: Skia requires glx 1.3 or newer */
SkNativeGLContext::AutoContextRestore::AutoContextRestore() { /* This struct is taken from a mesa demo. Please update as required */
fOldGLXContext = glXGetCurrentContext(); static const struct { int major, minor; } gl_versions[] = {
fOldDisplay = glXGetCurrentDisplay(); {1, 0},
fOldDrawable = glXGetCurrentDrawable(); {1, 1},
} {1, 2},
{1, 3},
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { {1, 4},
if (fOldDisplay) { {1, 5},
glXMakeCurrent(fOldDisplay, fOldDrawable, fOldGLXContext); {2, 0},
} {2, 1},
} {3, 0},
{3, 1},
/////////////////////////////////////////////////////////////////////////////// {3, 2},
{3, 3},
{4, 0},
{4, 1},
{4, 2},
{4, 3},
{4, 4},
{0, 0} /* end of list */
};
#define NUM_GL_VERSIONS SK_ARRAY_COUNT(gl_versions)
static bool ctxErrorOccurred = false; static bool ctxErrorOccurred = false;
static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) { static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
@ -31,18 +44,37 @@ static int ctxErrorHandler(Display *dpy, XErrorEvent *ev) {
return 0; return 0;
} }
SkNativeGLContext::SkNativeGLContext() class GLXGLContext : public SkGLContext {
public:
GLXGLContext();
virtual ~GLXGLContext();
virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
private:
GLXContext fContext;
Display* fDisplay;
Pixmap fPixmap;
GLXPixmap fGlxPixmap;
};
GLXGLContext::GLXGLContext()
: fContext(NULL) : fContext(NULL)
, fDisplay(NULL) , fDisplay(NULL)
, fPixmap(0) , fPixmap(0)
, fGlxPixmap(0) { , fGlxPixmap(0) {
} }
SkNativeGLContext::~SkNativeGLContext() { GLXGLContext::~GLXGLContext() {
this->destroyGLContext(); this->destroyGLContext();
} }
void SkNativeGLContext::destroyGLContext() { void GLXGLContext::destroyGLContext() {
if (fDisplay) { if (fDisplay) {
glXMakeCurrent(fDisplay, 0, 0); glXMakeCurrent(fDisplay, 0, 0);
@ -66,7 +98,7 @@ void SkNativeGLContext::destroyGLContext() {
} }
} }
const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { const GrGLInterface* GLXGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
fDisplay = XOpenDisplay(0); fDisplay = XOpenDisplay(0);
if (!fDisplay) { if (!fDisplay) {
@ -277,12 +309,18 @@ const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAP
return interface; return interface;
} }
void SkNativeGLContext::makeCurrent() const { void GLXGLContext::makeCurrent() const {
if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) { if (!glXMakeCurrent(fDisplay, fGlxPixmap, fContext)) {
SkDebugf("Could not set the context.\n"); SkDebugf("Could not set the context.\n");
} }
} }
void SkNativeGLContext::swapBuffers() const { void GLXGLContext::swapBuffers() const {
glXSwapBuffers(fDisplay, fGlxPixmap); glXSwapBuffers(fDisplay, fGlxPixmap);
} }
} // anonymous namespace
SkGLContext* SkCreatePlatformGLContext() {
return SkNEW(GLXGLContext);
}

View File

@ -6,36 +6,38 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
#import <OpenGLES/EAGL.h> #import <OpenGLES/EAGL.h>
#define EAGLCTX ((EAGLContext*)(fEAGLContext)) #define EAGLCTX ((EAGLContext*)(fEAGLContext))
SkNativeGLContext::AutoContextRestore::AutoContextRestore() { namespace {
fEAGLContext = [EAGLContext currentContext];
if (EAGLCTX) {
[EAGLCTX retain];
}
}
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { class IOSGLContext : public SkGLContext {
if (EAGLCTX) { public:
[EAGLContext setCurrentContext:EAGLCTX]; IOSGLContext();
[EAGLCTX release];
}
}
/////////////////////////////////////////////////////////////////////////////// virtual ~IOSGLContext();
SkNativeGLContext::SkNativeGLContext() virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
private:
void* fEAGLContext;
};
IOSGLContext::IOSGLContext()
: fEAGLContext(NULL) { : fEAGLContext(NULL) {
} }
SkNativeGLContext::~SkNativeGLContext() { IOSGLContext::~IOSGLContext() {
this->destroyGLContext(); this->destroyGLContext();
} }
void SkNativeGLContext::destroyGLContext() { void IOSGLContext::destroyGLContext() {
if (fEAGLContext) { if (fEAGLContext) {
if ([EAGLContext currentContext] == EAGLCTX) { if ([EAGLContext currentContext] == EAGLCTX) {
[EAGLContext setCurrentContext:nil]; [EAGLContext setCurrentContext:nil];
@ -45,7 +47,7 @@ void SkNativeGLContext::destroyGLContext() {
} }
} }
const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { const GrGLInterface* IOSGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
if (kGL_GrGLStandard == forcedGpuAPI) { if (kGL_GrGLStandard == forcedGpuAPI) {
return NULL; return NULL;
} }
@ -62,10 +64,18 @@ const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAP
return interface; return interface;
} }
void SkNativeGLContext::makeCurrent() const { void IOSGLContext::makeCurrent() const {
if (![EAGLContext setCurrentContext:EAGLCTX]) { if (![EAGLContext setCurrentContext:EAGLCTX]) {
SkDebugf("Could not set the context.\n"); SkDebugf("Could not set the context.\n");
} }
} }
void SkNativeGLContext::swapBuffers() const { } void IOSGLContext::swapBuffers() const { }
} // anonymous namespace
SkGLContext* SkCreatePlatformGLContext() {
return SkNEW(IOSGLContext);
}

View File

@ -5,34 +5,43 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
#include "AvailabilityMacros.h" #include "AvailabilityMacros.h"
SkNativeGLContext::AutoContextRestore::AutoContextRestore() { #include <OpenGL/OpenGL.h>
fOldCGLContext = CGLGetCurrentContext();
}
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { namespace {
CGLSetCurrentContext(fOldCGLContext); class MacGLContext : public SkGLContext {
} public:
MacGLContext();
/////////////////////////////////////////////////////////////////////////////// virtual ~MacGLContext();
SkNativeGLContext::SkNativeGLContext() virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
private:
CGLContextObj fContext;
};
MacGLContext::MacGLContext()
: fContext(NULL) { : fContext(NULL) {
} }
SkNativeGLContext::~SkNativeGLContext() { MacGLContext::~MacGLContext() {
this->destroyGLContext(); this->destroyGLContext();
} }
void SkNativeGLContext::destroyGLContext() { void MacGLContext::destroyGLContext() {
if (fContext) { if (fContext) {
CGLReleaseContext(fContext); CGLReleaseContext(fContext);
} }
} }
const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { const GrGLInterface* MacGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
SkASSERT(NULL == fContext); SkASSERT(NULL == fContext);
if (kGLES_GrGLStandard == forcedGpuAPI) { if (kGLES_GrGLStandard == forcedGpuAPI) {
return NULL; return NULL;
@ -75,10 +84,16 @@ const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAP
return interface; return interface;
} }
void SkNativeGLContext::makeCurrent() const { void MacGLContext::makeCurrent() const {
CGLSetCurrentContext(fContext); CGLSetCurrentContext(fContext);
} }
void SkNativeGLContext::swapBuffers() const { void MacGLContext::swapBuffers() const {
CGLFlushDrawable(fContext); CGLFlushDrawable(fContext);
} }
} // anonymous namespace
SkGLContext* SkCreatePlatformGLContext() {
return SkNEW(MacGLContext);
}

View File

@ -11,24 +11,6 @@
#include "gl/SkMesaGLContext.h" #include "gl/SkMesaGLContext.h"
#include "gl/GrGLDefines.h" #include "gl/GrGLDefines.h"
SkMesaGLContext::AutoContextRestore::AutoContextRestore() {
fOldContext = (Context)OSMesaGetCurrentContext();
if (fOldContext) {
OSMesaGetColorBuffer((OSMesaContext)fOldContext,
&fOldWidth, &fOldHeight,
&fOldFormat, &fOldImage);
}
}
SkMesaGLContext::AutoContextRestore::~AutoContextRestore() {
if (fOldContext) {
OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage,
fOldFormat, fOldWidth, fOldHeight);
}
}
///////////////////////////////////////////////////////////////////////////////
SkMesaGLContext::SkMesaGLContext() SkMesaGLContext::SkMesaGLContext()
: fContext(static_cast<Context>(NULL)) : fContext(static_cast<Context>(NULL))
, fImage(NULL) { , fImage(NULL) {

View File

@ -0,0 +1,60 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gl/SkGLContext.h"
#include <GLES2/gl2.h>
#include <EGL/egl.h>
namespace {
class NACLGLContext : public SkGLContext {
public:
SkGLContextEGL();
virtual ~NACLGLContext();
virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
private:
EGLContext fContext;
EGLDisplay fDisplay;
EGLSurface fSurface;
};
NACLGLContext::NACLGLContext()
: fContext(NULL)
, fDisplay(NULL)
{
}
NACLGLContext::~NACLGLContext() {
this->destroyGLContext();
}
void NACLGLContext::destroyGLContext() {
}
const GrGLInterface* NACLGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
return NULL;
}
void NACLGLContext::makeCurrent() const {
}
void NACLGLContext::swapBuffers() const {
}
} // anonymous namespace
NACLGLContext* SkCreatePlatformGLContext() {
return SkNEW(NACLGLContext);
}

View File

@ -1,37 +0,0 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gl/SkNativeGLContext.h"
SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
}
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
}
SkNativeGLContext::SkNativeGLContext()
: fContext(NULL)
, fDisplay(NULL)
{
}
SkNativeGLContext::~SkNativeGLContext() {
this->destroyGLContext();
}
void SkNativeGLContext::destroyGLContext() {
}
const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
return NULL;
}
void SkNativeGLContext::makeCurrent() const {
}
void SkNativeGLContext::swapBuffers() const {
}

View File

@ -6,36 +6,51 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
#include <windows.h>
#include <GL/GL.h>
#include "SkWGL.h"
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
SkNativeGLContext::AutoContextRestore::AutoContextRestore() { namespace {
fOldHGLRC = wglGetCurrentContext();
fOldHDC = wglGetCurrentDC();
}
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { class WinGLContext : public SkGLContext {
wglMakeCurrent(fOldHDC, fOldHGLRC); public:
} WinGLContext();
/////////////////////////////////////////////////////////////////////////////// virtual ~WinGLContext();
ATOM SkNativeGLContext::gWC = 0; virtual void makeCurrent() const SK_OVERRIDE;
virtual void swapBuffers() const SK_OVERRIDE;
protected:
virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_OVERRIDE;
virtual void destroyGLContext() SK_OVERRIDE;
SkNativeGLContext::SkNativeGLContext() private:
HWND fWindow;
HDC fDeviceContext;
HGLRC fGlRenderContext;
static ATOM gWC;
SkWGLPbufferContext* fPbufferContext;
};
ATOM WinGLContext::gWC = 0;
WinGLContext::WinGLContext()
: fWindow(NULL) : fWindow(NULL)
, fDeviceContext(NULL) , fDeviceContext(NULL)
, fGlRenderContext(0) , fGlRenderContext(0)
, fPbufferContext(NULL) { , fPbufferContext(NULL) {
} }
SkNativeGLContext::~SkNativeGLContext() { WinGLContext::~WinGLContext() {
this->destroyGLContext(); this->destroyGLContext();
} }
void SkNativeGLContext::destroyGLContext() { void WinGLContext::destroyGLContext() {
SkSafeSetNull(fPbufferContext); SkSafeSetNull(fPbufferContext);
if (fGlRenderContext) { if (fGlRenderContext) {
wglDeleteContext(fGlRenderContext); wglDeleteContext(fGlRenderContext);
@ -51,7 +66,7 @@ void SkNativeGLContext::destroyGLContext() {
} }
} }
const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { const GrGLInterface* WinGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
if (!gWC) { if (!gWC) {
@ -134,7 +149,7 @@ const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAP
return interface; return interface;
} }
void SkNativeGLContext::makeCurrent() const { void WinGLContext::makeCurrent() const {
HDC dc; HDC dc;
HGLRC glrc; HGLRC glrc;
@ -151,7 +166,7 @@ void SkNativeGLContext::makeCurrent() const {
} }
} }
void SkNativeGLContext::swapBuffers() const { void WinGLContext::swapBuffers() const {
HDC dc; HDC dc;
if (NULL == fPbufferContext) { if (NULL == fPbufferContext) {
@ -163,3 +178,10 @@ void SkNativeGLContext::swapBuffers() const {
SkDebugf("Could not complete SwapBuffers.\n"); SkDebugf("Could not complete SwapBuffers.\n");
} }
} }
} // anonymous namespace
SkGLContext* SkCreatePlatformGLContext() {
return SkNEW(WinGLContext);
}

View File

@ -17,20 +17,20 @@ DEF_GPUTEST(GLInterfaceValidation, reporter, factory) {
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType)i; GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType)i;
// this forces the factory to make the context if it hasn't yet // this forces the factory to make the context if it hasn't yet
factory->get(glCtxType); factory->get(glCtxType);
SkGLContextHelper* glCtxHelper = factory->getGLContext(glCtxType); SkGLContext* glCtx = factory->getGLContext(glCtxType);
// We're supposed to fail the NVPR context type when we the native context that does not // We're supposed to fail the NVPR context type when we the native context that does not
// support the NVPR extension. // support the NVPR extension.
if (GrContextFactory::kNVPR_GLContextType == glCtxType && if (GrContextFactory::kNVPR_GLContextType == glCtxType &&
factory->getGLContext(GrContextFactory::kNative_GLContextType) && factory->getGLContext(GrContextFactory::kNative_GLContextType) &&
!factory->getGLContext(GrContextFactory::kNative_GLContextType)->hasExtension("GL_NV_path_rendering")) { !factory->getGLContext(GrContextFactory::kNative_GLContextType)->hasExtension("GL_NV_path_rendering")) {
REPORTER_ASSERT(reporter, NULL == glCtxHelper); REPORTER_ASSERT(reporter, NULL == glCtx);
continue; continue;
} }
REPORTER_ASSERT(reporter, glCtxHelper); REPORTER_ASSERT(reporter, glCtx);
if (glCtxHelper) { if (glCtx) {
const GrGLInterface* interface = glCtxHelper->gl(); const GrGLInterface* interface = glCtx->gl();
REPORTER_ASSERT(reporter, interface->validate()); REPORTER_ASSERT(reporter, interface->validate());
} }
} }

View File

@ -15,7 +15,7 @@
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
#include "GrContext.h" #include "GrContext.h"
#include "gl/SkNativeGLContext.h" #include "gl/SkGLContext.h"
#else #else
class GrContext; class GrContext;
#endif #endif

View File

@ -232,7 +232,7 @@ void PictureRenderer::buildBBoxHierarchy() {
void PictureRenderer::resetState(bool callFinish) { void PictureRenderer::resetState(bool callFinish) {
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
SkGLContextHelper* glContext = this->getGLContext(); SkGLContext* glContext = this->getGLContext();
if (NULL == glContext) { if (NULL == glContext) {
SkASSERT(kBitmap_DeviceType == fDeviceType); SkASSERT(kBitmap_DeviceType == fDeviceType);
return; return;
@ -252,7 +252,7 @@ void PictureRenderer::purgeTextures() {
pool->dumpPool(); pool->dumpPool();
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
SkGLContextHelper* glContext = this->getGLContext(); SkGLContext* glContext = this->getGLContext();
if (NULL == glContext) { if (NULL == glContext) {
SkASSERT(kBitmap_DeviceType == fDeviceType); SkASSERT(kBitmap_DeviceType == fDeviceType);
return; return;

View File

@ -30,7 +30,7 @@
class SkBitmap; class SkBitmap;
class SkCanvas; class SkCanvas;
class SkGLContextHelper; class SkGLContext;
class SkThread; class SkThread;
namespace sk_tools { namespace sk_tools {
@ -371,7 +371,7 @@ public:
} }
} }
SkGLContextHelper* getGLContext() { SkGLContext* getGLContext() {
GrContextFactory::GLContextType glContextType GrContextFactory::GLContextType glContextType
= GrContextFactory::kNull_GLContextType; = GrContextFactory::kNull_GLContextType;
switch(fDeviceType) { switch(fDeviceType) {

View File

@ -6,10 +6,10 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "GpuTimer.h" #include "GpuTimer.h"
#include "gl/SkGLContextHelper.h" #include "gl/SkGLContext.h"
#include "gl/GrGLUtil.h" #include "gl/GrGLUtil.h"
GpuTimer::GpuTimer(const SkGLContextHelper* glctx) : fContext(glctx) { GpuTimer::GpuTimer(const SkGLContext* glctx) : fContext(glctx) {
if (fContext) { if (fContext) {
fContext->ref(); fContext->ref();
fContext->makeCurrent(); fContext->makeCurrent();

View File

@ -7,18 +7,18 @@
#ifndef GpuTimer_DEFINED #ifndef GpuTimer_DEFINED
#define GpuTimer_DEFINED #define GpuTimer_DEFINED
class SkGLContextHelper; class SkGLContext;
class GpuTimer { class GpuTimer {
public: public:
GpuTimer(const SkGLContextHelper*); GpuTimer(const SkGLContext*);
~GpuTimer(); ~GpuTimer();
void start(); void start();
double end(); double end();
private: private:
unsigned fQuery; unsigned fQuery;
int fStarted; int fStarted;
const SkGLContextHelper* fContext; const SkGLContext* fContext;
bool fSupported; bool fSupported;
}; };

View File

@ -6,7 +6,7 @@
*/ */
#include "Timer.h" #include "Timer.h"
Timer::Timer(SkGLContextHelper* gl) Timer::Timer(SkGLContext* gl)
: fCpu(-1.0) : fCpu(-1.0)
, fWall(-1.0) , fWall(-1.0)
, fTruncatedCpu(-1.0) , fTruncatedCpu(-1.0)

View File

@ -21,7 +21,7 @@
#include "GpuTimer.h" #include "GpuTimer.h"
#endif #endif
class SkGLContextHelper; class SkGLContext;
/** /**
* SysTimers and GpuTimers are implemented orthogonally. * SysTimers and GpuTimers are implemented orthogonally.
@ -34,7 +34,7 @@ class SkGLContextHelper;
*/ */
class Timer { class Timer {
public: public:
explicit Timer(SkGLContextHelper* gl = NULL); explicit Timer(SkGLContext* gl = NULL);
void start(); void start();
void truncatedEnd(); void truncatedEnd();