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