blob: c83971815542f9ba60c9ba6db7918cdd3bb7afc5 [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 */
Chris Craik96a5c4c2015-01-27 15:46:35 -080016#include "renderstate/RenderState.h"
John Reck3b202512014-06-23 13:13:08 -070017
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)
Chris Craikd41c4d82015-01-05 15:51:13 -080026 , mCaches(nullptr)
Chris Craik96a5c4c2015-01-27 15:46:35 -080027 , mMeshState(nullptr)
28 , mScissor(nullptr)
29 , mStencil(nullptr)
John Reck3b202512014-06-23 13:13:08 -070030 , mViewportWidth(0)
31 , mViewportHeight(0)
32 , mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070033 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070034}
35
36RenderState::~RenderState() {
Chris Craik96a5c4c2015-01-27 15:46:35 -080037 LOG_ALWAYS_FATAL_IF(mMeshState || mScissor || mStencil,
38 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070039}
40
41void RenderState::onGLContextCreated() {
Chris Craik96a5c4c2015-01-27 15:46:35 -080042 LOG_ALWAYS_FATAL_IF(mMeshState || mScissor || mStencil,
43 "State object lifecycle not managed correctly");
44 mMeshState = new MeshState();
45 mScissor = new Scissor();
46 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080047
Chris Craik96a5c4c2015-01-27 15:46:35 -080048 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080049 if (!mCaches) {
50 mCaches = &Caches::createInstance(*this);
51 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080052 mCaches->init();
53 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070054}
55
John Reck49bc4ac2015-01-29 12:53:38 -080056static void layerLostGlContext(Layer* layer) {
57 layer->onGlContextLost();
58}
59
Chris Craik1d477422014-08-26 17:30:15 -070060void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070061/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070062 size_t size = mActiveLayers.size();
63 if (CC_UNLIKELY(size != 0)) {
64 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
65 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070066 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070067 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
68 cit != mRegisteredContexts.end(); cit++) {
69 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070070 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
71 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070072 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
73 pit != context->mPrefetechedLayers.end(); pit++) {
74 (*pit)->debugDumpLayers(" ");
75 }
76 context->mRootRenderNode->debugDumpLayers(" ");
77 }
Chris Craik599e2542014-09-05 15:17:11 -070078
Chris Craikbfd1cd62014-09-10 13:04:31 -070079
80 if (mActiveLayers.begin() == mActiveLayers.end()) {
81 ALOGE("set has become empty. wat.");
82 }
Chris Craik599e2542014-09-05 15:17:11 -070083 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
84 lit != mActiveLayers.end(); lit++) {
85 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070086 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
87 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070088 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070089 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070090 }
Chris Craik21029ef2014-09-12 09:29:10 -070091*/
John Reck49bc4ac2015-01-29 12:53:38 -080092
Chris Craik96a5c4c2015-01-27 15:46:35 -080093 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080094 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080095 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080096
97 delete mMeshState;
98 mMeshState = nullptr;
99 delete mScissor;
100 mScissor = nullptr;
101 delete mStencil;
102 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -0700103}
104
John Reck3b202512014-06-23 13:13:08 -0700105void RenderState::setViewport(GLsizei width, GLsizei height) {
106 mViewportWidth = width;
107 mViewportHeight = height;
108 glViewport(0, 0, mViewportWidth, mViewportHeight);
109}
110
111
112void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
113 *outWidth = mViewportWidth;
114 *outHeight = mViewportHeight;
115}
116
117void RenderState::bindFramebuffer(GLuint fbo) {
118 if (mFramebuffer != fbo) {
119 mFramebuffer = fbo;
120 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
121 }
122}
123
124void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
125 interruptForFunctorInvoke();
126 (*functor)(mode, info);
127 resumeFromFunctorInvoke();
128}
129
130void RenderState::interruptForFunctorInvoke() {
131 if (mCaches->currentProgram) {
132 if (mCaches->currentProgram->isInUse()) {
133 mCaches->currentProgram->remove();
Chris Craikd41c4d82015-01-05 15:51:13 -0800134 mCaches->currentProgram = nullptr;
John Reck3b202512014-06-23 13:13:08 -0700135 }
136 }
137 mCaches->resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800138 meshState().unbindMeshBuffer();
139 meshState().unbindIndicesBuffer();
140 meshState().resetVertexPointers();
141 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700142 debugOverdraw(false, false);
143}
144
145void RenderState::resumeFromFunctorInvoke() {
146 glViewport(0, 0, mViewportWidth, mViewportHeight);
147 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
148 debugOverdraw(false, false);
149
150 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
151
Chris Craik65fe5ee2015-01-26 18:06:29 -0800152 scissor().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700153
154 mCaches->activeTexture(0);
155 mCaches->resetBoundTextures();
156
157 mCaches->blend = true;
158 glEnable(GL_BLEND);
159 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
160 glBlendEquation(GL_FUNC_ADD);
161}
162
163void RenderState::debugOverdraw(bool enable, bool clear) {
164 if (mCaches->debugOverdraw && mFramebuffer == 0) {
165 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800166 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800167 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700168 }
169 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800170 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700171 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800172 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700173 }
174 }
175}
176
John Reck0e89e2b2014-10-31 14:49:06 -0700177void RenderState::requireGLContext() {
178 assertOnGLThread();
179 mRenderThread.eglManager().requireGlContext();
180}
181
182void RenderState::assertOnGLThread() {
183 pthread_t curr = pthread_self();
184 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
185}
186
187
188class DecStrongTask : public renderthread::RenderTask {
189public:
190 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
191
Chris Craikd41c4d82015-01-05 15:51:13 -0800192 virtual void run() override {
193 mObject->decStrong(nullptr);
194 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700195 delete this;
196 }
197
198private:
199 VirtualLightRefBase* mObject;
200};
201
202void RenderState::postDecStrong(VirtualLightRefBase* object) {
203 mRenderThread.queue(new DecStrongTask(object));
204}
205
John Reck3b202512014-06-23 13:13:08 -0700206} /* namespace uirenderer */
207} /* namespace android */