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