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