diff options
author | 2012-03-28 19:37:51 +0000 | |
---|---|---|
committer | 2012-03-28 19:37:51 +0000 | |
commit | 78ff6b4ab677e156b016054ece6a4d08d5846cb7 (patch) | |
tree | 79ec7608136b2201431a9d51ec93c3974d7fc438 /src/gpu/gl/mesa/SkMesaGLContext.cpp | |
parent | 3d29b6330dc65196d842ac44f8ea2305223ef39b (diff) |
Moved mac, unix, win and mesa directories under src/gpu/gl
http://codereview.appspot.com/5937052/
git-svn-id: http://skia.googlecode.com/svn/trunk@3530 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/gpu/gl/mesa/SkMesaGLContext.cpp')
-rw-r--r-- | src/gpu/gl/mesa/SkMesaGLContext.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/gpu/gl/mesa/SkMesaGLContext.cpp b/src/gpu/gl/mesa/SkMesaGLContext.cpp new file mode 100644 index 0000000000..c4f84cf8d2 --- /dev/null +++ b/src/gpu/gl/mesa/SkMesaGLContext.cpp @@ -0,0 +1,103 @@ + +/* + * 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/SkMesaGLContext.h" + +SkMesaGLContext::AutoContextRestore::AutoContextRestore() { + fOldContext = (Context)OSMesaGetCurrentContext(); + if (NULL != (OSMesaContext)fOldContext) { + OSMesaGetColorBuffer((OSMesaContext)fOldContext, + &fOldWidth, &fOldHeight, + &fOldFormat, &fOldImage); + } +} + +SkMesaGLContext::AutoContextRestore::~AutoContextRestore() { + if (NULL != (OSMesaContext)fOldContext) { + OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage, + fOldFormat, fOldWidth, fOldHeight); + } +} + +/////////////////////////////////////////////////////////////////////////////// + +SkMesaGLContext::SkMesaGLContext() + : fContext(NULL) + , fImage(NULL) { + GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); +} + +SkMesaGLContext::~SkMesaGLContext() { + this->destroyGLContext(); +} + +void SkMesaGLContext::destroyGLContext() { + if (fImage) { + sk_free(fImage); + } + + if (fContext) { + OSMesaDestroyContext((OSMesaContext)fContext); + } +} + +static const GrGLint gBOGUS_SIZE = 16; + +const GrGLInterface* SkMesaGLContext::createGLContext() { + /* 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, NULL); +#else + fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL); +#endif + if (!fContext) { + SkDebugf("OSMesaCreateContext failed!\n"); + this->destroyGLContext(); + return NULL; + } + // 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 NULL; + } + + // 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 NULL; + } + + const GrGLInterface* interface = GrGLCreateMesaInterface(); + if (!interface) { + SkDebugf("Could not create GL interface!\n"); + this->destroyGLContext(); + return NULL; + } + return interface; + +} + +void SkMesaGLContext::makeCurrent() const { + if (fContext) { + if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage, + GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { + SkDebugf("Could not make MESA context current."); + } + } +} |