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