diff --git a/BUILD.gn b/BUILD.gn index f66ffc739f..18a62f0ae1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -760,14 +760,21 @@ if (skia_enable_tools) { # TODO: worth fixing? executable("fiddle") { libs = [] - if (is_linux) { - libs += [ "OSMesa" ] - } - sources = [ "tools/fiddle/draw.cpp", "tools/fiddle/fiddle_main.cpp", ] + + if (skia_use_egl) { + sources += [ "tools/fiddle/egl_context.cpp" ] + } else if (skia_use_mesa) { + sources += [ "tools/fiddle/mesa_context.cpp" ] + if (is_linux) { + libs += [ "OSMesa" ] + } + } else { + sources += [ "tools/fiddle/null_context.cpp" ] + } testonly = true deps = [ ":flags", diff --git a/tools/fiddle/egl_context.cpp b/tools/fiddle/egl_context.cpp new file mode 100644 index 0000000000..cbab44cb37 --- /dev/null +++ b/tools/fiddle/egl_context.cpp @@ -0,0 +1,73 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "fiddle_main.h" + +#include + +static const EGLint configAttribs[] = { + EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, + EGL_BLUE_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_RED_SIZE, 8, + EGL_DEPTH_SIZE, 8, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_NONE +}; + +static const int pbufferWidth = 9; +static const int pbufferHeight = 9; + +static const EGLint pbufferAttribs[] = { + EGL_WIDTH, pbufferWidth, + EGL_HEIGHT, pbufferHeight, + EGL_NONE, +}; + +// create_grcontext implementation for EGL. +sk_sp create_grcontext() { + EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + if (EGL_NO_DISPLAY == eglDpy) { + return nullptr; + } + + EGLint major, minor; + if (EGL_TRUE != eglInitialize(eglDpy, &major, &minor)) { + return nullptr; + } + + EGLint numConfigs; + EGLConfig eglCfg; + if (EGL_TRUE != eglChooseConfig(eglDpy, configAttribs, &eglCfg, 1, &numConfigs)) { + return nullptr; + } + + EGLSurface eglSurf = eglCreatePbufferSurface(eglDpy, eglCfg, pbufferAttribs); + if (EGL_NO_SURFACE == eglSurf) { + return nullptr; + } + + if (EGL_TRUE != eglBindAPI(EGL_OPENGL_API)) { + return nullptr; + } + + EGLContext eglCtx = eglCreateContext(eglDpy, eglCfg, EGL_NO_CONTEXT, NULL); + if (EGL_NO_CONTEXT == eglCtx) { + return nullptr; + } + if (EGL_FALSE == eglMakeCurrent(eglDpy, eglSurf, eglSurf, eglCtx)) { + return nullptr; + } + + auto interface = GrGLCreateNativeInterface(); + if (!interface) { + return nullptr; + } + eglTerminate(eglDpy); + + return sk_sp(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface)); +} diff --git a/tools/fiddle/fiddle_main.cpp b/tools/fiddle/fiddle_main.cpp index 2ce9beb944..a097583b30 100644 --- a/tools/fiddle/fiddle_main.cpp +++ b/tools/fiddle/fiddle_main.cpp @@ -90,34 +90,6 @@ static SkData* encode_snapshot(const sk_sp& surface) { return img ? img->encode() : nullptr; } -#if defined(__linux) && !defined(__ANDROID__) - #include - static sk_sp create_grcontext() { - // We just leak the OSMesaContext... the process will die soon anyway. - if (OSMesaContext osMesaContext = OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr)) { - static uint32_t buffer[16 * 16]; - OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); - } - - auto osmesa_get = [](void* ctx, const char name[]) { - SkASSERT(nullptr == ctx); - SkASSERT(OSMesaGetCurrentContext()); - return OSMesaGetProcAddress(name); - }; - sk_sp mesa(GrGLAssembleInterface(nullptr, osmesa_get)); - if (!mesa) { - return nullptr; - } - return sk_sp(GrContext::Create( - kOpenGL_GrBackend, - reinterpret_cast(mesa.get()))); - } -#else - static sk_sp create_grcontext() { return nullptr; } -#endif - - - int main(int argc, char** argv) { SkCommandLineFlags::Parse(argc, argv); duration = FLAGS_duration; diff --git a/tools/fiddle/fiddle_main.h b/tools/fiddle/fiddle_main.h index 7be317b8c1..4fadd365fa 100644 --- a/tools/fiddle/fiddle_main.h +++ b/tools/fiddle/fiddle_main.h @@ -55,4 +55,8 @@ extern DrawOptions GetDrawOptions(); extern void SkDebugf(const char * format, ...); extern void draw(SkCanvas*); +// There are different implementations of create_grcontext() for EGL, Mesa, +// and a fallback to a null context. +extern sk_sp create_grcontext(); + #endif // fiddle_main_DEFINED diff --git a/tools/fiddle/mesa_context.cpp b/tools/fiddle/mesa_context.cpp new file mode 100644 index 0000000000..70ee62bcc8 --- /dev/null +++ b/tools/fiddle/mesa_context.cpp @@ -0,0 +1,32 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "fiddle_main.h" + +#include + +// create_grcontext implementation for Mesa. +sk_sp create_grcontext() { + // We just leak the OSMesaContext... the process will die soon anyway. + if (OSMesaContext osMesaContext = OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr)) { + static uint32_t buffer[16 * 16]; + OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); + } + + auto osmesa_get = [](void* ctx, const char name[]) { + SkASSERT(nullptr == ctx); + SkASSERT(OSMesaGetCurrentContext()); + return OSMesaGetProcAddress(name); + }; + sk_sp mesa(GrGLAssembleInterface(nullptr, osmesa_get)); + if (!mesa) { + return nullptr; + } + return sk_sp(GrContext::Create( + kOpenGL_GrBackend, + reinterpret_cast(mesa.get()))); +} diff --git a/tools/fiddle/null_context.cpp b/tools/fiddle/null_context.cpp new file mode 100644 index 0000000000..c331e8a98c --- /dev/null +++ b/tools/fiddle/null_context.cpp @@ -0,0 +1,13 @@ +/* + * 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 "fiddle_main.h" + +// create_grcontext for when neither Mesa nor EGL are available. +sk_sp create_grcontext() { + return nullptr; +}