blob: f913cd915776ea1f1fa6e8ed85e97ca169ec4292 [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)
John Reck3b202512014-06-23 13:13:08 -070026 , mViewportWidth(0)
27 , mViewportHeight(0)
28 , mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070029 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070030}
31
32RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080033 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080034 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070035}
36
37void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080038 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080039 "State object lifecycle not managed correctly");
Chris Craik44eb2c02015-01-29 09:45:09 -080040 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080041 mMeshState = new MeshState();
42 mScissor = new Scissor();
43 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080044
Chris Craik96a5c4c2015-01-27 15:46:35 -080045 // This is delayed because the first access of Caches makes GL calls
46 mCaches = &Caches::createInstance(*this);
47 mCaches->init();
48 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070049}
50
John Reck49bc4ac2015-01-29 12:53:38 -080051static void layerLostGlContext(Layer* layer) {
52 layer->onGlContextLost();
53}
54
Chris Craik1d477422014-08-26 17:30:15 -070055void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070056/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070057 size_t size = mActiveLayers.size();
58 if (CC_UNLIKELY(size != 0)) {
59 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
60 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070061 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070062 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
63 cit != mRegisteredContexts.end(); cit++) {
64 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070065 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
66 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070067 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
68 pit != context->mPrefetechedLayers.end(); pit++) {
69 (*pit)->debugDumpLayers(" ");
70 }
71 context->mRootRenderNode->debugDumpLayers(" ");
72 }
Chris Craik599e2542014-09-05 15:17:11 -070073
Chris Craikbfd1cd62014-09-10 13:04:31 -070074
75 if (mActiveLayers.begin() == mActiveLayers.end()) {
76 ALOGE("set has become empty. wat.");
77 }
Chris Craik599e2542014-09-05 15:17:11 -070078 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
79 lit != mActiveLayers.end(); lit++) {
80 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070081 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
82 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070083 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070084 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070085 }
Chris Craik21029ef2014-09-12 09:29:10 -070086*/
John Reck49bc4ac2015-01-29 12:53:38 -080087
Chris Craik96a5c4c2015-01-27 15:46:35 -080088 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080089 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080090 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080091
Chris Craik44eb2c02015-01-29 09:45:09 -080092 mCaches->terminate();
93
94 delete mBlend;
95 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080096 delete mMeshState;
97 mMeshState = nullptr;
98 delete mScissor;
99 mScissor = nullptr;
100 delete mStencil;
101 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -0700102}
103
John Reck3b202512014-06-23 13:13:08 -0700104void RenderState::setViewport(GLsizei width, GLsizei height) {
105 mViewportWidth = width;
106 mViewportHeight = height;
107 glViewport(0, 0, mViewportWidth, mViewportHeight);
108}
109
110
111void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
112 *outWidth = mViewportWidth;
113 *outHeight = mViewportHeight;
114}
115
116void RenderState::bindFramebuffer(GLuint fbo) {
117 if (mFramebuffer != fbo) {
118 mFramebuffer = fbo;
119 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
120 }
121}
122
123void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
124 interruptForFunctorInvoke();
125 (*functor)(mode, info);
126 resumeFromFunctorInvoke();
127}
128
129void RenderState::interruptForFunctorInvoke() {
130 if (mCaches->currentProgram) {
131 if (mCaches->currentProgram->isInUse()) {
132 mCaches->currentProgram->remove();
Chris Craikd41c4d82015-01-05 15:51:13 -0800133 mCaches->currentProgram = nullptr;
John Reck3b202512014-06-23 13:13:08 -0700134 }
135 }
Chris Craik44eb2c02015-01-29 09:45:09 -0800136 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800137 meshState().unbindMeshBuffer();
138 meshState().unbindIndicesBuffer();
139 meshState().resetVertexPointers();
140 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700141 debugOverdraw(false, false);
142}
143
144void RenderState::resumeFromFunctorInvoke() {
145 glViewport(0, 0, mViewportWidth, mViewportHeight);
146 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
147 debugOverdraw(false, false);
148
149 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
150
Chris Craik65fe5ee2015-01-26 18:06:29 -0800151 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800152 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700153
Chris Craik44eb2c02015-01-29 09:45:09 -0800154 mCaches->textureState().activateTexture(0);
155 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700156}
157
158void RenderState::debugOverdraw(bool enable, bool clear) {
159 if (mCaches->debugOverdraw && mFramebuffer == 0) {
160 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800161 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800162 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700163 }
164 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800165 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700166 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800167 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700168 }
169 }
170}
171
John Reck0e89e2b2014-10-31 14:49:06 -0700172void RenderState::requireGLContext() {
173 assertOnGLThread();
174 mRenderThread.eglManager().requireGlContext();
175}
176
177void RenderState::assertOnGLThread() {
178 pthread_t curr = pthread_self();
179 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
180}
181
182
183class DecStrongTask : public renderthread::RenderTask {
184public:
185 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
186
Chris Craikd41c4d82015-01-05 15:51:13 -0800187 virtual void run() override {
188 mObject->decStrong(nullptr);
189 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700190 delete this;
191 }
192
193private:
194 VirtualLightRefBase* mObject;
195};
196
197void RenderState::postDecStrong(VirtualLightRefBase* object) {
198 mRenderThread.queue(new DecStrongTask(object));
199}
200
John Reck3b202512014-06-23 13:13:08 -0700201} /* namespace uirenderer */
202} /* namespace android */