diff options
Diffstat (limited to 'tools/gpu/gl/mesa')
-rw-r--r-- | tools/gpu/gl/mesa/GLContext_mesa.cpp | 151 | ||||
-rw-r--r-- | tools/gpu/gl/mesa/GLContext_mesa.h | 17 | ||||
-rw-r--r-- | tools/gpu/gl/mesa/osmesa_wrapper.h | 16 |
3 files changed, 184 insertions, 0 deletions
diff --git a/tools/gpu/gl/mesa/GLContext_mesa.cpp b/tools/gpu/gl/mesa/GLContext_mesa.cpp new file mode 100644 index 0000000000..e6cc7c7f4b --- /dev/null +++ b/tools/gpu/gl/mesa/GLContext_mesa.cpp @@ -0,0 +1,151 @@ + +/* + * Copyright 2011 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <GL/osmesa.h> + +#include "gl/mesa/GLContext_mesa.h" +#include "gl/GrGLDefines.h" + +#include "gl/GrGLAssembleInterface.h" +#include "gl/GrGLUtil.h" +#include "osmesa_wrapper.h" + +namespace { + +static GrGLFuncPtr osmesa_get(void* ctx, const char name[]) { + SkASSERT(nullptr == ctx); + SkASSERT(OSMesaGetCurrentContext()); + return OSMesaGetProcAddress(name); +} + +static const GrGLInterface* create_mesa_interface() { + if (nullptr == OSMesaGetCurrentContext()) { + return nullptr; + } + return GrGLAssembleInterface(nullptr, osmesa_get); +} + +static const GrGLint gBOGUS_SIZE = 16; + +class MesaGLContext : public sk_gpu_test::GLContext { +private: + typedef intptr_t Context; + +public: + MesaGLContext(); + ~MesaGLContext() override; + +private: + void destroyGLContext(); + + void onPlatformMakeCurrent() const override; + + void onPlatformSwapBuffers() const override; + + GrGLFuncPtr onPlatformGetProcAddress(const char *) const override; + + Context fContext; + GrGLubyte *fImage; +}; + +MesaGLContext::MesaGLContext() : fContext(static_cast<Context>(0)), fImage(nullptr) { + GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); + + /* Create an RGBA-mode context */ +#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 + /* specify Z, stencil, accum sizes */ + fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr); +#else + fContext = (Context) OSMesaCreateContext(OSMESA_BGRA, nullptr); +#endif + if (!fContext) { + SkDebugf("OSMesaCreateContext failed!\n"); + this->destroyGLContext(); + return; + } + // Allocate the image buffer + fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * + 4 * sizeof(GrGLubyte)); + if (!fImage) { + SkDebugf("Alloc image buffer failed!\n"); + this->destroyGLContext(); + return; + } + + // Bind the buffer to the context and make it current + if (!OSMesaMakeCurrent((OSMesaContext) fContext, + fImage, + GR_GL_UNSIGNED_BYTE, + gBOGUS_SIZE, + gBOGUS_SIZE)) { + SkDebugf("OSMesaMakeCurrent failed!\n"); + this->destroyGLContext(); + return; + } + + SkAutoTUnref<const GrGLInterface> gl(create_mesa_interface()); + if (nullptr == gl.get()) { + SkDebugf("Could not create GL interface!\n"); + this->destroyGLContext(); + return; + } + + if (!gl->validate()) { + SkDebugf("Could not validate GL interface!\n"); + this->destroyGLContext(); + return; + } + + this->init(gl.release()); +} + +MesaGLContext::~MesaGLContext() { + this->teardown(); + this->destroyGLContext(); +} + +void MesaGLContext::destroyGLContext() { + if (fImage) { + sk_free(fImage); + fImage = nullptr; + } + + if (fContext) { + OSMesaDestroyContext((OSMesaContext) fContext); + fContext = static_cast<Context>(0); + } +} + + +void MesaGLContext::onPlatformMakeCurrent() const { + if (fContext) { + if (!OSMesaMakeCurrent((OSMesaContext) fContext, fImage, + GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { + SkDebugf("Could not make MESA context current."); + } + } +} + +void MesaGLContext::onPlatformSwapBuffers() const { } + +GrGLFuncPtr MesaGLContext::onPlatformGetProcAddress(const char *procName) const { + return OSMesaGetProcAddress(procName); +} +} // anonymous namespace + + +namespace sk_gpu_test { +GLContext *CreateMesaGLContext() { + MesaGLContext *ctx = new MesaGLContext; + if (!ctx->isValid()) { + delete ctx; + return nullptr; + } + return ctx; +} +} // sk_gpu_test diff --git a/tools/gpu/gl/mesa/GLContext_mesa.h b/tools/gpu/gl/mesa/GLContext_mesa.h new file mode 100644 index 0000000000..0d6ee4dc10 --- /dev/null +++ b/tools/gpu/gl/mesa/GLContext_mesa.h @@ -0,0 +1,17 @@ + +/* + * 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 GLContext_mesa_DEFINED +#define GLContext_mesa_DEFINED + +#include "gl/GLContext.h" + +namespace sk_gpu_test { +GLContext* CreateMesaGLContext(); +} // namespace sk_gpu_test + +#endif diff --git a/tools/gpu/gl/mesa/osmesa_wrapper.h b/tools/gpu/gl/mesa/osmesa_wrapper.h new file mode 100644 index 0000000000..70de99376d --- /dev/null +++ b/tools/gpu/gl/mesa/osmesa_wrapper.h @@ -0,0 +1,16 @@ + +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +// Older versions of XQuartz have a bug where a header included by osmesa.h +// defines GL_GLEXT_PROTOTYPES. This will cause a redefinition warning if +// the file that includes osmesa.h already defined it. XCode 3 uses a version +// of gcc (4.2.1) that does not support the diagnostic pragma to disable a +// warning (added in 4.2.4). So we use the system_header pragma to shut GCC +// up about warnings in osmesa.h +#pragma GCC system_header +#include <GL/osmesa.h> |