RenderState.cpp revision 30036092b40badecbe64d9c2bff4850132147f78
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& fill = glop.fill; 213 214 // --------------------------------------------- 215 // ---------- Program + uniform setup ---------- 216 // --------------------------------------------- 217 mCaches->setProgram(fill.program); 218 219 if (fill.colorEnabled) { 220 fill.program->setColor(fill.color); 221 } 222 223 fill.program->set(glop.transform.ortho, 224 glop.transform.modelView, 225 glop.transform.canvas, 226 glop.transform.fudgingOffset); 227 228 // Color filter uniforms 229 if (glop.fill.filterMode == ProgramDescription::kColorBlend) { 230 const FloatColor& color = glop.fill.filter.color; 231 glUniform4f(mCaches->program().getUniform("colorBlend"), 232 color.r, color.g, color.b, color.a); 233 } else if (glop.fill.filterMode == ProgramDescription::kColorMatrix) { 234 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE, 235 glop.fill.filter.matrix.matrix); 236 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1, 237 glop.fill.filter.matrix.vector); 238 } 239 240 // Round rect clipping uniforms 241 if (glop.roundRectClipState) { 242 // TODO: avoid query, and cache values (or RRCS ptr) in program 243 const RoundRectClipState* state = glop.roundRectClipState; 244 const Rect& innerRect = state->innerRect; 245 glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"), 246 innerRect.left, innerRect.top, 247 innerRect.right, innerRect.bottom); 248 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"), 249 1, GL_FALSE, &state->matrix.data[0]); 250 251 // add half pixel to round out integer rect space to cover pixel centers 252 float roundedOutRadius = state->radius + 0.5f; 253 glUniform1f(fill.program->getUniform("roundRectRadius"), 254 roundedOutRadius); 255 } 256 257 // -------------------------------- 258 // ---------- Mesh setup ---------- 259 // -------------------------------- 260 // vertices 261 const bool force = meshState().bindMeshBufferInternal(mesh.vertexBufferObject) 262 || (mesh.vertices != nullptr); 263 meshState().bindPositionVertexPointer(force, mesh.vertices, mesh.stride); 264 265 // indices 266 meshState().bindIndicesBufferInternal(mesh.indexBufferObject); 267 268 if (mesh.vertexFlags & kTextureCoord_Attrib) { 269 // TODO: to support shaders, increment texture unit 270 mCaches->textureState().activateTexture(0); 271 272 if (glop.fill.textureClamp != GL_INVALID_ENUM) { 273 glop.fill.texture->setWrap(glop.fill.textureClamp, true); 274 } 275 if (glop.fill.textureFilter != GL_INVALID_ENUM) { 276 glop.fill.texture->setFilter(glop.fill.textureFilter, true); 277 } 278 279 mCaches->textureState().bindTexture(fill.texture->id); 280 meshState().enableTexCoordsVertexArray(); 281 meshState().bindTexCoordsVertexPointer(force, mesh.texCoordOffset); 282 } else { 283 meshState().disableTexCoordsVertexArray(); 284 } 285 if (mesh.vertexFlags & kColor_Attrib) { 286 LOG_ALWAYS_FATAL("color vertex attribute not yet supported"); 287 // TODO: enable color attribute, disable when done 288 } 289 int alphaSlot = -1; 290 if (mesh.vertexFlags & kAlpha_Attrib) { 291 const void* alphaCoords = ((const GLbyte*) glop.mesh.vertices) + kVertexAlphaOffset; 292 alphaSlot = fill.program->getAttrib("vtxAlpha"); 293 glEnableVertexAttribArray(alphaSlot); 294 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords); 295 } 296 297 // ------------------------------------ 298 // ---------- GL state setup ---------- 299 // ------------------------------------ 300 blend().setFactors(glop.blend.src, glop.blend.dst); 301 302 // ------------------------------------ 303 // ---------- Actual drawing ---------- 304 // ------------------------------------ 305 if (mesh.indexBufferObject == meshState().getQuadListIBO()) { 306 // Since the indexed quad list is of limited length, we loop over 307 // the glDrawXXX method while updating the vertex pointer 308 GLsizei elementsCount = mesh.elementCount; 309 const GLbyte* vertices = static_cast<const GLbyte*>(mesh.vertices); 310 while (elementsCount > 0) { 311 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6); 312 313 // TODO: this double binds on first pass 314 meshState().bindPositionVertexPointer(true, vertices, mesh.stride); 315 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr); 316 elementsCount -= drawCount; 317 vertices += (drawCount / 6) * 4 * mesh.stride; 318 } 319 } else if (mesh.indexBufferObject || mesh.indices) { 320 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, mesh.indices); 321 } else { 322 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount); 323 } 324 325 // ----------------------------------- 326 // ---------- Mesh teardown ---------- 327 // ----------------------------------- 328 if (glop.mesh.vertexFlags & kAlpha_Attrib) { 329 glDisableVertexAttribArray(alphaSlot); 330 } 331} 332 333void RenderState::dump() { 334 blend().dump(); 335 meshState().dump(); 336 scissor().dump(); 337 stencil().dump(); 338} 339 340} /* namespace uirenderer */ 341} /* namespace android */ 342