blob: cac021c170d4888fc66fb01b5e7d9535386b9c06 [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
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
tomhudson@google.com6c8c34e2012-02-14 15:31:03 +000011#include "gl/SkMesaGLContext.h"
bsalomon@google.com003a8b92012-05-07 17:38:49 +000012#include "gl/GrGLDefines.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000013
bsalomon@google.com57f5d982011-10-24 21:17:53 +000014SkMesaGLContext::AutoContextRestore::AutoContextRestore() {
15 fOldContext = (Context)OSMesaGetCurrentContext();
16 if (NULL != (OSMesaContext)fOldContext) {
17 OSMesaGetColorBuffer((OSMesaContext)fOldContext,
18 &fOldWidth, &fOldHeight,
rmistry@google.comfbfcd562012-08-23 18:09:54 +000019 &fOldFormat, &fOldImage);
bsalomon@google.com57f5d982011-10-24 21:17:53 +000020 }
21}
22
23SkMesaGLContext::AutoContextRestore::~AutoContextRestore() {
bsalomon@google.com31648eb2011-11-23 15:01:08 +000024 if (NULL != (OSMesaContext)fOldContext) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000025 OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage,
bsalomon@google.com57f5d982011-10-24 21:17:53 +000026 fOldFormat, fOldWidth, fOldHeight);
27 }
28}
29
30///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com373a6632011-10-19 20:43:20 +000031
32SkMesaGLContext::SkMesaGLContext()
33 : fContext(NULL)
34 , fImage(NULL) {
35 GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext));
36}
37
38SkMesaGLContext::~SkMesaGLContext() {
39 this->destroyGLContext();
40}
41
42void SkMesaGLContext::destroyGLContext() {
43 if (fImage) {
44 sk_free(fImage);
45 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
bsalomon@google.com373a6632011-10-19 20:43:20 +000047 if (fContext) {
48 OSMesaDestroyContext((OSMesaContext)fContext);
49 }
50}
51
52static const GrGLint gBOGUS_SIZE = 16;
53
54const 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
bsalomon@google.com373a6632011-10-19 20:43:20 +000076 // Bind the buffer to the context and make it current
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077 if (!OSMesaMakeCurrent((OSMesaContext)fContext,
78 fImage,
79 GR_GL_UNSIGNED_BYTE,
80 gBOGUS_SIZE,
bsalomon@google.com373a6632011-10-19 20:43:20 +000081 gBOGUS_SIZE)) {
82 SkDebugf("OSMesaMakeCurrent failed!\n");
83 this->destroyGLContext();
84 return NULL;
85 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
bsalomon@google.com373a6632011-10-19 20:43:20 +000087 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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094
bsalomon@google.com373a6632011-10-19 20:43:20 +000095}
96
97void SkMesaGLContext::makeCurrent() const {
98 if (fContext) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099 if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage,
bsalomon@google.com373a6632011-10-19 20:43:20 +0000100 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) {
101 SkDebugf("Could not make MESA context current.");
102 }
103 }
104}