blob: 0199d12c1964e3efa067705a816a215d3e93cfa8 [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()
bsalomon@google.com674a3a22013-01-07 17:21:07 +000033 : fContext(static_cast<Context>(NULL))
bsalomon@google.com373a6632011-10-19 20:43:20 +000034 , 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);
bsalomon@google.comdf8114d2013-03-01 18:10:41 +000045 fImage = NULL;
bsalomon@google.com373a6632011-10-19 20:43:20 +000046 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
bsalomon@google.com373a6632011-10-19 20:43:20 +000048 if (fContext) {
49 OSMesaDestroyContext((OSMesaContext)fContext);
bsalomon@google.comdf8114d2013-03-01 18:10:41 +000050 fContext = static_cast<Context>(NULL);
bsalomon@google.com373a6632011-10-19 20:43:20 +000051 }
52}
53
54static const GrGLint gBOGUS_SIZE = 16;
55
56const GrGLInterface* SkMesaGLContext::createGLContext() {
57 /* Create an RGBA-mode context */
58#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
59 /* specify Z, stencil, accum sizes */
60 fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL);
61#else
62 fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL);
63#endif
64 if (!fContext) {
65 SkDebugf("OSMesaCreateContext failed!\n");
66 this->destroyGLContext();
67 return NULL;
68 }
69 // Allocate the image buffer
70 fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE *
71 4 * sizeof(GrGLubyte));
72 if (!fImage) {
73 SkDebugf("Alloc image buffer failed!\n");
74 this->destroyGLContext();
75 return NULL;
76 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
bsalomon@google.com373a6632011-10-19 20:43:20 +000078 // Bind the buffer to the context and make it current
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079 if (!OSMesaMakeCurrent((OSMesaContext)fContext,
80 fImage,
81 GR_GL_UNSIGNED_BYTE,
82 gBOGUS_SIZE,
bsalomon@google.com373a6632011-10-19 20:43:20 +000083 gBOGUS_SIZE)) {
84 SkDebugf("OSMesaMakeCurrent failed!\n");
85 this->destroyGLContext();
86 return NULL;
87 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
bsalomon@google.com373a6632011-10-19 20:43:20 +000089 const GrGLInterface* interface = GrGLCreateMesaInterface();
90 if (!interface) {
91 SkDebugf("Could not create GL interface!\n");
92 this->destroyGLContext();
93 return NULL;
94 }
95 return interface;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096
bsalomon@google.com373a6632011-10-19 20:43:20 +000097}
98
99void SkMesaGLContext::makeCurrent() const {
100 if (fContext) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000101 if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage,
bsalomon@google.com373a6632011-10-19 20:43:20 +0000102 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) {
103 SkDebugf("Could not make MESA context current.");
104 }
105 }
106}
djsollen@google.comc9542ca2013-10-09 18:25:38 +0000107
108void SkMesaGLContext::swapBuffers() const { }