3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "gl/SkMesaGLContext.h"
12 #include "gl/GrGLDefines.h"
14 SkMesaGLContext::AutoContextRestore::AutoContextRestore() {
15 fOldContext = (Context)OSMesaGetCurrentContext();
17 OSMesaGetColorBuffer((OSMesaContext)fOldContext,
18 &fOldWidth, &fOldHeight,
19 &fOldFormat, &fOldImage);
23 SkMesaGLContext::AutoContextRestore::~AutoContextRestore() {
25 OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage,
26 fOldFormat, fOldWidth, fOldHeight);
30 ///////////////////////////////////////////////////////////////////////////////
32 SkMesaGLContext::SkMesaGLContext()
33 : fContext(static_cast<Context>(NULL))
35 GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext));
38 SkMesaGLContext::~SkMesaGLContext() {
39 this->destroyGLContext();
42 void SkMesaGLContext::destroyGLContext() {
49 OSMesaDestroyContext((OSMesaContext)fContext);
50 fContext = static_cast<Context>(NULL);
54 static const GrGLint gBOGUS_SIZE = 16;
56 const GrGLInterface* SkMesaGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
57 if (kGLES_GrGLStandard == forcedGpuAPI) {
61 /* Create an RGBA-mode context */
62 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
63 /* specify Z, stencil, accum sizes */
64 fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL);
66 fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL);
69 SkDebugf("OSMesaCreateContext failed!\n");
70 this->destroyGLContext();
73 // Allocate the image buffer
74 fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE *
75 4 * sizeof(GrGLubyte));
77 SkDebugf("Alloc image buffer failed!\n");
78 this->destroyGLContext();
82 // Bind the buffer to the context and make it current
83 if (!OSMesaMakeCurrent((OSMesaContext)fContext,
88 SkDebugf("OSMesaMakeCurrent failed!\n");
89 this->destroyGLContext();
93 const GrGLInterface* interface = GrGLCreateMesaInterface();
95 SkDebugf("Could not create GL interface!\n");
96 this->destroyGLContext();
103 void SkMesaGLContext::makeCurrent() const {
105 if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage,
106 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) {
107 SkDebugf("Could not make MESA context current.");
112 void SkMesaGLContext::swapBuffers() const { }