blob: 3a9a92e3520e7f2d6037a5ad2290b917099f173b [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)
Chris Craikd41c4d82015-01-05 15:51:13 -080026 , mCaches(nullptr)
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);
Chris Craik65fe5ee2015-01-26 18:06:29 -080042
43 LOG_ALWAYS_FATAL_IF(scissor().isEnabled(), "scissor used before GL context created");
44 glDisable(GL_SCISSOR_TEST);
John Reck3b202512014-06-23 13:13:08 -070045}
46
Chris Craik1d477422014-08-26 17:30:15 -070047void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070048/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070049 size_t size = mActiveLayers.size();
50 if (CC_UNLIKELY(size != 0)) {
51 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
52 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070053 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070054 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
55 cit != mRegisteredContexts.end(); cit++) {
56 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070057 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
58 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070059 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
60 pit != context->mPrefetechedLayers.end(); pit++) {
61 (*pit)->debugDumpLayers(" ");
62 }
63 context->mRootRenderNode->debugDumpLayers(" ");
64 }
Chris Craik599e2542014-09-05 15:17:11 -070065
Chris Craikbfd1cd62014-09-10 13:04:31 -070066
67 if (mActiveLayers.begin() == mActiveLayers.end()) {
68 ALOGE("set has become empty. wat.");
69 }
Chris Craik599e2542014-09-05 15:17:11 -070070 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
71 lit != mActiveLayers.end(); lit++) {
72 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070073 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
74 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070075 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070076 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070077 }
Chris Craik21029ef2014-09-12 09:29:10 -070078*/
John Reckebd52612014-12-10 16:47:36 -080079 mAssetAtlas.terminate();
Chris Craik1d477422014-08-26 17:30:15 -070080}
81
John Reck3b202512014-06-23 13:13:08 -070082void RenderState::setViewport(GLsizei width, GLsizei height) {
83 mViewportWidth = width;
84 mViewportHeight = height;
85 glViewport(0, 0, mViewportWidth, mViewportHeight);
86}
87
88
89void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
90 *outWidth = mViewportWidth;
91 *outHeight = mViewportHeight;
92}
93
94void RenderState::bindFramebuffer(GLuint fbo) {
95 if (mFramebuffer != fbo) {
96 mFramebuffer = fbo;
97 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
98 }
99}
100
101void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
102 interruptForFunctorInvoke();
103 (*functor)(mode, info);
104 resumeFromFunctorInvoke();
105}
106
107void RenderState::interruptForFunctorInvoke() {
108 if (mCaches->currentProgram) {
109 if (mCaches->currentProgram->isInUse()) {
110 mCaches->currentProgram->remove();
Chris Craikd41c4d82015-01-05 15:51:13 -0800111 mCaches->currentProgram = nullptr;
John Reck3b202512014-06-23 13:13:08 -0700112 }
113 }
114 mCaches->resetActiveTexture();
115 mCaches->unbindMeshBuffer();
116 mCaches->unbindIndicesBuffer();
117 mCaches->resetVertexPointers();
118 mCaches->disableTexCoordsVertexArray();
119 debugOverdraw(false, false);
120}
121
122void RenderState::resumeFromFunctorInvoke() {
123 glViewport(0, 0, mViewportWidth, mViewportHeight);
124 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
125 debugOverdraw(false, false);
126
127 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
128
Chris Craik65fe5ee2015-01-26 18:06:29 -0800129 scissor().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700130
131 mCaches->activeTexture(0);
132 mCaches->resetBoundTextures();
133
134 mCaches->blend = true;
135 glEnable(GL_BLEND);
136 glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode);
137 glBlendEquation(GL_FUNC_ADD);
138}
139
140void RenderState::debugOverdraw(bool enable, bool clear) {
141 if (mCaches->debugOverdraw && mFramebuffer == 0) {
142 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800143 scissor().setEnabled(false);
John Reck3b202512014-06-23 13:13:08 -0700144 mCaches->stencil.clear();
145 }
146 if (enable) {
147 mCaches->stencil.enableDebugWrite();
148 } else {
149 mCaches->stencil.disable();
150 }
151 }
152}
153
John Reck0e89e2b2014-10-31 14:49:06 -0700154void RenderState::requireGLContext() {
155 assertOnGLThread();
156 mRenderThread.eglManager().requireGlContext();
157}
158
159void RenderState::assertOnGLThread() {
160 pthread_t curr = pthread_self();
161 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
162}
163
164
165class DecStrongTask : public renderthread::RenderTask {
166public:
167 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
168
Chris Craikd41c4d82015-01-05 15:51:13 -0800169 virtual void run() override {
170 mObject->decStrong(nullptr);
171 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700172 delete this;
173 }
174
175private:
176 VirtualLightRefBase* mObject;
177};
178
179void RenderState::postDecStrong(VirtualLightRefBase* object) {
180 mRenderThread.queue(new DecStrongTask(object));
181}
182
John Reck3b202512014-06-23 13:13:08 -0700183} /* namespace uirenderer */
184} /* namespace android */