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