blob: 86bd7dd00ccd8568ee6f905c3c33f6343e9adc07 [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 Craik21029ef2014-09-12 09:29:10 -070041/*
Chris Craik599e2542014-09-05 15:17:11 -070042 AutoMutex _lock(mLayerLock);
Chris Craikbfd1cd62014-09-10 13:04:31 -070043 size_t size = mActiveLayers.size();
44 if (CC_UNLIKELY(size != 0)) {
45 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
46 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070047 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070048 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
49 cit != mRegisteredContexts.end(); cit++) {
50 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070051 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
52 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070053 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
54 pit != context->mPrefetechedLayers.end(); pit++) {
55 (*pit)->debugDumpLayers(" ");
56 }
57 context->mRootRenderNode->debugDumpLayers(" ");
58 }
Chris Craik599e2542014-09-05 15:17:11 -070059
Chris Craikbfd1cd62014-09-10 13:04:31 -070060
61 if (mActiveLayers.begin() == mActiveLayers.end()) {
62 ALOGE("set has become empty. wat.");
63 }
Chris Craik599e2542014-09-05 15:17:11 -070064 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
65 lit != mActiveLayers.end(); lit++) {
66 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070067 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
68 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070069 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070070 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070071 }
Chris Craik21029ef2014-09-12 09:29:10 -070072*/
Chris Craik1d477422014-08-26 17:30:15 -070073}
74
John Reck3b202512014-06-23 13:13:08 -070075void RenderState::setViewport(GLsizei width, GLsizei height) {
76 mViewportWidth = width;
77 mViewportHeight = height;
78 glViewport(0, 0, mViewportWidth, mViewportHeight);
79}
80
81
82void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
83 *outWidth = mViewportWidth;
84 *outHeight = mViewportHeight;
85}
86
87void RenderState::bindFramebuffer(GLuint fbo) {
88 if (mFramebuffer != fbo) {
89 mFramebuffer = fbo;
90 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
91 }
92}
93
94void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
95 interruptForFunctorInvoke();
96 (*functor)(mode, info);
97 resumeFromFunctorInvoke();
98}
99
100void RenderState::interruptForFunctorInvoke() {
101 if (mCaches->currentProgram) {
102 if (mCaches->currentProgram->isInUse()) {
103 mCaches->currentProgram->remove();
104 mCaches->currentProgram = NULL;
105 }
106 }
107 mCaches->resetActiveTexture();
108 mCaches->unbindMeshBuffer();
109 mCaches->unbindIndicesBuffer();
110 mCaches->resetVertexPointers();
111 mCaches->disableTexCoordsVertexArray();
112 debugOverdraw(false, false);
113}
114
115void RenderState::resumeFromFunctorInvoke() {
116 glViewport(0, 0, mViewportWidth, mViewportHeight);
117 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
118 debugOverdraw(false, false);
119
120 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
121
122 mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
123 mCaches->enableScissor();
124 mCaches->resetScissor();
125
126 mCaches->activeTexture(0);
127 mCaches->resetBoundTextures();
128
129 mCaches->blend = true;
130 glEnable(GL_BLEND);
131 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
132 glBlendEquation(GL_FUNC_ADD);
133}
134
135void RenderState::debugOverdraw(bool enable, bool clear) {
136 if (mCaches->debugOverdraw && mFramebuffer == 0) {
137 if (clear) {
138 mCaches->disableScissor();
139 mCaches->stencil.clear();
140 }
141 if (enable) {
142 mCaches->stencil.enableDebugWrite();
143 } else {
144 mCaches->stencil.disable();
145 }
146 }
147}
148
149} /* namespace uirenderer */
150} /* namespace android */