blob: 54af52802fecdd7634f519b8e51a84832786b274 [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
John Reck443a7142014-09-04 17:40:05 -070018#include "renderthread/CanvasContext.h"
19
John Reck3b202512014-06-23 13:13:08 -070020namespace android {
21namespace uirenderer {
22
23RenderState::RenderState()
24 : mCaches(NULL)
25 , mViewportWidth(0)
26 , mViewportHeight(0)
27 , mFramebuffer(0) {
28}
29
30RenderState::~RenderState() {
31}
32
33void RenderState::onGLContextCreated() {
34 // This is delayed because the first access of Caches makes GL calls
35 mCaches = &Caches::getInstance();
36 mCaches->init();
John Reck17035b02014-09-03 07:39:53 -070037 mCaches->setRenderState(this);
John Reck3b202512014-06-23 13:13:08 -070038}
39
Chris Craik1d477422014-08-26 17:30:15 -070040void RenderState::onGLContextDestroyed() {
Chris Craik599e2542014-09-05 15:17:11 -070041 AutoMutex _lock(mLayerLock);
Chris Craikbfd1cd62014-09-10 13:04:31 -070042 size_t size = mActiveLayers.size();
43 if (CC_UNLIKELY(size != 0)) {
44 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
45 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070046 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070047 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
48 cit != mRegisteredContexts.end(); cit++) {
49 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070050 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
51 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070052 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
53 pit != context->mPrefetechedLayers.end(); pit++) {
54 (*pit)->debugDumpLayers(" ");
55 }
56 context->mRootRenderNode->debugDumpLayers(" ");
57 }
Chris Craik599e2542014-09-05 15:17:11 -070058
Chris Craikbfd1cd62014-09-10 13:04:31 -070059
60 if (mActiveLayers.begin() == mActiveLayers.end()) {
61 ALOGE("set has become empty. wat.");
62 }
Chris Craik599e2542014-09-05 15:17:11 -070063 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
64 lit != mActiveLayers.end(); lit++) {
65 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070066 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
67 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070068 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070069 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070070 }
Chris Craik1d477422014-08-26 17:30:15 -070071}
72
John Reck3b202512014-06-23 13:13:08 -070073void RenderState::setViewport(GLsizei width, GLsizei height) {
74 mViewportWidth = width;
75 mViewportHeight = height;
76 glViewport(0, 0, mViewportWidth, mViewportHeight);
77}
78
79
80void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
81 *outWidth = mViewportWidth;
82 *outHeight = mViewportHeight;
83}
84
85void RenderState::bindFramebuffer(GLuint fbo) {
86 if (mFramebuffer != fbo) {
87 mFramebuffer = fbo;
88 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
89 }
90}
91
92void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
93 interruptForFunctorInvoke();
94 (*functor)(mode, info);
95 resumeFromFunctorInvoke();
96}
97
98void RenderState::interruptForFunctorInvoke() {
99 if (mCaches->currentProgram) {
100 if (mCaches->currentProgram->isInUse()) {
101 mCaches->currentProgram->remove();
102 mCaches->currentProgram = NULL;
103 }
104 }
105 mCaches->resetActiveTexture();
106 mCaches->unbindMeshBuffer();
107 mCaches->unbindIndicesBuffer();
108 mCaches->resetVertexPointers();
109 mCaches->disableTexCoordsVertexArray();
110 debugOverdraw(false, false);
111}
112
113void RenderState::resumeFromFunctorInvoke() {
114 glViewport(0, 0, mViewportWidth, mViewportHeight);
115 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
116 debugOverdraw(false, false);
117
118 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
119
120 mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
121 mCaches->enableScissor();
122 mCaches->resetScissor();
123
124 mCaches->activeTexture(0);
125 mCaches->resetBoundTextures();
126
127 mCaches->blend = true;
128 glEnable(GL_BLEND);
129 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
130 glBlendEquation(GL_FUNC_ADD);
131}
132
133void RenderState::debugOverdraw(bool enable, bool clear) {
134 if (mCaches->debugOverdraw && mFramebuffer == 0) {
135 if (clear) {
136 mCaches->disableScissor();
137 mCaches->stencil.clear();
138 }
139 if (enable) {
140 mCaches->stencil.enableDebugWrite();
141 } else {
142 mCaches->stencil.disable();
143 }
144 }
145}
146
147} /* namespace uirenderer */
148} /* namespace android */