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 | */ |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 16 | #include "renderstate/RenderState.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 17 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 18 | #include "renderthread/CanvasContext.h" |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 19 | #include "renderthread/EglManager.h" |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 20 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 24 | RenderState::RenderState(renderthread::RenderThread& thread) |
| 25 | : mRenderThread(thread) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 26 | , mCaches(nullptr) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 27 | , mMeshState(nullptr) |
| 28 | , mScissor(nullptr) |
| 29 | , mStencil(nullptr) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 30 | , mViewportWidth(0) |
| 31 | , mViewportHeight(0) |
| 32 | , mFramebuffer(0) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 33 | mThreadId = pthread_self(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | RenderState::~RenderState() { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 37 | LOG_ALWAYS_FATAL_IF(mMeshState || mScissor || mStencil, |
| 38 | "State object lifecycle not managed correctly"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void RenderState::onGLContextCreated() { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 42 | LOG_ALWAYS_FATAL_IF(mMeshState || mScissor || mStencil, |
| 43 | "State object lifecycle not managed correctly"); |
| 44 | mMeshState = new MeshState(); |
| 45 | mScissor = new Scissor(); |
| 46 | mStencil = new Stencil(); |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 47 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 48 | // This is delayed because the first access of Caches makes GL calls |
Chris Craik | ff5c8e8 | 2015-01-30 09:46:18 -0800 | [diff] [blame^] | 49 | if (!mCaches) { |
| 50 | mCaches = &Caches::createInstance(*this); |
| 51 | } |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 52 | mCaches->init(); |
| 53 | mCaches->textureCache.setAssetAtlas(&mAssetAtlas); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 54 | } |
| 55 | |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 56 | static void layerLostGlContext(Layer* layer) { |
| 57 | layer->onGlContextLost(); |
| 58 | } |
| 59 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 60 | void RenderState::onGLContextDestroyed() { |
Chris Craik | 21029ef | 2014-09-12 09:29:10 -0700 | [diff] [blame] | 61 | /* |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 62 | size_t size = mActiveLayers.size(); |
| 63 | if (CC_UNLIKELY(size != 0)) { |
| 64 | ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d", |
| 65 | mRegisteredContexts.size(), size, mActiveLayers.empty()); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 66 | mCaches->dumpMemoryUsage(); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 67 | for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin(); |
| 68 | cit != mRegisteredContexts.end(); cit++) { |
| 69 | renderthread::CanvasContext* context = *cit; |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 70 | ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get()); |
| 71 | ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size()); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 72 | for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin(); |
| 73 | pit != context->mPrefetechedLayers.end(); pit++) { |
| 74 | (*pit)->debugDumpLayers(" "); |
| 75 | } |
| 76 | context->mRootRenderNode->debugDumpLayers(" "); |
| 77 | } |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 78 | |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 79 | |
| 80 | if (mActiveLayers.begin() == mActiveLayers.end()) { |
| 81 | ALOGE("set has become empty. wat."); |
| 82 | } |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 83 | for (std::set<const Layer*>::iterator lit = mActiveLayers.begin(); |
| 84 | lit != mActiveLayers.end(); lit++) { |
| 85 | const Layer* layer = *(lit); |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 86 | ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d", |
| 87 | layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered); |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 88 | } |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 90 | } |
Chris Craik | 21029ef | 2014-09-12 09:29:10 -0700 | [diff] [blame] | 91 | */ |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 92 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 93 | // TODO: reset all cached state in state objects |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 94 | std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext); |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 95 | mAssetAtlas.terminate(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 96 | |
| 97 | delete mMeshState; |
| 98 | mMeshState = nullptr; |
| 99 | delete mScissor; |
| 100 | mScissor = nullptr; |
| 101 | delete mStencil; |
| 102 | mStencil = nullptr; |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 103 | } |
| 104 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 105 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 106 | mViewportWidth = width; |
| 107 | mViewportHeight = height; |
| 108 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 113 | *outWidth = mViewportWidth; |
| 114 | *outHeight = mViewportHeight; |
| 115 | } |
| 116 | |
| 117 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 118 | if (mFramebuffer != fbo) { |
| 119 | mFramebuffer = fbo; |
| 120 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { |
| 125 | interruptForFunctorInvoke(); |
| 126 | (*functor)(mode, info); |
| 127 | resumeFromFunctorInvoke(); |
| 128 | } |
| 129 | |
| 130 | void RenderState::interruptForFunctorInvoke() { |
| 131 | if (mCaches->currentProgram) { |
| 132 | if (mCaches->currentProgram->isInUse()) { |
| 133 | mCaches->currentProgram->remove(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 134 | mCaches->currentProgram = nullptr; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | mCaches->resetActiveTexture(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 138 | meshState().unbindMeshBuffer(); |
| 139 | meshState().unbindIndicesBuffer(); |
| 140 | meshState().resetVertexPointers(); |
| 141 | meshState().disableTexCoordsVertexArray(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 142 | debugOverdraw(false, false); |
| 143 | } |
| 144 | |
| 145 | void RenderState::resumeFromFunctorInvoke() { |
| 146 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 147 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 148 | debugOverdraw(false, false); |
| 149 | |
| 150 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 151 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 152 | scissor().invalidate(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 153 | |
| 154 | mCaches->activeTexture(0); |
| 155 | mCaches->resetBoundTextures(); |
| 156 | |
| 157 | mCaches->blend = true; |
| 158 | glEnable(GL_BLEND); |
| 159 | glBlendFunc(mCaches->lastSrcMode, mCaches->lastDstMode); |
| 160 | glBlendEquation(GL_FUNC_ADD); |
| 161 | } |
| 162 | |
| 163 | void RenderState::debugOverdraw(bool enable, bool clear) { |
| 164 | if (mCaches->debugOverdraw && mFramebuffer == 0) { |
| 165 | if (clear) { |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 166 | scissor().setEnabled(false); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 167 | stencil().clear(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 168 | } |
| 169 | if (enable) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 170 | stencil().enableDebugWrite(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 171 | } else { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 172 | stencil().disable(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 177 | void RenderState::requireGLContext() { |
| 178 | assertOnGLThread(); |
| 179 | mRenderThread.eglManager().requireGlContext(); |
| 180 | } |
| 181 | |
| 182 | void RenderState::assertOnGLThread() { |
| 183 | pthread_t curr = pthread_self(); |
| 184 | LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!"); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | class DecStrongTask : public renderthread::RenderTask { |
| 189 | public: |
| 190 | DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} |
| 191 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 192 | virtual void run() override { |
| 193 | mObject->decStrong(nullptr); |
| 194 | mObject = nullptr; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 195 | delete this; |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | VirtualLightRefBase* mObject; |
| 200 | }; |
| 201 | |
| 202 | void RenderState::postDecStrong(VirtualLightRefBase* object) { |
| 203 | mRenderThread.queue(new DecStrongTask(object)); |
| 204 | } |
| 205 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 206 | } /* namespace uirenderer */ |
| 207 | } /* namespace android */ |