blob: 59ac31daff081af6f8bcd79fb890222bc95ce003 [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
tomhudson@google.com6bf38b52012-02-14 15:11:59 +00008#include "gl/SkGLContext.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +00009#include "gl/GrGLUtil.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000010
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000011SK_DEFINE_INST_COUNT(SkGLContext)
12
bsalomon@google.com373a6632011-10-19 20:43:20 +000013SkGLContext::SkGLContext()
14 : 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
20SkGLContext::~SkGLContext() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +000021
22 if (fGL) {
23 SK_GL(*this, DeleteFramebuffers(1, &fFBO));
robertphillips@google.com7c959422012-03-22 20:43:56 +000024 SK_GL(*this, DeleteRenderbuffers(1, &fColorBufferID));
25 SK_GL(*this, DeleteRenderbuffers(1, &fDepthStencilBufferID));
robertphillips@google.comf6f123d2012-03-21 17:57:55 +000026 }
27
bsalomon@google.com373a6632011-10-19 20:43:20 +000028 SkSafeUnref(fGL);
29}
30
bsalomon@google.com6e859372012-02-09 15:25:13 +000031bool SkGLContext::hasExtension(const char* extensionName) const {
32 return GrGLHasExtensionFromString(extensionName, fExtensionString.c_str());
33}
34
bsalomon@google.com373a6632011-10-19 20:43:20 +000035bool SkGLContext::init(int width, int height) {
36 if (fGL) {
37 fGL->unref();
38 this->destroyGLContext();
39 }
40
41 fGL = this->createGLContext();
42 if (fGL) {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000043 const GrGLubyte* temp;
44
45 SK_GL_RET(*this, temp, GetString(GR_GL_EXTENSIONS));
46 fExtensionString = reinterpret_cast<const char*>(temp);
47
48 SK_GL_RET(*this, temp, GetString(GR_GL_VERSION));
49 const char* versionStr = reinterpret_cast<const char*>(temp);
bsalomon@google.com6e859372012-02-09 15:25:13 +000050 GrGLVersion version = GrGLGetVersionFromString(versionStr);
51
bsalomon@google.com675c5c42011-12-06 19:54:37 +000052 // clear any existing GL erorrs
53 GrGLenum error;
54 do {
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000055 SK_GL_RET(*this, error, GetError());
bsalomon@google.com675c5c42011-12-06 19:54:37 +000056 } while (GR_GL_NO_ERROR != error);
bsalomon@google.com6e859372012-02-09 15:25:13 +000057
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000058 GrGLBinding bindingInUse = GrGLGetBindingInUse(this->gl());
59
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) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085 supportsPackedDepthStencil =
bsalomon@google.com6e859372012-02-09 15:25:13 +000086 this->hasExtension("GL_OES_packed_depth_stencil");
bsalomon@google.com675c5c42011-12-06 19:54:37 +000087 } else {
bsalomon@google.com6e859372012-02-09 15:25:13 +000088 supportsPackedDepthStencil = version >= GR_GL_VER(3,0) ||
89 this->hasExtension("GL_EXT_packed_depth_stencil") ||
90 this->hasExtension("GL_ARB_framebuffer_object");
91 }
92
93 if (supportsPackedDepthStencil) {
94 // ES2 requires sized internal formats for RenderbufferStorage
95 // On Desktop we let the driver decide.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096 GrGLenum format = kES2_GrGLBinding == bindingInUse ?
bsalomon@google.com6e859372012-02-09 15:25:13 +000097 GR_GL_DEPTH24_STENCIL8 :
98 GR_GL_DEPTH_STENCIL;
bsalomon@google.com675c5c42011-12-06 19:54:37 +000099 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com6e859372012-02-09 15:25:13 +0000100 format,
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000101 width, height));
102 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
103 GR_GL_DEPTH_ATTACHMENT,
104 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +0000105 fDepthStencilBufferID));
bsalomon@google.com6e859372012-02-09 15:25:13 +0000106 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000107 GrGLenum format = kES2_GrGLBinding == bindingInUse ?
bsalomon@google.com6e859372012-02-09 15:25:13 +0000108 GR_GL_STENCIL_INDEX8 :
109 GR_GL_STENCIL_INDEX;
110 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
111 format,
112 width, height));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000113 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000114 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
115 GR_GL_STENCIL_ATTACHMENT,
116 GR_GL_RENDERBUFFER,
robertphillips@google.com7c959422012-03-22 20:43:56 +0000117 fDepthStencilBufferID));
bsalomon@google.com373a6632011-10-19 20:43:20 +0000118 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.comfbfcd562012-08-23 18:09:54 +0000121
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000122 SK_GL_RET(*this, error, GetError());
123 GrGLenum status;
124 SK_GL_RET(*this, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000125
126 if (GR_GL_FRAMEBUFFER_COMPLETE != status ||
127 GR_GL_NO_ERROR != error) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000128 fFBO = 0;
robertphillips@google.com7c959422012-03-22 20:43:56 +0000129 fColorBufferID = 0;
130 fDepthStencilBufferID = 0;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000131 fGL->unref();
132 fGL = NULL;
133 this->destroyGLContext();
134 return false;
135 } else {
136 return true;
137 }
138 }
139 return false;
140}