blob: 9dcc8ecbb8b3f58f11a3d006b80a957e1c2439a3 [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 +000011SK_DEFINE_INST_COUNT(SkGLContextHelper)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000012
robertphillips@google.com6177e692013-02-28 20:16:25 +000013SkGLContextHelper::SkGLContextHelper()
bsalomon@google.com373a6632011-10-19 20:43:20 +000014 : fFBO(0)
robertphillips@google.com7c959422012-03-22 20:43:56 +000015 , fColorBufferID(0)
16 , fDepthStencilBufferID(0)
bsalomon@google.com373a6632011-10-19 20:43:20 +000017 , fGL(NULL) {
18}
19
robertphillips@google.com6177e692013-02-28 20:16:25 +000020SkGLContextHelper::~SkGLContextHelper() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +000021
22 if (fGL) {
robertphillips@google.comedbd21a2013-02-07 21:16:41 +000023 // 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.comf6f123d2012-03-21 17:57:55 +000027 }
28
bsalomon@google.com373a6632011-10-19 20:43:20 +000029 SkSafeUnref(fGL);
30}
31
robertphillips@google.com6177e692013-02-28 20:16:25 +000032bool SkGLContextHelper::init(int width, int height) {
bsalomon@google.com373a6632011-10-19 20:43:20 +000033 if (fGL) {
34 fGL->unref();
35 this->destroyGLContext();
36 }
37
38 fGL = this->createGLContext();
39 if (fGL) {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000040 const GrGLubyte* temp;
41
bsalomon@google.com1744f972013-02-26 21:46:32 +000042 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.comfe1b5362013-02-07 19:45:46 +000049
50 SK_GL_RET(*this, temp, GetString(GR_GL_VERSION));
51 const char* versionStr = reinterpret_cast<const char*>(temp);
bsalomon@google.com6e859372012-02-09 15:25:13 +000052 GrGLVersion version = GrGLGetVersionFromString(versionStr);
53
bsalomon@google.com675c5c42011-12-06 19:54:37 +000054 // clear any existing GL erorrs
55 GrGLenum error;
56 do {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000057 SK_GL_RET(*this, error, GetError());
bsalomon@google.com675c5c42011-12-06 19:54:37 +000058 } while (GR_GL_NO_ERROR != error);
bsalomon@google.com6e859372012-02-09 15:25:13 +000059
bsalomon@google.com373a6632011-10-19 20:43:20 +000060 SK_GL(*this, GenFramebuffers(1, &fFBO));
61 SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO));
robertphillips@google.com7c959422012-03-22 20:43:56 +000062 SK_GL(*this, GenRenderbuffers(1, &fColorBufferID));
63 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fColorBufferID));
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000064 if (kES2_GrGLBinding == bindingInUse) {
bsalomon@google.com675c5c42011-12-06 19:54:37 +000065 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.com373a6632011-10-19 20:43:20 +000073 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
74 GR_GL_COLOR_ATTACHMENT0,
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +000076 fColorBufferID));
77 SK_GL(*this, GenRenderbuffers(1, &fDepthStencilBufferID));
78 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fDepthStencilBufferID));
bsalomon@google.com6e859372012-02-09 15:25:13 +000079
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.com89ec61e2012-02-10 20:05:18 +000084 if (kES2_GrGLBinding == bindingInUse) {
bsalomon@google.com1744f972013-02-26 21:46:32 +000085 supportsPackedDepthStencil = this->hasExtension("GL_OES_packed_depth_stencil");
bsalomon@google.com675c5c42011-12-06 19:54:37 +000086 } else {
bsalomon@google.com6e859372012-02-09 15:25:13 +000087 supportsPackedDepthStencil = version >= GR_GL_VER(3,0) ||
bsalomon@google.com1744f972013-02-26 21:46:32 +000088 this->hasExtension("GL_EXT_packed_depth_stencil") ||
89 this->hasExtension("GL_ARB_framebuffer_object");
bsalomon@google.com6e859372012-02-09 15:25:13 +000090 }
91
92 if (supportsPackedDepthStencil) {
93 // ES2 requires sized internal formats for RenderbufferStorage
94 // On Desktop we let the driver decide.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000095 GrGLenum format = kES2_GrGLBinding == bindingInUse ?
bsalomon@google.com6e859372012-02-09 15:25:13 +000096 GR_GL_DEPTH24_STENCIL8 :
97 GR_GL_DEPTH_STENCIL;
bsalomon@google.com675c5c42011-12-06 19:54:37 +000098 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com6e859372012-02-09 15:25:13 +000099 format,
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000100 width, height));
101 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
102 GR_GL_DEPTH_ATTACHMENT,
103 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +0000104 fDepthStencilBufferID));
bsalomon@google.com6e859372012-02-09 15:25:13 +0000105 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106 GrGLenum format = kES2_GrGLBinding == bindingInUse ?
bsalomon@google.com6e859372012-02-09 15:25:13 +0000107 GR_GL_STENCIL_INDEX8 :
108 GR_GL_STENCIL_INDEX;
109 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
110 format,
111 width, height));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000112 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000113 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
114 GR_GL_STENCIL_ATTACHMENT,
115 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +0000116 fDepthStencilBufferID));
bsalomon@google.com373a6632011-10-19 20:43:20 +0000117 SK_GL(*this, Viewport(0, 0, width, height));
118 SK_GL(*this, ClearStencil(0));
119 SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000121 SK_GL_RET(*this, error, GetError());
122 GrGLenum status;
123 SK_GL_RET(*this, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000124
125 if (GR_GL_FRAMEBUFFER_COMPLETE != status ||
126 GR_GL_NO_ERROR != error) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000127 fFBO = 0;
robertphillips@google.com7c959422012-03-22 20:43:56 +0000128 fColorBufferID = 0;
129 fDepthStencilBufferID = 0;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000130 fGL->unref();
131 fGL = NULL;
132 this->destroyGLContext();
133 return false;
134 } else {
135 return true;
136 }
137 }
138 return false;
139}