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