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 "gl/SkMesaGLContext.h"
12
AutoContextRestore()13 SkMesaGLContext::AutoContextRestore::AutoContextRestore() {
14 fOldContext = (Context)OSMesaGetCurrentContext();
15 if (NULL != (OSMesaContext)fOldContext) {
16 OSMesaGetColorBuffer((OSMesaContext)fOldContext,
17 &fOldWidth, &fOldHeight,
18 &fOldFormat, &fOldImage);
19 }
20 }
21
~AutoContextRestore()22 SkMesaGLContext::AutoContextRestore::~AutoContextRestore() {
23 if (NULL != (OSMesaContext)fOldContext) {
24 OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage,
25 fOldFormat, fOldWidth, fOldHeight);
26 }
27 }
28
29 ///////////////////////////////////////////////////////////////////////////////
30
SkMesaGLContext()31 SkMesaGLContext::SkMesaGLContext()
32 : fContext(NULL)
33 , fImage(NULL) {
34 GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext));
35 }
36
~SkMesaGLContext()37 SkMesaGLContext::~SkMesaGLContext() {
38 this->destroyGLContext();
39 }
40
destroyGLContext()41 void SkMesaGLContext::destroyGLContext() {
42 if (fImage) {
43 sk_free(fImage);
44 }
45
46 if (fContext) {
47 OSMesaDestroyContext((OSMesaContext)fContext);
48 }
49 }
50
51 static const GrGLint gBOGUS_SIZE = 16;
52
createGLContext()53 const GrGLInterface* SkMesaGLContext::createGLContext() {
54 /* Create an RGBA-mode context */
55 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
56 /* specify Z, stencil, accum sizes */
57 fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL);
58 #else
59 fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL);
60 #endif
61 if (!fContext) {
62 SkDebugf("OSMesaCreateContext failed!\n");
63 this->destroyGLContext();
64 return NULL;
65 }
66 // Allocate the image buffer
67 fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE *
68 4 * sizeof(GrGLubyte));
69 if (!fImage) {
70 SkDebugf("Alloc image buffer failed!\n");
71 this->destroyGLContext();
72 return NULL;
73 }
74
75 // Bind the buffer to the context and make it current
76 if (!OSMesaMakeCurrent((OSMesaContext)fContext,
77 fImage,
78 GR_GL_UNSIGNED_BYTE,
79 gBOGUS_SIZE,
80 gBOGUS_SIZE)) {
81 SkDebugf("OSMesaMakeCurrent failed!\n");
82 this->destroyGLContext();
83 return NULL;
84 }
85
86 const GrGLInterface* interface = GrGLCreateMesaInterface();
87 if (!interface) {
88 SkDebugf("Could not create GL interface!\n");
89 this->destroyGLContext();
90 return NULL;
91 }
92 return interface;
93
94 }
95
makeCurrent() const96 void SkMesaGLContext::makeCurrent() const {
97 if (fContext) {
98 if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage,
99 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) {
100 SkDebugf("Could not make MESA context current.");
101 }
102 }
103 }
104