1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/mesa/SkMesaGLContext.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 +#include <GL/osmesa.h> 1.13 + 1.14 +#include "gl/SkMesaGLContext.h" 1.15 +#include "gl/GrGLDefines.h" 1.16 + 1.17 +SkMesaGLContext::AutoContextRestore::AutoContextRestore() { 1.18 + fOldContext = (Context)OSMesaGetCurrentContext(); 1.19 + if (NULL != (OSMesaContext)fOldContext) { 1.20 + OSMesaGetColorBuffer((OSMesaContext)fOldContext, 1.21 + &fOldWidth, &fOldHeight, 1.22 + &fOldFormat, &fOldImage); 1.23 + } 1.24 +} 1.25 + 1.26 +SkMesaGLContext::AutoContextRestore::~AutoContextRestore() { 1.27 + if (NULL != (OSMesaContext)fOldContext) { 1.28 + OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage, 1.29 + fOldFormat, fOldWidth, fOldHeight); 1.30 + } 1.31 +} 1.32 + 1.33 +/////////////////////////////////////////////////////////////////////////////// 1.34 + 1.35 +SkMesaGLContext::SkMesaGLContext() 1.36 + : fContext(static_cast<Context>(NULL)) 1.37 + , fImage(NULL) { 1.38 + GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); 1.39 +} 1.40 + 1.41 +SkMesaGLContext::~SkMesaGLContext() { 1.42 + this->destroyGLContext(); 1.43 +} 1.44 + 1.45 +void SkMesaGLContext::destroyGLContext() { 1.46 + if (fImage) { 1.47 + sk_free(fImage); 1.48 + fImage = NULL; 1.49 + } 1.50 + 1.51 + if (fContext) { 1.52 + OSMesaDestroyContext((OSMesaContext)fContext); 1.53 + fContext = static_cast<Context>(NULL); 1.54 + } 1.55 +} 1.56 + 1.57 +static const GrGLint gBOGUS_SIZE = 16; 1.58 + 1.59 +const GrGLInterface* SkMesaGLContext::createGLContext() { 1.60 + /* Create an RGBA-mode context */ 1.61 +#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 1.62 + /* specify Z, stencil, accum sizes */ 1.63 + fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL); 1.64 +#else 1.65 + fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL); 1.66 +#endif 1.67 + if (!fContext) { 1.68 + SkDebugf("OSMesaCreateContext failed!\n"); 1.69 + this->destroyGLContext(); 1.70 + return NULL; 1.71 + } 1.72 + // Allocate the image buffer 1.73 + fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * 1.74 + 4 * sizeof(GrGLubyte)); 1.75 + if (!fImage) { 1.76 + SkDebugf("Alloc image buffer failed!\n"); 1.77 + this->destroyGLContext(); 1.78 + return NULL; 1.79 + } 1.80 + 1.81 + // Bind the buffer to the context and make it current 1.82 + if (!OSMesaMakeCurrent((OSMesaContext)fContext, 1.83 + fImage, 1.84 + GR_GL_UNSIGNED_BYTE, 1.85 + gBOGUS_SIZE, 1.86 + gBOGUS_SIZE)) { 1.87 + SkDebugf("OSMesaMakeCurrent failed!\n"); 1.88 + this->destroyGLContext(); 1.89 + return NULL; 1.90 + } 1.91 + 1.92 + const GrGLInterface* interface = GrGLCreateMesaInterface(); 1.93 + if (!interface) { 1.94 + SkDebugf("Could not create GL interface!\n"); 1.95 + this->destroyGLContext(); 1.96 + return NULL; 1.97 + } 1.98 + return interface; 1.99 + 1.100 +} 1.101 + 1.102 +void SkMesaGLContext::makeCurrent() const { 1.103 + if (fContext) { 1.104 + if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage, 1.105 + GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { 1.106 + SkDebugf("Could not make MESA context current."); 1.107 + } 1.108 + } 1.109 +} 1.110 + 1.111 +void SkMesaGLContext::swapBuffers() const { }