RenderState.cpp revision 117bdbcfa3e8306dad21e7e01fa71b00cdfa7265
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#include "utils/GLUtils.h" 21 22namespace android { 23namespace uirenderer { 24 25RenderState::RenderState(renderthread::RenderThread& thread) 26 : mRenderThread(thread) 27 , mViewportWidth(0) 28 , mViewportHeight(0) 29 , mFramebuffer(0) { 30 mThreadId = pthread_self(); 31} 32 33RenderState::~RenderState() { 34 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, 35 "State object lifecycle not managed correctly"); 36} 37 38void RenderState::onGLContextCreated() { 39 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, 40 "State object lifecycle not managed correctly"); 41 mBlend = new Blend(); 42 mMeshState = new MeshState(); 43 mScissor = new Scissor(); 44 mStencil = new Stencil(); 45 46 // This is delayed because the first access of Caches makes GL calls 47 mCaches = &Caches::createInstance(*this); 48 mCaches->init(); 49 mCaches->textureCache.setAssetAtlas(&mAssetAtlas); 50} 51 52static void layerLostGlContext(Layer* layer) { 53 layer->onGlContextLost(); 54} 55 56void RenderState::onGLContextDestroyed() { 57/* 58 size_t size = mActiveLayers.size(); 59 if (CC_UNLIKELY(size != 0)) { 60 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d", 61 mRegisteredContexts.size(), size, mActiveLayers.empty()); 62 mCaches->dumpMemoryUsage(); 63 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin(); 64 cit != mRegisteredContexts.end(); cit++) { 65 renderthread::CanvasContext* context = *cit; 66 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get()); 67 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size()); 68 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin(); 69 pit != context->mPrefetechedLayers.end(); pit++) { 70 (*pit)->debugDumpLayers(" "); 71 } 72 context->mRootRenderNode->debugDumpLayers(" "); 73 } 74 75 76 if (mActiveLayers.begin() == mActiveLayers.end()) { 77 ALOGE("set has become empty. wat."); 78 } 79 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin(); 80 lit != mActiveLayers.end(); lit++) { 81 const Layer* layer = *(lit); 82 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d", 83 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered); 84 } 85 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size); 86 } 87*/ 88 89 // TODO: reset all cached state in state objects 90 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext); 91 mAssetAtlas.terminate(); 92 93 mCaches->terminate(); 94 95 delete mBlend; 96 mBlend = nullptr; 97 delete mMeshState; 98 mMeshState = nullptr; 99 delete mScissor; 100 mScissor = nullptr; 101 delete mStencil; 102 mStencil = nullptr; 103} 104 105void RenderState::setViewport(GLsizei width, GLsizei height) { 106 mViewportWidth = width; 107 mViewportHeight = height; 108 glViewport(0, 0, mViewportWidth, mViewportHeight); 109} 110 111 112void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { 113 *outWidth = mViewportWidth; 114 *outHeight = mViewportHeight; 115} 116 117void RenderState::bindFramebuffer(GLuint fbo) { 118 if (mFramebuffer != fbo) { 119 mFramebuffer = fbo; 120 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); 121 } 122} 123 124void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { 125 interruptForFunctorInvoke(); 126 (*functor)(mode, info); 127 resumeFromFunctorInvoke(); 128} 129 130void RenderState::interruptForFunctorInvoke() { 131 mCaches->setProgram(nullptr); 132 mCaches->textureState().resetActiveTexture(); 133 meshState().unbindMeshBuffer(); 134 meshState().unbindIndicesBuffer(); 135 meshState().resetVertexPointers(); 136 meshState().disableTexCoordsVertexArray(); 137 debugOverdraw(false, false); 138} 139 140void RenderState::resumeFromFunctorInvoke() { 141 glViewport(0, 0, mViewportWidth, mViewportHeight); 142 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); 143 debugOverdraw(false, false); 144 145 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 146 147 scissor().invalidate(); 148 blend().invalidate(); 149 150 mCaches->textureState().activateTexture(0); 151 mCaches->textureState().resetBoundTextures(); 152} 153 154void RenderState::debugOverdraw(bool enable, bool clear) { 155 if (mCaches->debugOverdraw && mFramebuffer == 0) { 156 if (clear) { 157 scissor().setEnabled(false); 158 stencil().clear(); 159 } 160 if (enable) { 161 stencil().enableDebugWrite(); 162 } else { 163 stencil().disable(); 164 } 165 } 166} 167 168void RenderState::requireGLContext() { 169 assertOnGLThread(); 170 mRenderThread.eglManager().requireGlContext(); 171} 172 173void RenderState::assertOnGLThread() { 174 pthread_t curr = pthread_self(); 175 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!"); 176} 177 178class DecStrongTask : public renderthread::RenderTask { 179public: 180 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} 181 182 virtual void run() override { 183 mObject->decStrong(nullptr); 184 mObject = nullptr; 185 delete this; 186 } 187 188private: 189 VirtualLightRefBase* mObject; 190}; 191 192void RenderState::postDecStrong(VirtualLightRefBase* object) { 193 mRenderThread.queue(new DecStrongTask(object)); 194} 195 196/////////////////////////////////////////////////////////////////////////////// 197// Render 198/////////////////////////////////////////////////////////////////////////////// 199 200/* 201 * Not yet supported: 202 * 203 * Textures + coordinates 204 * SkiaShader 205 * ColorFilter 206 * 207 // TODO: texture coord 208 // TODO: texture support 209 // TODO: skiashader support 210 // TODO: color filter support 211 */ 212 213void RenderState::render(const Glop& glop) { 214 const Glop::Mesh& mesh = glop.mesh; 215 const Glop::Fill& shader = glop.fill; 216 217 // -------------------------------------------- 218 // ---------- Shader + uniform setup ---------- 219 // -------------------------------------------- 220 mCaches->setProgram(shader.program); 221 222 Glop::FloatColor color = shader.color; 223 shader.program->setColor(color.r, color.g, color.b, color.a); 224 225 shader.program->set(glop.transform.ortho, 226 glop.transform.modelView, 227 glop.transform.canvas, 228 glop.transform.fudgingOffset); 229 230 if (glop.fill.filterMode == ProgramDescription::kColorBlend) { 231 const Glop::FloatColor& color = glop.fill.filter.color; 232 glUniform4f(mCaches->program().getUniform("colorBlend"), 233 color.r, color.g, color.b, color.a); 234 } else if (glop.fill.filterMode == ProgramDescription::kColorMatrix) { 235 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE, 236 glop.fill.filter.matrix.matrix); 237 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1, 238 glop.fill.filter.matrix.vector); 239 } 240 241 // -------------------------------- 242 // ---------- Mesh setup ---------- 243 // -------------------------------- 244 // vertices 245 bool force = meshState().bindMeshBufferInternal(mesh.vertexBufferObject) 246 || (mesh.vertices != nullptr); 247 meshState().bindPositionVertexPointer(force, mesh.vertices, mesh.stride); 248 249 // indices 250 meshState().bindIndicesBufferInternal(mesh.indexBufferObject); 251 252 if (glop.mesh.vertexFlags & kTextureCoord_Attrib) { 253 // TODO: support textures 254 LOG_ALWAYS_FATAL("textures not yet supported"); 255 } else { 256 meshState().disableTexCoordsVertexArray(); 257 } 258 if (glop.mesh.vertexFlags & kColor_Attrib) { 259 LOG_ALWAYS_FATAL("color vertex attribute not yet supported"); 260 // TODO: enable color, disable when done 261 } 262 int alphaSlot = -1; 263 if (glop.mesh.vertexFlags & kAlpha_Attrib) { 264 const void* alphaCoords = ((const GLbyte*) glop.mesh.vertices) + kVertexAlphaOffset; 265 alphaSlot = shader.program->getAttrib("vtxAlpha"); 266 glEnableVertexAttribArray(alphaSlot); 267 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords); 268 } 269 270 // ------------------------------------ 271 // ---------- GL state setup ---------- 272 // ------------------------------------ 273 blend().setFactors(glop.blend.src, glop.blend.dst); 274 275 // ------------------------------------ 276 // ---------- GL state setup ---------- 277 // ------------------------------------ 278 if (mesh.indexBufferObject || mesh.indices) { 279 glDrawElements(glop.mesh.primitiveMode, glop.mesh.vertexCount, 280 GL_UNSIGNED_SHORT, mesh.indices); 281 } else { 282 glDrawArrays(glop.mesh.primitiveMode, 0, glop.mesh.vertexCount); 283 } 284 285 if (glop.mesh.vertexFlags & kAlpha_Attrib) { 286 glDisableVertexAttribArray(alphaSlot); 287 } 288} 289 290void RenderState::dump() { 291 blend().dump(); 292 meshState().dump(); 293 scissor().dump(); 294 stencil().dump(); 295} 296 297} /* namespace uirenderer */ 298} /* namespace android */ 299