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