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 | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 21 | #include "renderstate/MeshState.h" |
| 22 | #include "renderstate/RenderState.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 23 | #include "SkiaShader.h" |
| 24 | #include "Texture.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 25 | #include "utils/PaintUtils.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 26 | #include "VertexBuffer.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 27 | |
| 28 | #include <GLES2/gl2.h> |
| 29 | #include <SkPaint.h> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 34 | #define TRIGGER_STAGE(stageFlag) \ |
| 35 | LOG_ALWAYS_FATAL_IF(stageFlag & mStageFlags, "Stage %d cannot be run twice"); \ |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 36 | mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag)) |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 37 | |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 38 | #define REQUIRE_STAGES(requiredFlags) \ |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 39 | LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \ |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 40 | "not prepared for current stage") |
| 41 | |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 42 | static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) { |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 43 | TextureVertex::setUV(quadVertex++, uvs.left, uvs.top); |
| 44 | TextureVertex::setUV(quadVertex++, uvs.right, uvs.top); |
| 45 | TextureVertex::setUV(quadVertex++, uvs.left, uvs.bottom); |
| 46 | TextureVertex::setUV(quadVertex++, uvs.right, uvs.bottom); |
| 47 | } |
| 48 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 49 | GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop) |
| 50 | : mRenderState(renderState) |
| 51 | , mCaches(caches) |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 52 | , mShader(nullptr) |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 53 | , mOutGlop(outGlop) { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 54 | mStageFlags = kInitialStage; |
| 55 | } |
| 56 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 57 | //////////////////////////////////////////////////////////////////////////////// |
| 58 | // Mesh |
| 59 | //////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | GlopBuilder& GlopBuilder::setMeshUnitQuad() { |
| 62 | TRIGGER_STAGE(kMeshStage); |
| 63 | |
| 64 | mOutGlop->mesh.vertexFlags = kNone_Attrib; |
| 65 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 66 | mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO(); |
| 67 | mOutGlop->mesh.vertices = nullptr; |
| 68 | mOutGlop->mesh.indexBufferObject = 0; |
| 69 | mOutGlop->mesh.indices = nullptr; |
| 70 | mOutGlop->mesh.elementCount = 4; |
| 71 | mOutGlop->mesh.stride = kTextureVertexStride; |
| 72 | mOutGlop->mesh.texCoordOffset = nullptr; |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper, |
| 77 | bool isAlphaMaskTexture) { |
| 78 | TRIGGER_STAGE(kMeshStage); |
| 79 | |
| 80 | mOutGlop->mesh.vertexFlags = kTextureCoord_Attrib; |
| 81 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 82 | |
| 83 | if (CC_UNLIKELY(uvMapper)) { |
| 84 | Rect uvs(0, 0, 1, 1); |
| 85 | uvMapper->map(uvs); |
| 86 | setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]); |
| 87 | |
| 88 | mOutGlop->mesh.vertexBufferObject = 0; |
| 89 | mOutGlop->mesh.vertices = &mOutGlop->mesh.mappedVertices[0]; |
| 90 | } else { |
| 91 | // standard UV coordinates, use regular unit quad VBO |
| 92 | mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO(); |
| 93 | mOutGlop->mesh.vertices = nullptr; |
| 94 | } |
| 95 | mOutGlop->mesh.indexBufferObject = 0; |
| 96 | mOutGlop->mesh.indices = nullptr; |
| 97 | mOutGlop->mesh.elementCount = 4; |
| 98 | mOutGlop->mesh.stride = kTextureVertexStride; |
| 99 | mOutGlop->mesh.texCoordOffset = (GLvoid*) kMeshTextureOffset; |
| 100 | |
| 101 | mDescription.hasTexture = true; |
| 102 | mDescription.hasAlpha8Texture = isAlphaMaskTexture; |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | GlopBuilder& GlopBuilder::setMeshIndexedQuads(void* vertexData, int quadCount) { |
| 107 | TRIGGER_STAGE(kMeshStage); |
| 108 | |
| 109 | mOutGlop->mesh.vertexFlags = kNone_Attrib; |
| 110 | mOutGlop->mesh.primitiveMode = GL_TRIANGLES; |
| 111 | mOutGlop->mesh.vertexBufferObject = 0; |
| 112 | mOutGlop->mesh.vertices = vertexData; |
| 113 | mOutGlop->mesh.indexBufferObject = mRenderState.meshState().getQuadListIBO(); |
| 114 | mOutGlop->mesh.indices = nullptr; |
| 115 | mOutGlop->mesh.elementCount = 6 * quadCount; |
| 116 | mOutGlop->mesh.stride = kVertexStride; |
| 117 | mOutGlop->mesh.texCoordOffset = nullptr; |
| 118 | |
| 119 | return *this; |
| 120 | } |
| 121 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 122 | GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) { |
| 123 | TRIGGER_STAGE(kMeshStage); |
| 124 | |
| 125 | const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags(); |
| 126 | |
| 127 | bool alphaVertex = flags & VertexBuffer::kAlpha; |
| 128 | bool indices = flags & VertexBuffer::kIndices; |
| 129 | mOutGlop->mesh.vertexFlags = alphaVertex ? kAlpha_Attrib : kNone_Attrib; |
| 130 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 131 | mOutGlop->mesh.vertexBufferObject = 0; |
| 132 | mOutGlop->mesh.vertices = vertexBuffer.getBuffer(); |
| 133 | mOutGlop->mesh.indexBufferObject = 0; |
| 134 | mOutGlop->mesh.indices = vertexBuffer.getIndices(); |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 135 | mOutGlop->mesh.elementCount = indices |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 136 | ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount(); |
| 137 | mOutGlop->mesh.stride = alphaVertex ? kAlphaVertexStride : kVertexStride; |
| 138 | |
| 139 | mDescription.hasVertexAlpha = alphaVertex; |
| 140 | mDescription.useShadowAlphaInterp = shadowInterp; |
| 141 | return *this; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 144 | //////////////////////////////////////////////////////////////////////////////// |
| 145 | // Fill |
| 146 | //////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 147 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 148 | void GlopBuilder::setFill(int color, float alphaScale, SkXfermode::Mode mode, |
| 149 | const SkShader* shader, const SkColorFilter* colorFilter) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 150 | if (mode != SkXfermode::kClear_Mode) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 151 | float alpha = (SkColorGetA(color) / 255.0f) * alphaScale; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 152 | if (!shader) { |
| 153 | float colorScale = alpha / 255.0f; |
| 154 | mOutGlop->fill.color = { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 155 | colorScale * SkColorGetR(color), |
| 156 | colorScale * SkColorGetG(color), |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 157 | colorScale * SkColorGetB(color), |
| 158 | alpha |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 159 | }; |
| 160 | } else { |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 161 | mOutGlop->fill.color = { 1, 1, 1, alpha }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 162 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 163 | } else { |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 164 | mOutGlop->fill.color = { 0, 0, 0, 1 }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 165 | } |
| 166 | const bool SWAP_SRC_DST = false; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 167 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 168 | mOutGlop->blend = { GL_ZERO, GL_ZERO }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 169 | if (mOutGlop->fill.color.a < 1.0f |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 170 | || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib) |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 171 | || (mOutGlop->fill.texture && mOutGlop->fill.texture->blend) |
| 172 | || mOutGlop->roundRectClipState |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 173 | || PaintUtils::isBlendedShader(shader) |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 174 | || PaintUtils::isBlendedColorFilter(colorFilter) |
| 175 | || mode != SkXfermode::kSrcOver_Mode) { |
| 176 | if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) { |
| 177 | Blend::getFactors(mode, SWAP_SRC_DST, |
| 178 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 179 | } else { |
| 180 | // These blend modes are not supported by OpenGL directly and have |
| 181 | // to be implemented using shaders. Since the shader will perform |
| 182 | // the blending, don't enable GL blending off here |
| 183 | // If the blend mode cannot be implemented using shaders, fall |
| 184 | // back to the default SrcOver blend mode instead |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 185 | if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 186 | mDescription.framebufferMode = mode; |
| 187 | mDescription.swapSrcDst = SWAP_SRC_DST; |
| 188 | // blending in shader, don't enable |
| 189 | } else { |
| 190 | // unsupported |
| 191 | Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST, |
| 192 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 193 | } |
| 194 | } |
| 195 | } |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 196 | mShader = shader; // shader resolved in ::build() |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 197 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 198 | if (colorFilter) { |
| 199 | SkColor color; |
| 200 | SkXfermode::Mode mode; |
| 201 | SkScalar srcColorMatrix[20]; |
| 202 | if (colorFilter->asColorMode(&color, &mode)) { |
| 203 | mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend; |
| 204 | mDescription.colorMode = mode; |
| 205 | |
| 206 | const float alpha = SkColorGetA(color) / 255.0f; |
| 207 | float colorScale = alpha / 255.0f; |
| 208 | mOutGlop->fill.filter.color = { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 209 | colorScale * SkColorGetR(color), |
| 210 | colorScale * SkColorGetG(color), |
| 211 | colorScale * SkColorGetB(color), |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 212 | alpha, |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 213 | }; |
| 214 | } else if (colorFilter->asColorMatrix(srcColorMatrix)) { |
| 215 | mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix; |
| 216 | |
| 217 | float* colorMatrix = mOutGlop->fill.filter.matrix.matrix; |
| 218 | memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float)); |
| 219 | memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float)); |
| 220 | memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float)); |
| 221 | memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float)); |
| 222 | |
| 223 | // Skia uses the range [0..255] for the addition vector, but we need |
| 224 | // the [0..1] range to apply the vector in GLSL |
| 225 | float* colorVector = mOutGlop->fill.filter.matrix.vector; |
| 226 | colorVector[0] = srcColorMatrix[4] / 255.0f; |
| 227 | colorVector[1] = srcColorMatrix[9] / 255.0f; |
| 228 | colorVector[2] = srcColorMatrix[14] / 255.0f; |
| 229 | colorVector[3] = srcColorMatrix[19] / 255.0f; |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 230 | } else { |
| 231 | LOG_ALWAYS_FATAL("unsupported ColorFilter"); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 232 | } |
| 233 | } else { |
| 234 | mOutGlop->fill.filterMode = ProgramDescription::kColorNone; |
| 235 | } |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 236 | } |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 237 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 238 | GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, bool isAlphaMaskTexture, |
| 239 | const SkPaint* paint, float alphaScale) { |
| 240 | TRIGGER_STAGE(kFillStage); |
| 241 | REQUIRE_STAGES(kMeshStage); |
| 242 | |
| 243 | mOutGlop->fill.texture = &texture; |
| 244 | mOutGlop->fill.textureFilter = PaintUtils::getFilter(paint); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 245 | mOutGlop->fill.textureClamp = GL_CLAMP_TO_EDGE; |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 246 | |
| 247 | if (paint) { |
| 248 | int color = paint->getColor(); |
| 249 | SkShader* shader = paint->getShader(); |
| 250 | |
| 251 | if (!isAlphaMaskTexture) { |
| 252 | // Texture defines color, so disable shaders, and reset all non-alpha color channels |
| 253 | color |= 0x00FFFFFF; |
| 254 | shader = nullptr; |
| 255 | } |
| 256 | setFill(color, alphaScale, PaintUtils::getXfermode(paint->getXfermode()), |
| 257 | shader, paint->getColorFilter()); |
| 258 | } else { |
| 259 | mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale }; |
| 260 | |
| 261 | const bool SWAP_SRC_DST = false; |
| 262 | if (alphaScale < 1.0f |
| 263 | || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib) |
| 264 | || texture.blend |
| 265 | || mOutGlop->roundRectClipState) { |
| 266 | Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST, |
| 267 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 268 | } else { |
| 269 | mOutGlop->blend = { GL_ZERO, GL_ZERO }; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (isAlphaMaskTexture) { |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame^] | 274 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 275 | } else { |
| 276 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 277 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 278 | return *this; |
| 279 | } |
| 280 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 281 | GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) { |
| 282 | TRIGGER_STAGE(kFillStage); |
| 283 | REQUIRE_STAGES(kMeshStage); |
| 284 | |
| 285 | mOutGlop->fill.texture = nullptr; |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 286 | mOutGlop->fill.textureFilter = GL_INVALID_ENUM; |
| 287 | mOutGlop->fill.textureClamp = GL_INVALID_ENUM; |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 288 | |
| 289 | setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()), |
| 290 | paint.getShader(), paint.getColorFilter()); |
| 291 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 292 | return *this; |
| 293 | } |
| 294 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame^] | 295 | GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture, |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 296 | const SkPaint& paint, float alphaScale) { |
| 297 | TRIGGER_STAGE(kFillStage); |
| 298 | REQUIRE_STAGES(kMeshStage); |
| 299 | |
| 300 | mOutGlop->fill.texture = &texture; |
| 301 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame^] | 302 | //specify invalid, since these are always static for PathTextures |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 303 | mOutGlop->fill.textureFilter = GL_INVALID_ENUM; |
| 304 | mOutGlop->fill.textureClamp = GL_INVALID_ENUM; |
| 305 | |
| 306 | setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()), |
| 307 | paint.getShader(), paint.getColorFilter()); |
| 308 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame^] | 309 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
Chris Craik | 3003609 | 2015-02-12 10:41:39 -0800 | [diff] [blame] | 310 | return *this; |
| 311 | } |
| 312 | |
Chris Craik | 2bb8f56 | 2015-02-17 16:42:02 -0800 | [diff] [blame^] | 313 | GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor, |
| 314 | const SkPaint& paint, float alphaScale) { |
| 315 | TRIGGER_STAGE(kFillStage); |
| 316 | REQUIRE_STAGES(kMeshStage); |
| 317 | |
| 318 | mOutGlop->fill.texture = &texture; |
| 319 | |
| 320 | //specify invalid, since these are always static for ShadowTextures |
| 321 | mOutGlop->fill.textureFilter = GL_INVALID_ENUM; |
| 322 | mOutGlop->fill.textureClamp = GL_INVALID_ENUM; |
| 323 | |
| 324 | const int ALPHA_BITMASK = SK_ColorBLACK; |
| 325 | const int COLOR_BITMASK = ~ALPHA_BITMASK; |
| 326 | if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) { |
| 327 | // shadow color is fully opaque: override its alpha with that of paint |
| 328 | shadowColor &= paint.getColor() | COLOR_BITMASK; |
| 329 | } |
| 330 | |
| 331 | setFill(shadowColor, alphaScale, PaintUtils::getXfermode(paint.getXfermode()), |
| 332 | paint.getShader(), paint.getColorFilter()); |
| 333 | |
| 334 | mDescription.modulate = mOutGlop->fill.color.isNotBlack(); |
| 335 | return *this; |
| 336 | } |
| 337 | |
| 338 | GlopBuilder& GlopBuilder::setFillBlack() { |
| 339 | TRIGGER_STAGE(kFillStage); |
| 340 | REQUIRE_STAGES(kMeshStage); |
| 341 | |
| 342 | mOutGlop->fill.texture = nullptr; |
| 343 | mOutGlop->fill.textureFilter = GL_INVALID_ENUM; |
| 344 | mOutGlop->fill.textureClamp = GL_INVALID_ENUM; |
| 345 | |
| 346 | setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, nullptr, nullptr); |
| 347 | |
| 348 | return *this; |
| 349 | } |
| 350 | |
| 351 | GlopBuilder& GlopBuilder::setFillClear() { |
| 352 | TRIGGER_STAGE(kFillStage); |
| 353 | REQUIRE_STAGES(kMeshStage); |
| 354 | |
| 355 | mOutGlop->fill.texture = nullptr; |
| 356 | mOutGlop->fill.textureFilter = GL_INVALID_ENUM; |
| 357 | mOutGlop->fill.textureClamp = GL_INVALID_ENUM; |
| 358 | |
| 359 | setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, nullptr, nullptr); |
| 360 | |
| 361 | return *this; |
| 362 | } |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 363 | //////////////////////////////////////////////////////////////////////////////// |
| 364 | // Transform |
| 365 | //////////////////////////////////////////////////////////////////////////////// |
| 366 | |
| 367 | GlopBuilder& GlopBuilder::setTransformClip(const Matrix4& ortho, |
| 368 | const Matrix4& transform, bool fudgingOffset) { |
| 369 | TRIGGER_STAGE(kTransformStage); |
| 370 | |
| 371 | mOutGlop->transform.ortho.load(ortho); |
| 372 | mOutGlop->transform.canvas.load(transform); |
| 373 | mOutGlop->transform.fudgingOffset = fudgingOffset; |
| 374 | return *this; |
| 375 | } |
| 376 | |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 377 | //////////////////////////////////////////////////////////////////////////////// |
| 378 | // ModelView |
| 379 | //////////////////////////////////////////////////////////////////////////////// |
| 380 | |
| 381 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) { |
| 382 | TRIGGER_STAGE(kModelViewStage); |
| 383 | |
| 384 | mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f); |
| 385 | mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f); |
| 386 | mOutGlop->bounds = destination; |
| 387 | return *this; |
| 388 | } |
| 389 | |
| 390 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) { |
| 391 | TRIGGER_STAGE(kModelViewStage); |
| 392 | REQUIRE_STAGES(kTransformStage | kFillStage); |
| 393 | |
| 394 | float left = destination.left; |
| 395 | float top = destination.top; |
| 396 | |
| 397 | const Matrix4& canvasTransform = mOutGlop->transform.canvas; |
| 398 | if (CC_LIKELY(canvasTransform.isPureTranslate())) { |
| 399 | const float translateX = canvasTransform.getTranslateX(); |
| 400 | const float translateY = canvasTransform.getTranslateY(); |
| 401 | |
| 402 | left = (int) floorf(left + translateX + 0.5f) - translateX; |
| 403 | top = (int) floorf(top + translateY + 0.5f) - translateY; |
| 404 | mOutGlop->fill.textureFilter = GL_NEAREST; |
| 405 | } |
| 406 | |
| 407 | mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f); |
| 408 | mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f); |
| 409 | mOutGlop->bounds = destination; |
| 410 | return *this; |
| 411 | } |
| 412 | |
| 413 | GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) { |
| 414 | TRIGGER_STAGE(kModelViewStage); |
| 415 | |
| 416 | mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f); |
| 417 | mOutGlop->bounds = source; |
| 418 | mOutGlop->bounds.translate(offsetX, offsetY); |
| 419 | return *this; |
| 420 | } |
| 421 | |
| 422 | GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) { |
| 423 | TRIGGER_STAGE(kRoundRectClipStage); |
| 424 | |
| 425 | mOutGlop->roundRectClipState = roundRectClipState; |
| 426 | mDescription.hasRoundRectClip = roundRectClipState != nullptr; |
| 427 | return *this; |
| 428 | } |
| 429 | |
| 430 | //////////////////////////////////////////////////////////////////////////////// |
| 431 | // Build |
| 432 | //////////////////////////////////////////////////////////////////////////////// |
| 433 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 434 | void GlopBuilder::build() { |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 435 | REQUIRE_STAGES(kAllStages); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 436 | |
Chris Craik | 922d3a7 | 2015-02-13 17:47:21 -0800 | [diff] [blame] | 437 | // serialize shader info into ShaderData |
| 438 | GLuint textureUnit = mOutGlop->fill.texture ? 1 : 0; |
| 439 | SkiaShader::store(mCaches, mShader, mOutGlop->transform.modelView, |
| 440 | &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData)); |
| 441 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 442 | mOutGlop->fill.program = mCaches.programCache.get(mDescription); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 443 | mOutGlop->transform.canvas.mapRect(mOutGlop->bounds); |
Chris Craik | 0519c810 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 444 | |
| 445 | // duplicates ProgramCache's definition of color uniform presence |
| 446 | const bool singleColor = !mDescription.hasTexture |
| 447 | && !mDescription.hasExternalTexture |
| 448 | && !mDescription.hasGradient |
| 449 | && !mDescription.hasBitmap; |
| 450 | mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | } /* namespace uirenderer */ |
| 454 | } /* namespace android */ |