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