blob: c50ee6ee1f4a6076bb02223688aea2707fd47cb4 [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
19bool SkGLContext::init(int width, int height) {
20 if (fGL) {
21 fGL->unref();
22 this->destroyGLContext();
23 }
24
25 fGL = this->createGLContext();
26 if (fGL) {
27 GrGLuint cbID;
28 GrGLuint dsID;
29 SK_GL(*this, GenFramebuffers(1, &fFBO));
30 SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO));
31 SK_GL(*this, GenRenderbuffers(1, &cbID));
32 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
33 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
34 GR_GL_RGBA,
35 width, height));
36 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
37 GR_GL_COLOR_ATTACHMENT0,
38 GR_GL_RENDERBUFFER,
39 cbID));
40 SK_GL(*this, GenRenderbuffers(1, &dsID));
41 SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, dsID));
42 SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER,
43 GR_GL_DEPTH_STENCIL,
44 width, height));
45 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
46 GR_GL_DEPTH_ATTACHMENT,
47 GR_GL_RENDERBUFFER,
48 dsID));
49 SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
50 GR_GL_STENCIL_ATTACHMENT,
51 GR_GL_RENDERBUFFER,
52 dsID));
53 SK_GL(*this, Viewport(0, 0, width, height));
54 SK_GL(*this, ClearStencil(0));
55 SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT));
56
57 GrGLenum status =
58 SK_GL(*this, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
59 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
60 fFBO = 0;
61 fGL->unref();
62 fGL = NULL;
63 this->destroyGLContext();
64 return false;
65 } else {
66 return true;
67 }
68 }
69 return false;
70}