RenderState.cpp revision 9dfd7bd520ee598b3033a0c47b8b649bd3988c7c
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 mCaches->setProgram(nullptr); 133 mCaches->textureState().resetActiveTexture(); 134 meshState().unbindMeshBuffer(); 135 meshState().unbindIndicesBuffer(); 136 meshState().resetVertexPointers(); 137 meshState().disableTexCoordsVertexArray(); 138 debugOverdraw(false, false); 139} 140 141void RenderState::resumeFromFunctorInvoke() { 142 glViewport(0, 0, mViewportWidth, mViewportHeight); 143 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); 144 debugOverdraw(false, false); 145 146 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 147 148 scissor().invalidate(); 149 blend().invalidate(); 150 151 mCaches->textureState().activateTexture(0); 152 mCaches->textureState().resetBoundTextures(); 153} 154 155void RenderState::debugOverdraw(bool enable, bool clear) { 156 if (mCaches->debugOverdraw && mFramebuffer == 0) { 157 if (clear) { 158 scissor().setEnabled(false); 159 stencil().clear(); 160 } 161 if (enable) { 162 stencil().enableDebugWrite(); 163 } else { 164 stencil().disable(); 165 } 166 } 167} 168 169void 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 179class DecStrongTask : public renderthread::RenderTask { 180public: 181 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} 182 183 virtual void run() override { 184 mObject->decStrong(nullptr); 185 mObject = nullptr; 186 delete this; 187 } 188 189private: 190 VirtualLightRefBase* mObject; 191}; 192 193void RenderState::postDecStrong(VirtualLightRefBase* object) { 194 mRenderThread.queue(new DecStrongTask(object)); 195} 196 197/////////////////////////////////////////////////////////////////////////////// 198// Render 199/////////////////////////////////////////////////////////////////////////////// 200 201/* 202 * Not yet supported: 203 * 204 * Textures + coordinates 205 * SkiaShader 206 * ColorFilter 207 * 208 // TODO: texture coord 209 // TODO: texture support 210 // TODO: skiashader support 211 // TODO: color filter support 212 */ 213 214void RenderState::render(const Glop& glop) { 215 const Glop::Mesh& mesh = glop.mesh; 216 const Glop::Fill& shader = glop.fill; 217 218 // ---------- Shader + uniform setup ---------- 219 mCaches->setProgram(shader.program); 220 221 Glop::Fill::Color color = shader.color; 222 shader.program->setColor(color.a, color.r, color.g, color.b); 223 224 shader.program->set(glop.transform.ortho, 225 glop.transform.modelView, 226 glop.transform.canvas, 227 glop.transform.offset); 228 229 // ---------- Mesh setup ---------- 230 if (glop.mesh.vertexFlags & kTextureCoord_Attrib) { 231 // TODO: support textures 232 LOG_ALWAYS_FATAL("textures not yet supported"); 233 } else { 234 meshState().disableTexCoordsVertexArray(); 235 } 236 if (glop.mesh.vertexFlags & kColor_Attrib) { 237 LOG_ALWAYS_FATAL("color attribute not yet supported"); 238 // TODO: enable color, disable when done 239 } 240 if (glop.mesh.vertexFlags & kAlpha_Attrib) { 241 LOG_ALWAYS_FATAL("alpha attribute not yet supported"); 242 // TODO: enable alpha attribute, disable when done 243 } 244 245 /** 246 * Hard-coded vertex assumptions: 247 * - required 248 * - xy floats 249 * - 0 offset 250 * - in VBO 251 */ 252 bool force = meshState().bindMeshBuffer(mesh.vertexBufferObject); 253 meshState().bindPositionVertexPointer(force, nullptr, mesh.stride); 254 255 /** 256 * Hard-coded index assumptions: 257 * - optional 258 * - 0 offset 259 * - in IBO 260 */ 261 meshState().bindIndicesBufferInternal(mesh.indexBufferObject); 262 263 // ---------- GL state setup ---------- 264 265 if (glop.blend.mode != Glop::Blend::kDisable) { 266 blend().enable(glop.blend.mode, glop.blend.swapSrcDst); 267 } else { 268 blend().disable(); 269 } 270 271 glDrawElements(glop.mesh.primitiveMode, glop.mesh.vertexCount, GL_UNSIGNED_BYTE, nullptr); 272} 273 274} /* namespace uirenderer */ 275} /* namespace android */ 276