blob: e4c87456e4958fc9fe5f58d5bfc3245432716dcd [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
49 mCaches = &Caches::createInstance(*this);
50 mCaches->init();
51 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070052}
53
John Reck49bc4ac2015-01-29 12:53:38 -080054static void layerLostGlContext(Layer* layer) {
55 layer->onGlContextLost();
56}
57
Chris Craik1d477422014-08-26 17:30:15 -070058void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070059/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070060 size_t size = mActiveLayers.size();
61 if (CC_UNLIKELY(size != 0)) {
62 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
63 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070064 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070065 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
66 cit != mRegisteredContexts.end(); cit++) {
67 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070068 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
69 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070070 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
71 pit != context->mPrefetechedLayers.end(); pit++) {
72 (*pit)->debugDumpLayers(" ");
73 }
74 context->mRootRenderNode->debugDumpLayers(" ");
75 }
Chris Craik599e2542014-09-05 15:17:11 -070076
Chris Craikbfd1cd62014-09-10 13:04:31 -070077
78 if (mActiveLayers.begin() == mActiveLayers.end()) {
79 ALOGE("set has become empty. wat.");
80 }
Chris Craik599e2542014-09-05 15:17:11 -070081 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
82 lit != mActiveLayers.end(); lit++) {
83 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070084 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
85 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070086 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070087 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070088 }
Chris Craik21029ef2014-09-12 09:29:10 -070089*/
John Reck49bc4ac2015-01-29 12:53:38 -080090
Chris Craik96a5c4c2015-01-27 15:46:35 -080091 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080092 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080093 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080094
95 delete mMeshState;
96 mMeshState = nullptr;
97 delete mScissor;
98 mScissor = nullptr;
99 delete mStencil;
100 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -0700101}
102
John Reck3b202512014-06-23 13:13:08 -0700103void RenderState::setViewport(GLsizei width, GLsizei height) {
104 mViewportWidth = width;
105 mViewportHeight = height;
106 glViewport(0, 0, mViewportWidth, mViewportHeight);
107}
108
109
110void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
111 *outWidth = mViewportWidth;
112 *outHeight = mViewportHeight;
113}
114
115void RenderState::bindFramebuffer(GLuint fbo) {
116 if (mFramebuffer != fbo) {
117 mFramebuffer = fbo;
118 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
119 }
120}
121
122void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
123 interruptForFunctorInvoke();
124 (*functor)(mode, info);
125 resumeFromFunctorInvoke();
126}
127
128void RenderState::interruptForFunctorInvoke() {
129 if (mCaches->currentProgram) {
130 if (mCaches->currentProgram->isInUse()) {
131 mCaches->currentProgram->remove();
Chris Craikd41c4d82015-01-05 15:51:13 -0800132 mCaches->currentProgram = nullptr;
John Reck3b202512014-06-23 13:13:08 -0700133 }
134 }
135 mCaches->resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800136 meshState().unbindMeshBuffer();
137 meshState().unbindIndicesBuffer();
138 meshState().resetVertexPointers();
139 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700140 debugOverdraw(false, false);
141}
142
143void RenderState::resumeFromFunctorInvoke() {
144 glViewport(0, 0, mViewportWidth, mViewportHeight);
145 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
146 debugOverdraw(false, false);
147
148 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
149
Chris Craik65fe5ee2015-01-26 18:06:29 -0800150 scissor().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700151
152 mCaches->activeTexture(0);
153 mCaches->resetBoundTextures();
154
155 mCaches->blend = true;
156 glEnable(GL_BLEND);
157 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
158 glBlendEquation(GL_FUNC_ADD);
159}
160
161void RenderState::debugOverdraw(bool enable, bool clear) {
162 if (mCaches->debugOverdraw && mFramebuffer == 0) {
163 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800164 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800165 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700166 }
167 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800168 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700169 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800170 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700171 }
172 }
173}
174
John Reck0e89e2b2014-10-31 14:49:06 -0700175void RenderState::requireGLContext() {
176 assertOnGLThread();
177 mRenderThread.eglManager().requireGlContext();
178}
179
180void RenderState::assertOnGLThread() {
181 pthread_t curr = pthread_self();
182 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
183}
184
185
186class DecStrongTask : public renderthread::RenderTask {
187public:
188 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
189
Chris Craikd41c4d82015-01-05 15:51:13 -0800190 virtual void run() override {
191 mObject->decStrong(nullptr);
192 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700193 delete this;
194 }
195
196private:
197 VirtualLightRefBase* mObject;
198};
199
200void RenderState::postDecStrong(VirtualLightRefBase* object) {
201 mRenderThread.queue(new DecStrongTask(object));
202}
203
John Reck3b202512014-06-23 13:13:08 -0700204} /* namespace uirenderer */
205} /* namespace android */