John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 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.h" |
| 17 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 18 | #include "renderthread/CanvasContext.h" |
| 19 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 20 | namespace android { |
| 21 | namespace uirenderer { |
| 22 | |
| 23 | RenderState::RenderState() |
| 24 | : mCaches(NULL) |
| 25 | , mViewportWidth(0) |
| 26 | , mViewportHeight(0) |
| 27 | , mFramebuffer(0) { |
| 28 | } |
| 29 | |
| 30 | RenderState::~RenderState() { |
| 31 | } |
| 32 | |
| 33 | void RenderState::onGLContextCreated() { |
| 34 | // This is delayed because the first access of Caches makes GL calls |
| 35 | mCaches = &Caches::getInstance(); |
| 36 | mCaches->init(); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 37 | mCaches->setRenderState(this); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 40 | void RenderState::onGLContextDestroyed() { |
Chris Craik | 21029ef | 2014-09-12 09:29:10 -0700 | [diff] [blame] | 41 | /* |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 42 | AutoMutex _lock(mLayerLock); |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 43 | size_t size = mActiveLayers.size(); |
| 44 | if (CC_UNLIKELY(size != 0)) { |
| 45 | ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d", |
| 46 | mRegisteredContexts.size(), size, mActiveLayers.empty()); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 47 | mCaches->dumpMemoryUsage(); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 48 | for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin(); |
| 49 | cit != mRegisteredContexts.end(); cit++) { |
| 50 | renderthread::CanvasContext* context = *cit; |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 51 | ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get()); |
| 52 | ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size()); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 53 | for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin(); |
| 54 | pit != context->mPrefetechedLayers.end(); pit++) { |
| 55 | (*pit)->debugDumpLayers(" "); |
| 56 | } |
| 57 | context->mRootRenderNode->debugDumpLayers(" "); |
| 58 | } |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 59 | |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 60 | |
| 61 | if (mActiveLayers.begin() == mActiveLayers.end()) { |
| 62 | ALOGE("set has become empty. wat."); |
| 63 | } |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 64 | for (std::set<const Layer*>::iterator lit = mActiveLayers.begin(); |
| 65 | lit != mActiveLayers.end(); lit++) { |
| 66 | const Layer* layer = *(lit); |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 67 | ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d", |
| 68 | layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered); |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 69 | } |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 71 | } |
Chris Craik | 21029ef | 2014-09-12 09:29:10 -0700 | [diff] [blame] | 72 | */ |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 75 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 76 | mViewportWidth = width; |
| 77 | mViewportHeight = height; |
| 78 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 83 | *outWidth = mViewportWidth; |
| 84 | *outHeight = mViewportHeight; |
| 85 | } |
| 86 | |
| 87 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 88 | if (mFramebuffer != fbo) { |
| 89 | mFramebuffer = fbo; |
| 90 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { |
| 95 | interruptForFunctorInvoke(); |
| 96 | (*functor)(mode, info); |
| 97 | resumeFromFunctorInvoke(); |
| 98 | } |
| 99 | |
| 100 | void RenderState::interruptForFunctorInvoke() { |
| 101 | if (mCaches->currentProgram) { |
| 102 | if (mCaches->currentProgram->isInUse()) { |
| 103 | mCaches->currentProgram->remove(); |
| 104 | mCaches->currentProgram = NULL; |
| 105 | } |
| 106 | } |
| 107 | mCaches->resetActiveTexture(); |
| 108 | mCaches->unbindMeshBuffer(); |
| 109 | mCaches->unbindIndicesBuffer(); |
| 110 | mCaches->resetVertexPointers(); |
| 111 | mCaches->disableTexCoordsVertexArray(); |
| 112 | debugOverdraw(false, false); |
| 113 | } |
| 114 | |
| 115 | void RenderState::resumeFromFunctorInvoke() { |
| 116 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 117 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 118 | debugOverdraw(false, false); |
| 119 | |
| 120 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 121 | |
| 122 | mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST); |
| 123 | mCaches->enableScissor(); |
| 124 | mCaches->resetScissor(); |
| 125 | |
| 126 | mCaches->activeTexture(0); |
| 127 | mCaches->resetBoundTextures(); |
| 128 | |
| 129 | mCaches->blend = true; |
| 130 | glEnable(GL_BLEND); |
| 131 | glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode); |
| 132 | glBlendEquation(GL_FUNC_ADD); |
| 133 | } |
| 134 | |
| 135 | void RenderState::debugOverdraw(bool enable, bool clear) { |
| 136 | if (mCaches->debugOverdraw && mFramebuffer == 0) { |
| 137 | if (clear) { |
| 138 | mCaches->disableScissor(); |
| 139 | mCaches->stencil.clear(); |
| 140 | } |
| 141 | if (enable) { |
| 142 | mCaches->stencil.enableDebugWrite(); |
| 143 | } else { |
| 144 | mCaches->stencil.disable(); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } /* namespace uirenderer */ |
| 150 | } /* namespace android */ |