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 | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 16 | #include <GpuMemoryTracker.h> |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 17 | #include "renderstate/RenderState.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 18 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 19 | #include "renderthread/CanvasContext.h" |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 20 | #include "renderthread/EglManager.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 21 | #include "utils/GLUtils.h" |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 27 | RenderState::RenderState(renderthread::RenderThread& thread) |
| 28 | : mRenderThread(thread) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 29 | , mViewportWidth(0) |
| 30 | , mViewportHeight(0) |
| 31 | , mFramebuffer(0) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 32 | mThreadId = pthread_self(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | RenderState::~RenderState() { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 36 | LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 37 | "State object lifecycle not managed correctly"); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void RenderState::onGLContextCreated() { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 41 | LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil, |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 42 | "State object lifecycle not managed correctly"); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 43 | GpuMemoryTracker::onGLContextCreated(); |
| 44 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 45 | mBlend = new Blend(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 46 | mMeshState = new MeshState(); |
| 47 | mScissor = new Scissor(); |
| 48 | mStencil = new Stencil(); |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 49 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 50 | // This is delayed because the first access of Caches makes GL calls |
Chris Craik | ff5c8e8 | 2015-01-30 09:46:18 -0800 | [diff] [blame] | 51 | if (!mCaches) { |
| 52 | mCaches = &Caches::createInstance(*this); |
| 53 | } |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 54 | mCaches->init(); |
| 55 | mCaches->textureCache.setAssetAtlas(&mAssetAtlas); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 56 | } |
| 57 | |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 58 | static void layerLostGlContext(Layer* layer) { |
| 59 | layer->onGlContextLost(); |
| 60 | } |
| 61 | |
Chris Craik | 1d47742 | 2014-08-26 17:30:15 -0700 | [diff] [blame] | 62 | void RenderState::onGLContextDestroyed() { |
Chris Craik | 21029ef | 2014-09-12 09:29:10 -0700 | [diff] [blame] | 63 | /* |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 64 | size_t size = mActiveLayers.size(); |
| 65 | if (CC_UNLIKELY(size != 0)) { |
| 66 | ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d", |
| 67 | mRegisteredContexts.size(), size, mActiveLayers.empty()); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 68 | mCaches->dumpMemoryUsage(); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 69 | for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin(); |
| 70 | cit != mRegisteredContexts.end(); cit++) { |
| 71 | renderthread::CanvasContext* context = *cit; |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 72 | ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get()); |
| 73 | ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size()); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 74 | for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin(); |
| 75 | pit != context->mPrefetechedLayers.end(); pit++) { |
| 76 | (*pit)->debugDumpLayers(" "); |
| 77 | } |
| 78 | context->mRootRenderNode->debugDumpLayers(" "); |
| 79 | } |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 80 | |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 81 | |
| 82 | if (mActiveLayers.begin() == mActiveLayers.end()) { |
| 83 | ALOGE("set has become empty. wat."); |
| 84 | } |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 85 | for (std::set<const Layer*>::iterator lit = mActiveLayers.begin(); |
| 86 | lit != mActiveLayers.end(); lit++) { |
| 87 | const Layer* layer = *(lit); |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 88 | ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d", |
| 89 | layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered); |
Chris Craik | 599e254 | 2014-09-05 15:17:11 -0700 | [diff] [blame] | 90 | } |
Chris Craik | bfd1cd6 | 2014-09-10 13:04:31 -0700 | [diff] [blame] | 91 | LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size); |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 92 | } |
Chris Craik | 21029ef | 2014-09-12 09:29:10 -0700 | [diff] [blame] | 93 | */ |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 94 | |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 95 | mLayerPool.clear(); |
| 96 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 97 | // TODO: reset all cached state in state objects |
John Reck | 49bc4ac | 2015-01-29 12:53:38 -0800 | [diff] [blame] | 98 | std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext); |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 99 | mAssetAtlas.terminate(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 100 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 101 | mCaches->terminate(); |
| 102 | |
| 103 | delete mBlend; |
| 104 | mBlend = nullptr; |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 105 | delete mMeshState; |
| 106 | mMeshState = nullptr; |
| 107 | delete mScissor; |
| 108 | mScissor = nullptr; |
| 109 | delete mStencil; |
| 110 | mStencil = nullptr; |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 111 | |
| 112 | GpuMemoryTracker::onGLContextDestroyed(); |
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: |
| 118 | // fall through |
| 119 | case Caches::FlushMode::Moderate: |
| 120 | // fall through |
| 121 | case Caches::FlushMode::Layers: |
| 122 | mLayerPool.clear(); |
| 123 | break; |
| 124 | } |
| 125 | mCaches->flush(mode); |
| 126 | } |
| 127 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 128 | void RenderState::setViewport(GLsizei width, GLsizei height) { |
| 129 | mViewportWidth = width; |
| 130 | mViewportHeight = height; |
| 131 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) { |
| 136 | *outWidth = mViewportWidth; |
| 137 | *outHeight = mViewportHeight; |
| 138 | } |
| 139 | |
| 140 | void RenderState::bindFramebuffer(GLuint fbo) { |
| 141 | if (mFramebuffer != fbo) { |
| 142 | mFramebuffer = fbo; |
| 143 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 144 | } |
| 145 | } |
| 146 | |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 147 | GLuint RenderState::genFramebuffer() { |
| 148 | GLuint ret; |
| 149 | glGenFramebuffers(1, &ret); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | void RenderState::deleteFramebuffer(GLuint fbo) { |
| 154 | if (mFramebuffer == fbo) { |
| 155 | // GL defines that deleting the currently bound FBO rebinds FBO 0. |
| 156 | // Reflect this in our cached value. |
| 157 | mFramebuffer = 0; |
| 158 | } |
| 159 | glDeleteFramebuffers(1, &fbo); |
| 160 | } |
| 161 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 162 | void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) { |
John Reck | 95cd24b | 2015-08-04 11:17:39 -0700 | [diff] [blame] | 163 | if (mode == DrawGlInfo::kModeProcessNoContext) { |
| 164 | // If there's no context we don't need to interrupt as there's |
| 165 | // no gl state to save/restore |
| 166 | (*functor)(mode, info); |
| 167 | } else { |
| 168 | interruptForFunctorInvoke(); |
| 169 | (*functor)(mode, info); |
| 170 | resumeFromFunctorInvoke(); |
| 171 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void RenderState::interruptForFunctorInvoke() { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 175 | mCaches->setProgram(nullptr); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 176 | mCaches->textureState().resetActiveTexture(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 177 | meshState().unbindMeshBuffer(); |
| 178 | meshState().unbindIndicesBuffer(); |
| 179 | meshState().resetVertexPointers(); |
| 180 | meshState().disableTexCoordsVertexArray(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 181 | debugOverdraw(false, false); |
| 182 | } |
| 183 | |
| 184 | void RenderState::resumeFromFunctorInvoke() { |
| 185 | glViewport(0, 0, mViewportWidth, mViewportHeight); |
| 186 | glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer); |
| 187 | debugOverdraw(false, false); |
| 188 | |
| 189 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 190 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 191 | scissor().invalidate(); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 192 | blend().invalidate(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 193 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 194 | mCaches->textureState().activateTexture(0); |
| 195 | mCaches->textureState().resetBoundTextures(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void RenderState::debugOverdraw(bool enable, bool clear) { |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 199 | if (Properties::debugOverdraw && mFramebuffer == 0) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 200 | if (clear) { |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 201 | scissor().setEnabled(false); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 202 | stencil().clear(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 203 | } |
| 204 | if (enable) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 205 | stencil().enableDebugWrite(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 206 | } else { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 207 | stencil().disable(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 212 | class DecStrongTask : public renderthread::RenderTask { |
| 213 | public: |
| 214 | DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} |
| 215 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 216 | virtual void run() override { |
| 217 | mObject->decStrong(nullptr); |
| 218 | mObject = nullptr; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 219 | delete this; |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | VirtualLightRefBase* mObject; |
| 224 | }; |
| 225 | |
| 226 | void RenderState::postDecStrong(VirtualLightRefBase* object) { |
John Reck | a55b5d6 | 2016-01-14 15:09:10 -0800 | [diff] [blame] | 227 | if (pthread_equal(mThreadId, pthread_self())) { |
| 228 | object->decStrong(nullptr); |
| 229 | } else { |
| 230 | mRenderThread.queue(new DecStrongTask(object)); |
| 231 | } |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 234 | /////////////////////////////////////////////////////////////////////////////// |
| 235 | // Render |
| 236 | /////////////////////////////////////////////////////////////////////////////// |
| 237 | |
Chris Craik | 12efe64 | 2015-09-28 15:52:12 -0700 | [diff] [blame] | 238 | void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix) { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 239 | const Glop::Mesh& mesh = glop.mesh; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 240 | const Glop::Mesh::Vertices& vertices = mesh.vertices; |
| 241 | const Glop::Mesh::Indices& indices = mesh.indices; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 242 | const Glop::Fill& fill = glop.fill; |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 243 | |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame^] | 244 | GL_CHECKPOINT(); |
| 245 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 246 | // --------------------------------------------- |
| 247 | // ---------- Program + uniform setup ---------- |
| 248 | // --------------------------------------------- |
| 249 | mCaches->setProgram(fill.program); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 250 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 251 | if (fill.colorEnabled) { |
| 252 | fill.program->setColor(fill.color); |
| 253 | } |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 254 | |
Chris Craik | 12efe64 | 2015-09-28 15:52:12 -0700 | [diff] [blame] | 255 | fill.program->set(orthoMatrix, |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 256 | glop.transform.modelView, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 257 | glop.transform.meshTransform(), |
| 258 | glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 259 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 260 | // Color filter uniforms |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 261 | if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 262 | const FloatColor& color = fill.filter.color; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 263 | glUniform4f(mCaches->program().getUniform("colorBlend"), |
| 264 | color.r, color.g, color.b, color.a); |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 265 | } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 266 | glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 267 | fill.filter.matrix.matrix); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 268 | glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 269 | fill.filter.matrix.vector); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 272 | // Round rect clipping uniforms |
| 273 | if (glop.roundRectClipState) { |
| 274 | // TODO: avoid query, and cache values (or RRCS ptr) in program |
| 275 | const RoundRectClipState* state = glop.roundRectClipState; |
| 276 | const Rect& innerRect = state->innerRect; |
| 277 | glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"), |
| 278 | innerRect.left, innerRect.top, |
| 279 | innerRect.right, innerRect.bottom); |
| 280 | glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"), |
| 281 | 1, GL_FALSE, &state->matrix.data[0]); |
| 282 | |
| 283 | // add half pixel to round out integer rect space to cover pixel centers |
| 284 | float roundedOutRadius = state->radius + 0.5f; |
| 285 | glUniform1f(fill.program->getUniform("roundRectRadius"), |
| 286 | roundedOutRadius); |
| 287 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 288 | |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame^] | 289 | GL_CHECKPOINT(); |
| 290 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 291 | // -------------------------------- |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 292 | // ---------- Mesh setup ---------- |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 293 | // -------------------------------- |
| 294 | // vertices |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 295 | const bool force = meshState().bindMeshBufferInternal(vertices.bufferObject) |
| 296 | || (vertices.position != nullptr); |
| 297 | meshState().bindPositionVertexPointer(force, vertices.position, vertices.stride); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 298 | |
| 299 | // indices |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 300 | meshState().bindIndicesBufferInternal(indices.bufferObject); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 301 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 302 | if (vertices.attribFlags & VertexAttribFlags::TextureCoord) { |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 303 | const Glop::Fill::TextureData& texture = fill.texture; |
| 304 | // texture always takes slot 0, shader samplers increment from there |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 305 | mCaches->textureState().activateTexture(0); |
| 306 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 307 | if (texture.clamp != GL_INVALID_ENUM) { |
Chris Craik | 5f1356c | 2015-05-27 11:06:21 -0700 | [diff] [blame] | 308 | texture.texture->setWrap(texture.clamp, true, false, texture.target); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 309 | } |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 310 | if (texture.filter != GL_INVALID_ENUM) { |
Chris Craik | 5f1356c | 2015-05-27 11:06:21 -0700 | [diff] [blame] | 311 | texture.texture->setFilter(texture.filter, true, false, texture.target); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 312 | } |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 313 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 314 | mCaches->textureState().bindTexture(texture.target, texture.texture->id()); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 315 | meshState().enableTexCoordsVertexArray(); |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 316 | meshState().bindTexCoordsVertexPointer(force, vertices.texCoord, vertices.stride); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 317 | |
| 318 | if (texture.textureTransform) { |
| 319 | glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1, |
| 320 | GL_FALSE, &texture.textureTransform->data[0]); |
| 321 | } |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 322 | } else { |
| 323 | meshState().disableTexCoordsVertexArray(); |
| 324 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 325 | int colorLocation = -1; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 326 | if (vertices.attribFlags & VertexAttribFlags::Color) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 327 | colorLocation = fill.program->getAttrib("colors"); |
| 328 | glEnableVertexAttribArray(colorLocation); |
| 329 | glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 330 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 331 | int alphaLocation = -1; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 332 | if (vertices.attribFlags & VertexAttribFlags::Alpha) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 333 | // NOTE: alpha vertex position is computed assuming no VBO |
| 334 | const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset; |
| 335 | alphaLocation = fill.program->getAttrib("vtxAlpha"); |
| 336 | glEnableVertexAttribArray(alphaLocation); |
| 337 | glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 338 | } |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 339 | // Shader uniforms |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 340 | SkiaShader::apply(*mCaches, fill.skiaShaderData); |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 341 | |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame^] | 342 | GL_CHECKPOINT(); |
| 343 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 344 | // ------------------------------------ |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 345 | // ---------- GL state setup ---------- |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 346 | // ------------------------------------ |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 347 | blend().setFactors(glop.blend.src, glop.blend.dst); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 348 | |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame^] | 349 | GL_CHECKPOINT(); |
| 350 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 351 | // ------------------------------------ |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 352 | // ---------- Actual drawing ---------- |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 353 | // ------------------------------------ |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 354 | if (indices.bufferObject == meshState().getQuadListIBO()) { |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 355 | // Since the indexed quad list is of limited length, we loop over |
| 356 | // the glDrawXXX method while updating the vertex pointer |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 357 | GLsizei elementsCount = mesh.elementCount; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 358 | const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position); |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 359 | while (elementsCount > 0) { |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 360 | GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6); |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 361 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 362 | // rebind pointers without forcing, since initial bind handled above |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 363 | meshState().bindPositionVertexPointer(false, vertexData, vertices.stride); |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 364 | if (vertices.attribFlags & VertexAttribFlags::TextureCoord) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 365 | meshState().bindTexCoordsVertexPointer(false, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 366 | vertexData + kMeshTextureOffset, vertices.stride); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 369 | glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr); |
| 370 | elementsCount -= drawCount; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 371 | vertexData += (drawCount / 6) * 4 * vertices.stride; |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 372 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 373 | } else if (indices.bufferObject || indices.indices) { |
| 374 | glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 375 | } else { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 376 | glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 377 | } |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 378 | |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame^] | 379 | GL_CHECKPOINT(); |
| 380 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 381 | // ----------------------------------- |
| 382 | // ---------- Mesh teardown ---------- |
| 383 | // ----------------------------------- |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 384 | if (vertices.attribFlags & VertexAttribFlags::Alpha) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 385 | glDisableVertexAttribArray(alphaLocation); |
| 386 | } |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 387 | if (vertices.attribFlags & VertexAttribFlags::Color) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 388 | glDisableVertexAttribArray(colorLocation); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 389 | } |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame^] | 390 | |
| 391 | GL_CHECKPOINT(); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void RenderState::dump() { |
| 395 | blend().dump(); |
| 396 | meshState().dump(); |
| 397 | scissor().dump(); |
| 398 | stencil().dump(); |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 399 | } |
| 400 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 401 | } /* namespace uirenderer */ |
| 402 | } /* namespace android */ |