blob: 8c339c7fac4fc50ac837b4155464b92d6a8e7b69 [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.com373a6632011-10-19 20:43:20 +000014SkMesaGLContext::SkMesaGLContext()
bsalomon@google.com674a3a22013-01-07 17:21:07 +000015 : fContext(static_cast<Context>(NULL))
bsalomon@google.com373a6632011-10-19 20:43:20 +000016 , fImage(NULL) {
17 GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext));
18}
19
20SkMesaGLContext::~SkMesaGLContext() {
21 this->destroyGLContext();
22}
23
24void SkMesaGLContext::destroyGLContext() {
25 if (fImage) {
26 sk_free(fImage);
bsalomon@google.comdf8114d2013-03-01 18:10:41 +000027 fImage = NULL;
bsalomon@google.com373a6632011-10-19 20:43:20 +000028 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029
bsalomon@google.com373a6632011-10-19 20:43:20 +000030 if (fContext) {
31 OSMesaDestroyContext((OSMesaContext)fContext);
bsalomon@google.comdf8114d2013-03-01 18:10:41 +000032 fContext = static_cast<Context>(NULL);
bsalomon@google.com373a6632011-10-19 20:43:20 +000033 }
34}
35
36static const GrGLint gBOGUS_SIZE = 16;
37
kkinnunen80549fc2014-06-30 06:36:31 -070038const GrGLInterface* SkMesaGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
39 if (kGLES_GrGLStandard == forcedGpuAPI) {
40 return NULL;
41 }
42
bsalomon@google.com373a6632011-10-19 20:43:20 +000043 /* Create an RGBA-mode context */
44#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
45 /* specify Z, stencil, accum sizes */
46 fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL);
47#else
48 fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL);
49#endif
50 if (!fContext) {
51 SkDebugf("OSMesaCreateContext failed!\n");
52 this->destroyGLContext();
53 return NULL;
54 }
55 // Allocate the image buffer
56 fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE *
57 4 * sizeof(GrGLubyte));
58 if (!fImage) {
59 SkDebugf("Alloc image buffer failed!\n");
60 this->destroyGLContext();
61 return NULL;
62 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063
bsalomon@google.com373a6632011-10-19 20:43:20 +000064 // Bind the buffer to the context and make it current
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065 if (!OSMesaMakeCurrent((OSMesaContext)fContext,
66 fImage,
67 GR_GL_UNSIGNED_BYTE,
68 gBOGUS_SIZE,
bsalomon@google.com373a6632011-10-19 20:43:20 +000069 gBOGUS_SIZE)) {
70 SkDebugf("OSMesaMakeCurrent failed!\n");
71 this->destroyGLContext();
72 return NULL;
73 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074
bsalomon@google.com373a6632011-10-19 20:43:20 +000075 const GrGLInterface* interface = GrGLCreateMesaInterface();
76 if (!interface) {
77 SkDebugf("Could not create GL interface!\n");
78 this->destroyGLContext();
79 return NULL;
80 }
81 return interface;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
bsalomon@google.com373a6632011-10-19 20:43:20 +000083}
84
85void SkMesaGLContext::makeCurrent() const {
86 if (fContext) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087 if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage,
bsalomon@google.com373a6632011-10-19 20:43:20 +000088 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) {
89 SkDebugf("Could not make MESA context current.");
90 }
91 }
92}
djsollen@google.comc9542ca2013-10-09 18:25:38 +000093
94void SkMesaGLContext::swapBuffers() const { }