| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 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 | */ |
| tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 8 | #include "gl/SkGLContext.h" |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 9 | #include "gl/GrGLUtil.h" |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 10 | |
| robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 11 | SK_DEFINE_INST_COUNT(SkGLContext) |
| 12 | |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 13 | SkGLContext::SkGLContext() |
| 14 | : fFBO(0) |
| robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 15 | , fColorBufferID(0) |
| 16 | , fDepthStencilBufferID(0) |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 17 | , fGL(NULL) { |
| 18 | } |
| 19 | |
| 20 | SkGLContext::~SkGLContext() { |
| robertphillips@google.com | f6f123d | 2012-03-21 17:57:55 +0000 | [diff] [blame] | 21 | |
| 22 | if (fGL) { |
| robertphillips@google.com | edbd21a | 2013-02-07 21:16:41 +0000 | [diff] [blame] | 23 | // TODO: determine why DeleteFramebuffers is generating a GL error in tests |
| 24 | SK_GL_NOERRCHECK(*this, DeleteFramebuffers(1, &fFBO)); |
| 25 | SK_GL_NOERRCHECK(*this, DeleteRenderbuffers(1, &fColorBufferID)); |
| 26 | SK_GL_NOERRCHECK(*this, DeleteRenderbuffers(1, &fDepthStencilBufferID)); |
| robertphillips@google.com | f6f123d | 2012-03-21 17:57:55 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 29 | SkSafeUnref(fGL); |
| 30 | } |
| 31 | |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 32 | bool SkGLContext::hasExtension(const char* extensionName) const { |
| 33 | return GrGLHasExtensionFromString(extensionName, fExtensionString.c_str()); |
| 34 | } |
| 35 | |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 36 | bool SkGLContext::init(int width, int height) { |
| 37 | if (fGL) { |
| 38 | fGL->unref(); |
| 39 | this->destroyGLContext(); |
| 40 | } |
| 41 | |
| 42 | fGL = this->createGLContext(); |
| 43 | if (fGL) { |
| robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 44 | const GrGLubyte* temp; |
| 45 | |
| 46 | SK_GL_RET(*this, temp, GetString(GR_GL_EXTENSIONS)); |
| 47 | fExtensionString = reinterpret_cast<const char*>(temp); |
| 48 | |
| 49 | SK_GL_RET(*this, temp, GetString(GR_GL_VERSION)); |
| 50 | const char* versionStr = reinterpret_cast<const char*>(temp); |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 51 | GrGLVersion version = GrGLGetVersionFromString(versionStr); |
| 52 | |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 53 | // clear any existing GL erorrs |
| 54 | GrGLenum error; |
| 55 | do { |
| robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 56 | SK_GL_RET(*this, error, GetError()); |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 57 | } while (GR_GL_NO_ERROR != error); |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 58 | |
| bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 59 | GrGLBinding bindingInUse = GrGLGetBindingInUse(this->gl()); |
| 60 | |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 61 | SK_GL(*this, GenFramebuffers(1, &fFBO)); |
| 62 | SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO)); |
| robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 63 | SK_GL(*this, GenRenderbuffers(1, &fColorBufferID)); |
| 64 | SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fColorBufferID)); |
| bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 65 | if (kES2_GrGLBinding == bindingInUse) { |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 66 | SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 67 | GR_GL_RGBA8, |
| 68 | width, height)); |
| 69 | } else { |
| 70 | SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 71 | GR_GL_RGBA, |
| 72 | width, height)); |
| 73 | } |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 74 | SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 75 | GR_GL_COLOR_ATTACHMENT0, |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 76 | GR_GL_RENDERBUFFER, |
| robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 77 | fColorBufferID)); |
| 78 | SK_GL(*this, GenRenderbuffers(1, &fDepthStencilBufferID)); |
| 79 | SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fDepthStencilBufferID)); |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 80 | |
| 81 | // Some drivers that support packed depth stencil will only succeed |
| 82 | // in binding a packed format an FBO. However, we can't rely on packed |
| 83 | // depth stencil being available. |
| 84 | bool supportsPackedDepthStencil; |
| bsalomon@google.com | 89ec61e | 2012-02-10 20:05:18 +0000 | [diff] [blame] | 85 | if (kES2_GrGLBinding == bindingInUse) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 86 | supportsPackedDepthStencil = |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 87 | this->hasExtension("GL_OES_packed_depth_stencil"); |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 88 | } else { |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 89 | supportsPackedDepthStencil = version >= GR_GL_VER(3,0) || |
| 90 | this->hasExtension("GL_EXT_packed_depth_stencil") || |
| 91 | this->hasExtension("GL_ARB_framebuffer_object"); |
| 92 | } |
| 93 | |
| 94 | if (supportsPackedDepthStencil) { |
| 95 | // ES2 requires sized internal formats for RenderbufferStorage |
| 96 | // On Desktop we let the driver decide. |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 97 | GrGLenum format = kES2_GrGLBinding == bindingInUse ? |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 98 | GR_GL_DEPTH24_STENCIL8 : |
| 99 | GR_GL_DEPTH_STENCIL; |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 100 | SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 101 | format, |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 102 | width, height)); |
| 103 | SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 104 | GR_GL_DEPTH_ATTACHMENT, |
| 105 | GR_GL_RENDERBUFFER, |
| robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 106 | fDepthStencilBufferID)); |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 107 | } else { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 108 | GrGLenum format = kES2_GrGLBinding == bindingInUse ? |
| bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 109 | GR_GL_STENCIL_INDEX8 : |
| 110 | GR_GL_STENCIL_INDEX; |
| 111 | SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 112 | format, |
| 113 | width, height)); |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 114 | } |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 115 | SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 116 | GR_GL_STENCIL_ATTACHMENT, |
| 117 | GR_GL_RENDERBUFFER, |
| robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 118 | fDepthStencilBufferID)); |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 119 | SK_GL(*this, Viewport(0, 0, width, height)); |
| 120 | SK_GL(*this, ClearStencil(0)); |
| 121 | SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT)); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 122 | |
| robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 123 | SK_GL_RET(*this, error, GetError()); |
| 124 | GrGLenum status; |
| 125 | SK_GL_RET(*this, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| bsalomon@google.com | 675c5c4 | 2011-12-06 19:54:37 +0000 | [diff] [blame] | 126 | |
| 127 | if (GR_GL_FRAMEBUFFER_COMPLETE != status || |
| 128 | GR_GL_NO_ERROR != error) { |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 129 | fFBO = 0; |
| robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 130 | fColorBufferID = 0; |
| 131 | fDepthStencilBufferID = 0; |
| bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 132 | fGL->unref(); |
| 133 | fGL = NULL; |
| 134 | this->destroyGLContext(); |
| 135 | return false; |
| 136 | } else { |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 | return false; |
| 141 | } |