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 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 16 | #include "renderstate/RenderState.h" |
| 17 | #include <GpuMemoryTracker.h> |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 18 | #include "DeferredLayerUpdater.h" |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 19 | #include "GlLayer.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 20 | #include "VkLayer.h" |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 21 | #include "Snapshot.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 22 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 23 | #include "renderthread/CanvasContext.h" |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 24 | #include "renderthread/EglManager.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 25 | #include "utils/GLUtils.h" |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 26 | |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 29 | #include <ui/ColorSpace.h> |
| 30 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 34 | RenderState::RenderState(renderthread::RenderThread& thread) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 35 | : mRenderThread(thread), mViewportWidth(0), mViewportHeight(0), mFramebuffer(0) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 36 | mThreadId = pthread_self(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | RenderState::~RenderState() { |
| 40 | } |
| 41 | |
| 42 | void RenderState::onGLContextCreated() { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 43 | GpuMemoryTracker::onGpuContextCreated(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -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 |
Chris Craik | ff5c8e8 | 2015-01-30 09:46:18 -0800 | [diff] [blame] | 46 | if (!mCaches) { |
| 47 | mCaches = &Caches::createInstance(*this); |
| 48 | } |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 49 | mCaches->init(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 50 | } |
| 51 | |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 52 | static void layerLostGlContext(Layer* layer) { |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 53 | LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 54 | "layerLostGlContext on non GL layer"); |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 55 | static_cast<GlLayer*>(layer)->onGlContextLost(); |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 58 | void RenderState::onGLContextDestroyed() { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 59 | // TODO: reset all cached state in state objects |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 60 | std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 61 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 62 | mCaches->terminate(); |
| 63 | |
sergeyv | c3f1316 | 2017-02-06 11:45:14 -0800 | [diff] [blame] | 64 | destroyLayersInUpdater(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 65 | GpuMemoryTracker::onGpuContextDestroyed(); |
| 66 | } |
| 67 | |
| 68 | void RenderState::onVkContextCreated() { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 69 | GpuMemoryTracker::onGpuContextCreated(); |
| 70 | } |
| 71 | |
| 72 | static void layerDestroyedVkContext(Layer* layer) { |
| 73 | LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan, |
| 74 | "layerLostVkContext on non Vulkan layer"); |
| 75 | static_cast<VkLayer*>(layer)->onVkContextDestroyed(); |
| 76 | } |
| 77 | |
| 78 | void RenderState::onVkContextDestroyed() { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 79 | std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext); |
Derek Sollenberger | 7a4216b | 2017-10-25 09:52:23 -0400 | [diff] [blame] | 80 | destroyLayersInUpdater(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 81 | GpuMemoryTracker::onGpuContextDestroyed(); |
| 82 | } |
| 83 | |
| 84 | GrContext* RenderState::getGrContext() const { |
| 85 | return mRenderThread.getGrContext(); |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 88 | void RenderState::flush(Caches::FlushMode mode) { |
John Reck | 642ebea | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 89 | if (mCaches) mCaches->flush(mode); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 90 | } |
| 91 | |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 92 | void RenderState::onBitmapDestroyed(uint32_t pixelRefId) { |
Mike Reed | 8cafcc6 | 2018-05-03 11:32:46 -0400 | [diff] [blame] | 93 | // DEAD CODE |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 94 | } |
| 95 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 96 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 97 | mViewportWidth = width; |
| 98 | mViewportHeight = height; |
| 99 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 100 | } |
| 101 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 102 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 103 | *outWidth = mViewportWidth; |
| 104 | *outHeight = mViewportHeight; |
| 105 | } |
| 106 | |
| 107 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 108 | if (mFramebuffer != fbo) { |
| 109 | mFramebuffer = fbo; |
| 110 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 111 | } |
| 112 | } |
| 113 | |
John Reck | 0b8d067 | 2016-01-29 14:18:22 -0800 | [diff] [blame] | 114 | GLuint RenderState::createFramebuffer() { |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 115 | GLuint ret; |
| 116 | glGenFramebuffers(1, &ret); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | void RenderState::deleteFramebuffer(GLuint fbo) { |
| 121 | if (mFramebuffer == fbo) { |
| 122 | // GL defines that deleting the currently bound FBO rebinds FBO 0. |
| 123 | // Reflect this in our cached value. |
| 124 | mFramebuffer = 0; |
| 125 | } |
| 126 | glDeleteFramebuffers(1, &fbo); |
| 127 | } |
| 128 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 129 | void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { |
John Reck | 95cd24b | 2015-08-04 11:17:39 -0700 | [diff] [blame] | 130 | if (mode == DrawGlInfo::kModeProcessNoContext) { |
| 131 | // If there's no context we don't need to interrupt as there's |
| 132 | // no gl state to save/restore |
| 133 | (*functor)(mode, info); |
| 134 | } else { |
| 135 | interruptForFunctorInvoke(); |
| 136 | (*functor)(mode, info); |
| 137 | resumeFromFunctorInvoke(); |
| 138 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void RenderState::interruptForFunctorInvoke() { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 142 | mCaches->textureState().resetActiveTexture(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 143 | debugOverdraw(false, false); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 144 | // TODO: We need a way to know whether the functor is sRGB aware (b/32072673) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 145 | if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 146 | glDisable(GL_FRAMEBUFFER_SRGB_EXT); |
| 147 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void RenderState::resumeFromFunctorInvoke() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 151 | if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 152 | glEnable(GL_FRAMEBUFFER_SRGB_EXT); |
| 153 | } |
| 154 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 155 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 156 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 157 | debugOverdraw(false, false); |
| 158 | |
| 159 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 160 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 161 | mCaches->textureState().activateTexture(0); |
| 162 | mCaches->textureState().resetBoundTextures(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void RenderState::debugOverdraw(bool enable, bool clear) { |
Mike Reed | e7f688b | 2018-05-04 15:21:43 -0400 | [diff] [blame] | 166 | // DEAD CODE |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 167 | } |
| 168 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 169 | static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) { |
| 170 | layerUpdater->destroyLayer(); |
| 171 | } |
| 172 | |
| 173 | void RenderState::destroyLayersInUpdater() { |
| 174 | std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater); |
| 175 | } |
| 176 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 177 | void RenderState::postDecStrong(VirtualLightRefBase* object) { |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 178 | if (pthread_equal(mThreadId, pthread_self())) { |
| 179 | object->decStrong(nullptr); |
| 180 | } else { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 181 | mRenderThread.queue().post([object]() { object->decStrong(nullptr); }); |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 182 | } |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 185 | /////////////////////////////////////////////////////////////////////////////// |
| 186 | // Render |
| 187 | /////////////////////////////////////////////////////////////////////////////// |
| 188 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 189 | void RenderState::dump() { |
Mike Reed | e7f688b | 2018-05-04 15:21:43 -0400 | [diff] [blame] | 190 | // DEAD CODE |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 191 | } |
| 192 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 193 | } /* namespace uirenderer */ |
| 194 | } /* namespace android */ |