bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include <GL/osmesa.h> |
| 10 | |
| 11 | #include "SkMesaGLContext.h" |
| 12 | |
| 13 | |
| 14 | SkMesaGLContext::SkMesaGLContext() |
| 15 | : fContext(NULL) |
| 16 | , fImage(NULL) { |
| 17 | GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); |
| 18 | } |
| 19 | |
| 20 | SkMesaGLContext::~SkMesaGLContext() { |
| 21 | this->destroyGLContext(); |
| 22 | } |
| 23 | |
| 24 | void SkMesaGLContext::destroyGLContext() { |
| 25 | if (fImage) { |
| 26 | sk_free(fImage); |
| 27 | } |
| 28 | |
| 29 | if (fContext) { |
| 30 | OSMesaDestroyContext((OSMesaContext)fContext); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static const GrGLint gBOGUS_SIZE = 16; |
| 35 | |
| 36 | const GrGLInterface* SkMesaGLContext::createGLContext() { |
| 37 | /* Create an RGBA-mode context */ |
| 38 | #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 |
| 39 | /* specify Z, stencil, accum sizes */ |
| 40 | fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL); |
| 41 | #else |
| 42 | fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL); |
| 43 | #endif |
| 44 | if (!fContext) { |
| 45 | SkDebugf("OSMesaCreateContext failed!\n"); |
| 46 | this->destroyGLContext(); |
| 47 | return NULL; |
| 48 | } |
| 49 | // Allocate the image buffer |
| 50 | fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * |
| 51 | 4 * sizeof(GrGLubyte)); |
| 52 | if (!fImage) { |
| 53 | SkDebugf("Alloc image buffer failed!\n"); |
| 54 | this->destroyGLContext(); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | // Bind the buffer to the context and make it current |
| 59 | if (!OSMesaMakeCurrent((OSMesaContext)fContext, |
| 60 | fImage, |
| 61 | GR_GL_UNSIGNED_BYTE, |
| 62 | gBOGUS_SIZE, |
| 63 | gBOGUS_SIZE)) { |
| 64 | SkDebugf("OSMesaMakeCurrent failed!\n"); |
| 65 | this->destroyGLContext(); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | const GrGLInterface* interface = GrGLCreateMesaInterface(); |
| 70 | if (!interface) { |
| 71 | SkDebugf("Could not create GL interface!\n"); |
| 72 | this->destroyGLContext(); |
| 73 | return NULL; |
| 74 | } |
| 75 | return interface; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | void SkMesaGLContext::makeCurrent() const { |
| 80 | if (fContext) { |
| 81 | if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage, |
| 82 | GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { |
| 83 | SkDebugf("Could not make MESA context current."); |
| 84 | } |
| 85 | } |
| 86 | } |