2016-03-31 01:56:19 +00:00
|
|
|
|
2011-11-03 13:08:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-03-31 17:22:40 +00:00
|
|
|
#include "gl/GLContext.h"
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
#include <GLES2/gl2.h>
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
#define EGL_EGLEXT_PROTOTYPES
|
2014-10-09 12:24:15 +00:00
|
|
|
#include <EGL/egl.h>
|
2015-06-23 20:23:44 +00:00
|
|
|
#include <EGL/eglext.h>
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2015-11-22 22:51:00 +00:00
|
|
|
#include "gl/GrGLDefines.h"
|
|
|
|
#include "gl/GrGLUtil.h"
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
namespace {
|
2014-10-08 11:14:24 +00:00
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
// TODO: Share this class with ANGLE if/when it gets support for EGL_KHR_fence_sync.
|
2016-03-31 17:22:40 +00:00
|
|
|
class SkEGLFenceSync : public SkGpuFenceSync {
|
2015-06-23 20:23:44 +00:00
|
|
|
public:
|
2016-03-31 17:22:40 +00:00
|
|
|
static SkEGLFenceSync* CreateIfSupported(EGLDisplay);
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override;
|
2016-01-20 18:54:58 +00:00
|
|
|
bool waitFence(SkPlatformGpuFence fence, bool flush) const override;
|
2015-06-23 20:23:44 +00:00
|
|
|
void deleteFence(SkPlatformGpuFence fence) const override;
|
|
|
|
|
|
|
|
private:
|
2016-03-31 17:22:40 +00:00
|
|
|
SkEGLFenceSync(EGLDisplay display) : fDisplay(display) {}
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
EGLDisplay fDisplay;
|
|
|
|
|
|
|
|
typedef SkGpuFenceSync INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
class EGLGLContext : public sk_gpu_test::GLContext {
|
2014-10-09 12:24:15 +00:00
|
|
|
public:
|
2016-03-31 17:22:40 +00:00
|
|
|
EGLGLContext(GrGLStandard forcedGpuAPI);
|
|
|
|
~EGLGLContext() override;
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2015-11-22 22:51:00 +00:00
|
|
|
GrEGLImage texture2DToEGLImage(GrGLuint texID) const override;
|
|
|
|
void destroyEGLImage(GrEGLImage) const override;
|
|
|
|
GrGLuint eglImageToExternalTexture(GrEGLImage) const override;
|
2016-03-31 17:22:40 +00:00
|
|
|
sk_gpu_test::GLContext* createNew() const override;
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
private:
|
2014-10-16 06:03:54 +00:00
|
|
|
void destroyGLContext();
|
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
void onPlatformMakeCurrent() const override;
|
|
|
|
void onPlatformSwapBuffers() const override;
|
|
|
|
GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
|
|
|
|
|
2014-10-09 12:24:15 +00:00
|
|
|
EGLContext fContext;
|
|
|
|
EGLDisplay fDisplay;
|
|
|
|
EGLSurface fSurface;
|
|
|
|
};
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
EGLGLContext::EGLGLContext(GrGLStandard forcedGpuAPI)
|
2011-11-03 13:08:29 +00:00
|
|
|
: fContext(EGL_NO_CONTEXT)
|
|
|
|
, fDisplay(EGL_NO_DISPLAY)
|
|
|
|
, fSurface(EGL_NO_SURFACE) {
|
2013-09-03 17:50:50 +00:00
|
|
|
static const EGLint kEGLContextAttribsForOpenGL[] = {
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EGLint kEGLContextAttribsForOpenGLES[] = {
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
2013-09-06 18:57:14 +00:00
|
|
|
static const struct {
|
|
|
|
const EGLint* fContextAttribs;
|
|
|
|
EGLenum fAPI;
|
|
|
|
EGLint fRenderableTypeBit;
|
2014-01-16 16:35:09 +00:00
|
|
|
GrGLStandard fStandard;
|
2013-09-06 18:57:14 +00:00
|
|
|
} kAPIs[] = {
|
|
|
|
{ // OpenGL
|
|
|
|
kEGLContextAttribsForOpenGL,
|
|
|
|
EGL_OPENGL_API,
|
|
|
|
EGL_OPENGL_BIT,
|
2014-01-16 16:35:09 +00:00
|
|
|
kGL_GrGLStandard
|
2013-09-06 18:57:14 +00:00
|
|
|
},
|
|
|
|
{ // OpenGL ES. This seems to work for both ES2 and 3 (when available).
|
|
|
|
kEGLContextAttribsForOpenGLES,
|
|
|
|
EGL_OPENGL_ES_API,
|
|
|
|
EGL_OPENGL_ES2_BIT,
|
2014-01-16 16:35:09 +00:00
|
|
|
kGLES_GrGLStandard
|
2013-09-06 18:57:14 +00:00
|
|
|
},
|
|
|
|
};
|
2013-09-03 17:50:50 +00:00
|
|
|
|
2014-06-30 13:36:31 +00:00
|
|
|
size_t apiLimit = SK_ARRAY_COUNT(kAPIs);
|
|
|
|
size_t api = 0;
|
|
|
|
if (forcedGpuAPI == kGL_GrGLStandard) {
|
|
|
|
apiLimit = 1;
|
|
|
|
} else if (forcedGpuAPI == kGLES_GrGLStandard) {
|
|
|
|
api = 1;
|
|
|
|
}
|
|
|
|
SkASSERT(forcedGpuAPI == kNone_GrGLStandard || kAPIs[api].fStandard == forcedGpuAPI);
|
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
SkAutoTUnref<const GrGLInterface> gl;
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
for (; nullptr == gl.get() && api < apiLimit; ++api) {
|
2013-09-06 18:57:14 +00:00
|
|
|
fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2013-09-06 18:57:14 +00:00
|
|
|
EGLint majorVersion;
|
|
|
|
EGLint minorVersion;
|
|
|
|
eglInitialize(fDisplay, &majorVersion, &minorVersion);
|
2012-09-11 20:39:09 +00:00
|
|
|
|
2013-09-06 18:57:14 +00:00
|
|
|
#if 0
|
|
|
|
SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR));
|
|
|
|
SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS));
|
|
|
|
SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION));
|
|
|
|
SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS));
|
|
|
|
#endif
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2013-09-06 18:57:14 +00:00
|
|
|
if (!eglBindAPI(kAPIs[api].fAPI)) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2014-12-01 14:46:01 +00:00
|
|
|
EGLint numConfigs = 0;
|
2013-09-06 18:57:14 +00:00
|
|
|
const EGLint configAttribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
|
|
|
|
EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit,
|
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_ALPHA_SIZE, 8,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLConfig surfaceConfig;
|
|
|
|
if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
|
|
|
|
SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError());
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2014-12-01 14:46:01 +00:00
|
|
|
if (0 == numConfigs) {
|
|
|
|
SkDebugf("No suitable EGL config found.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, kAPIs[api].fContextAttribs);
|
2013-09-06 18:57:14 +00:00
|
|
|
if (EGL_NO_CONTEXT == fContext) {
|
|
|
|
SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetError());
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2013-09-06 18:57:14 +00:00
|
|
|
static const EGLint kSurfaceAttribs[] = {
|
|
|
|
EGL_WIDTH, 1,
|
|
|
|
EGL_HEIGHT, 1,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs);
|
|
|
|
if (EGL_NO_SURFACE == fSurface) {
|
|
|
|
SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError());
|
|
|
|
this->destroyGLContext();
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2013-09-06 18:57:14 +00:00
|
|
|
if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
|
|
|
|
SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError());
|
|
|
|
this->destroyGLContext();
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-03 13:08:29 +00:00
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
gl.reset(GrGLCreateNativeInterface());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == gl.get()) {
|
2013-09-06 18:57:14 +00:00
|
|
|
SkDebugf("Failed to create gl interface.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-23 20:23:44 +00:00
|
|
|
if (!gl->validate()) {
|
2014-10-16 06:03:54 +00:00
|
|
|
SkDebugf("Failed to validate gl interface.\n");
|
2013-09-06 18:57:14 +00:00
|
|
|
this->destroyGLContext();
|
2014-10-16 06:03:54 +00:00
|
|
|
continue;
|
2013-09-06 18:57:14 +00:00
|
|
|
}
|
2015-06-23 20:23:44 +00:00
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
this->init(gl.release(), SkEGLFenceSync::CreateIfSupported(fDisplay));
|
2015-06-23 20:23:44 +00:00
|
|
|
break;
|
2011-11-03 13:08:29 +00:00
|
|
|
}
|
2014-10-16 06:03:54 +00:00
|
|
|
}
|
2013-09-03 17:50:50 +00:00
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
EGLGLContext::~EGLGLContext() {
|
2015-06-23 20:23:44 +00:00
|
|
|
this->teardown();
|
2014-10-16 06:03:54 +00:00
|
|
|
this->destroyGLContext();
|
2011-11-03 13:08:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
void EGLGLContext::destroyGLContext() {
|
2014-10-16 06:03:54 +00:00
|
|
|
if (fDisplay) {
|
|
|
|
eglMakeCurrent(fDisplay, 0, 0, 0);
|
|
|
|
|
|
|
|
if (fContext) {
|
|
|
|
eglDestroyContext(fDisplay, fContext);
|
|
|
|
fContext = EGL_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fSurface) {
|
|
|
|
eglDestroySurface(fDisplay, fSurface);
|
|
|
|
fSurface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO should we close the display?
|
|
|
|
fDisplay = EGL_NO_DISPLAY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
GrEGLImage EGLGLContext::texture2DToEGLImage(GrGLuint texID) const {
|
2015-11-22 22:51:00 +00:00
|
|
|
if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
|
|
|
|
return GR_EGL_NO_IMAGE;
|
|
|
|
}
|
|
|
|
GrEGLImage img;
|
|
|
|
GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, GR_EGL_NONE };
|
|
|
|
GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>(texID);
|
|
|
|
GR_GL_CALL_RET(this->gl(), img,
|
|
|
|
EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clientBuffer, attribs));
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
void EGLGLContext::destroyEGLImage(GrEGLImage image) const {
|
2015-11-22 22:51:00 +00:00
|
|
|
GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image));
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
GrGLuint EGLGLContext::eglImageToExternalTexture(GrEGLImage image) const {
|
2015-11-22 22:51:00 +00:00
|
|
|
GrGLClearErr(this->gl());
|
|
|
|
if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-18 19:07:24 +00:00
|
|
|
typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage);
|
|
|
|
|
|
|
|
EGLImageTargetTexture2DProc glEGLImageTargetTexture2D =
|
|
|
|
(EGLImageTargetTexture2DProc) eglGetProcAddress("glEGLImageTargetTexture2DOES");
|
2015-11-22 22:51:00 +00:00
|
|
|
if (!glEGLImageTargetTexture2D) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
GrGLuint texID;
|
|
|
|
glGenTextures(1, &texID);
|
|
|
|
if (!texID) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
glBindTexture(GR_GL_TEXTURE_EXTERNAL, texID);
|
|
|
|
if (glGetError() != GR_GL_NO_ERROR) {
|
|
|
|
glDeleteTextures(1, &texID);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image);
|
|
|
|
if (glGetError() != GR_GL_NO_ERROR) {
|
|
|
|
glDeleteTextures(1, &texID);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return texID;
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
sk_gpu_test::GLContext* EGLGLContext::createNew() const {
|
|
|
|
sk_gpu_test::GLContext* ctx = new EGLGLContext(this->gl()->fStandard);
|
2015-11-22 22:51:00 +00:00
|
|
|
if (ctx) {
|
|
|
|
ctx->makeCurrent();
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
2014-10-16 06:03:54 +00:00
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
void EGLGLContext::onPlatformMakeCurrent() const {
|
2011-11-03 13:08:29 +00:00
|
|
|
if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
|
|
|
|
SkDebugf("Could not set the context.\n");
|
|
|
|
}
|
|
|
|
}
|
2013-10-09 18:25:38 +00:00
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
void EGLGLContext::onPlatformSwapBuffers() const {
|
2013-10-09 18:25:38 +00:00
|
|
|
if (!eglSwapBuffers(fDisplay, fSurface)) {
|
|
|
|
SkDebugf("Could not complete eglSwapBuffers.\n");
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 12:24:15 +00:00
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
GrGLFuncPtr EGLGLContext::onPlatformGetProcAddress(const char* procName) const {
|
2015-06-23 20:23:44 +00:00
|
|
|
return eglGetProcAddress(procName);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool supports_egl_extension(EGLDisplay display, const char* extension) {
|
2015-12-10 21:46:04 +00:00
|
|
|
size_t extensionLength = strlen(extension);
|
2015-06-23 20:23:44 +00:00
|
|
|
const char* extensionsStr = eglQueryString(display, EGL_EXTENSIONS);
|
|
|
|
while (const char* match = strstr(extensionsStr, extension)) {
|
|
|
|
// Ensure the string we found is its own extension, not a substring of a larger extension
|
|
|
|
// (e.g. GL_ARB_occlusion_query / GL_ARB_occlusion_query2).
|
|
|
|
if ((match == extensionsStr || match[-1] == ' ') &&
|
|
|
|
(match[extensionLength] == ' ' || match[extensionLength] == '\0')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
extensionsStr = match + extensionLength;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
SkEGLFenceSync* SkEGLFenceSync::CreateIfSupported(EGLDisplay display) {
|
2015-06-23 20:23:44 +00:00
|
|
|
if (!display || !supports_egl_extension(display, "EGL_KHR_fence_sync")) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
2016-03-31 17:22:40 +00:00
|
|
|
return new SkEGLFenceSync(display);
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
SkPlatformGpuFence SkEGLFenceSync::insertFence() const {
|
2015-08-27 14:41:13 +00:00
|
|
|
return eglCreateSyncKHR(fDisplay, EGL_SYNC_FENCE_KHR, nullptr);
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
bool SkEGLFenceSync::waitFence(SkPlatformGpuFence platformFence, bool flush) const {
|
2015-06-23 20:23:44 +00:00
|
|
|
EGLSyncKHR eglsync = static_cast<EGLSyncKHR>(platformFence);
|
2016-01-20 18:54:58 +00:00
|
|
|
return EGL_CONDITION_SATISFIED_KHR ==
|
|
|
|
eglClientWaitSyncKHR(fDisplay,
|
|
|
|
eglsync,
|
|
|
|
flush ? EGL_SYNC_FLUSH_COMMANDS_BIT_KHR : 0,
|
|
|
|
EGL_FOREVER_KHR);
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:22:40 +00:00
|
|
|
void SkEGLFenceSync::deleteFence(SkPlatformGpuFence platformFence) const {
|
2015-06-23 20:23:44 +00:00
|
|
|
EGLSyncKHR eglsync = static_cast<EGLSyncKHR>(platformFence);
|
|
|
|
eglDestroySyncKHR(fDisplay, eglsync);
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
} // anonymous namespace
|
2014-10-09 12:24:15 +00:00
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
namespace sk_gpu_test {
|
2016-03-31 17:22:40 +00:00
|
|
|
GLContext *CreatePlatformGLContext(GrGLStandard forcedGpuAPI, GLContext *shareContext) {
|
2016-01-20 16:07:01 +00:00
|
|
|
SkASSERT(!shareContext);
|
|
|
|
if (shareContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-31 17:22:40 +00:00
|
|
|
EGLGLContext *ctx = new EGLGLContext(forcedGpuAPI);
|
2014-10-16 06:03:54 +00:00
|
|
|
if (!ctx->isValid()) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete ctx;
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-10-16 06:03:54 +00:00
|
|
|
}
|
|
|
|
return ctx;
|
2014-10-09 12:24:15 +00:00
|
|
|
}
|
2016-03-31 01:56:19 +00:00
|
|
|
} // namespace sk_gpu_test
|
|
|
|
|