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() { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame^] | 130 | mCaches->setProgram(nullptr); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 131 | mCaches->textureState().resetActiveTexture(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 132 | meshState().unbindMeshBuffer(); |
| 133 | meshState().unbindIndicesBuffer(); |
| 134 | meshState().resetVertexPointers(); |
| 135 | meshState().disableTexCoordsVertexArray(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 136 | debugOverdraw(false, false); |
| 137 | } |
| 138 | |
| 139 | void RenderState::resumeFromFunctorInvoke() { |
| 140 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 141 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 142 | debugOverdraw(false, false); |
| 143 | |
| 144 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 145 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 146 | scissor().invalidate(); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 147 | blend().invalidate(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 148 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 149 | mCaches->textureState().activateTexture(0); |
| 150 | mCaches->textureState().resetBoundTextures(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void RenderState::debugOverdraw(bool enable, bool clear) { |
| 154 | if (mCaches->debugOverdraw && mFramebuffer == 0) { |
| 155 | if (clear) { |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 156 | scissor().setEnabled(false); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 157 | stencil().clear(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 158 | } |
| 159 | if (enable) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 160 | stencil().enableDebugWrite(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 161 | } else { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 162 | stencil().disable(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 167 | void RenderState::requireGLContext() { |
| 168 | assertOnGLThread(); |
| 169 | mRenderThread.eglManager().requireGlContext(); |
| 170 | } |
| 171 | |
| 172 | void RenderState::assertOnGLThread() { |
| 173 | pthread_t curr = pthread_self(); |
| 174 | LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!"); |
| 175 | } |
| 176 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 177 | class DecStrongTask : public renderthread::RenderTask { |
| 178 | public: |
| 179 | DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} |
| 180 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 181 | virtual void run() override { |
| 182 | mObject->decStrong(nullptr); |
| 183 | mObject = nullptr; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 184 | delete this; |
| 185 | } |
| 186 | |
| 187 | private: |
| 188 | VirtualLightRefBase* mObject; |
| 189 | }; |
| 190 | |
| 191 | void RenderState::postDecStrong(VirtualLightRefBase* object) { |
| 192 | mRenderThread.queue(new DecStrongTask(object)); |
| 193 | } |
| 194 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame^] | 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | // Render |
| 197 | /////////////////////////////////////////////////////////////////////////////// |
| 198 | |
| 199 | /* |
| 200 | * Not yet supported: |
| 201 | * |
| 202 | * Textures + coordinates |
| 203 | * SkiaShader |
| 204 | * ColorFilter |
| 205 | * |
| 206 | // TODO: texture coord |
| 207 | // TODO: texture support |
| 208 | // TODO: skiashader support |
| 209 | // TODO: color filter support |
| 210 | */ |
| 211 | |
| 212 | void RenderState::render(const Glop& glop) { |
| 213 | const Glop::Mesh& mesh = glop.mesh; |
| 214 | const Glop::Fill& shader = glop.fill; |
| 215 | |
| 216 | // ---------- Shader + uniform setup ---------- |
| 217 | mCaches->setProgram(shader.program); |
| 218 | |
| 219 | Glop::Fill::Color color = shader.color; |
| 220 | shader.program->setColor(color.a, color.r, color.g, color.b); |
| 221 | |
| 222 | shader.program->set(glop.transform.ortho, |
| 223 | glop.transform.modelView, |
| 224 | glop.transform.canvas, |
| 225 | glop.transform.offset); |
| 226 | |
| 227 | // ---------- Mesh setup ---------- |
| 228 | if (glop.mesh.vertexFlags & kTextureCoord_Attrib) { |
| 229 | // TODO: support textures |
| 230 | LOG_ALWAYS_FATAL("textures not yet supported"); |
| 231 | } else { |
| 232 | meshState().disableTexCoordsVertexArray(); |
| 233 | } |
| 234 | if (glop.mesh.vertexFlags & kColor_Attrib) { |
| 235 | LOG_ALWAYS_FATAL("color attribute not yet supported"); |
| 236 | // TODO: enable color, disable when done |
| 237 | } |
| 238 | if (glop.mesh.vertexFlags & kAlpha_Attrib) { |
| 239 | LOG_ALWAYS_FATAL("alpha attribute not yet supported"); |
| 240 | // TODO: enable alpha attribute, disable when done |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Hard-coded vertex assumptions: |
| 245 | * - required |
| 246 | * - xy floats |
| 247 | * - 0 offset |
| 248 | * - in VBO |
| 249 | */ |
| 250 | bool force = meshState().bindMeshBuffer(mesh.vertexBufferObject); |
| 251 | meshState().bindPositionVertexPointer(force, nullptr, mesh.stride); |
| 252 | |
| 253 | /** |
| 254 | * Hard-coded index assumptions: |
| 255 | * - optional |
| 256 | * - 0 offset |
| 257 | * - in IBO |
| 258 | */ |
| 259 | meshState().bindIndicesBufferInternal(mesh.indexBufferObject); |
| 260 | |
| 261 | // ---------- GL state setup ---------- |
| 262 | |
| 263 | if (glop.blend.mode != Glop::Blend::kDisable) { |
| 264 | blend().enable(glop.blend.mode, glop.blend.swapSrcDst); |
| 265 | } else { |
| 266 | blend().disable(); |
| 267 | } |
| 268 | |
| 269 | glDrawElements(glop.mesh.primitiveMode, glop.mesh.vertexCount, GL_UNSIGNED_BYTE, nullptr); |
| 270 | } |
| 271 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 272 | } /* namespace uirenderer */ |
| 273 | } /* namespace android */ |