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" 21 #include "Patch.h" 22 #include "renderstate/MeshState.h" 23 #include "renderstate/RenderState.h" 24 #include "SkiaShader.h" 25 #include "Texture.h" 26 #include "utils/PaintUtils.h" 27 #include "VertexBuffer.h" 28 29 #include <GLES2/gl2.h> 30 #include <SkPaint.h> 31 32 #define DEBUG_GLOP_BUILDER 0 33 34 #if DEBUG_GLOP_BUILDER 35 36 #define TRIGGER_STAGE(stageFlag) \ 37 LOG_ALWAYS_FATAL_IF((stageFlag) & mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \ 38 mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag)) 39 40 #define REQUIRE_STAGES(requiredFlags) \ 41 LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \ 42 "not prepared for current stage") 43 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 54 static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) { 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}; 59 } 60 61 GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop) 62 : mRenderState(renderState) 63 , mCaches(caches) 64 , mShader(nullptr) 65 , mOutGlop(outGlop) { 66 mStageFlags = kInitialStage; 67 } 68 69 //////////////////////////////////////////////////////////////////////////////// 70 // Mesh 71 //////////////////////////////////////////////////////////////////////////////// 72 73 GlopBuilder& GlopBuilder::setMeshUnitQuad() { 74 TRIGGER_STAGE(kMeshStage); 75 76 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; 77 mOutGlop->mesh.indices = { 0, nullptr }; 78 mOutGlop->mesh.vertices = { 79 mRenderState.meshState().getUnitQuadVBO(), 80 VertexAttribFlags::None, 81 nullptr, nullptr, nullptr, 82 kTextureVertexStride }; 83 mOutGlop->mesh.elementCount = 4; 84 return *this; 85 } 86 87 GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) { 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 95 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; 96 mOutGlop->mesh.indices = { 0, nullptr }; 97 mOutGlop->mesh.vertices = { 98 mRenderState.meshState().getUnitQuadVBO(), 99 VertexAttribFlags::TextureCoord, 100 nullptr, (const void*) kMeshTextureOffset, nullptr, 101 kTextureVertexStride }; 102 mOutGlop->mesh.elementCount = 4; 103 return *this; 104 } 105 106 GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) { 107 TRIGGER_STAGE(kMeshStage); 108 109 if (CC_UNLIKELY(uvMapper)) { 110 uvMapper->map(uvs); 111 } 112 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]); 113 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, 119 VertexAttribFlags::TextureCoord, 120 &textureVertex[0].x, &textureVertex[0].u, nullptr, 121 kTextureVertexStride }; 122 mOutGlop->mesh.elementCount = 4; 123 return *this; 124 } 125 126 GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) { 127 TRIGGER_STAGE(kMeshStage); 128 129 mOutGlop->mesh.primitiveMode = GL_TRIANGLES; 130 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; 131 mOutGlop->mesh.vertices = { 132 0, 133 VertexAttribFlags::None, 134 vertexData, nullptr, nullptr, 135 kVertexStride }; 136 mOutGlop->mesh.elementCount = 6 * quadCount; 137 return *this; 138 } 139 140 GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) { 141 TRIGGER_STAGE(kMeshStage); 142 143 mOutGlop->mesh.primitiveMode = GL_TRIANGLES; 144 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr }; 145 mOutGlop->mesh.vertices = { 146 0, 147 VertexAttribFlags::TextureCoord, 148 &vertexData[0].x, &vertexData[0].u, nullptr, 149 kTextureVertexStride }; 150 mOutGlop->mesh.elementCount = elementCount; 151 return *this; 152 } 153 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, 161 VertexAttribFlags::TextureCoord, 162 &vertexData[0].x, &vertexData[0].u, nullptr, 163 kTextureVertexStride }; 164 mOutGlop->mesh.elementCount = elementCount; 165 return *this; 166 } 167 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, 175 VertexAttribFlags::TextureCoord | VertexAttribFlags::Color, 176 &vertexData[0].x, &vertexData[0].u, &vertexData[0].r, 177 kColorTextureVertexStride }; 178 mOutGlop->mesh.elementCount = elementCount; 179 return *this; 180 } 181 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; 189 190 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; 191 mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() }; 192 mOutGlop->mesh.vertices = { 193 0, 194 alphaVertex ? VertexAttribFlags::Alpha : VertexAttribFlags::None, 195 vertexBuffer.getBuffer(), nullptr, nullptr, 196 alphaVertex ? kAlphaVertexStride : kVertexStride }; 197 mOutGlop->mesh.elementCount = indices 198 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount(); 199 200 mDescription.useShadowAlphaInterp = shadowInterp; 201 return *this; 202 } 203 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(), 211 VertexAttribFlags::TextureCoord, 212 (void*)patch.positionOffset, (void*)patch.textureOffset, nullptr, 213 kTextureVertexStride }; 214 mOutGlop->mesh.elementCount = patch.indexCount; 215 return *this; 216 } 217 218 //////////////////////////////////////////////////////////////////////////////// 219 // Fill 220 //////////////////////////////////////////////////////////////////////////////// 221 222 void GlopBuilder::setFill(int color, float alphaScale, 223 SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage, 224 const SkShader* shader, const SkColorFilter* colorFilter) { 225 if (mode != SkXfermode::kClear_Mode) { 226 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale; 227 if (!shader) { 228 float colorScale = alpha / 255.0f; 229 mOutGlop->fill.color = { 230 colorScale * SkColorGetR(color), 231 colorScale * SkColorGetG(color), 232 colorScale * SkColorGetB(color), 233 alpha 234 }; 235 } else { 236 mOutGlop->fill.color = { 1, 1, 1, alpha }; 237 } 238 } else { 239 mOutGlop->fill.color = { 0, 0, 0, 1 }; 240 } 241 242 mOutGlop->blend = { GL_ZERO, GL_ZERO }; 243 if (mOutGlop->fill.color.a < 1.0f 244 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha) 245 || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend) 246 || mOutGlop->roundRectClipState 247 || PaintUtils::isBlendedShader(shader) 248 || PaintUtils::isBlendedColorFilter(colorFilter) 249 || mode != SkXfermode::kSrcOver_Mode) { 250 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) { 251 Blend::getFactors(mode, modeUsage, 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 259 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) { 260 mDescription.framebufferMode = mode; 261 mDescription.swapSrcDst = (modeUsage == Blend::ModeOrderSwap::Swap); 262 // blending in shader, don't enable 263 } else { 264 // unsupported 265 Blend::getFactors(SkXfermode::kSrcOver_Mode, modeUsage, 266 &mOutGlop->blend.src, &mOutGlop->blend.dst); 267 } 268 } 269 } 270 mShader = shader; // shader resolved in ::build() 271 272 if (colorFilter) { 273 SkColor color; 274 SkXfermode::Mode mode; 275 SkScalar srcColorMatrix[20]; 276 if (colorFilter->asColorMode(&color, &mode)) { 277 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend; 278 mDescription.colorMode = mode; 279 280 const float alpha = SkColorGetA(color) / 255.0f; 281 float colorScale = alpha / 255.0f; 282 mOutGlop->fill.filter.color = { 283 colorScale * SkColorGetR(color), 284 colorScale * SkColorGetG(color), 285 colorScale * SkColorGetB(color), 286 alpha, 287 }; 288 } else if (colorFilter->asColorMatrix(srcColorMatrix)) { 289 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix; 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; 304 } else { 305 LOG_ALWAYS_FATAL("unsupported ColorFilter"); 306 } 307 } else { 308 mOutGlop->fill.filterMode = ProgramDescription::kColorNone; 309 } 310 } 311 312 GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, 313 const int textureFillFlags, const SkPaint* paint, float alphaScale) { 314 TRIGGER_STAGE(kFillStage); 315 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 316 317 GLenum filter = (textureFillFlags & TextureFillFlags::ForceFilter) 318 ? GL_LINEAR : PaintUtils::getFilter(paint); 319 mOutGlop->fill.texture = { &texture, 320 GL_TEXTURE_2D, filter, GL_CLAMP_TO_EDGE, nullptr }; 321 322 if (paint) { 323 int color = paint->getColor(); 324 SkShader* shader = paint->getShader(); 325 326 if (!(textureFillFlags & TextureFillFlags::IsAlphaMaskTexture)) { 327 // Texture defines color, so disable shaders, and reset all non-alpha color channels 328 color |= 0x00FFFFFF; 329 shader = nullptr; 330 } 331 setFill(color, alphaScale, 332 PaintUtils::getXfermode(paint->getXfermode()), Blend::ModeOrderSwap::NoSwap, 333 shader, paint->getColorFilter()); 334 } else { 335 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale }; 336 337 if (alphaScale < 1.0f 338 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha) 339 || texture.blend 340 || mOutGlop->roundRectClipState) { 341 Blend::getFactors(SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap, 342 &mOutGlop->blend.src, &mOutGlop->blend.dst); 343 } else { 344 mOutGlop->blend = { GL_ZERO, GL_ZERO }; 345 } 346 } 347 348 if (textureFillFlags & TextureFillFlags::IsAlphaMaskTexture) { 349 mDescription.modulate = mOutGlop->fill.color.isNotBlack(); 350 mDescription.hasAlpha8Texture = true; 351 } else { 352 mDescription.modulate = mOutGlop->fill.color.a < 1.0f; 353 } 354 return *this; 355 } 356 357 GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) { 358 TRIGGER_STAGE(kFillStage); 359 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 360 361 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; 362 363 setFill(paint.getColor(), alphaScale, 364 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, 365 paint.getShader(), paint.getColorFilter()); 366 mDescription.modulate = mOutGlop->fill.color.a < 1.0f; 367 return *this; 368 } 369 370 GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture, 371 const SkPaint& paint, float alphaScale) { 372 TRIGGER_STAGE(kFillStage); 373 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 374 375 //specify invalid filter/clamp, since these are always static for PathTextures 376 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; 377 378 setFill(paint.getColor(), alphaScale, 379 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, 380 paint.getShader(), paint.getColorFilter()); 381 382 mDescription.hasAlpha8Texture = true; 383 mDescription.modulate = mOutGlop->fill.color.isNotBlack(); 384 return *this; 385 } 386 387 GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor, 388 const SkPaint& paint, float alphaScale) { 389 TRIGGER_STAGE(kFillStage); 390 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 391 392 //specify invalid filter/clamp, since these are always static for ShadowTextures 393 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; 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 402 setFill(shadowColor, alphaScale, 403 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap, 404 paint.getShader(), paint.getColorFilter()); 405 406 mDescription.hasAlpha8Texture = true; 407 mDescription.modulate = mOutGlop->fill.color.isNotBlack(); 408 return *this; 409 } 410 411 GlopBuilder& GlopBuilder::setFillBlack() { 412 TRIGGER_STAGE(kFillStage); 413 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 414 415 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; 416 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap, 417 nullptr, nullptr); 418 return *this; 419 } 420 421 GlopBuilder& GlopBuilder::setFillClear() { 422 TRIGGER_STAGE(kFillStage); 423 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 424 425 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr }; 426 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, Blend::ModeOrderSwap::NoSwap, 427 nullptr, nullptr); 428 return *this; 429 } 430 431 GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter, 432 float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage) { 433 TRIGGER_STAGE(kFillStage); 434 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 435 436 mOutGlop->fill.texture = { &texture, 437 GL_TEXTURE_2D, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr }; 438 mOutGlop->fill.color = { alpha, alpha, alpha, alpha }; 439 440 setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter); 441 442 mDescription.modulate = mOutGlop->fill.color.a < 1.0f; 443 return *this; 444 } 445 446 GlopBuilder& GlopBuilder::setFillTextureLayer(Layer& layer, float alpha) { 447 TRIGGER_STAGE(kFillStage); 448 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage); 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 454 setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap, 455 nullptr, layer.getColorFilter()); 456 457 mDescription.modulate = mOutGlop->fill.color.a < 1.0f; 458 mDescription.hasTextureTransform = true; 459 return *this; 460 } 461 462 //////////////////////////////////////////////////////////////////////////////// 463 // Transform 464 //////////////////////////////////////////////////////////////////////////////// 465 466 void GlopBuilder::setTransform(const Matrix4& ortho, const Matrix4& canvas, 467 const int transformFlags) { 468 TRIGGER_STAGE(kTransformStage); 469 470 mOutGlop->transform.ortho.load(ortho); 471 mOutGlop->transform.canvas.load(canvas); 472 mOutGlop->transform.transformFlags = transformFlags; 473 } 474 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 495 const Matrix4& meshTransform = mOutGlop->transform.meshTransform(); 496 if (CC_LIKELY(meshTransform.isPureTranslate())) { 497 // snap by adjusting the model view matrix 498 const float translateX = meshTransform.getTranslateX(); 499 const float translateY = meshTransform.getTranslateY(); 500 501 left = (int) floorf(left + translateX + 0.5f) - translateX; 502 top = (int) floorf(top + translateY + 0.5f) - translateY; 503 mOutGlop->fill.texture.filter = GL_NEAREST; 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 521 GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) { 522 TRIGGER_STAGE(kModelViewStage); 523 REQUIRE_STAGES(kTransformStage | kFillStage); 524 525 const Matrix4& meshTransform = mOutGlop->transform.meshTransform(); 526 if (CC_LIKELY(meshTransform.isPureTranslate())) { 527 // snap by adjusting the model view matrix 528 const float translateX = meshTransform.getTranslateX(); 529 const float translateY = meshTransform.getTranslateY(); 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); 537 mOutGlop->bounds = source; 538 mOutGlop->bounds.translate(offsetX, offsetY); 539 return *this; 540 } 541 542 //////////////////////////////////////////////////////////////////////////////// 543 // RoundRectClip 544 //////////////////////////////////////////////////////////////////////////////// 545 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 558 void verify(const ProgramDescription& description, const Glop& glop) { 559 if (glop.fill.texture.texture != nullptr) { 560 LOG_ALWAYS_FATAL_IF(((description.hasTexture && description.hasExternalTexture) 561 || (!description.hasTexture && !description.hasExternalTexture) 562 || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) == 0)), 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 570 || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) != 0)), 571 "No texture, hT%d, hET %d, attribFlags %x", 572 description.hasTexture, description.hasExternalTexture, 573 glop.mesh.vertices.attribFlags); 574 } 575 576 if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::Alpha) 577 && glop.mesh.vertices.bufferObject) { 578 LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible"); 579 } 580 581 if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) { 582 LOG_ALWAYS_FATAL("Texture transform incorrectly specified"); 583 } 584 } 585 586 void GlopBuilder::build() { 587 REQUIRE_STAGES(kAllStages); 588 if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) { 589 if (mOutGlop->fill.texture.target == GL_TEXTURE_2D) { 590 mDescription.hasTexture = true; 591 } else { 592 mDescription.hasExternalTexture = true; 593 } 594 } 595 596 mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Color; 597 mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha; 598 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 601 mDescription.hasDebugHighlight = !Properties::debugOverdraw 602 && Properties::debugStencilClip == StencilClipDebug::ShowHighlight 603 && mRenderState.stencil().isTestEnabled(); 604 605 // serialize shader info into ShaderData 606 GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0; 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 { 618 shaderMatrix.load(mOutGlop->transform.modelView); 619 } 620 SkiaShader::store(mCaches, *mShader, shaderMatrix, 621 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData)); 622 } 623 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; 630 631 verify(mDescription, *mOutGlop); 632 633 // Final step: populate program and map bounds into render target space 634 mOutGlop->fill.program = mCaches.programCache.get(mDescription); 635 mOutGlop->transform.meshTransform().mapRect(mOutGlop->bounds); 636 } 637 638 } /* namespace uirenderer */ 639 } /* namespace android */ 640