OLD | NEW |
(Empty) | |
| 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/mesa/GLContext_mesa.h" |
| 12 #include "gl/GrGLDefines.h" |
| 13 |
| 14 #include "gl/GrGLAssembleInterface.h" |
| 15 #include "gl/GrGLUtil.h" |
| 16 #include "osmesa_wrapper.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 static GrGLFuncPtr osmesa_get(void* ctx, const char name[]) { |
| 21 SkASSERT(nullptr == ctx); |
| 22 SkASSERT(OSMesaGetCurrentContext()); |
| 23 return OSMesaGetProcAddress(name); |
| 24 } |
| 25 |
| 26 static const GrGLInterface* create_mesa_interface() { |
| 27 if (nullptr == OSMesaGetCurrentContext()) { |
| 28 return nullptr; |
| 29 } |
| 30 return GrGLAssembleInterface(nullptr, osmesa_get); |
| 31 } |
| 32 |
| 33 static const GrGLint gBOGUS_SIZE = 16; |
| 34 |
| 35 class MesaGLContext : public sk_gpu_test::GLContext { |
| 36 private: |
| 37 typedef intptr_t Context; |
| 38 |
| 39 public: |
| 40 MesaGLContext(); |
| 41 ~MesaGLContext() override; |
| 42 |
| 43 private: |
| 44 void destroyGLContext(); |
| 45 |
| 46 void onPlatformMakeCurrent() const override; |
| 47 |
| 48 void onPlatformSwapBuffers() const override; |
| 49 |
| 50 GrGLFuncPtr onPlatformGetProcAddress(const char *) const override; |
| 51 |
| 52 Context fContext; |
| 53 GrGLubyte *fImage; |
| 54 }; |
| 55 |
| 56 MesaGLContext::MesaGLContext() : fContext(static_cast<Context>(0)), fImage(nullp
tr) { |
| 57 GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); |
| 58 |
| 59 /* Create an RGBA-mode context */ |
| 60 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 |
| 61 /* specify Z, stencil, accum sizes */ |
| 62 fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr); |
| 63 #else |
| 64 fContext = (Context) OSMesaCreateContext(OSMESA_BGRA, nullptr); |
| 65 #endif |
| 66 if (!fContext) { |
| 67 SkDebugf("OSMesaCreateContext failed!\n"); |
| 68 this->destroyGLContext(); |
| 69 return; |
| 70 } |
| 71 // Allocate the image buffer |
| 72 fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * |
| 73 4 * sizeof(GrGLubyte)); |
| 74 if (!fImage) { |
| 75 SkDebugf("Alloc image buffer failed!\n"); |
| 76 this->destroyGLContext(); |
| 77 return; |
| 78 } |
| 79 |
| 80 // Bind the buffer to the context and make it current |
| 81 if (!OSMesaMakeCurrent((OSMesaContext) fContext, |
| 82 fImage, |
| 83 GR_GL_UNSIGNED_BYTE, |
| 84 gBOGUS_SIZE, |
| 85 gBOGUS_SIZE)) { |
| 86 SkDebugf("OSMesaMakeCurrent failed!\n"); |
| 87 this->destroyGLContext(); |
| 88 return; |
| 89 } |
| 90 |
| 91 SkAutoTUnref<const GrGLInterface> gl(create_mesa_interface()); |
| 92 if (nullptr == gl.get()) { |
| 93 SkDebugf("Could not create GL interface!\n"); |
| 94 this->destroyGLContext(); |
| 95 return; |
| 96 } |
| 97 |
| 98 if (!gl->validate()) { |
| 99 SkDebugf("Could not validate GL interface!\n"); |
| 100 this->destroyGLContext(); |
| 101 return; |
| 102 } |
| 103 |
| 104 this->init(gl.release()); |
| 105 } |
| 106 |
| 107 MesaGLContext::~MesaGLContext() { |
| 108 this->teardown(); |
| 109 this->destroyGLContext(); |
| 110 } |
| 111 |
| 112 void MesaGLContext::destroyGLContext() { |
| 113 if (fImage) { |
| 114 sk_free(fImage); |
| 115 fImage = nullptr; |
| 116 } |
| 117 |
| 118 if (fContext) { |
| 119 OSMesaDestroyContext((OSMesaContext) fContext); |
| 120 fContext = static_cast<Context>(0); |
| 121 } |
| 122 } |
| 123 |
| 124 |
| 125 void MesaGLContext::onPlatformMakeCurrent() const { |
| 126 if (fContext) { |
| 127 if (!OSMesaMakeCurrent((OSMesaContext) fContext, fImage, |
| 128 GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { |
| 129 SkDebugf("Could not make MESA context current."); |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 void MesaGLContext::onPlatformSwapBuffers() const { } |
| 135 |
| 136 GrGLFuncPtr MesaGLContext::onPlatformGetProcAddress(const char *procName) const
{ |
| 137 return OSMesaGetProcAddress(procName); |
| 138 } |
| 139 } // anonymous namespace |
| 140 |
| 141 |
| 142 namespace sk_gpu_test { |
| 143 GLContext *CreateMesaGLContext() { |
| 144 MesaGLContext *ctx = new MesaGLContext; |
| 145 if (!ctx->isValid()) { |
| 146 delete ctx; |
| 147 return nullptr; |
| 148 } |
| 149 return ctx; |
| 150 } |
| 151 } // sk_gpu_test |
OLD | NEW |