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