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