RenderState.cpp revision d7328ae7909328ff4aa2205b0de0d4f6f72a2e66
1/* 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/RenderState.h" 17 18#include "renderthread/CanvasContext.h" 19#include "renderthread/EglManager.h" 20 21namespace android { 22namespace uirenderer { 23 24RenderState::RenderState(renderthread::RenderThread& thread) 25 : mRenderThread(thread) 26 , mViewportWidth(0) 27 , mViewportHeight(0) 28 , mFramebuffer(0) { 29 mThreadId = pthread_self(); 30} 31 32RenderState::~RenderState() { 33 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, 34 "State object lifecycle not managed correctly"); 35} 36 37void RenderState::onGLContextCreated() { 38 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, 39 "State object lifecycle not managed correctly"); 40 mBlend = new Blend(); 41 mMeshState = new MeshState(); 42 mScissor = new Scissor(); 43 mStencil = new Stencil(); 44 45 // This is delayed because the first access of Caches makes GL calls 46 if (!mCaches) { 47 mCaches = &Caches::createInstance(*this); 48 } 49 mCaches->init(); 50 mCaches->textureCache.setAssetAtlas(&mAssetAtlas); 51} 52 53static void layerLostGlContext(Layer* layer) { 54 layer->onGlContextLost(); 55} 56 57void RenderState::onGLContextDestroyed() { 58/* 59 size_t size = mActiveLayers.size(); 60 if (CC_UNLIKELY(size != 0)) { 61 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d", 62 mRegisteredContexts.size(), size, mActiveLayers.empty()); 63 mCaches->dumpMemoryUsage(); 64 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin(); 65 cit != mRegisteredContexts.end(); cit++) { 66 renderthread::CanvasContext* context = *cit; 67 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get()); 68 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size()); 69 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin(); 70 pit != context->mPrefetechedLayers.end(); pit++) { 71 (*pit)->debugDumpLayers(" "); 72 } 73 context->mRootRenderNode->debugDumpLayers(" "); 74 } 75 76 77 if (mActiveLayers.begin() == mActiveLayers.end()) { 78 ALOGE("set has become empty. wat."); 79 } 80 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin(); 81 lit != mActiveLayers.end(); lit++) { 82 const Layer* layer = *(lit); 83 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d", 84 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered); 85 } 86 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size); 87 } 88*/ 89 90 // TODO: reset all cached state in state objects 91 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext); 92 mAssetAtlas.terminate(); 93 94 mCaches->terminate(); 95 96 delete mBlend; 97 mBlend = nullptr; 98 delete mMeshState; 99 mMeshState = nullptr; 100 delete mScissor; 101 mScissor = nullptr; 102 delete mStencil; 103 mStencil = nullptr; 104} 105 106void RenderState::setViewport(GLsizei width, GLsizei height) { 107 mViewportWidth = width; 108 mViewportHeight = height; 109 glViewport(0, 0, mViewportWidth, mViewportHeight); 110} 111 112 113void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { 114 *outWidth = mViewportWidth; 115 *outHeight = mViewportHeight; 116} 117 118void RenderState::bindFramebuffer(GLuint fbo) { 119 if (mFramebuffer != fbo) { 120 mFramebuffer = fbo; 121 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); 122 } 123} 124 125void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { 126 interruptForFunctorInvoke(); 127 (*functor)(mode, info); 128 resumeFromFunctorInvoke(); 129} 130 131void RenderState::interruptForFunctorInvoke() { 132 if (mCaches->currentProgram) { 133 if (mCaches->currentProgram->isInUse()) { 134 mCaches->currentProgram->remove(); 135 mCaches->currentProgram = nullptr; 136 } 137 } 138 mCaches->textureState().resetActiveTexture(); 139 meshState().unbindMeshBuffer(); 140 meshState().unbindIndicesBuffer(); 141 meshState().resetVertexPointers(); 142 meshState().disableTexCoordsVertexArray(); 143 debugOverdraw(false, false); 144} 145 146void RenderState::resumeFromFunctorInvoke() { 147 glViewport(0, 0, mViewportWidth, mViewportHeight); 148 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); 149 debugOverdraw(false, false); 150 151 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 152 153 scissor().invalidate(); 154 blend().invalidate(); 155 156 mCaches->textureState().activateTexture(0); 157 mCaches->textureState().resetBoundTextures(); 158} 159 160void RenderState::debugOverdraw(bool enable, bool clear) { 161 if (mCaches->debugOverdraw && mFramebuffer == 0) { 162 if (clear) { 163 scissor().setEnabled(false); 164 stencil().clear(); 165 } 166 if (enable) { 167 stencil().enableDebugWrite(); 168 } else { 169 stencil().disable(); 170 } 171 } 172} 173 174void RenderState::requireGLContext() { 175 assertOnGLThread(); 176 mRenderThread.eglManager().requireGlContext(); 177} 178 179void RenderState::assertOnGLThread() { 180 pthread_t curr = pthread_self(); 181 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!"); 182} 183 184 185class DecStrongTask : public renderthread::RenderTask { 186public: 187 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} 188 189 virtual void run() override { 190 mObject->decStrong(nullptr); 191 mObject = nullptr; 192 delete this; 193 } 194 195private: 196 VirtualLightRefBase* mObject; 197}; 198 199void RenderState::postDecStrong(VirtualLightRefBase* object) { 200 mRenderThread.queue(new DecStrongTask(object)); 201} 202 203} /* namespace uirenderer */ 204} /* namespace android */ 205