2016-03-31 01:56:19 +00:00
|
|
|
|
2015-08-27 17:38:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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 01:56:19 +00:00
|
|
|
|
2016-04-13 22:03:59 +00:00
|
|
|
#include "SkMutex.h"
|
2015-08-27 17:38:39 +00:00
|
|
|
#include "SkOnce.h"
|
|
|
|
#include "gl/GrGLInterface.h"
|
|
|
|
#include "gl/GrGLAssembleInterface.h"
|
2016-03-31 17:59:06 +00:00
|
|
|
#include "gl/command_buffer/GLTestContext_command_buffer.h"
|
2015-10-02 01:29:34 +00:00
|
|
|
#include "../ports/SkOSLibrary.h"
|
2015-08-27 17:38:39 +00:00
|
|
|
|
2015-10-27 17:04:34 +00:00
|
|
|
typedef void *EGLDisplay;
|
|
|
|
typedef unsigned int EGLBoolean;
|
|
|
|
typedef void *EGLConfig;
|
|
|
|
typedef void *EGLSurface;
|
|
|
|
typedef void *EGLContext;
|
|
|
|
typedef int32_t EGLint;
|
|
|
|
typedef void* EGLNativeDisplayType;
|
|
|
|
typedef void* EGLNativeWindowType;
|
|
|
|
typedef void (*__eglMustCastToProperFunctionPointerType)(void);
|
|
|
|
#define EGL_FALSE 0
|
|
|
|
#define EGL_OPENGL_ES2_BIT 0x0004
|
|
|
|
#define EGL_CONTEXT_CLIENT_VERSION 0x3098
|
|
|
|
#define EGL_NO_SURFACE ((EGLSurface)0)
|
|
|
|
#define EGL_NO_DISPLAY ((EGLDisplay)0)
|
|
|
|
#define EGL_NO_CONTEXT ((EGLContext)0)
|
|
|
|
#define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)0)
|
|
|
|
#define EGL_SURFACE_TYPE 0x3033
|
|
|
|
#define EGL_PBUFFER_BIT 0x0001
|
|
|
|
#define EGL_RENDERABLE_TYPE 0x3040
|
|
|
|
#define EGL_RED_SIZE 0x3024
|
|
|
|
#define EGL_GREEN_SIZE 0x3023
|
|
|
|
#define EGL_BLUE_SIZE 0x3022
|
|
|
|
#define EGL_ALPHA_SIZE 0x3021
|
|
|
|
#define EGL_DEPTH_SIZE 0x3025
|
|
|
|
#define EGL_STENCIL_SIZE 0x3025
|
|
|
|
#define EGL_SAMPLES 0x3031
|
|
|
|
#define EGL_SAMPLE_BUFFERS 0x3032
|
|
|
|
#define EGL_NONE 0x3038
|
|
|
|
#define EGL_WIDTH 0x3057
|
|
|
|
#define EGL_HEIGHT 0x3056
|
|
|
|
|
2015-08-27 17:38:39 +00:00
|
|
|
typedef EGLDisplay (*GetDisplayProc)(EGLNativeDisplayType display_id);
|
|
|
|
typedef EGLBoolean (*InitializeProc)(EGLDisplay dpy, EGLint *major, EGLint *minor);
|
|
|
|
typedef EGLBoolean (*TerminateProc)(EGLDisplay dpy);
|
|
|
|
typedef EGLBoolean (*ChooseConfigProc)(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* configs, EGLint config_size, EGLint* num_config);
|
|
|
|
typedef EGLBoolean (*GetConfigAttrib)(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* value);
|
|
|
|
typedef EGLSurface (*CreateWindowSurfaceProc)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* attrib_list);
|
|
|
|
typedef EGLSurface (*CreatePbufferSurfaceProc)(EGLDisplay dpy, EGLConfig config, const EGLint* attrib_list);
|
|
|
|
typedef EGLBoolean (*DestroySurfaceProc)(EGLDisplay dpy, EGLSurface surface);
|
|
|
|
typedef EGLContext (*CreateContextProc)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint* attrib_list);
|
|
|
|
typedef EGLBoolean (*DestroyContextProc)(EGLDisplay dpy, EGLContext ctx);
|
|
|
|
typedef EGLBoolean (*MakeCurrentProc)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
|
|
|
|
typedef EGLBoolean (*SwapBuffersProc)(EGLDisplay dpy, EGLSurface surface);
|
|
|
|
typedef __eglMustCastToProperFunctionPointerType (*GetProcAddressProc)(const char* procname);
|
|
|
|
|
|
|
|
static GetDisplayProc gfGetDisplay = nullptr;
|
|
|
|
static InitializeProc gfInitialize = nullptr;
|
|
|
|
static TerminateProc gfTerminate = nullptr;
|
|
|
|
static ChooseConfigProc gfChooseConfig = nullptr;
|
|
|
|
static GetConfigAttrib gfGetConfigAttrib = nullptr;
|
|
|
|
static CreateWindowSurfaceProc gfCreateWindowSurface = nullptr;
|
|
|
|
static CreatePbufferSurfaceProc gfCreatePbufferSurface = nullptr;
|
|
|
|
static DestroySurfaceProc gfDestroySurface = nullptr;
|
|
|
|
static CreateContextProc gfCreateContext = nullptr;
|
|
|
|
static DestroyContextProc gfDestroyContext = nullptr;
|
|
|
|
static MakeCurrentProc gfMakeCurrent = nullptr;
|
|
|
|
static SwapBuffersProc gfSwapBuffers = nullptr;
|
|
|
|
static GetProcAddressProc gfGetProcAddress = nullptr;
|
|
|
|
|
2015-10-02 01:29:34 +00:00
|
|
|
static void* gLibrary = nullptr;
|
2015-08-27 17:38:39 +00:00
|
|
|
static bool gfFunctionsLoadedSuccessfully = false;
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
namespace {
|
2015-08-27 17:38:39 +00:00
|
|
|
static void load_command_buffer_functions() {
|
2015-10-02 01:29:34 +00:00
|
|
|
if (!gLibrary) {
|
2016-08-10 19:26:00 +00:00
|
|
|
static constexpr const char* libName =
|
2015-10-02 01:29:34 +00:00
|
|
|
#if defined _WIN32
|
2016-08-10 19:26:00 +00:00
|
|
|
"command_buffer_gles2.dll";
|
2015-10-27 17:04:34 +00:00
|
|
|
#elif defined SK_BUILD_FOR_MAC
|
2016-08-10 19:26:00 +00:00
|
|
|
"libcommand_buffer_gles2.dylib";
|
2015-10-02 01:29:34 +00:00
|
|
|
#else
|
2016-08-10 19:26:00 +00:00
|
|
|
"libcommand_buffer_gles2.so";
|
2015-10-02 01:29:34 +00:00
|
|
|
#endif // defined _WIN32
|
2016-08-10 19:26:00 +00:00
|
|
|
gLibrary = DynamicLoadLibrary(libName);
|
2015-10-02 01:29:34 +00:00
|
|
|
if (gLibrary) {
|
|
|
|
gfGetDisplay = (GetDisplayProc)GetProcedureAddress(gLibrary, "eglGetDisplay");
|
|
|
|
gfInitialize = (InitializeProc)GetProcedureAddress(gLibrary, "eglInitialize");
|
|
|
|
gfTerminate = (TerminateProc)GetProcedureAddress(gLibrary, "eglTerminate");
|
|
|
|
gfChooseConfig = (ChooseConfigProc)GetProcedureAddress(gLibrary, "eglChooseConfig");
|
|
|
|
gfGetConfigAttrib = (GetConfigAttrib)GetProcedureAddress(gLibrary, "eglGetConfigAttrib");
|
|
|
|
gfCreateWindowSurface = (CreateWindowSurfaceProc)GetProcedureAddress(gLibrary, "eglCreateWindowSurface");
|
|
|
|
gfCreatePbufferSurface = (CreatePbufferSurfaceProc)GetProcedureAddress(gLibrary, "eglCreatePbufferSurface");
|
|
|
|
gfDestroySurface = (DestroySurfaceProc)GetProcedureAddress(gLibrary, "eglDestroySurface");
|
|
|
|
gfCreateContext = (CreateContextProc)GetProcedureAddress(gLibrary, "eglCreateContext");
|
|
|
|
gfDestroyContext = (DestroyContextProc)GetProcedureAddress(gLibrary, "eglDestroyContext");
|
|
|
|
gfMakeCurrent = (MakeCurrentProc)GetProcedureAddress(gLibrary, "eglMakeCurrent");
|
|
|
|
gfSwapBuffers = (SwapBuffersProc)GetProcedureAddress(gLibrary, "eglSwapBuffers");
|
|
|
|
gfGetProcAddress = (GetProcAddressProc)GetProcedureAddress(gLibrary, "eglGetProcAddress");
|
2015-08-27 17:38:39 +00:00
|
|
|
|
|
|
|
gfFunctionsLoadedSuccessfully = gfGetDisplay && gfInitialize && gfTerminate &&
|
|
|
|
gfChooseConfig && gfCreateWindowSurface &&
|
|
|
|
gfCreatePbufferSurface && gfDestroySurface &&
|
|
|
|
gfCreateContext && gfDestroyContext && gfMakeCurrent &&
|
|
|
|
gfSwapBuffers && gfGetProcAddress;
|
|
|
|
|
2016-08-10 19:26:00 +00:00
|
|
|
} else {
|
|
|
|
SkDebugf("Could not load %s.\n", libName);
|
2015-10-27 17:04:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GrGLFuncPtr command_buffer_get_gl_proc(void* ctx, const char name[]) {
|
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return gfGetProcAddress(name);
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
static void load_command_buffer_once() {
|
2016-04-18 15:09:11 +00:00
|
|
|
static SkOnce once;
|
|
|
|
once(load_command_buffer_functions);
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
static const GrGLInterface* create_command_buffer_interface() {
|
|
|
|
load_command_buffer_once();
|
2015-08-27 17:38:39 +00:00
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-02 01:29:34 +00:00
|
|
|
return GrGLAssembleGLESInterface(gLibrary, command_buffer_get_gl_proc);
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
2017-02-21 21:58:08 +00:00
|
|
|
CommandBufferGLTestContext::CommandBufferGLTestContext(CommandBufferGLTestContext* shareContext)
|
2016-03-31 01:56:19 +00:00
|
|
|
: fContext(EGL_NO_CONTEXT), fDisplay(EGL_NO_DISPLAY), fSurface(EGL_NO_SURFACE) {
|
2015-08-27 17:38:39 +00:00
|
|
|
|
|
|
|
static const EGLint configAttribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
|
2016-03-03 15:39:48 +00:00
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
2015-08-27 17:38:39 +00:00
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_ALPHA_SIZE, 8,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EGLint surfaceAttribs[] = {
|
|
|
|
EGL_WIDTH, 1,
|
|
|
|
EGL_HEIGHT, 1,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
load_command_buffer_once();
|
2015-08-27 17:38:39 +00:00
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
2016-09-29 17:49:46 +00:00
|
|
|
static SkOnce once;
|
|
|
|
once([] { SkDebugf("Command Buffer: Could not load EGL functions.\n"); });
|
2015-08-27 17:38:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-17 13:01:40 +00:00
|
|
|
fDisplay = gfGetDisplay(EGL_DEFAULT_DISPLAY);
|
2015-08-27 17:38:39 +00:00
|
|
|
if (EGL_NO_DISPLAY == fDisplay) {
|
2015-12-09 07:52:40 +00:00
|
|
|
SkDebugf("Command Buffer: Could not create EGL display.\n");
|
2015-08-27 17:38:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-17 13:01:40 +00:00
|
|
|
if (!gfInitialize(fDisplay, nullptr, nullptr)) {
|
|
|
|
SkDebugf("Command Buffer: Could not initialize EGL display.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
|
|
|
}
|
2015-08-27 17:38:39 +00:00
|
|
|
EGLint numConfigs;
|
2016-03-31 01:56:19 +00:00
|
|
|
if (!gfChooseConfig(fDisplay, configAttribs, static_cast<EGLConfig *>(&fConfig), 1,
|
2016-03-03 15:39:48 +00:00
|
|
|
&numConfigs) || numConfigs != 1) {
|
2015-12-09 07:52:40 +00:00
|
|
|
SkDebugf("Command Buffer: Could not choose EGL config.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
|
|
|
}
|
2015-08-27 17:38:39 +00:00
|
|
|
|
2017-02-21 21:58:08 +00:00
|
|
|
fSurface = gfCreatePbufferSurface(fDisplay,
|
|
|
|
static_cast<EGLConfig>(fConfig),
|
|
|
|
surfaceAttribs);
|
|
|
|
|
2015-12-09 07:52:40 +00:00
|
|
|
if (EGL_NO_SURFACE == fSurface) {
|
|
|
|
SkDebugf("Command Buffer: Could not create EGL surface.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const EGLint contextAttribs[] = {
|
2016-03-03 15:39:48 +00:00
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
2015-08-27 17:38:39 +00:00
|
|
|
EGL_NONE
|
|
|
|
};
|
2017-02-21 21:58:08 +00:00
|
|
|
EGLContext eglShareContext = shareContext
|
|
|
|
? reinterpret_cast<EGLContext>(shareContext->fContext) : nullptr;
|
|
|
|
fContext = gfCreateContext(fDisplay, static_cast<EGLConfig>(fConfig), eglShareContext,
|
|
|
|
contextAttribs);
|
2015-12-09 07:52:40 +00:00
|
|
|
if (EGL_NO_CONTEXT == fContext) {
|
|
|
|
SkDebugf("Command Buffer: Could not create EGL context.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
|
|
|
}
|
2015-08-27 17:38:39 +00:00
|
|
|
|
2015-12-09 07:52:40 +00:00
|
|
|
if (!gfMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
|
|
|
|
SkDebugf("Command Buffer: Could not make EGL context current.\n");
|
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
|
|
|
}
|
2015-08-27 17:38:39 +00:00
|
|
|
|
2016-11-03 20:26:13 +00:00
|
|
|
sk_sp<const GrGLInterface> gl(create_command_buffer_interface());
|
2015-08-27 17:38:39 +00:00
|
|
|
if (nullptr == gl.get()) {
|
2015-12-09 07:52:40 +00:00
|
|
|
SkDebugf("Command Buffer: Could not create CommandBuffer GL interface.\n");
|
2015-08-27 17:38:39 +00:00
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!gl->validate()) {
|
2015-12-09 07:52:40 +00:00
|
|
|
SkDebugf("Command Buffer: Could not validate CommandBuffer GL interface.\n");
|
2015-08-27 17:38:39 +00:00
|
|
|
this->destroyGLContext();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-16 20:53:35 +00:00
|
|
|
this->init(gl.release());
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
CommandBufferGLTestContext::~CommandBufferGLTestContext() {
|
2015-08-27 17:38:39 +00:00
|
|
|
this->teardown();
|
|
|
|
this->destroyGLContext();
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void CommandBufferGLTestContext::destroyGLContext() {
|
2015-08-27 17:38:39 +00:00
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-17 13:01:40 +00:00
|
|
|
if (EGL_NO_DISPLAY == fDisplay) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (EGL_NO_CONTEXT != fContext) {
|
|
|
|
gfDestroyContext(fDisplay, fContext);
|
|
|
|
fContext = EGL_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
// Call MakeCurrent after destroying the context, so that the EGL implementation knows that
|
|
|
|
// the context is not used anymore after it is released from being current. This way
|
|
|
|
// command buffer does not need to abandon the context before destruction, and no
|
|
|
|
// client-side errors are printed.
|
|
|
|
gfMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
if (EGL_NO_SURFACE != fSurface) {
|
|
|
|
gfDestroySurface(fDisplay, fSurface);
|
|
|
|
fSurface = EGL_NO_SURFACE;
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
2016-05-17 13:01:40 +00:00
|
|
|
fDisplay = EGL_NO_DISPLAY;
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void CommandBufferGLTestContext::onPlatformMakeCurrent() const {
|
2015-08-27 17:38:39 +00:00
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!gfMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
|
2015-12-09 07:52:40 +00:00
|
|
|
SkDebugf("Command Buffer: Could not make EGL context current.\n");
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void CommandBufferGLTestContext::onPlatformSwapBuffers() const {
|
2015-08-27 17:38:39 +00:00
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!gfSwapBuffers(fDisplay, fSurface)) {
|
2015-12-09 07:52:40 +00:00
|
|
|
SkDebugf("Command Buffer: Could not complete gfSwapBuffers.\n");
|
2015-08-27 17:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
GrGLFuncPtr CommandBufferGLTestContext::onPlatformGetProcAddress(const char *name) const {
|
2015-08-27 17:38:39 +00:00
|
|
|
if (!gfFunctionsLoadedSuccessfully) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return gfGetProcAddress(name);
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void CommandBufferGLTestContext::presentCommandBuffer() {
|
2015-08-27 17:38:39 +00:00
|
|
|
if (this->gl()) {
|
|
|
|
this->gl()->fFunctions.fFlush();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->onPlatformSwapBuffers();
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
bool CommandBufferGLTestContext::makeCurrent() {
|
2015-08-27 17:38:39 +00:00
|
|
|
return gfMakeCurrent(fDisplay, fSurface, fSurface, fContext) != EGL_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
int CommandBufferGLTestContext::getStencilBits() {
|
2015-08-27 17:38:39 +00:00
|
|
|
EGLint result = 0;
|
2015-12-09 07:52:40 +00:00
|
|
|
gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_STENCIL_SIZE, &result);
|
2015-08-27 17:38:39 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
int CommandBufferGLTestContext::getSampleCount() {
|
2015-08-27 17:38:39 +00:00
|
|
|
EGLint result = 0;
|
2015-12-09 07:52:40 +00:00
|
|
|
gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_SAMPLES, &result);
|
2015-08-27 17:38:39 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-03-31 01:56:19 +00:00
|
|
|
|
|
|
|
} // namespace sk_gpu_test
|