blob: 4b190f08eaec541b1751bd134734659597983f46 [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
Chris Craik1d477422014-08-26 17:30:15 -070054void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070055/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070056 size_t size = mActiveLayers.size();
57 if (CC_UNLIKELY(size != 0)) {
58 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
59 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070060 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070061 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
62 cit != mRegisteredContexts.end(); cit++) {
63 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070064 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
65 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070066 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
67 pit != context->mPrefetechedLayers.end(); pit++) {
68 (*pit)->debugDumpLayers(" ");
69 }
70 context->mRootRenderNode->debugDumpLayers(" ");
71 }
Chris Craik599e2542014-09-05 15:17:11 -070072
Chris Craikbfd1cd62014-09-10 13:04:31 -070073
74 if (mActiveLayers.begin() == mActiveLayers.end()) {
75 ALOGE("set has become empty. wat.");
76 }
Chris Craik599e2542014-09-05 15:17:11 -070077 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
78 lit != mActiveLayers.end(); lit++) {
79 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070080 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
81 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070082 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070083 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070084 }
Chris Craik21029ef2014-09-12 09:29:10 -070085*/
Chris Craik96a5c4c2015-01-27 15:46:35 -080086 // TODO: reset all cached state in state objects
John Reckebd52612014-12-10 16:47:36 -080087 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080088
89 delete mMeshState;
90 mMeshState = nullptr;
91 delete mScissor;
92 mScissor = nullptr;
93 delete mStencil;
94 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -070095}
96
John Reck3b202512014-06-23 13:13:08 -070097void RenderState::setViewport(GLsizei width, GLsizei height) {
98 mViewportWidth = width;
99 mViewportHeight = height;
100 glViewport(0, 0, mViewportWidth, mViewportHeight);
101}
102
103
104void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
105 *outWidth = mViewportWidth;
106 *outHeight = mViewportHeight;
107}
108
109void RenderState::bindFramebuffer(GLuint fbo) {
110 if (mFramebuffer != fbo) {
111 mFramebuffer = fbo;
112 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
113 }
114}
115
116void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
117 interruptForFunctorInvoke();
118 (*functor)(mode, info);
119 resumeFromFunctorInvoke();
120}
121
122void RenderState::interruptForFunctorInvoke() {
123 if (mCaches->currentProgram) {
124 if (mCaches->currentProgram->isInUse()) {
125 mCaches->currentProgram->remove();
Chris Craikd41c4d82015-01-05 15:51:13 -0800126 mCaches->currentProgram = nullptr;
John Reck3b202512014-06-23 13:13:08 -0700127 }
128 }
129 mCaches->resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800130 meshState().unbindMeshBuffer();
131 meshState().unbindIndicesBuffer();
132 meshState().resetVertexPointers();
133 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700134 debugOverdraw(false, false);
135}
136
137void RenderState::resumeFromFunctorInvoke() {
138 glViewport(0, 0, mViewportWidth, mViewportHeight);
139 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
140 debugOverdraw(false, false);
141
142 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
143
Chris Craik65fe5ee2015-01-26 18:06:29 -0800144 scissor().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700145
146 mCaches->activeTexture(0);
147 mCaches->resetBoundTextures();
148
149 mCaches->blend = true;
150 glEnable(GL_BLEND);
151 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
152 glBlendEquation(GL_FUNC_ADD);
153}
154
155void RenderState::debugOverdraw(bool enable, bool clear) {
156 if (mCaches->debugOverdraw && mFramebuffer == 0) {
157 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800158 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800159 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700160 }
161 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800162 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700163 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800164 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700165 }
166 }
167}
168
John Reck0e89e2b2014-10-31 14:49:06 -0700169void RenderState::requireGLContext() {
170 assertOnGLThread();
171 mRenderThread.eglManager().requireGlContext();
172}
173
174void RenderState::assertOnGLThread() {
175 pthread_t curr = pthread_self();
176 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
177}
178
179
180class DecStrongTask : public renderthread::RenderTask {
181public:
182 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
183
Chris Craikd41c4d82015-01-05 15:51:13 -0800184 virtual void run() override {
185 mObject->decStrong(nullptr);
186 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700187 delete this;
188 }
189
190private:
191 VirtualLightRefBase* mObject;
192};
193
194void RenderState::postDecStrong(VirtualLightRefBase* object) {
195 mRenderThread.queue(new DecStrongTask(object));
196}
197
John Reck3b202512014-06-23 13:13:08 -0700198} /* namespace uirenderer */
199} /* namespace android */