blob: ca640e5c14ede96001e488bfcfd14b05950f5ce6 [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"
John Reck0e89e2b2014-10-31 14:49:06 -070019#include "renderthread/EglManager.h"
John Reck443a7142014-09-04 17:40:05 -070020
John Reck3b202512014-06-23 13:13:08 -070021namespace android {
22namespace uirenderer {
23
John Reck0e89e2b2014-10-31 14:49:06 -070024RenderState::RenderState(renderthread::RenderThread& thread)
25 : mRenderThread(thread)
26 , mCaches(NULL)
John Reck3b202512014-06-23 13:13:08 -070027 , mViewportWidth(0)
28 , mViewportHeight(0)
29 , mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070030 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070031}
32
33RenderState::~RenderState() {
34}
35
36void RenderState::onGLContextCreated() {
37 // This is delayed because the first access of Caches makes GL calls
38 mCaches = &Caches::getInstance();
39 mCaches->init();
John Reck17035b02014-09-03 07:39:53 -070040 mCaches->setRenderState(this);
John Reckebd52612014-12-10 16:47:36 -080041 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070042}
43
John Reck57998012015-01-29 10:17:57 -080044static void layerLostGlContext(Layer* layer) {
45 layer->onGlContextLost();
46}
47
Chris Craik1d477422014-08-26 17:30:15 -070048void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070049/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070050 size_t size = mActiveLayers.size();
51 if (CC_UNLIKELY(size != 0)) {
52 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
53 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070054 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070055 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
56 cit != mRegisteredContexts.end(); cit++) {
57 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070058 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
59 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070060 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
61 pit != context->mPrefetechedLayers.end(); pit++) {
62 (*pit)->debugDumpLayers(" ");
63 }
64 context->mRootRenderNode->debugDumpLayers(" ");
65 }
Chris Craik599e2542014-09-05 15:17:11 -070066
Chris Craikbfd1cd62014-09-10 13:04:31 -070067
68 if (mActiveLayers.begin() == mActiveLayers.end()) {
69 ALOGE("set has become empty. wat.");
70 }
Chris Craik599e2542014-09-05 15:17:11 -070071 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
72 lit != mActiveLayers.end(); lit++) {
73 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070074 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
75 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070076 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070077 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070078 }
Chris Craik21029ef2014-09-12 09:29:10 -070079*/
John Reck57998012015-01-29 10:17:57 -080080 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080081 mAssetAtlas.terminate();
Chris Craik1d477422014-08-26 17:30:15 -070082}
83
John Reck3b202512014-06-23 13:13:08 -070084void RenderState::setViewport(GLsizei width, GLsizei height) {
85 mViewportWidth = width;
86 mViewportHeight = height;
87 glViewport(0, 0, mViewportWidth, mViewportHeight);
88}
89
90
91void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
92 *outWidth = mViewportWidth;
93 *outHeight = mViewportHeight;
94}
95
96void RenderState::bindFramebuffer(GLuint fbo) {
97 if (mFramebuffer != fbo) {
98 mFramebuffer = fbo;
99 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
100 }
101}
102
103void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
104 interruptForFunctorInvoke();
105 (*functor)(mode, info);
106 resumeFromFunctorInvoke();
107}
108
109void RenderState::interruptForFunctorInvoke() {
110 if (mCaches->currentProgram) {
111 if (mCaches->currentProgram->isInUse()) {
112 mCaches->currentProgram->remove();
113 mCaches->currentProgram = NULL;
114 }
115 }
116 mCaches->resetActiveTexture();
117 mCaches->unbindMeshBuffer();
118 mCaches->unbindIndicesBuffer();
119 mCaches->resetVertexPointers();
120 mCaches->disableTexCoordsVertexArray();
121 debugOverdraw(false, false);
122}
123
124void RenderState::resumeFromFunctorInvoke() {
125 glViewport(0, 0, mViewportWidth, mViewportHeight);
126 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
127 debugOverdraw(false, false);
128
129 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
130
131 mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
132 mCaches->enableScissor();
133 mCaches->resetScissor();
134
135 mCaches->activeTexture(0);
136 mCaches->resetBoundTextures();
137
138 mCaches->blend = true;
139 glEnable(GL_BLEND);
140 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
141 glBlendEquation(GL_FUNC_ADD);
142}
143
144void RenderState::debugOverdraw(bool enable, bool clear) {
145 if (mCaches->debugOverdraw && mFramebuffer == 0) {
146 if (clear) {
147 mCaches->disableScissor();
148 mCaches->stencil.clear();
149 }
150 if (enable) {
151 mCaches->stencil.enableDebugWrite();
152 } else {
153 mCaches->stencil.disable();
154 }
155 }
156}
157
John Reck0e89e2b2014-10-31 14:49:06 -0700158void RenderState::requireGLContext() {
159 assertOnGLThread();
160 mRenderThread.eglManager().requireGlContext();
161}
162
163void RenderState::assertOnGLThread() {
164 pthread_t curr = pthread_self();
165 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
166}
167
168
169class DecStrongTask : public renderthread::RenderTask {
170public:
171 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
172
173 virtual void run() {
174 mObject->decStrong(0);
175 mObject = 0;
176 delete this;
177 }
178
179private:
180 VirtualLightRefBase* mObject;
181};
182
183void RenderState::postDecStrong(VirtualLightRefBase* object) {
184 mRenderThread.queue(new DecStrongTask(object));
185}
186
John Reck3b202512014-06-23 13:13:08 -0700187} /* namespace uirenderer */
188} /* namespace android */