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 | |
| 18 | namespace android { |
| 19 | namespace uirenderer { |
| 20 | |
| 21 | RenderState::RenderState() |
| 22 | : mCaches(NULL) |
| 23 | , mViewportWidth(0) |
| 24 | , mViewportHeight(0) |
| 25 | , mFramebuffer(0) { |
| 26 | } |
| 27 | |
| 28 | RenderState::~RenderState() { |
| 29 | } |
| 30 | |
| 31 | void RenderState::onGLContextCreated() { |
| 32 | // This is delayed because the first access of Caches makes GL calls |
| 33 | mCaches = &Caches::getInstance(); |
| 34 | mCaches->init(); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame^] | 35 | mCaches->setRenderState(this); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 38 | void RenderState::onGLContextDestroyed() { |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame^] | 39 | if (CC_UNLIKELY(!mActiveLayers.empty())) { |
| 40 | mCaches->dumpMemoryUsage(); |
| 41 | LOG_ALWAYS_FATAL("layers have survived gl context destruction"); |
| 42 | } |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 43 | } |
| 44 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 45 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 46 | mViewportWidth = width; |
| 47 | mViewportHeight = height; |
| 48 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 53 | *outWidth = mViewportWidth; |
| 54 | *outHeight = mViewportHeight; |
| 55 | } |
| 56 | |
| 57 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 58 | if (mFramebuffer != fbo) { |
| 59 | mFramebuffer = fbo; |
| 60 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { |
| 65 | interruptForFunctorInvoke(); |
| 66 | (*functor)(mode, info); |
| 67 | resumeFromFunctorInvoke(); |
| 68 | } |
| 69 | |
| 70 | void RenderState::interruptForFunctorInvoke() { |
| 71 | if (mCaches->currentProgram) { |
| 72 | if (mCaches->currentProgram->isInUse()) { |
| 73 | mCaches->currentProgram->remove(); |
| 74 | mCaches->currentProgram = NULL; |
| 75 | } |
| 76 | } |
| 77 | mCaches->resetActiveTexture(); |
| 78 | mCaches->unbindMeshBuffer(); |
| 79 | mCaches->unbindIndicesBuffer(); |
| 80 | mCaches->resetVertexPointers(); |
| 81 | mCaches->disableTexCoordsVertexArray(); |
| 82 | debugOverdraw(false, false); |
| 83 | } |
| 84 | |
| 85 | void RenderState::resumeFromFunctorInvoke() { |
| 86 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 87 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 88 | debugOverdraw(false, false); |
| 89 | |
| 90 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 91 | |
| 92 | mCaches->scissorEnabled = glIsEnabled(GL_SCISSOR_TEST); |
| 93 | mCaches->enableScissor(); |
| 94 | mCaches->resetScissor(); |
| 95 | |
| 96 | mCaches->activeTexture(0); |
| 97 | mCaches->resetBoundTextures(); |
| 98 | |
| 99 | mCaches->blend = true; |
| 100 | glEnable(GL_BLEND); |
| 101 | glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode); |
| 102 | glBlendEquation(GL_FUNC_ADD); |
| 103 | } |
| 104 | |
| 105 | void RenderState::debugOverdraw(bool enable, bool clear) { |
| 106 | if (mCaches->debugOverdraw && mFramebuffer == 0) { |
| 107 | if (clear) { |
| 108 | mCaches->disableScissor(); |
| 109 | mCaches->stencil.clear(); |
| 110 | } |
| 111 | if (enable) { |
| 112 | mCaches->stencil.enableDebugWrite(); |
| 113 | } else { |
| 114 | mCaches->stencil.disable(); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | } /* namespace uirenderer */ |
| 120 | } /* namespace android */ |