blob: b06f755f677196bfabd5fcd58dda6a213cda665d [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
2/*
robertphillips@google.com6177e692013-02-28 20:16:25 +00003 * Copyright 2013 Google Inc.
bsalomon@google.com373a6632011-10-19 20:43:20 +00004 *
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.com6177e692013-02-28 20:16:25 +00008#include "gl/SkGLContextHelper.h"
bsalomon@google.com1744f972013-02-26 21:46:32 +00009#include "GrGLUtil.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000010
robertphillips@google.com6177e692013-02-28 20:16:25 +000011SkGLContextHelper::SkGLContextHelper()
bsalomon@google.com373a6632011-10-19 20:43:20 +000012 : fFBO(0)
robertphillips@google.com7c959422012-03-22 20:43:56 +000013 , fColorBufferID(0)
14 , fDepthStencilBufferID(0)
bsalomon@google.com373a6632011-10-19 20:43:20 +000015 , fGL(NULL) {
16}
17
robertphillips@google.com6177e692013-02-28 20:16:25 +000018SkGLContextHelper::~SkGLContextHelper() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +000019
20 if (fGL) {
robertphillips@google.comedbd21a2013-02-07 21:16:41 +000021 // 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.comf6f123d2012-03-21 17:57:55 +000025 }
26
bsalomon@google.com373a6632011-10-19 20:43:20 +000027 SkSafeUnref(fGL);
28}
29
robertphillips@google.com6177e692013-02-28 20:16:25 +000030bool SkGLContextHelper::init(int width, int height) {
bsalomon@google.com373a6632011-10-19 20:43:20 +000031 if (fGL) {
32 fGL->unref();
33 this->destroyGLContext();
34 }
35
36 fGL = this->createGLContext();
37 if (fGL) {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000038 const GrGLubyte* temp;
39
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000040 if (!fGL->validate()) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000041 fGL = NULL;
42 this->destroyGLContext();
43 return false;
44 }
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000045
46 SK_GL_RET(*this, temp, GetString(GR_GL_VERSION));
47 const char* versionStr = reinterpret_cast<const char*>(temp);
bsalomon@google.com6e859372012-02-09 15:25:13 +000048 GrGLVersion version = GrGLGetVersionFromString(versionStr);
49
bsalomon@google.com675c5c42011-12-06 19:54:37 +000050 // clear any existing GL erorrs
51 GrGLenum error;
52 do {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000053 SK_GL_RET(*this, error, GetError());
bsalomon@google.com675c5c42011-12-06 19:54:37 +000054 } while (GR_GL_NO_ERROR != error);
bsalomon@google.com6e859372012-02-09 15:25:13 +000055
bsalomon@google.com373a6632011-10-19 20:43:20 +000056 SK_GL(*this, GenFramebuffers(1, &fFBO));
57 SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO));
robertphillips@google.com7c959422012-03-22 20:43:56 +000058 SK_GL(*this, GenRenderbuffers(1, &fColorBufferID));
59 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fColorBufferID));
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000060 if (kGLES_GrGLStandard == this->gl()->fStandard) {
bsalomon@google.com675c5c42011-12-06 19:54:37 +000061 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
62 GR_GL_RGBA8,
63 width, height));
64 } else {
65 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
66 GR_GL_RGBA,
67 width, height));
68 }
bsalomon@google.com373a6632011-10-19 20:43:20 +000069 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
70 GR_GL_COLOR_ATTACHMENT0,
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +000072 fColorBufferID));
73 SK_GL(*this, GenRenderbuffers(1, &fDepthStencilBufferID));
74 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fDepthStencilBufferID));
bsalomon@google.com6e859372012-02-09 15:25:13 +000075
76 // Some drivers that support packed depth stencil will only succeed
77 // in binding a packed format an FBO. However, we can't rely on packed
78 // depth stencil being available.
79 bool supportsPackedDepthStencil;
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000080 if (kGLES_GrGLStandard == this->gl()->fStandard) {
commit-bot@chromium.org04c500f2013-09-06 15:28:01 +000081 supportsPackedDepthStencil = version >= GR_GL_VER(3,0) ||
82 this->hasExtension("GL_OES_packed_depth_stencil");
bsalomon@google.com675c5c42011-12-06 19:54:37 +000083 } else {
bsalomon@google.com6e859372012-02-09 15:25:13 +000084 supportsPackedDepthStencil = version >= GR_GL_VER(3,0) ||
bsalomon@google.com1744f972013-02-26 21:46:32 +000085 this->hasExtension("GL_EXT_packed_depth_stencil") ||
86 this->hasExtension("GL_ARB_framebuffer_object");
bsalomon@google.com6e859372012-02-09 15:25:13 +000087 }
88
89 if (supportsPackedDepthStencil) {
90 // ES2 requires sized internal formats for RenderbufferStorage
91 // On Desktop we let the driver decide.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000092 GrGLenum format = kGLES_GrGLStandard == this->gl()->fStandard ?
bsalomon@google.com6e859372012-02-09 15:25:13 +000093 GR_GL_DEPTH24_STENCIL8 :
94 GR_GL_DEPTH_STENCIL;
bsalomon@google.com675c5c42011-12-06 19:54:37 +000095 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com6e859372012-02-09 15:25:13 +000096 format,
bsalomon@google.com675c5c42011-12-06 19:54:37 +000097 width, height));
98 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
99 GR_GL_DEPTH_ATTACHMENT,
100 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +0000101 fDepthStencilBufferID));
bsalomon@google.com6e859372012-02-09 15:25:13 +0000102 } else {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000103 GrGLenum format = kGLES_GrGLStandard == this->gl()->fStandard ? GR_GL_STENCIL_INDEX8 :
104 GR_GL_STENCIL_INDEX;
bsalomon@google.com6e859372012-02-09 15:25:13 +0000105 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
106 format,
107 width, height));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000108 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000109 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
110 GR_GL_STENCIL_ATTACHMENT,
111 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +0000112 fDepthStencilBufferID));
bsalomon@google.com373a6632011-10-19 20:43:20 +0000113 SK_GL(*this, Viewport(0, 0, width, height));
114 SK_GL(*this, ClearStencil(0));
115 SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000116
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000117 SK_GL_RET(*this, error, GetError());
118 GrGLenum status;
119 SK_GL_RET(*this, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000120
121 if (GR_GL_FRAMEBUFFER_COMPLETE != status ||
122 GR_GL_NO_ERROR != error) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000123 fFBO = 0;
robertphillips@google.com7c959422012-03-22 20:43:56 +0000124 fColorBufferID = 0;
125 fDepthStencilBufferID = 0;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000126 fGL->unref();
127 fGL = NULL;
128 this->destroyGLContext();
129 return false;
130 } else {
131 return true;
132 }
133 }
134 return false;
135}