diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/gpu/GrContextFactory.cpp | 8 | ||||
-rw-r--r-- | src/gpu/GrContextFactory.h | 7 | ||||
-rw-r--r-- | src/gpu/gl/angle/GrGLCreateANGLEInterface.cpp | 2 | ||||
-rw-r--r-- | src/gpu/gl/command_buffer/SkCommandBufferGLContext.cpp | 271 | ||||
-rw-r--r-- | src/views/win/SkOSWindow_win.cpp | 79 |
5 files changed, 365 insertions, 2 deletions
diff --git a/src/gpu/GrContextFactory.cpp b/src/gpu/GrContextFactory.cpp index d84e589f84..0fe01df16b 100755 --- a/src/gpu/GrContextFactory.cpp +++ b/src/gpu/GrContextFactory.cpp @@ -11,6 +11,9 @@ #if SK_ANGLE #include "gl/angle/SkANGLEGLContext.h" #endif +#if SK_COMMAND_BUFFER + #include "gl/command_buffer/SkCommandBufferGLContext.h" +#endif #include "gl/debug/SkDebugGLContext.h" #if SK_MESA #include "gl/mesa/SkMesaGLContext.h" @@ -43,6 +46,11 @@ GrContext* GrContextFactory::get(GLContextType type, GrGLStandard forcedGpuAPI) glCtx.reset(SkANGLEGLContext::Create(forcedGpuAPI)); break; #endif +#ifdef SK_COMMAND_BUFFER + case kCommandBuffer_GLContextType: + glCtx.reset(SkCommandBufferGLContext::Create(forcedGpuAPI)); + break; +#endif #ifdef SK_MESA case kMESA_GLContextType: glCtx.reset(SkMesaGLContext::Create(forcedGpuAPI)); diff --git a/src/gpu/GrContextFactory.h b/src/gpu/GrContextFactory.h index b557f98fea..2accd7329f 100644 --- a/src/gpu/GrContextFactory.h +++ b/src/gpu/GrContextFactory.h @@ -34,6 +34,9 @@ public: #if SK_ANGLE kANGLE_GLContextType, #endif +#if SK_COMMAND_BUFFER + kCommandBuffer_GLContextType, +#endif #if SK_MESA kMESA_GLContextType, #endif @@ -68,6 +71,10 @@ public: case kANGLE_GLContextType: return "angle"; #endif +#if SK_COMMAND_BUFFER + case kCommandBuffer_GLContextType: + return "commandbuffer"; +#endif #if SK_MESA case kMESA_GLContextType: return "mesa"; diff --git a/src/gpu/gl/angle/GrGLCreateANGLEInterface.cpp b/src/gpu/gl/angle/GrGLCreateANGLEInterface.cpp index 4f26c06afe..51e461a0d8 100644 --- a/src/gpu/gl/angle/GrGLCreateANGLEInterface.cpp +++ b/src/gpu/gl/angle/GrGLCreateANGLEInterface.cpp @@ -12,7 +12,7 @@ #define WIN32_LEAN_AND_MEAN #include <windows.h> -#include "EGL/egl.h" +#include <EGL/egl.h> static GrGLFuncPtr angle_get_gl_proc(void* ctx, const char name[]) { GrGLFuncPtr proc = (GrGLFuncPtr) GetProcAddress((HMODULE)ctx, name); diff --git a/src/gpu/gl/command_buffer/SkCommandBufferGLContext.cpp b/src/gpu/gl/command_buffer/SkCommandBufferGLContext.cpp new file mode 100644 index 0000000000..7f904c2cd1 --- /dev/null +++ b/src/gpu/gl/command_buffer/SkCommandBufferGLContext.cpp @@ -0,0 +1,271 @@ + +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include <EGL/egl.h> + +#include "SkOnce.h" +#include "gl/GrGLInterface.h" +#include "gl/GrGLAssembleInterface.h" +#include "gl/command_buffer/SkCommandBufferGLContext.h" + +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; + +static HMODULE ghLibrary = nullptr; +static bool gfFunctionsLoadedSuccessfully = false; + +static void load_command_buffer_functions() { + if (!ghLibrary) { + ghLibrary = LoadLibrary("command_buffer_gles2.dll"); + + if (ghLibrary) { + gfGetDisplay = (GetDisplayProc)::GetProcAddress(ghLibrary, "CommandBuffer_GetDisplay"); + gfInitialize = (InitializeProc)::GetProcAddress(ghLibrary, "CommandBuffer_Initialize"); + gfTerminate = (TerminateProc)::GetProcAddress(ghLibrary, "CommandBuffer_Terminate"); + gfChooseConfig = (ChooseConfigProc)::GetProcAddress(ghLibrary, "CommandBuffer_ChooseConfig"); + gfGetConfigAttrib = (GetConfigAttrib)::GetProcAddress(ghLibrary, "CommandBuffer_GetConfigAttrib"); + gfCreateWindowSurface = (CreateWindowSurfaceProc)::GetProcAddress(ghLibrary, "CommandBuffer_CreateWindowSurface"); + gfCreatePbufferSurface = (CreatePbufferSurfaceProc)::GetProcAddress(ghLibrary, "CommandBuffer_CreatePbufferSurface"); + gfDestroySurface = (DestroySurfaceProc)::GetProcAddress(ghLibrary, "CommandBuffer_DestroySurface"); + gfCreateContext = (CreateContextProc)::GetProcAddress(ghLibrary, "CommandBuffer_CreateContext"); + gfDestroyContext = (DestroyContextProc)::GetProcAddress(ghLibrary, "CommandBuffer_DestroyContext"); + gfMakeCurrent = (MakeCurrentProc)::GetProcAddress(ghLibrary, "CommandBuffer_MakeCurrent"); + gfSwapBuffers = (SwapBuffersProc)::GetProcAddress(ghLibrary, "CommandBuffer_SwapBuffers"); + gfGetProcAddress = (GetProcAddressProc)::GetProcAddress(ghLibrary, "CommandBuffer_GetProcAddress"); + + gfFunctionsLoadedSuccessfully = gfGetDisplay && gfInitialize && gfTerminate && + gfChooseConfig && gfCreateWindowSurface && + gfCreatePbufferSurface && gfDestroySurface && + gfCreateContext && gfDestroyContext && gfMakeCurrent && + gfSwapBuffers && gfGetProcAddress; + + } + } +} + +static GrGLFuncPtr command_buffer_get_gl_proc(void* ctx, const char name[]) { + GrGLFuncPtr proc = (GrGLFuncPtr) GetProcAddress((HMODULE)ctx, name); + if (proc) { + return proc; + } + if (!gfFunctionsLoadedSuccessfully) { + return nullptr; + } + return gfGetProcAddress(name); +} + +SK_DECLARE_STATIC_ONCE(loadCommandBufferOnce); +void LoadCommandBufferOnce() { + SkOnce(&loadCommandBufferOnce, load_command_buffer_functions); +} + +const GrGLInterface* GrGLCreateCommandBufferInterface() { + LoadCommandBufferOnce(); + if (!gfFunctionsLoadedSuccessfully) { + return nullptr; + } + return GrGLAssembleGLESInterface(ghLibrary, command_buffer_get_gl_proc); +} + +SkCommandBufferGLContext::SkCommandBufferGLContext() + : fContext(EGL_NO_CONTEXT) + , fDisplay(EGL_NO_DISPLAY) + , fSurface(EGL_NO_SURFACE) { + + static const EGLint configAttribs[] = { + EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + 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 + }; + + initializeGLContext(nullptr, configAttribs, surfaceAttribs); +} + +SkCommandBufferGLContext::SkCommandBufferGLContext(void* nativeWindow, int msaaSampleCount) { + static const EGLint surfaceAttribs[] = { EGL_NONE }; + + EGLint configAttribs[] = { + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + EGL_ALPHA_SIZE, 8, + EGL_DEPTH_SIZE, 8, + EGL_STENCIL_SIZE, 8, + EGL_SAMPLE_BUFFERS, 1, + EGL_SAMPLES, msaaSampleCount, + EGL_NONE + }; + if (msaaSampleCount == 0) { + configAttribs[12] = EGL_NONE; + } + + initializeGLContext(nativeWindow, configAttribs, surfaceAttribs); +} + +void SkCommandBufferGLContext::initializeGLContext(void* nativeWindow, const int* configAttribs, + const int* surfaceAttribs) { + LoadCommandBufferOnce(); + if (!gfFunctionsLoadedSuccessfully) { + return; + } + + fDisplay = gfGetDisplay(static_cast<EGLNativeDisplayType>(EGL_DEFAULT_DISPLAY)); + if (EGL_NO_DISPLAY == fDisplay) { + SkDebugf("Could not create EGL display!"); + return; + } + + EGLint majorVersion; + EGLint minorVersion; + gfInitialize(fDisplay, &majorVersion, &minorVersion); + + EGLConfig surfaceConfig = static_cast<EGLConfig>(fConfig); + EGLint numConfigs; + gfChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); + + if (nativeWindow) { + fSurface = gfCreateWindowSurface(fDisplay, surfaceConfig, + (EGLNativeWindowType)nativeWindow, surfaceAttribs); + } else { + fSurface = gfCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); + } + + static const EGLint contextAttribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + fContext = gfCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs); + + gfMakeCurrent(fDisplay, fSurface, fSurface, fContext); + + SkAutoTUnref<const GrGLInterface> gl(GrGLCreateCommandBufferInterface()); + if (nullptr == gl.get()) { + SkDebugf("Could not create CommandBuffer GL interface!\n"); + this->destroyGLContext(); + return; + } + if (!gl->validate()) { + SkDebugf("Could not validate CommandBuffer GL interface!\n"); + this->destroyGLContext(); + return; + } + + this->init(gl.detach()); +} + +SkCommandBufferGLContext::~SkCommandBufferGLContext() { + this->teardown(); + this->destroyGLContext(); +} + +void SkCommandBufferGLContext::destroyGLContext() { + if (!gfFunctionsLoadedSuccessfully) { + return; + } + if (fDisplay) { + gfMakeCurrent(fDisplay, 0, 0, 0); + + if (fContext) { + gfDestroyContext(fDisplay, fContext); + fContext = EGL_NO_CONTEXT; + } + + if (fSurface) { + gfDestroySurface(fDisplay, fSurface); + fSurface = EGL_NO_SURFACE; + } + + gfTerminate(fDisplay); + fDisplay = EGL_NO_DISPLAY; + } +} + +void SkCommandBufferGLContext::onPlatformMakeCurrent() const { + if (!gfFunctionsLoadedSuccessfully) { + return; + } + if (!gfMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { + SkDebugf("Could not set the context.\n"); + } +} + +void SkCommandBufferGLContext::onPlatformSwapBuffers() const { + if (!gfFunctionsLoadedSuccessfully) { + return; + } + if (!gfSwapBuffers(fDisplay, fSurface)) { + SkDebugf("Could not complete gfSwapBuffers.\n"); + } +} + +GrGLFuncPtr SkCommandBufferGLContext::onPlatformGetProcAddress(const char* name) const { + if (!gfFunctionsLoadedSuccessfully) { + return nullptr; + } + return gfGetProcAddress(name); +} + +void SkCommandBufferGLContext::presentCommandBuffer() { + if (this->gl()) { + this->gl()->fFunctions.fFlush(); + } + + this->onPlatformSwapBuffers(); +} + +bool SkCommandBufferGLContext::makeCurrent() { + return gfMakeCurrent(fDisplay, fSurface, fSurface, fContext) != EGL_FALSE; +} + +int SkCommandBufferGLContext::getStencilBits() { + EGLint result = 0; + EGLConfig surfaceConfig = static_cast<EGLConfig>(fConfig); + gfGetConfigAttrib(fDisplay, surfaceConfig, EGL_STENCIL_SIZE, &result); + return result; +} + +int SkCommandBufferGLContext::getSampleCount() { + EGLint result = 0; + EGLConfig surfaceConfig = static_cast<EGLConfig>(fConfig); + gfGetConfigAttrib(fDisplay, surfaceConfig, EGL_SAMPLES, &result); + return result; +} diff --git a/src/views/win/SkOSWindow_win.cpp b/src/views/win/SkOSWindow_win.cpp index b0bb76ca19..e4609fb410 100644 --- a/src/views/win/SkOSWindow_win.cpp +++ b/src/views/win/SkOSWindow_win.cpp @@ -30,7 +30,17 @@ (IFACE)->fFunctions.f##X; \ } while (false) -#endif +#endif // SK_ANGLE + +#if SK_COMMAND_BUFFER +#include "gl/command_buffer/SkCommandBufferGLContext.h" + +#define COMMAND_BUFFER_GL_CALL(IFACE, X) \ + do { \ + (IFACE)->fFunctions.f##X; \ + } while (false) + +#endif // SK_COMMAND_BUFFER #define WM_EVENT_CALLBACK (WM_USER+0) @@ -53,6 +63,10 @@ SkOSWindow::SkOSWindow(const void* winInit) { fContext = EGL_NO_CONTEXT; fSurface = EGL_NO_SURFACE; #endif +#if SK_COMMAND_BUFFER + fCommandBuffer = nullptr; +#endif // SK_COMMAND_BUFFER + fHGLRC = NULL; #endif fAttached = kNone_BackEndType; @@ -80,6 +94,10 @@ SkOSWindow::~SkOSWindow() { fDisplay = EGL_NO_DISPLAY; } #endif // SK_ANGLE +#if SK_COMMAND_BUFFER + delete fCommandBuffer; +#endif // SK_COMMAND_BUFFER + #endif // SK_SUPPORT_GPU this->closeWindow(); } @@ -530,6 +548,50 @@ void SkOSWindow::presentANGLE() { eglSwapBuffers(fDisplay, fSurface); } #endif // SK_ANGLE + +#if SK_COMMAND_BUFFER + +bool SkOSWindow::attachCommandBuffer(int msaaSampleCount, AttachmentInfo* info) { + if (!fCommandBuffer) { + fCommandBuffer = SkCommandBufferGLContext::Create((HWND)fHWND, msaaSampleCount); + if (!fCommandBuffer) + return false; + + SkAutoTUnref<const GrGLInterface> intf(GrGLCreateCommandBufferInterface()); + if (intf) { + COMMAND_BUFFER_GL_CALL(intf, ClearStencil(0)); + COMMAND_BUFFER_GL_CALL(intf, ClearColor(0, 0, 0, 0)); + COMMAND_BUFFER_GL_CALL(intf, StencilMask(0xffffffff)); + COMMAND_BUFFER_GL_CALL(intf, Clear(GL_STENCIL_BUFFER_BIT |GL_COLOR_BUFFER_BIT)); + } + } + + if (fCommandBuffer->makeCurrent()) { + info->fStencilBits = fCommandBuffer->getStencilBits(); + info->fSampleCount = fCommandBuffer->getSampleCount(); + + SkAutoTUnref<const GrGLInterface> intf(GrGLCreateCommandBufferInterface()); + + if (intf ) { + COMMAND_BUFFER_GL_CALL(intf, Viewport(0, 0, + SkScalarRoundToInt(this->width()), + SkScalarRoundToInt(this->height()))); + } + return true; + } + return false; +} + +void SkOSWindow::detachCommandBuffer() { + delete fCommandBuffer; + fCommandBuffer = nullptr; +} + +void SkOSWindow::presentCommandBuffer() { + fCommandBuffer->presentCommandBuffer(); +} +#endif // SK_COMMAND_BUFFER + #endif // SK_SUPPORT_GPU // return true on success @@ -554,6 +616,11 @@ bool SkOSWindow::attach(SkBackEndTypes attachType, int msaaSampleCount, Attachme result = attachANGLE(msaaSampleCount, info); break; #endif // SK_ANGLE +#if SK_COMMAND_BUFFER + case kCommandBuffer_BackEndType: + result = attachCommandBuffer(msaaSampleCount, info); + break; +#endif // SK_COMMAND_BUFFER #endif // SK_SUPPORT_GPU default: SkASSERT(false); @@ -582,6 +649,11 @@ void SkOSWindow::detach() { detachANGLE(); break; #endif // SK_ANGLE +#if SK_COMMAND_BUFFER + case kCommandBuffer_BackEndType: + detachCommandBuffer(); + break; +#endif // SK_COMMAND_BUFFER #endif // SK_SUPPORT_GPU default: SkASSERT(false); @@ -604,6 +676,11 @@ void SkOSWindow::present() { presentANGLE(); break; #endif // SK_ANGLE +#if SK_COMMAND_BUFFER + case kCommandBuffer_BackEndType: + presentCommandBuffer(); + break; +#endif // SK_COMMAND_BUFFER #endif // SK_SUPPORT_GPU default: SkASSERT(false); |