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