blob: 31402c5a3cf79cd3ebcd392ba5a85c1114583867 [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
bsalomon10805962014-10-08 04:45:09 -070014SkMesaGLContext::AutoContextRestore::AutoContextRestore() {
15 fOldContext = (Context)OSMesaGetCurrentContext();
16 if (fOldContext) {
17 OSMesaGetColorBuffer((OSMesaContext)fOldContext,
18 &fOldWidth, &fOldHeight,
19 &fOldFormat, &fOldImage);
20 }
21}
22
23SkMesaGLContext::AutoContextRestore::~AutoContextRestore() {
24 if (fOldContext) {
25 OSMesaMakeCurrent((OSMesaContext)fOldContext, fOldImage,
26 fOldFormat, fOldWidth, fOldHeight);
27 }
28}
29
30///////////////////////////////////////////////////////////////////////////////
31
bsalomon@google.com373a6632011-10-19 20:43:20 +000032SkMesaGLContext::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
kkinnunen80549fc2014-06-30 06:36:31 -070056const GrGLInterface* SkMesaGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
57 if (kGLES_GrGLStandard == forcedGpuAPI) {
58 return NULL;
59 }
60
bsalomon@google.com373a6632011-10-19 20:43:20 +000061 /* Create an RGBA-mode context */
62#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
63 /* specify Z, stencil, accum sizes */
64 fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL);
65#else
66 fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL);
67#endif
68 if (!fContext) {
69 SkDebugf("OSMesaCreateContext failed!\n");
70 this->destroyGLContext();
71 return NULL;
72 }
73 // Allocate the image buffer
74 fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE *
75 4 * sizeof(GrGLubyte));
76 if (!fImage) {
77 SkDebugf("Alloc image buffer failed!\n");
78 this->destroyGLContext();
79 return NULL;
80 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081
bsalomon@google.com373a6632011-10-19 20:43:20 +000082 // Bind the buffer to the context and make it current
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083 if (!OSMesaMakeCurrent((OSMesaContext)fContext,
84 fImage,
85 GR_GL_UNSIGNED_BYTE,
86 gBOGUS_SIZE,
bsalomon@google.com373a6632011-10-19 20:43:20 +000087 gBOGUS_SIZE)) {
88 SkDebugf("OSMesaMakeCurrent failed!\n");
89 this->destroyGLContext();
90 return NULL;
91 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000092
bsalomon@google.com373a6632011-10-19 20:43:20 +000093 const GrGLInterface* interface = GrGLCreateMesaInterface();
94 if (!interface) {
95 SkDebugf("Could not create GL interface!\n");
96 this->destroyGLContext();
97 return NULL;
98 }
99 return interface;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000100
bsalomon@google.com373a6632011-10-19 20:43:20 +0000101}
102
103void SkMesaGLContext::makeCurrent() const {
104 if (fContext) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105 if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage,
bsalomon@google.com373a6632011-10-19 20:43:20 +0000106 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) {
107 SkDebugf("Could not make MESA context current.");
108 }
109 }
110}
djsollen@google.comc9542ca2013-10-09 18:25:38 +0000111
112void SkMesaGLContext::swapBuffers() const { }