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