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