michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "gl/SkMesaGLContext.h" michael@0: #include "gl/GrGLDefines.h" michael@0: michael@0: SkMesaGLContext::AutoContextRestore::AutoContextRestore() { michael@0: fOldContext = (Context)OSMesaGetCurrentContext(); michael@0: if (NULL != (OSMesaContext)fOldContext) { michael@0: OSMesaGetColorBuffer((OSMesaContext)fOldContext, michael@0: &fOldWidth, &fOldHeight, michael@0: &fOldFormat, &fOldImage); michael@0: } michael@0: } michael@0: michael@0: SkMesaGLContext::AutoContextRestore::~AutoContextRestore() { michael@0: if (NULL != (OSMesaContext)fOldContext) { michael@0: OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage, michael@0: fOldFormat, fOldWidth, fOldHeight); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkMesaGLContext::SkMesaGLContext() michael@0: : fContext(static_cast(NULL)) michael@0: , fImage(NULL) { michael@0: GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); michael@0: } michael@0: michael@0: SkMesaGLContext::~SkMesaGLContext() { michael@0: this->destroyGLContext(); michael@0: } michael@0: michael@0: void SkMesaGLContext::destroyGLContext() { michael@0: if (fImage) { michael@0: sk_free(fImage); michael@0: fImage = NULL; michael@0: } michael@0: michael@0: if (fContext) { michael@0: OSMesaDestroyContext((OSMesaContext)fContext); michael@0: fContext = static_cast(NULL); michael@0: } michael@0: } michael@0: michael@0: static const GrGLint gBOGUS_SIZE = 16; michael@0: michael@0: const GrGLInterface* SkMesaGLContext::createGLContext() { michael@0: /* Create an RGBA-mode context */ michael@0: #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 michael@0: /* specify Z, stencil, accum sizes */ michael@0: fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL); michael@0: #else michael@0: fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL); michael@0: #endif michael@0: if (!fContext) { michael@0: SkDebugf("OSMesaCreateContext failed!\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: // Allocate the image buffer michael@0: fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * michael@0: 4 * sizeof(GrGLubyte)); michael@0: if (!fImage) { michael@0: SkDebugf("Alloc image buffer failed!\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: // Bind the buffer to the context and make it current michael@0: if (!OSMesaMakeCurrent((OSMesaContext)fContext, michael@0: fImage, michael@0: GR_GL_UNSIGNED_BYTE, michael@0: gBOGUS_SIZE, michael@0: gBOGUS_SIZE)) { michael@0: SkDebugf("OSMesaMakeCurrent failed!\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: const GrGLInterface* interface = GrGLCreateMesaInterface(); michael@0: if (!interface) { michael@0: SkDebugf("Could not create GL interface!\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: return interface; michael@0: michael@0: } michael@0: michael@0: void SkMesaGLContext::makeCurrent() const { michael@0: if (fContext) { michael@0: if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage, michael@0: GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { michael@0: SkDebugf("Could not make MESA context current."); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkMesaGLContext::swapBuffers() const { }