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