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 | |
| 73 | GlopBuilder& GlopBuilder::setMeshUnitQuad() { |
| 74 | TRIGGER_STAGE(kMeshStage); |
| 75 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 76 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 77 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 78 | mOutGlop->mesh.vertices = { |
| 79 | mRenderState.meshState().getUnitQuadVBO(), |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 80 | VertexAttribFlags::None, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 81 | nullptr, nullptr, nullptr, |
| 82 | kTextureVertexStride }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 83 | mOutGlop->mesh.elementCount = 4; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 84 | return *this; |
| 85 | } |
| 86 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 87 | GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) { |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 88 | if (uvMapper) { |
| 89 | // can't use unit quad VBO, so build UV vertices manually |
| 90 | return setMeshTexturedUvQuad(uvMapper, Rect(0, 0, 1, 1)); |
| 91 | } |
| 92 | |
| 93 | TRIGGER_STAGE(kMeshStage); |
| 94 | |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 95 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 96 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 97 | mOutGlop->mesh.vertices = { |
| 98 | mRenderState.meshState().getUnitQuadVBO(), |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 99 | VertexAttribFlags::TextureCoord, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 100 | nullptr, (const void*) kMeshTextureOffset, nullptr, |
| 101 | kTextureVertexStride }; |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 102 | mOutGlop->mesh.elementCount = 4; |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 107 | TRIGGER_STAGE(kMeshStage); |
| 108 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 109 | if (CC_UNLIKELY(uvMapper)) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 110 | uvMapper->map(uvs); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 111 | } |
Chris Craik | 14100ac | 2015-02-24 13:46:29 -0800 | [diff] [blame] | 112 | setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]); |
| 113 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 114 | const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices; |
| 115 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 116 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 117 | mOutGlop->mesh.vertices = { |
| 118 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 119 | VertexAttribFlags::TextureCoord, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 120 | &textureVertex[0].x, &textureVertex[0].u, nullptr, |
| 121 | kTextureVertexStride }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 122 | mOutGlop->mesh.elementCount = 4; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 123 | return *this; |
| 124 | } |
| 125 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 126 | GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 127 | TRIGGER_STAGE(kMeshStage); |
| 128 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 129 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 130 | mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; |
| 131 | mOutGlop->mesh.vertices = { |
| 132 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 133 | VertexAttribFlags::None, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 134 | vertexData, nullptr, nullptr, |
| 135 | kVertexStride }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 136 | mOutGlop->mesh.elementCount = 6 * quadCount; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 137 | return *this; |
| 138 | } |
| 139 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 140 | GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) { |
| 141 | TRIGGER_STAGE(kMeshStage); |
| 142 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -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::TextureCoord, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 148 | &vertexData[0].x, &vertexData[0].u, nullptr, |
| 149 | kTextureVertexStride }; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 150 | mOutGlop->mesh.elementCount = elementCount; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 151 | return *this; |
| 152 | } |
| 153 | |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 154 | GlopBuilder& GlopBuilder::setMeshTexturedMesh(TextureVertex* vertexData, int elementCount) { |
| 155 | TRIGGER_STAGE(kMeshStage); |
| 156 | |
| 157 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
| 158 | mOutGlop->mesh.indices = { 0, nullptr }; |
| 159 | mOutGlop->mesh.vertices = { |
| 160 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 161 | VertexAttribFlags::TextureCoord, |
Chris Craik | a6b5219 | 2015-02-27 17:43:02 -0800 | [diff] [blame] | 162 | &vertexData[0].x, &vertexData[0].u, nullptr, |
| 163 | kTextureVertexStride }; |
| 164 | mOutGlop->mesh.elementCount = elementCount; |
| 165 | return *this; |
| 166 | } |
| 167 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 168 | GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* 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 | VertexAttribFlags::Color, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 176 | &vertexData[0].x, &vertexData[0].u, &vertexData[0].r, |
| 177 | kColorTextureVertexStride }; |
| 178 | mOutGlop->mesh.elementCount = elementCount; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 179 | return *this; |
| 180 | } |
| 181 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 182 | GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) { |
| 183 | TRIGGER_STAGE(kMeshStage); |
| 184 | |
| 185 | const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags(); |
| 186 | |
| 187 | bool alphaVertex = flags & VertexBuffer::kAlpha; |
| 188 | bool indices = flags & VertexBuffer::kIndices; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 189 | |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 190 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 191 | mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() }; |
| 192 | mOutGlop->mesh.vertices = { |
| 193 | 0, |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 194 | alphaVertex ? VertexAttribFlags::Alpha : VertexAttribFlags::None, |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 195 | vertexBuffer.getBuffer(), nullptr, nullptr, |
| 196 | alphaVertex ? kAlphaVertexStride : kVertexStride }; |
| 197 | mOutGlop->mesh.elementCount = indices |
| 198 | ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount(); |
| 199 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 200 | mDescription.useShadowAlphaInterp = shadowInterp; |
| 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 | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 357 | GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) { |
| 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 | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 361 | mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 362 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 363 | setFill(paint.getColor(), alphaScale, |
| 364 | PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 365 | paint.getShader(), paint.getColorFilter()); |
| 366 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 367 | return *this; |
| 368 | } |
| 369 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 370 | GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture, |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 371 | const SkPaint& paint, float alphaScale) { |
| 372 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 373 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 374 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 375 | //specify invalid filter/clamp, since these are always static for PathTextures |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 376 | 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] | 377 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 378 | setFill(paint.getColor(), alphaScale, |
| 379 | PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 380 | paint.getShader(), paint.getColorFilter()); |
| 381 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 382 | mDescription.hasAlpha8Texture = true; |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 383 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 384 | return *this; |
| 385 | } |
| 386 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 387 | GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor, |
| 388 | const SkPaint& paint, float alphaScale) { |
| 389 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 390 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 391 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 392 | //specify invalid filter/clamp, since these are always static for ShadowTextures |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 393 | 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] | 394 | |
| 395 | const int ALPHA_BITMASK = SK_ColorBLACK; |
| 396 | const int COLOR_BITMASK = ~ALPHA_BITMASK; |
| 397 | if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) { |
| 398 | // shadow color is fully opaque: override its alpha with that of paint |
| 399 | shadowColor &= paint.getColor() | COLOR_BITMASK; |
| 400 | } |
| 401 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 402 | setFill(shadowColor, alphaScale, |
| 403 | PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 404 | paint.getShader(), paint.getColorFilter()); |
| 405 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 406 | mDescription.hasAlpha8Texture = true; |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 407 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
| 408 | return *this; |
| 409 | } |
| 410 | |
| 411 | GlopBuilder& GlopBuilder::setFillBlack() { |
| 412 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 413 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 414 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 415 | 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] | 416 | setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap, |
| 417 | nullptr, nullptr); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 418 | return *this; |
| 419 | } |
| 420 | |
| 421 | GlopBuilder& GlopBuilder::setFillClear() { |
| 422 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 423 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 424 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 425 | 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] | 426 | setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, Blend::ModeOrderSwap::NoSwap, |
| 427 | nullptr, nullptr); |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame] | 428 | return *this; |
| 429 | } |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 430 | |
| 431 | GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter, |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 432 | float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 433 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 434 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 435 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 436 | mOutGlop->fill.texture = { &texture, |
| 437 | GL_TEXTURE_2D, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr }; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 438 | mOutGlop->fill.color = { alpha, alpha, alpha, alpha }; |
| 439 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 440 | setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 441 | |
| 442 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 443 | return *this; |
| 444 | } |
| 445 | |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 446 | GlopBuilder& GlopBuilder::setFillTextureLayer(Layer& layer, float alpha) { |
| 447 | TRIGGER_STAGE(kFillStage); |
Chris Craik | b1f990d | 2015-06-12 11:28:52 -0700 | [diff] [blame] | 448 | REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 449 | |
| 450 | mOutGlop->fill.texture = { &(layer.getTexture()), |
| 451 | layer.getRenderTarget(), GL_LINEAR, GL_CLAMP_TO_EDGE, &layer.getTexTransform() }; |
| 452 | mOutGlop->fill.color = { alpha, alpha, alpha, alpha }; |
| 453 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 454 | setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap, |
| 455 | nullptr, layer.getColorFilter()); |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 456 | |
| 457 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 458 | mDescription.hasTextureTransform = true; |
| 459 | return *this; |
| 460 | } |
| 461 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 462 | //////////////////////////////////////////////////////////////////////////////// |
| 463 | // Transform |
| 464 | //////////////////////////////////////////////////////////////////////////////// |
| 465 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 466 | void GlopBuilder::setTransform(const Matrix4& ortho, const Matrix4& canvas, |
| 467 | const int transformFlags) { |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 468 | TRIGGER_STAGE(kTransformStage); |
| 469 | |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 470 | mOutGlop->transform.ortho = ortho; |
| 471 | mOutGlop->transform.canvas = canvas; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 472 | mOutGlop->transform.transformFlags = transformFlags; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 475 | //////////////////////////////////////////////////////////////////////////////// |
| 476 | // ModelView |
| 477 | //////////////////////////////////////////////////////////////////////////////// |
| 478 | |
| 479 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) { |
| 480 | TRIGGER_STAGE(kModelViewStage); |
| 481 | |
| 482 | mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f); |
| 483 | mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f); |
| 484 | mOutGlop->bounds = destination; |
| 485 | return *this; |
| 486 | } |
| 487 | |
| 488 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) { |
| 489 | TRIGGER_STAGE(kModelViewStage); |
| 490 | REQUIRE_STAGES(kTransformStage | kFillStage); |
| 491 | |
| 492 | float left = destination.left; |
| 493 | float top = destination.top; |
| 494 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 495 | const Matrix4& meshTransform = mOutGlop->transform.meshTransform(); |
| 496 | if (CC_LIKELY(meshTransform.isPureTranslate())) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 497 | // snap by adjusting the model view matrix |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 498 | const float translateX = meshTransform.getTranslateX(); |
| 499 | const float translateY = meshTransform.getTranslateY(); |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 500 | |
| 501 | left = (int) floorf(left + translateX + 0.5f) - translateX; |
| 502 | top = (int) floorf(top + translateY + 0.5f) - translateY; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 503 | mOutGlop->fill.texture.filter = GL_NEAREST; |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f); |
| 507 | mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f); |
| 508 | mOutGlop->bounds = destination; |
| 509 | return *this; |
| 510 | } |
| 511 | |
| 512 | GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) { |
| 513 | TRIGGER_STAGE(kModelViewStage); |
| 514 | |
| 515 | mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f); |
| 516 | mOutGlop->bounds = source; |
| 517 | mOutGlop->bounds.translate(offsetX, offsetY); |
| 518 | return *this; |
| 519 | } |
| 520 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 521 | GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) { |
| 522 | TRIGGER_STAGE(kModelViewStage); |
| 523 | REQUIRE_STAGES(kTransformStage | kFillStage); |
| 524 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 525 | const Matrix4& meshTransform = mOutGlop->transform.meshTransform(); |
| 526 | if (CC_LIKELY(meshTransform.isPureTranslate())) { |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 527 | // snap by adjusting the model view matrix |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 528 | const float translateX = meshTransform.getTranslateX(); |
| 529 | const float translateY = meshTransform.getTranslateY(); |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 530 | |
| 531 | offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left; |
| 532 | offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top; |
| 533 | mOutGlop->fill.texture.filter = GL_NEAREST; |
| 534 | } |
| 535 | |
| 536 | mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f); |
Chris Craik | 8820fd1 | 2015-03-03 14:20:47 -0800 | [diff] [blame] | 537 | mOutGlop->bounds = source; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 538 | mOutGlop->bounds.translate(offsetX, offsetY); |
| 539 | return *this; |
| 540 | } |
| 541 | |
| 542 | //////////////////////////////////////////////////////////////////////////////// |
| 543 | // RoundRectClip |
| 544 | //////////////////////////////////////////////////////////////////////////////// |
| 545 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 546 | GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) { |
| 547 | TRIGGER_STAGE(kRoundRectClipStage); |
| 548 | |
| 549 | mOutGlop->roundRectClipState = roundRectClipState; |
| 550 | mDescription.hasRoundRectClip = roundRectClipState != nullptr; |
| 551 | return *this; |
| 552 | } |
| 553 | |
| 554 | //////////////////////////////////////////////////////////////////////////////// |
| 555 | // Build |
| 556 | //////////////////////////////////////////////////////////////////////////////// |
| 557 | |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 558 | void verify(const ProgramDescription& description, const Glop& glop) { |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 559 | if (glop.fill.texture.texture != nullptr) { |
| 560 | LOG_ALWAYS_FATAL_IF(((description.hasTexture && description.hasExternalTexture) |
| 561 | || (!description.hasTexture && !description.hasExternalTexture) |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 562 | || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) == 0)), |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 563 | "Texture %p, hT%d, hET %d, attribFlags %x", |
| 564 | glop.fill.texture.texture, |
| 565 | description.hasTexture, description.hasExternalTexture, |
| 566 | glop.mesh.vertices.attribFlags); |
| 567 | } else { |
| 568 | LOG_ALWAYS_FATAL_IF((description.hasTexture |
| 569 | || description.hasExternalTexture |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 570 | || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) != 0)), |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 571 | "No texture, hT%d, hET %d, attribFlags %x", |
| 572 | description.hasTexture, description.hasExternalTexture, |
| 573 | glop.mesh.vertices.attribFlags); |
| 574 | } |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 575 | |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 576 | if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::Alpha) |
Chris Craik | eb911c2 | 2015-03-06 17:30:11 -0800 | [diff] [blame] | 577 | && glop.mesh.vertices.bufferObject) { |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 578 | LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible"); |
| 579 | } |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 580 | |
| 581 | if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) { |
| 582 | LOG_ALWAYS_FATAL("Texture transform incorrectly specified"); |
| 583 | } |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 586 | void GlopBuilder::build() { |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 587 | REQUIRE_STAGES(kAllStages); |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 588 | if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) { |
Chris Craik | 48f650c | 2015-03-10 11:03:39 -0700 | [diff] [blame] | 589 | if (mOutGlop->fill.texture.target == GL_TEXTURE_2D) { |
| 590 | mDescription.hasTexture = true; |
| 591 | } else { |
| 592 | mDescription.hasExternalTexture = true; |
| 593 | } |
Chris Craik | 26bf342 | 2015-02-26 16:28:17 -0800 | [diff] [blame] | 594 | } |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 595 | |
| 596 | mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Color; |
| 597 | mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha; |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 598 | |
Chris Craik | 8284073 | 2015-04-03 09:37:49 -0700 | [diff] [blame] | 599 | // Enable debug highlight when what we're about to draw is tested against |
| 600 | // the stencil buffer and if stencil highlight debugging is on |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 601 | mDescription.hasDebugHighlight = !Properties::debugOverdraw |
| 602 | && Properties::debugStencilClip == StencilClipDebug::ShowHighlight |
Chris Craik | 8284073 | 2015-04-03 09:37:49 -0700 | [diff] [blame] | 603 | && mRenderState.stencil().isTestEnabled(); |
| 604 | |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 605 | // serialize shader info into ShaderData |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 606 | GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 607 | |
| 608 | if (CC_LIKELY(!mShader)) { |
| 609 | mOutGlop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType; |
| 610 | } else { |
| 611 | Matrix4 shaderMatrix; |
| 612 | if (mOutGlop->transform.transformFlags & TransformFlags::MeshIgnoresCanvasTransform) { |
| 613 | // canvas level transform was built into the modelView and geometry, |
| 614 | // so the shader matrix must reverse this |
| 615 | shaderMatrix.loadInverse(mOutGlop->transform.canvas); |
| 616 | shaderMatrix.multiply(mOutGlop->transform.modelView); |
| 617 | } else { |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 618 | shaderMatrix = mOutGlop->transform.modelView; |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 619 | } |
| 620 | SkiaShader::store(mCaches, *mShader, shaderMatrix, |
| 621 | &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData)); |
| 622 | } |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 623 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 624 | // duplicates ProgramCache's definition of color uniform presence |
| 625 | const bool singleColor = !mDescription.hasTexture |
| 626 | && !mDescription.hasExternalTexture |
| 627 | && !mDescription.hasGradient |
| 628 | && !mDescription.hasBitmap; |
| 629 | mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor; |
Chris Craik | f27133d | 2015-02-19 09:51:53 -0800 | [diff] [blame] | 630 | |
| 631 | verify(mDescription, *mOutGlop); |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 632 | |
| 633 | // Final step: populate program and map bounds into render target space |
| 634 | mOutGlop->fill.program = mCaches.programCache.get(mDescription); |
Chris Craik | 53e51e4 | 2015-06-01 10:35:35 -0700 | [diff] [blame] | 635 | mOutGlop->transform.meshTransform().mapRect(mOutGlop->bounds); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | } /* namespace uirenderer */ |
| 639 | } /* namespace android */ |