blob: 6142d3c572cdb93de78f5c97e56fe189bd0d7534 [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 */
8#include "SkGLContext.h"
9
10SkGLContext::SkGLContext()
11 : fFBO(0)
12 , fGL(NULL) {
13}
14
15SkGLContext::~SkGLContext() {
16 SkSafeUnref(fGL);
17}
18
bsalomon@google.com6e859372012-02-09 15:25:13 +000019bool SkGLContext::hasExtension(const char* extensionName) const {
20 return GrGLHasExtensionFromString(extensionName, fExtensionString.c_str());
21}
22
bsalomon@google.com373a6632011-10-19 20:43:20 +000023bool SkGLContext::init(int width, int height) {
24 if (fGL) {
25 fGL->unref();
26 this->destroyGLContext();
27 }
28
29 fGL = this->createGLContext();
30 if (fGL) {
bsalomon@google.com6e859372012-02-09 15:25:13 +000031 fExtensionString =
32 reinterpret_cast<const char*>(SK_GL(*this,
33 GetString(GR_GL_EXTENSIONS)));
34 const char* versionStr =
35 reinterpret_cast<const char*>(SK_GL(*this,
36 GetString(GR_GL_VERSION)));
37 GrGLVersion version = GrGLGetVersionFromString(versionStr);
38
bsalomon@google.com675c5c42011-12-06 19:54:37 +000039 // clear any existing GL erorrs
40 GrGLenum error;
41 do {
42 error = SK_GL(*this, GetError());
43 } while (GR_GL_NO_ERROR != error);
bsalomon@google.com6e859372012-02-09 15:25:13 +000044
bsalomon@google.com373a6632011-10-19 20:43:20 +000045 GrGLuint cbID;
46 GrGLuint dsID;
bsalomon@google.com6e859372012-02-09 15:25:13 +000047
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000048 GrGLBinding bindingInUse = GrGLGetBindingInUse(this->gl());
49
bsalomon@google.com373a6632011-10-19 20:43:20 +000050 SK_GL(*this, GenFramebuffers(1, &fFBO));
51 SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO));
52 SK_GL(*this, GenRenderbuffers(1, &cbID));
53 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000054 if (kES2_GrGLBinding == bindingInUse) {
bsalomon@google.com675c5c42011-12-06 19:54:37 +000055 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
56 GR_GL_RGBA8,
57 width, height));
58 } else {
59 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
60 GR_GL_RGBA,
61 width, height));
62 }
bsalomon@google.com373a6632011-10-19 20:43:20 +000063 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
64 GR_GL_COLOR_ATTACHMENT0,
65 GR_GL_RENDERBUFFER,
66 cbID));
67 SK_GL(*this, GenRenderbuffers(1, &dsID));
68 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, dsID));
bsalomon@google.com6e859372012-02-09 15:25:13 +000069
70 // Some drivers that support packed depth stencil will only succeed
71 // in binding a packed format an FBO. However, we can't rely on packed
72 // depth stencil being available.
73 bool supportsPackedDepthStencil;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000074 if (kES2_GrGLBinding == bindingInUse) {
bsalomon@google.com6e859372012-02-09 15:25:13 +000075 supportsPackedDepthStencil =
76 this->hasExtension("GL_OES_packed_depth_stencil");
bsalomon@google.com675c5c42011-12-06 19:54:37 +000077 } else {
bsalomon@google.com6e859372012-02-09 15:25:13 +000078 supportsPackedDepthStencil = version >= GR_GL_VER(3,0) ||
79 this->hasExtension("GL_EXT_packed_depth_stencil") ||
80 this->hasExtension("GL_ARB_framebuffer_object");
81 }
82
83 if (supportsPackedDepthStencil) {
84 // ES2 requires sized internal formats for RenderbufferStorage
85 // On Desktop we let the driver decide.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000086 GrGLenum format = kES2_GrGLBinding == bindingInUse ?
bsalomon@google.com6e859372012-02-09 15:25:13 +000087 GR_GL_DEPTH24_STENCIL8 :
88 GR_GL_DEPTH_STENCIL;
bsalomon@google.com675c5c42011-12-06 19:54:37 +000089 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com6e859372012-02-09 15:25:13 +000090 format,
bsalomon@google.com675c5c42011-12-06 19:54:37 +000091 width, height));
92 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
93 GR_GL_DEPTH_ATTACHMENT,
94 GR_GL_RENDERBUFFER,
95 dsID));
bsalomon@google.com6e859372012-02-09 15:25:13 +000096 } else {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000097 GrGLenum format = kES2_GrGLBinding == bindingInUse ?
bsalomon@google.com6e859372012-02-09 15:25:13 +000098 GR_GL_STENCIL_INDEX8 :
99 GR_GL_STENCIL_INDEX;
100 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
101 format,
102 width, height));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000103 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000104 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
105 GR_GL_STENCIL_ATTACHMENT,
106 GR_GL_RENDERBUFFER,
107 dsID));
108 SK_GL(*this, Viewport(0, 0, width, height));
109 SK_GL(*this, ClearStencil(0));
110 SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000111
112 error = SK_GL(*this, GetError());
bsalomon@google.com373a6632011-10-19 20:43:20 +0000113 GrGLenum status =
114 SK_GL(*this, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com675c5c42011-12-06 19:54:37 +0000115
116 if (GR_GL_FRAMEBUFFER_COMPLETE != status ||
117 GR_GL_NO_ERROR != error) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000118 fFBO = 0;
119 fGL->unref();
120 fGL = NULL;
121 this->destroyGLContext();
122 return false;
123 } else {
124 return true;
125 }
126 }
127 return false;
128}