blob: 50b8f395011c9ff5fb4545ad6f572d27e3860681 [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();
35}
36
Chris Craik1d477422014-08-26 17:30:15 -070037void RenderState::onGLContextDestroyed() {
38 LOG_ALWAYS_FATAL_IF(!mActiveLayers.empty(), "layers have survived gl context destruction");
39}
40
John Reck3b202512014-06-23 13:13:08 -070041void RenderState::setViewport(GLsizei width, GLsizei height) {
42 mViewportWidth = width;
43 mViewportHeight = height;
44 glViewport(0, 0, mViewportWidth, mViewportHeight);
45}
46
47
48void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
49 *outWidth = mViewportWidth;
50 *outHeight = mViewportHeight;
51}
52
53void RenderState::bindFramebuffer(GLuint fbo) {
54 if (mFramebuffer != fbo) {
55 mFramebuffer = fbo;
56 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
57 }
58}
59
60void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
61 interruptForFunctorInvoke();
62 (*functor)(mode, info);
63 resumeFromFunctorInvoke();
64}
65
66void RenderState::interruptForFunctorInvoke() {
67 if (mCaches->currentProgram) {
68 if (mCaches->currentProgram->isInUse()) {
69 mCaches->currentProgram->remove();
70 mCaches->currentProgram = NULL;
71 }
72 }
73 mCaches->resetActiveTexture();
74 mCaches->unbindMeshBuffer();
75 mCaches->unbindIndicesBuffer();
76 mCaches->resetVertexPointers();
77 mCaches->disableTexCoordsVertexArray();
78 debugOverdraw(false, false);
79}
80
81void RenderState::resumeFromFunctorInvoke() {
82 glViewport(0, 0, mViewportWidth, mViewportHeight);
83 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
84 debugOverdraw(false, false);
85
86 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
87
88 mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
89 mCaches->enableScissor();
90 mCaches->resetScissor();
91
92 mCaches->activeTexture(0);
93 mCaches->resetBoundTextures();
94
95 mCaches->blend = true;
96 glEnable(GL_BLEND);
97 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
98 glBlendEquation(GL_FUNC_ADD);
99}
100
101void RenderState::debugOverdraw(bool enable, bool clear) {
102 if (mCaches->debugOverdraw && mFramebuffer == 0) {
103 if (clear) {
104 mCaches->disableScissor();
105 mCaches->stencil.clear();
106 }
107 if (enable) {
108 mCaches->stencil.enableDebugWrite();
109 } else {
110 mCaches->stencil.disable();
111 }
112 }
113}
114
115} /* namespace uirenderer */
116} /* namespace android */