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