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