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