blob: 9948b44ac703d8882ba6eedade006473e9ba8169 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "RenderState.h"
17
18namespace android {
19namespace uirenderer {
20
21RenderState::RenderState()
22 : mCaches(NULL)
23 , mViewportWidth(0)
24 , mViewportHeight(0)
25 , mFramebuffer(0) {
26}
27
28RenderState::~RenderState() {
29}
30
31void RenderState::onGLContextCreated() {
32 // This is delayed because the first access of Caches makes GL calls
33 mCaches = &Caches::getInstance();
34 mCaches->init();
John Reck17035b02014-09-03 07:39:53 -070035 mCaches->setRenderState(this);
John Reck3b202512014-06-23 13:13:08 -070036}
37
Chris Craik1d477422014-08-26 17:30:15 -070038void RenderState::onGLContextDestroyed() {
John Reck17035b02014-09-03 07:39:53 -070039 if (CC_UNLIKELY(!mActiveLayers.empty())) {
40 mCaches->dumpMemoryUsage();
41 LOG_ALWAYS_FATAL("layers have survived gl context destruction");
42 }
Chris Craik1d477422014-08-26 17:30:15 -070043}
44
John Reck3b202512014-06-23 13:13:08 -070045void RenderState::setViewport(GLsizei width, GLsizei height) {
46 mViewportWidth = width;
47 mViewportHeight = height;
48 glViewport(0, 0, mViewportWidth, mViewportHeight);
49}
50
51
52void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
53 *outWidth = mViewportWidth;
54 *outHeight = mViewportHeight;
55}
56
57void RenderState::bindFramebuffer(GLuint fbo) {
58 if (mFramebuffer != fbo) {
59 mFramebuffer = fbo;
60 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
61 }
62}
63
64void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
65 interruptForFunctorInvoke();
66 (*functor)(mode, info);
67 resumeFromFunctorInvoke();
68}
69
70void RenderState::interruptForFunctorInvoke() {
71 if (mCaches->currentProgram) {
72 if (mCaches->currentProgram->isInUse()) {
73 mCaches->currentProgram->remove();
74 mCaches->currentProgram = NULL;
75 }
76 }
77 mCaches->resetActiveTexture();
78 mCaches->unbindMeshBuffer();
79 mCaches->unbindIndicesBuffer();
80 mCaches->resetVertexPointers();
81 mCaches->disableTexCoordsVertexArray();
82 debugOverdraw(false, false);
83}
84
85void RenderState::resumeFromFunctorInvoke() {
86 glViewport(0, 0, mViewportWidth, mViewportHeight);
87 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
88 debugOverdraw(false, false);
89
90 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
91
92 mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
93 mCaches->enableScissor();
94 mCaches->resetScissor();
95
96 mCaches->activeTexture(0);
97 mCaches->resetBoundTextures();
98
99 mCaches->blend = true;
100 glEnable(GL_BLEND);
101 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
102 glBlendEquation(GL_FUNC_ADD);
103}
104
105void RenderState::debugOverdraw(bool enable, bool clear) {
106 if (mCaches->debugOverdraw && mFramebuffer == 0) {
107 if (clear) {
108 mCaches->disableScissor();
109 mCaches->stencil.clear();
110 }
111 if (enable) {
112 mCaches->stencil.enableDebugWrite();
113 } else {
114 mCaches->stencil.disable();
115 }
116 }
117}
118
119} /* namespace uirenderer */
120} /* namespace android */