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