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 | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 21 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 22 | #include "renderthread/CanvasContext.h" |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 23 | #include "renderthread/EglManager.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 24 | #include "utils/GLUtils.h" |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 25 | |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 28 | #include <ui/ColorSpace.h> |
| 29 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 33 | RenderState::RenderState(renderthread::RenderThread& thread) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 34 | : mRenderThread(thread), mViewportWidth(0), mViewportHeight(0), mFramebuffer(0) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 35 | mThreadId = pthread_self(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | RenderState::~RenderState() { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 39 | LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 40 | "State object lifecycle not managed correctly"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void RenderState::onGLContextCreated() { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 44 | LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 45 | "State object lifecycle not managed correctly"); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 46 | GpuMemoryTracker::onGpuContextCreated(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 47 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 48 | mBlend = new Blend(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 49 | mMeshState = new MeshState(); |
| 50 | mScissor = new Scissor(); |
| 51 | mStencil = new Stencil(); |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 52 | |
John Reck | 642ebea | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 53 | // Deferred because creation needs GL context for texture limits |
| 54 | if (!mLayerPool) { |
| 55 | mLayerPool = new OffscreenBufferPool(); |
| 56 | } |
| 57 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 58 | // This is delayed because the first access of Caches makes GL calls |
Chris Craik | ff5c8e8 | 2015-01-30 09:46:18 -0800 | [diff] [blame] | 59 | if (!mCaches) { |
| 60 | mCaches = &Caches::createInstance(*this); |
| 61 | } |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 62 | mCaches->init(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 65 | static void layerLostGlContext(Layer* layer) { |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 66 | LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 67 | "layerLostGlContext on non GL layer"); |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 68 | static_cast<GlLayer*>(layer)->onGlContextLost(); |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 71 | void RenderState::onGLContextDestroyed() { |
John Reck | 642ebea | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 72 | mLayerPool->clear(); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 73 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 74 | // TODO: reset all cached state in state objects |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 75 | std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 76 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 77 | mCaches->terminate(); |
| 78 | |
| 79 | delete mBlend; |
| 80 | mBlend = nullptr; |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 81 | delete mMeshState; |
| 82 | mMeshState = nullptr; |
| 83 | delete mScissor; |
| 84 | mScissor = nullptr; |
| 85 | delete mStencil; |
| 86 | mStencil = nullptr; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 87 | |
sergeyv | c3f1316 | 2017-02-06 11:45:14 -0800 | [diff] [blame] | 88 | destroyLayersInUpdater(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 89 | GpuMemoryTracker::onGpuContextDestroyed(); |
| 90 | } |
| 91 | |
| 92 | void RenderState::onVkContextCreated() { |
| 93 | LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 94 | "State object lifecycle not managed correctly"); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 95 | GpuMemoryTracker::onGpuContextCreated(); |
| 96 | } |
| 97 | |
| 98 | static void layerDestroyedVkContext(Layer* layer) { |
| 99 | LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan, |
| 100 | "layerLostVkContext on non Vulkan layer"); |
| 101 | static_cast<VkLayer*>(layer)->onVkContextDestroyed(); |
| 102 | } |
| 103 | |
| 104 | void RenderState::onVkContextDestroyed() { |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 105 | std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext); |
Derek Sollenberger | 7a4216b | 2017-10-25 09:52:23 -0400 | [diff] [blame] | 106 | destroyLayersInUpdater(); |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 107 | GpuMemoryTracker::onGpuContextDestroyed(); |
| 108 | } |
| 109 | |
| 110 | GrContext* RenderState::getGrContext() const { |
| 111 | return mRenderThread.getGrContext(); |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 114 | void RenderState::flush(Caches::FlushMode mode) { |
| 115 | switch (mode) { |
| 116 | case Caches::FlushMode::Full: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 117 | // fall through |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 118 | case Caches::FlushMode::Moderate: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 119 | // fall through |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 120 | case Caches::FlushMode::Layers: |
John Reck | 642ebea | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 121 | if (mLayerPool) mLayerPool->clear(); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 122 | break; |
| 123 | } |
John Reck | 642ebea | 2017-07-17 09:55:02 -0700 | [diff] [blame] | 124 | if (mCaches) mCaches->flush(mode); |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 127 | void RenderState::onBitmapDestroyed(uint32_t pixelRefId) { |
John Reck | 36393c3 | 2017-05-23 15:32:08 -0700 | [diff] [blame] | 128 | if (mCaches && mCaches->textureCache.destroyTexture(pixelRefId)) { |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 129 | glFlush(); |
| 130 | GL_CHECKPOINT(MODERATE); |
| 131 | } |
| 132 | } |
| 133 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 134 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 135 | mViewportWidth = width; |
| 136 | mViewportHeight = height; |
| 137 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 138 | } |
| 139 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 140 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 141 | *outWidth = mViewportWidth; |
| 142 | *outHeight = mViewportHeight; |
| 143 | } |
| 144 | |
| 145 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 146 | if (mFramebuffer != fbo) { |
| 147 | mFramebuffer = fbo; |
| 148 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 149 | } |
| 150 | } |
| 151 | |
John Reck | 0b8d067 | 2016-01-29 14:18:22 -0800 | [diff] [blame] | 152 | GLuint RenderState::createFramebuffer() { |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 153 | GLuint ret; |
| 154 | glGenFramebuffers(1, &ret); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | void RenderState::deleteFramebuffer(GLuint fbo) { |
| 159 | if (mFramebuffer == fbo) { |
| 160 | // GL defines that deleting the currently bound FBO rebinds FBO 0. |
| 161 | // Reflect this in our cached value. |
| 162 | mFramebuffer = 0; |
| 163 | } |
| 164 | glDeleteFramebuffers(1, &fbo); |
| 165 | } |
| 166 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 167 | void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { |
John Reck | 95cd24b | 2015-08-04 11:17:39 -0700 | [diff] [blame] | 168 | if (mode == DrawGlInfo::kModeProcessNoContext) { |
| 169 | // If there's no context we don't need to interrupt as there's |
| 170 | // no gl state to save/restore |
| 171 | (*functor)(mode, info); |
| 172 | } else { |
| 173 | interruptForFunctorInvoke(); |
| 174 | (*functor)(mode, info); |
| 175 | resumeFromFunctorInvoke(); |
| 176 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void RenderState::interruptForFunctorInvoke() { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 180 | mCaches->setProgram(nullptr); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 181 | mCaches->textureState().resetActiveTexture(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 182 | meshState().unbindMeshBuffer(); |
| 183 | meshState().unbindIndicesBuffer(); |
| 184 | meshState().resetVertexPointers(); |
| 185 | meshState().disableTexCoordsVertexArray(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 186 | debugOverdraw(false, false); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 187 | // 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^] | 188 | if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 189 | glDisable(GL_FRAMEBUFFER_SRGB_EXT); |
| 190 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void RenderState::resumeFromFunctorInvoke() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 194 | if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 195 | glEnable(GL_FRAMEBUFFER_SRGB_EXT); |
| 196 | } |
| 197 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 198 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 199 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 200 | debugOverdraw(false, false); |
| 201 | |
| 202 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 203 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 204 | scissor().invalidate(); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 205 | blend().invalidate(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 206 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 207 | mCaches->textureState().activateTexture(0); |
| 208 | mCaches->textureState().resetBoundTextures(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void RenderState::debugOverdraw(bool enable, bool clear) { |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 212 | if (Properties::debugOverdraw && mFramebuffer == 0) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 213 | if (clear) { |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 214 | scissor().setEnabled(false); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 215 | stencil().clear(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 216 | } |
| 217 | if (enable) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 218 | stencil().enableDebugWrite(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 219 | } else { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 220 | stencil().disable(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 225 | static void destroyLayerInUpdater(DeferredLayerUpdater* layerUpdater) { |
| 226 | layerUpdater->destroyLayer(); |
| 227 | } |
| 228 | |
| 229 | void RenderState::destroyLayersInUpdater() { |
| 230 | std::for_each(mActiveLayerUpdaters.begin(), mActiveLayerUpdaters.end(), destroyLayerInUpdater); |
| 231 | } |
| 232 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 233 | void RenderState::postDecStrong(VirtualLightRefBase* object) { |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 234 | if (pthread_equal(mThreadId, pthread_self())) { |
| 235 | object->decStrong(nullptr); |
| 236 | } else { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 237 | mRenderThread.queue().post([object]() { object->decStrong(nullptr); }); |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 238 | } |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 241 | /////////////////////////////////////////////////////////////////////////////// |
| 242 | // Render |
| 243 | /////////////////////////////////////////////////////////////////////////////// |
| 244 | |
Arun | 530a2b4 | 2017-01-23 12:47:57 +0000 | [diff] [blame] | 245 | void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 246 | bool overrideDisableBlending) { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 247 | const Glop::Mesh& mesh = glop.mesh; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 248 | const Glop::Mesh::Vertices& vertices = mesh.vertices; |
| 249 | const Glop::Mesh::Indices& indices = mesh.indices; |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 250 | const Glop::Fill& fill = glop.fill; |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 251 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 252 | GL_CHECKPOINT(MODERATE); |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 253 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 254 | // --------------------------------------------- |
| 255 | // ---------- Program + uniform setup ---------- |
| 256 | // --------------------------------------------- |
| 257 | mCaches->setProgram(fill.program); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 258 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 259 | if (fill.colorEnabled) { |
| 260 | fill.program->setColor(fill.color); |
| 261 | } |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 262 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 263 | fill.program->set(orthoMatrix, glop.transform.modelView, glop.transform.meshTransform(), |
| 264 | glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 265 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 266 | // Color filter uniforms |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 267 | if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 268 | const FloatColor& color = fill.filter.color; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 269 | glUniform4f(mCaches->program().getUniform("colorBlend"), color.r, color.g, color.b, |
| 270 | color.a); |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 271 | } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 272 | glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 273 | fill.filter.matrix.matrix); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 274 | glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 275 | fill.filter.matrix.vector); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 278 | // Round rect clipping uniforms |
| 279 | if (glop.roundRectClipState) { |
| 280 | // TODO: avoid query, and cache values (or RRCS ptr) in program |
| 281 | const RoundRectClipState* state = glop.roundRectClipState; |
| 282 | const Rect& innerRect = state->innerRect; |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 283 | |
| 284 | // add half pixel to round out integer rect space to cover pixel centers |
| 285 | float roundedOutRadius = state->radius + 0.5f; |
Arun | 06e9f32 | 2017-01-23 11:59:21 +0000 | [diff] [blame] | 286 | |
| 287 | // Divide by the radius to simplify the calculations in the fragment shader |
| 288 | // roundRectPos is also passed from vertex shader relative to top/left & radius |
| 289 | glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 290 | innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius, |
| 291 | (innerRect.right - innerRect.left) / roundedOutRadius, |
| 292 | (innerRect.bottom - innerRect.top) / roundedOutRadius); |
Arun | 06e9f32 | 2017-01-23 11:59:21 +0000 | [diff] [blame] | 293 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 294 | glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"), 1, GL_FALSE, |
| 295 | &state->matrix.data[0]); |
Arun | 06e9f32 | 2017-01-23 11:59:21 +0000 | [diff] [blame] | 296 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 297 | glUniform1f(fill.program->getUniform("roundRectRadius"), roundedOutRadius); |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 298 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 299 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 300 | GL_CHECKPOINT(MODERATE); |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 301 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 302 | // -------------------------------- |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 303 | // ---------- Mesh setup ---------- |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 304 | // -------------------------------- |
| 305 | // vertices |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame] | 306 | meshState().bindMeshBuffer(vertices.bufferObject); |
| 307 | meshState().bindPositionVertexPointer(vertices.position, vertices.stride); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 308 | |
| 309 | // indices |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame] | 310 | meshState().bindIndicesBuffer(indices.bufferObject); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 311 | |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 312 | // texture |
| 313 | if (fill.texture.texture != nullptr) { |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 314 | const Glop::Fill::TextureData& texture = fill.texture; |
| 315 | // texture always takes slot 0, shader samplers increment from there |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 316 | mCaches->textureState().activateTexture(0); |
| 317 | |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 318 | mCaches->textureState().bindTexture(texture.texture->target(), texture.texture->id()); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 319 | if (texture.clamp != GL_INVALID_ENUM) { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 320 | texture.texture->setWrap(texture.clamp, false, false); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 321 | } |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 322 | if (texture.filter != GL_INVALID_ENUM) { |
sergeyv | 2a38c42 | 2016-10-25 15:21:50 -0700 | [diff] [blame] | 323 | texture.texture->setFilter(texture.filter, false, false); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 324 | } |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 325 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 326 | if (texture.textureTransform) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 327 | glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1, GL_FALSE, |
| 328 | &texture.textureTransform->data[0]); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 329 | } |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // vertex attributes (tex coord, color, alpha) |
| 333 | if (vertices.attribFlags & VertexAttribFlags::TextureCoord) { |
| 334 | meshState().enableTexCoordsVertexArray(); |
| 335 | meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 336 | } else { |
| 337 | meshState().disableTexCoordsVertexArray(); |
| 338 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 339 | int colorLocation = -1; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 340 | if (vertices.attribFlags & VertexAttribFlags::Color) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 341 | colorLocation = fill.program->getAttrib("colors"); |
| 342 | glEnableVertexAttribArray(colorLocation); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 343 | glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, |
| 344 | vertices.color); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 345 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 346 | int alphaLocation = -1; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 347 | if (vertices.attribFlags & VertexAttribFlags::Alpha) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 348 | // NOTE: alpha vertex position is computed assuming no VBO |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 349 | const void* alphaCoords = ((const GLbyte*)vertices.position) + kVertexAlphaOffset; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 350 | alphaLocation = fill.program->getAttrib("vtxAlpha"); |
| 351 | glEnableVertexAttribArray(alphaLocation); |
| 352 | glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 353 | } |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 354 | // Shader uniforms |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 355 | SkiaShader::apply(*mCaches, fill.skiaShaderData, mViewportWidth, mViewportHeight); |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 356 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 357 | GL_CHECKPOINT(MODERATE); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 358 | Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) |
| 359 | ? fill.skiaShaderData.bitmapData.bitmapTexture |
| 360 | : nullptr; |
Dohyun Lee | c5a3efd | 2016-01-21 13:46:21 +0900 | [diff] [blame] | 361 | const AutoTexture autoCleanup(texture); |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 362 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 363 | // If we have a shader and a base texture, the base texture is assumed to be an alpha mask |
| 364 | // which means the color space conversion applies to the shader's bitmap |
| 365 | Texture* colorSpaceTexture = texture != nullptr ? texture : fill.texture.texture; |
| 366 | if (colorSpaceTexture != nullptr) { |
| 367 | if (colorSpaceTexture->hasColorSpaceConversion()) { |
| 368 | const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 369 | glUniformMatrix3fv(fill.program->getUniform("colorSpaceMatrix"), 1, GL_FALSE, |
| 370 | connector->getTransform().asArray()); |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | TransferFunctionType transferFunction = colorSpaceTexture->getTransferFunctionType(); |
| 374 | if (transferFunction != TransferFunctionType::None) { |
| 375 | const ColorSpaceConnector* connector = colorSpaceTexture->getColorSpaceConnector(); |
| 376 | const ColorSpace& source = connector->getSource(); |
| 377 | |
| 378 | switch (transferFunction) { |
| 379 | case TransferFunctionType::None: |
| 380 | break; |
| 381 | case TransferFunctionType::Full: |
| 382 | glUniform1fv(fill.program->getUniform("transferFunction"), 7, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 383 | reinterpret_cast<const float*>(&source.getTransferParameters().g)); |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 384 | break; |
| 385 | case TransferFunctionType::Limited: |
| 386 | glUniform1fv(fill.program->getUniform("transferFunction"), 5, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 387 | reinterpret_cast<const float*>(&source.getTransferParameters().g)); |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 388 | break; |
| 389 | case TransferFunctionType::Gamma: |
| 390 | glUniform1f(fill.program->getUniform("transferFunctionGamma"), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 391 | source.getTransferParameters().g); |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 397 | // ------------------------------------ |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 398 | // ---------- GL state setup ---------- |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 399 | // ------------------------------------ |
Arun | 530a2b4 | 2017-01-23 12:47:57 +0000 | [diff] [blame] | 400 | if (CC_UNLIKELY(overrideDisableBlending)) { |
| 401 | blend().setFactors(GL_ZERO, GL_ZERO); |
| 402 | } else { |
| 403 | blend().setFactors(glop.blend.src, glop.blend.dst); |
| 404 | } |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 405 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 406 | GL_CHECKPOINT(MODERATE); |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 407 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 408 | // ------------------------------------ |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 409 | // ---------- Actual drawing ---------- |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 410 | // ------------------------------------ |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 411 | if (indices.bufferObject == meshState().getQuadListIBO()) { |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 412 | // Since the indexed quad list is of limited length, we loop over |
| 413 | // the glDrawXXX method while updating the vertex pointer |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 414 | GLsizei elementsCount = mesh.elementCount; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 415 | const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position); |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 416 | while (elementsCount > 0) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 417 | GLsizei drawCount = std::min(elementsCount, (GLsizei)kMaxNumberOfQuads * 6); |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 418 | GLsizei vertexCount = (drawCount / 6) * 4; |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame] | 419 | meshState().bindPositionVertexPointer(vertexData, vertices.stride); |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 420 | if (vertices.attribFlags & VertexAttribFlags::TextureCoord) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 421 | meshState().bindTexCoordsVertexPointer(vertexData + kMeshTextureOffset, |
| 422 | vertices.stride); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 425 | if (mCaches->extensions().getMajorGlVersion() >= 3) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 426 | glDrawRangeElements(mesh.primitiveMode, 0, vertexCount - 1, drawCount, |
| 427 | GL_UNSIGNED_SHORT, nullptr); |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 428 | } else { |
| 429 | glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr); |
| 430 | } |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 431 | elementsCount -= drawCount; |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 432 | vertexData += vertexCount * vertices.stride; |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 433 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 434 | } else if (indices.bufferObject || indices.indices) { |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 435 | if (mCaches->extensions().getMajorGlVersion() >= 3) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 436 | // use glDrawRangeElements to reduce CPU overhead (otherwise the driver has to determine |
| 437 | // the min/max index values) |
| 438 | glDrawRangeElements(mesh.primitiveMode, 0, mesh.vertexCount - 1, mesh.elementCount, |
| 439 | GL_UNSIGNED_SHORT, indices.indices); |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 440 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 441 | glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, |
| 442 | indices.indices); |
Arun | b0a9477 | 2017-01-23 11:41:06 +0000 | [diff] [blame] | 443 | } |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 444 | } else { |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 445 | glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 446 | } |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 447 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 448 | GL_CHECKPOINT(MODERATE); |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 449 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 450 | // ----------------------------------- |
| 451 | // ---------- Mesh teardown ---------- |
| 452 | // ----------------------------------- |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 453 | if (vertices.attribFlags & VertexAttribFlags::Alpha) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 454 | glDisableVertexAttribArray(alphaLocation); |
| 455 | } |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 456 | if (vertices.attribFlags & VertexAttribFlags::Color) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 457 | glDisableVertexAttribArray(colorLocation); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 458 | } |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 459 | |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 460 | GL_CHECKPOINT(MODERATE); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void RenderState::dump() { |
| 464 | blend().dump(); |
| 465 | meshState().dump(); |
| 466 | scissor().dump(); |
| 467 | stencil().dump(); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 468 | } |
| 469 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 470 | } /* namespace uirenderer */ |
| 471 | } /* namespace android */ |