Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
| 16 | #include "GlopBuilder.h" |
| 17 | |
| 18 | #include "Caches.h" |
| 19 | #include "Glop.h" |
| 20 | #include "Matrix.h" |
Chris Craik | 0556d90 | 2015-03-03 09:54:14 -0800 | [diff] [blame] | 21 | #include "Patch.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 22 | #include "renderstate/MeshState.h" |
| 23 | #include "renderstate/RenderState.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 24 | #include "SkiaShader.h" |
| 25 | #include "Texture.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 26 | #include "utils/PaintUtils.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 27 | #include "VertexBuffer.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 28 | |
| 29 | #include <GLES2/gl2.h> |
| 30 | #include <SkPaint.h> |
| 31 | |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 32 | #define DEBUG_GLOP_BUILDER 0 |
| 33 | |
| 34 | #if DEBUG_GLOP_BUILDER |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 35 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 36 | #define TRIGGER_STAGE(stageFlag) \ |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 37 | LOG_ALWAYS_FATAL_IF((stageFlag) & mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \ |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 38 | mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag)) |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 39 | |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 40 | #define REQUIRE_STAGES(requiredFlags) \ |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 41 | LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \ |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 42 | "not prepared for current stage") |
| 43 | |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 44 | #else |
| 45 | |
| 46 | #define TRIGGER_STAGE(stageFlag) ((void)0) |
| 47 | #define REQUIRE_STAGES(requiredFlags) ((void)0) |
| 48 | |
| 49 | #endif |
| 50 | |
| 51 | namespace android { |
| 52 | namespace uirenderer { |
| 53 | |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 54 | static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) { |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 55 | quadVertex[0] = {0, 0, uvs.left, uvs.top}; |
| 56 | quadVertex[1] = {1, 0, uvs.right, uvs.top}; |
| 57 | quadVertex[2] = {0, 1, uvs.left, uvs.bottom}; |
| 58 | quadVertex[3] = {1, 1, uvs.right, uvs.bottom}; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 61 | GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop) |
| 62 | : mRenderState(renderState) |
| 63 | , mCaches(caches) |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 64 | , mShader(nullptr) |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 65 | , mOutGlop(outGlop) { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 66 | mStageFlags = kInitialStage; |
| 67 | } |
| 68 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 69 | //////////////////////////////////////////////////////////////////////////////// |
| 70 | // Mesh |
| 71 | //////////////////////////////////////////////////////////////////////////////// |
| 72 | |
Chris Craik | 8d2cf94 | 2015-11-02 14:52:21 -0800 | [diff] [blame^] | 73 | GlopBuilder& GlopBuilder::setMeshTexturedIndexedVbo(GLuint vbo, GLsizei elementCount) { |
| 74 | TRIGGER_STAGE(kMeshStage); |
| 75 | |
| 76 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
| 77 | mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; |
| 78 | mOutGlop->mesh.vertices = { |
| 79 | vbo, |
| 80 | VertexAttribFlags::TextureCoord, |
| 81 | nullptr, nullptr, nullptr, |
| 82 | kTextureVertexStride }; |
| 83 | mOutGlop->mesh.elementCount = elementCount; |
| 84 | return *this; |
| 85 | } |
| 86 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 87 | GlopBuilder& GlopBuilder::setMeshUnitQuad() { |
| 88 | TRIGGER_STAGE(kMeshStage); |
| 89 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 90 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 91 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 92 | mOutGlop->mesh.vertices = { |
| 93 | mRenderState.meshState().getUnitQuadVBO(), |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 94 | VertexAttribFlags::None, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 95 | nullptr, nullptr, nullptr, |
| 96 | kTextureVertexStride }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 97 | mOutGlop->mesh.elementCount = 4; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 98 | return *this; |
| 99 | } |
| 100 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 101 | GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) { |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 102 | if (uvMapper) { |
| 103 | // can't use unit quad VBO, so build UV vertices manually |
| 104 | return setMeshTexturedUvQuad(uvMapper, Rect(0, 0, 1, 1)); |
| 105 | } |
| 106 | |
| 107 | TRIGGER_STAGE(kMeshStage); |
| 108 | |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 109 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 110 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 111 | mOutGlop->mesh.vertices = { |
| 112 | mRenderState.meshState().getUnitQuadVBO(), |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 113 | VertexAttribFlags::TextureCoord, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 114 | nullptr, (const void*) kMeshTextureOffset, nullptr, |
| 115 | kTextureVertexStride }; |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 116 | mOutGlop->mesh.elementCount = 4; |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 117 | return *this; |
| 118 | } |
| 119 | |
| 120 | GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 121 | TRIGGER_STAGE(kMeshStage); |
| 122 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 123 | if (CC_UNLIKELY(uvMapper)) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 124 | uvMapper->map(uvs); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 125 | } |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 126 | setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]); |
| 127 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 128 | const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices; |
| 129 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 130 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 131 | mOutGlop->mesh.vertices = { |
| 132 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 133 | VertexAttribFlags::TextureCoord, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 134 | &textureVertex[0].x, &textureVertex[0].u, nullptr, |
| 135 | kTextureVertexStride }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 136 | mOutGlop->mesh.elementCount = 4; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 137 | return *this; |
| 138 | } |
| 139 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 140 | GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 141 | TRIGGER_STAGE(kMeshStage); |
| 142 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 143 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 144 | mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; |
| 145 | mOutGlop->mesh.vertices = { |
| 146 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 147 | VertexAttribFlags::None, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 148 | vertexData, nullptr, nullptr, |
| 149 | kVertexStride }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 150 | mOutGlop->mesh.elementCount = 6 * quadCount; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 151 | return *this; |
| 152 | } |
| 153 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 154 | GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) { |
| 155 | TRIGGER_STAGE(kMeshStage); |
| 156 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 157 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 158 | mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; |
| 159 | mOutGlop->mesh.vertices = { |
| 160 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 161 | VertexAttribFlags::TextureCoord, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 162 | &vertexData[0].x, &vertexData[0].u, nullptr, |
| 163 | kTextureVertexStride }; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 164 | mOutGlop->mesh.elementCount = elementCount; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 165 | return *this; |
| 166 | } |
| 167 | |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 168 | GlopBuilder& GlopBuilder::setMeshTexturedMesh(TextureVertex* vertexData, int elementCount) { |
| 169 | TRIGGER_STAGE(kMeshStage); |
| 170 | |
| 171 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
| 172 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 173 | mOutGlop->mesh.vertices = { |
| 174 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 175 | VertexAttribFlags::TextureCoord, |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 176 | &vertexData[0].x, &vertexData[0].u, nullptr, |
| 177 | kTextureVertexStride }; |
| 178 | mOutGlop->mesh.elementCount = elementCount; |
| 179 | return *this; |
| 180 | } |
| 181 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 182 | GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount) { |
| 183 | TRIGGER_STAGE(kMeshStage); |
| 184 | |
| 185 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
| 186 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 187 | mOutGlop->mesh.vertices = { |
| 188 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 189 | VertexAttribFlags::TextureCoord | VertexAttribFlags::Color, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 190 | &vertexData[0].x, &vertexData[0].u, &vertexData[0].r, |
| 191 | kColorTextureVertexStride }; |
| 192 | mOutGlop->mesh.elementCount = elementCount; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 193 | return *this; |
| 194 | } |
| 195 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 196 | GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) { |
| 197 | TRIGGER_STAGE(kMeshStage); |
| 198 | |
| 199 | const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags(); |
| 200 | |
| 201 | bool alphaVertex = flags & VertexBuffer::kAlpha; |
| 202 | bool indices = flags & VertexBuffer::kIndices; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 203 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 204 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 205 | mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() }; |
| 206 | mOutGlop->mesh.vertices = { |
| 207 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 208 | alphaVertex ? VertexAttribFlags::Alpha : VertexAttribFlags::None, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 209 | vertexBuffer.getBuffer(), nullptr, nullptr, |
| 210 | alphaVertex ? kAlphaVertexStride : kVertexStride }; |
| 211 | mOutGlop->mesh.elementCount = indices |
| 212 | ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount(); |
| 213 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 214 | mDescription.useShadowAlphaInterp = shadowInterp; |
| 215 | return *this; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Chris Craik | 0556d90 | 2015-03-03 09:54:14 -0800 | [diff] [blame] | 218 | GlopBuilder& GlopBuilder::setMeshPatchQuads(const Patch& patch) { |
| 219 | TRIGGER_STAGE(kMeshStage); |
| 220 | |
| 221 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
| 222 | mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; |
| 223 | mOutGlop->mesh.vertices = { |
| 224 | mCaches.patchCache.getMeshBuffer(), |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 225 | VertexAttribFlags::TextureCoord, |
Chris Craik | 8820fd1 | 2015-03-03 14:20:47 -0800 | [diff] [blame] | 226 | (void*)patch.positionOffset, (void*)patch.textureOffset, nullptr, |
Chris Craik | 0556d90 | 2015-03-03 09:54:14 -0800 | [diff] [blame] | 227 | kTextureVertexStride }; |
| 228 | mOutGlop->mesh.elementCount = patch.indexCount; |
| 229 | return *this; |
| 230 | } |
| 231 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 232 | //////////////////////////////////////////////////////////////////////////////// |
| 233 | // Fill |
| 234 | //////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 235 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 236 | void GlopBuilder::setFill(int color, float alphaScale, |
| 237 | SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage, |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 238 | const SkShader* shader, const SkColorFilter* colorFilter) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 239 | if (mode != SkXfermode::kClear_Mode) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 240 | float alpha = (SkColorGetA(color) / 255.0f) * alphaScale; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 241 | if (!shader) { |
| 242 | float colorScale = alpha / 255.0f; |
| 243 | mOutGlop->fill.color = { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 244 | colorScale * SkColorGetR(color), |
| 245 | colorScale * SkColorGetG(color), |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 246 | colorScale * SkColorGetB(color), |
| 247 | alpha |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 248 | }; |
| 249 | } else { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 250 | mOutGlop->fill.color = { 1, 1, 1, alpha }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 251 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 252 | } else { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 253 | mOutGlop->fill.color = { 0, 0, 0, 1 }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 254 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 255 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 256 | mOutGlop->blend = { GL_ZERO, GL_ZERO }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 257 | if (mOutGlop->fill.color.a < 1.0f |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 258 | || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha) |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 259 | || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend) |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 260 | || mOutGlop->roundRectClipState |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 261 | || PaintUtils::isBlendedShader(shader) |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 262 | || PaintUtils::isBlendedColorFilter(colorFilter) |
| 263 | || mode != SkXfermode::kSrcOver_Mode) { |
| 264 | if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) { |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 265 | Blend::getFactors(mode, modeUsage, |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 266 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 267 | } else { |
| 268 | // These blend modes are not supported by OpenGL directly and have |
| 269 | // to be implemented using shaders. Since the shader will perform |
| 270 | // the blending, don't enable GL blending off here |
| 271 | // If the blend mode cannot be implemented using shaders, fall |
| 272 | // back to the default SrcOver blend mode instead |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 273 | if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 274 | mDescription.framebufferMode = mode; |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 275 | mDescription.swapSrcDst = (modeUsage == Blend::ModeOrderSwap::Swap); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 276 | // blending in shader, don't enable |
| 277 | } else { |
| 278 | // unsupported |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 279 | Blend::getFactors(SkXfermode::kSrcOver_Mode, modeUsage, |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 280 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 281 | } |
| 282 | } |
| 283 | } |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 284 | mShader = shader; // shader resolved in ::build() |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 285 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 286 | if (colorFilter) { |
| 287 | SkColor color; |
| 288 | SkXfermode::Mode mode; |
| 289 | SkScalar srcColorMatrix[20]; |
| 290 | if (colorFilter->asColorMode(&color, &mode)) { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 291 | mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::ColorFilterMode::Blend; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 292 | mDescription.colorMode = mode; |
| 293 | |
| 294 | const float alpha = SkColorGetA(color) / 255.0f; |
| 295 | float colorScale = alpha / 255.0f; |
| 296 | mOutGlop->fill.filter.color = { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 297 | colorScale * SkColorGetR(color), |
| 298 | colorScale * SkColorGetG(color), |
| 299 | colorScale * SkColorGetB(color), |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 300 | alpha, |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 301 | }; |
| 302 | } else if (colorFilter->asColorMatrix(srcColorMatrix)) { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 303 | mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::ColorFilterMode::Matrix; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 304 | |
| 305 | float* colorMatrix = mOutGlop->fill.filter.matrix.matrix; |
| 306 | memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float)); |
| 307 | memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float)); |
| 308 | memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float)); |
| 309 | memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float)); |
| 310 | |
| 311 | // Skia uses the range [0..255] for the addition vector, but we need |
| 312 | // the [0..1] range to apply the vector in GLSL |
| 313 | float* colorVector = mOutGlop->fill.filter.matrix.vector; |
| 314 | colorVector[0] = srcColorMatrix[4] / 255.0f; |
| 315 | colorVector[1] = srcColorMatrix[9] / 255.0f; |
| 316 | colorVector[2] = srcColorMatrix[14] / 255.0f; |
| 317 | colorVector[3] = srcColorMatrix[19] / 255.0f; |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 318 | } else { |
| 319 | LOG_ALWAYS_FATAL("unsupported ColorFilter"); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 320 | } |
| 321 | } else { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 322 | mOutGlop->fill.filterMode = ProgramDescription::ColorFilterMode::None; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 323 | } |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 324 | } |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 325 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 326 | GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, |
| 327 | const int textureFillFlags, const SkPaint* paint, float alphaScale) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 328 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 329 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 330 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 331 | GLenum filter = (textureFillFlags & TextureFillFlags::ForceFilter) |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 332 | ? GL_LINEAR : PaintUtils::getFilter(paint); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 333 | mOutGlop->fill.texture = { &texture, |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 334 | GL_TEXTURE_2D, filter, GL_CLAMP_TO_EDGE, nullptr }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 335 | |
| 336 | if (paint) { |
| 337 | int color = paint->getColor(); |
| 338 | SkShader* shader = paint->getShader(); |
| 339 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 340 | if (!(textureFillFlags & TextureFillFlags::IsAlphaMaskTexture)) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 341 | // Texture defines color, so disable shaders, and reset all non-alpha color channels |
| 342 | color |= 0x00FFFFFF; |
| 343 | shader = nullptr; |
| 344 | } |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 345 | setFill(color, alphaScale, |
| 346 | PaintUtils::getXfermode(paint->getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 347 | shader, paint->getColorFilter()); |
| 348 | } else { |
| 349 | mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale }; |
| 350 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 351 | if (alphaScale < 1.0f |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 352 | || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha) |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 353 | || texture.blend |
| 354 | || mOutGlop->roundRectClipState) { |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 355 | Blend::getFactors(SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 356 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 357 | } else { |
| 358 | mOutGlop->blend = { GL_ZERO, GL_ZERO }; |
| 359 | } |
| 360 | } |
| 361 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 362 | if (textureFillFlags & TextureFillFlags::IsAlphaMaskTexture) { |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 363 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 364 | mDescription.hasAlpha8Texture = true; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 365 | } else { |
| 366 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 367 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 368 | return *this; |
| 369 | } |
| 370 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 371 | GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) { |
| 372 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 373 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 374 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 375 | mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 376 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 377 | setFill(paint.getColor(), alphaScale, |
| 378 | PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 379 | paint.getShader(), paint.getColorFilter()); |
| 380 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 381 | return *this; |
| 382 | } |
| 383 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 384 | GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture, |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 385 | const SkPaint& paint, float alphaScale) { |
| 386 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 387 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 388 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 389 | //specify invalid filter/clamp, since these are always static for PathTextures |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 390 | mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 391 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 392 | setFill(paint.getColor(), alphaScale, |
| 393 | PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 394 | paint.getShader(), paint.getColorFilter()); |
| 395 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 396 | mDescription.hasAlpha8Texture = true; |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 397 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 398 | return *this; |
| 399 | } |
| 400 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 401 | GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor, |
| 402 | const SkPaint& paint, float alphaScale) { |
| 403 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 404 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 405 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 406 | //specify invalid filter/clamp, since these are always static for ShadowTextures |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 407 | mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 408 | |
| 409 | const int ALPHA_BITMASK = SK_ColorBLACK; |
| 410 | const int COLOR_BITMASK = ~ALPHA_BITMASK; |
| 411 | if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) { |
| 412 | // shadow color is fully opaque: override its alpha with that of paint |
| 413 | shadowColor &= paint.getColor() | COLOR_BITMASK; |
| 414 | } |
| 415 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 416 | setFill(shadowColor, alphaScale, |
| 417 | PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 418 | paint.getShader(), paint.getColorFilter()); |
| 419 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 420 | mDescription.hasAlpha8Texture = true; |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 421 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
| 422 | return *this; |
| 423 | } |
| 424 | |
| 425 | GlopBuilder& GlopBuilder::setFillBlack() { |
| 426 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 427 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 428 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 429 | mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 430 | setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap, |
| 431 | nullptr, nullptr); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 432 | return *this; |
| 433 | } |
| 434 | |
| 435 | GlopBuilder& GlopBuilder::setFillClear() { |
| 436 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 437 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 438 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 439 | mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 440 | setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, Blend::ModeOrderSwap::NoSwap, |
| 441 | nullptr, nullptr); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 442 | return *this; |
| 443 | } |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 444 | |
| 445 | GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter, |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 446 | float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 447 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 448 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 449 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 450 | mOutGlop->fill.texture = { &texture, |
| 451 | GL_TEXTURE_2D, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr }; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 452 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 453 | setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 454 | |
| 455 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 456 | return *this; |
| 457 | } |
| 458 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 459 | GlopBuilder& GlopBuilder::setFillTextureLayer(Layer& layer, float alpha) { |
| 460 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 461 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 462 | |
| 463 | mOutGlop->fill.texture = { &(layer.getTexture()), |
| 464 | layer.getRenderTarget(), GL_LINEAR, GL_CLAMP_TO_EDGE, &layer.getTexTransform() }; |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 465 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 466 | setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap, |
| 467 | nullptr, layer.getColorFilter()); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 468 | |
| 469 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 470 | mDescription.hasTextureTransform = true; |
| 471 | return *this; |
| 472 | } |
| 473 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 474 | //////////////////////////////////////////////////////////////////////////////// |
| 475 | // Transform |
| 476 | //////////////////////////////////////////////////////////////////////////////// |
| 477 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 478 | GlopBuilder& GlopBuilder::setTransform(const Matrix4& canvas, const int transformFlags) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 479 | TRIGGER_STAGE(kTransformStage); |
| 480 | |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 481 | mOutGlop->transform.canvas = canvas; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 482 | mOutGlop->transform.transformFlags = transformFlags; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 483 | return *this; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 486 | //////////////////////////////////////////////////////////////////////////////// |
| 487 | // ModelView |
| 488 | //////////////////////////////////////////////////////////////////////////////// |
| 489 | |
| 490 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) { |
| 491 | TRIGGER_STAGE(kModelViewStage); |
| 492 | |
| 493 | mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f); |
| 494 | mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f); |
| 495 | mOutGlop->bounds = destination; |
| 496 | return *this; |
| 497 | } |
| 498 | |
| 499 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) { |
| 500 | TRIGGER_STAGE(kModelViewStage); |
| 501 | REQUIRE_STAGES(kTransformStage | kFillStage); |
| 502 | |
| 503 | float left = destination.left; |
| 504 | float top = destination.top; |
| 505 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 506 | const Matrix4& meshTransform = mOutGlop->transform.meshTransform(); |
| 507 | if (CC_LIKELY(meshTransform.isPureTranslate())) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 508 | // snap by adjusting the model view matrix |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 509 | const float translateX = meshTransform.getTranslateX(); |
| 510 | const float translateY = meshTransform.getTranslateY(); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 511 | |
| 512 | left = (int) floorf(left + translateX + 0.5f) - translateX; |
| 513 | top = (int) floorf(top + translateY + 0.5f) - translateY; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 514 | mOutGlop->fill.texture.filter = GL_NEAREST; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f); |
| 518 | mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f); |
| 519 | mOutGlop->bounds = destination; |
| 520 | return *this; |
| 521 | } |
| 522 | |
| 523 | GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) { |
| 524 | TRIGGER_STAGE(kModelViewStage); |
| 525 | |
| 526 | mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f); |
| 527 | mOutGlop->bounds = source; |
| 528 | mOutGlop->bounds.translate(offsetX, offsetY); |
| 529 | return *this; |
| 530 | } |
| 531 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 532 | GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) { |
| 533 | TRIGGER_STAGE(kModelViewStage); |
| 534 | REQUIRE_STAGES(kTransformStage | kFillStage); |
| 535 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 536 | const Matrix4& meshTransform = mOutGlop->transform.meshTransform(); |
| 537 | if (CC_LIKELY(meshTransform.isPureTranslate())) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 538 | // snap by adjusting the model view matrix |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 539 | const float translateX = meshTransform.getTranslateX(); |
| 540 | const float translateY = meshTransform.getTranslateY(); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 541 | |
| 542 | offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left; |
| 543 | offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top; |
| 544 | mOutGlop->fill.texture.filter = GL_NEAREST; |
| 545 | } |
| 546 | |
| 547 | mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f); |
Chris Craik | 8820fd1 | 2015-03-03 14:20:47 -0800 | [diff] [blame] | 548 | mOutGlop->bounds = source; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 549 | mOutGlop->bounds.translate(offsetX, offsetY); |
| 550 | return *this; |
| 551 | } |
| 552 | |
| 553 | //////////////////////////////////////////////////////////////////////////////// |
| 554 | // RoundRectClip |
| 555 | //////////////////////////////////////////////////////////////////////////////// |
| 556 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 557 | GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) { |
| 558 | TRIGGER_STAGE(kRoundRectClipStage); |
| 559 | |
| 560 | mOutGlop->roundRectClipState = roundRectClipState; |
| 561 | mDescription.hasRoundRectClip = roundRectClipState != nullptr; |
| 562 | return *this; |
| 563 | } |
| 564 | |
| 565 | //////////////////////////////////////////////////////////////////////////////// |
| 566 | // Build |
| 567 | //////////////////////////////////////////////////////////////////////////////// |
| 568 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 569 | void verify(const ProgramDescription& description, const Glop& glop) { |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 570 | if (glop.fill.texture.texture != nullptr) { |
| 571 | LOG_ALWAYS_FATAL_IF(((description.hasTexture && description.hasExternalTexture) |
| 572 | || (!description.hasTexture && !description.hasExternalTexture) |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 573 | || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) == 0)), |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 574 | "Texture %p, hT%d, hET %d, attribFlags %x", |
| 575 | glop.fill.texture.texture, |
| 576 | description.hasTexture, description.hasExternalTexture, |
| 577 | glop.mesh.vertices.attribFlags); |
| 578 | } else { |
| 579 | LOG_ALWAYS_FATAL_IF((description.hasTexture |
| 580 | || description.hasExternalTexture |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 581 | || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) != 0)), |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 582 | "No texture, hT%d, hET %d, attribFlags %x", |
| 583 | description.hasTexture, description.hasExternalTexture, |
| 584 | glop.mesh.vertices.attribFlags); |
| 585 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 586 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 587 | if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::Alpha) |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 588 | && glop.mesh.vertices.bufferObject) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 589 | LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible"); |
| 590 | } |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 591 | |
| 592 | if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) { |
| 593 | LOG_ALWAYS_FATAL("Texture transform incorrectly specified"); |
| 594 | } |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 597 | void GlopBuilder::build() { |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 598 | REQUIRE_STAGES(kAllStages); |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 599 | if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) { |
Chris Craik | 48f650c | 2015-03-10 11:03:39 -0700 | [diff] [blame] | 600 | if (mOutGlop->fill.texture.target == GL_TEXTURE_2D) { |
| 601 | mDescription.hasTexture = true; |
| 602 | } else { |
| 603 | mDescription.hasExternalTexture = true; |
| 604 | } |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 605 | } |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 606 | |
| 607 | mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Color; |
| 608 | mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 609 | |
Chris Craik | 8284073 | 2015-04-03 09:37:49 -0700 | [diff] [blame] | 610 | // Enable debug highlight when what we're about to draw is tested against |
| 611 | // the stencil buffer and if stencil highlight debugging is on |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 612 | mDescription.hasDebugHighlight = !Properties::debugOverdraw |
| 613 | && Properties::debugStencilClip == StencilClipDebug::ShowHighlight |
Chris Craik | 8284073 | 2015-04-03 09:37:49 -0700 | [diff] [blame] | 614 | && mRenderState.stencil().isTestEnabled(); |
| 615 | |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 616 | // serialize shader info into ShaderData |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 617 | GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 618 | |
| 619 | if (CC_LIKELY(!mShader)) { |
| 620 | mOutGlop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType; |
| 621 | } else { |
| 622 | Matrix4 shaderMatrix; |
| 623 | if (mOutGlop->transform.transformFlags & TransformFlags::MeshIgnoresCanvasTransform) { |
| 624 | // canvas level transform was built into the modelView and geometry, |
| 625 | // so the shader matrix must reverse this |
| 626 | shaderMatrix.loadInverse(mOutGlop->transform.canvas); |
| 627 | shaderMatrix.multiply(mOutGlop->transform.modelView); |
| 628 | } else { |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 629 | shaderMatrix = mOutGlop->transform.modelView; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 630 | } |
| 631 | SkiaShader::store(mCaches, *mShader, shaderMatrix, |
| 632 | &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData)); |
| 633 | } |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 634 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 635 | // duplicates ProgramCache's definition of color uniform presence |
| 636 | const bool singleColor = !mDescription.hasTexture |
| 637 | && !mDescription.hasExternalTexture |
| 638 | && !mDescription.hasGradient |
| 639 | && !mDescription.hasBitmap; |
| 640 | mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 641 | |
| 642 | verify(mDescription, *mOutGlop); |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 643 | |
| 644 | // Final step: populate program and map bounds into render target space |
| 645 | mOutGlop->fill.program = mCaches.programCache.get(mDescription); |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 646 | mOutGlop->transform.meshTransform().mapRect(mOutGlop->bounds); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 649 | void GlopBuilder::dump(const Glop& glop) { |
| 650 | ALOGD("Glop Mesh"); |
| 651 | const Glop::Mesh& mesh = glop.mesh; |
| 652 | ALOGD(" primitive mode: %d", mesh.primitiveMode); |
| 653 | ALOGD(" indices: buffer obj %x, indices %p", mesh.indices.bufferObject, mesh.indices.indices); |
| 654 | |
| 655 | const Glop::Mesh::Vertices& vertices = glop.mesh.vertices; |
| 656 | ALOGD(" vertices: buffer obj %x, flags %x, pos %p, tex %p, clr %p, stride %d", |
| 657 | vertices.bufferObject, vertices.attribFlags, |
| 658 | vertices.position, vertices.texCoord, vertices.color, vertices.stride); |
| 659 | ALOGD(" element count: %d", mesh.elementCount); |
| 660 | |
| 661 | ALOGD("Glop Fill"); |
| 662 | const Glop::Fill& fill = glop.fill; |
| 663 | ALOGD(" program %p", fill.program); |
| 664 | if (fill.texture.texture) { |
| 665 | ALOGD(" texture %p, target %d, filter %d, clamp %d", |
| 666 | fill.texture.texture, fill.texture.target, fill.texture.filter, fill.texture.clamp); |
| 667 | if (fill.texture.textureTransform) { |
| 668 | fill.texture.textureTransform->dump("texture transform"); |
| 669 | } |
| 670 | } |
| 671 | ALOGD_IF(fill.colorEnabled, " color (argb) %.2f %.2f %.2f %.2f", |
| 672 | fill.color.a, fill.color.r, fill.color.g, fill.color.b); |
| 673 | ALOGD_IF(fill.filterMode != ProgramDescription::ColorFilterMode::None, |
| 674 | " filterMode %d", (int)fill.filterMode); |
| 675 | ALOGD_IF(fill.skiaShaderData.skiaShaderType, " shader type %d", |
| 676 | fill.skiaShaderData.skiaShaderType); |
| 677 | |
| 678 | ALOGD("Glop transform"); |
| 679 | glop.transform.modelView.dump("model view"); |
| 680 | glop.transform.canvas.dump("canvas"); |
| 681 | |
| 682 | ALOGD("Glop blend %d %d", glop.blend.src, glop.blend.dst); |
| 683 | ALOGD("Glop bounds " RECT_STRING, RECT_ARGS(glop.bounds)); |
| 684 | } |
| 685 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 686 | } /* namespace uirenderer */ |
| 687 | } /* namespace android */ |