BakedOpDispatcher.cpp revision 284b765e3c1647859d4dac772744e8859c033216
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 17#include "BakedOpDispatcher.h" 18 19#include "BakedOpRenderer.h" 20#include "Caches.h" 21#include "Glop.h" 22#include "GlopBuilder.h" 23#include "Patch.h" 24#include "PathTessellator.h" 25#include "renderstate/OffscreenBufferPool.h" 26#include "renderstate/RenderState.h" 27#include "utils/GLUtils.h" 28#include "VertexBuffer.h" 29 30#include <algorithm> 31#include <math.h> 32#include <SkPaintDefaults.h> 33#include <SkPathOps.h> 34 35namespace android { 36namespace uirenderer { 37 38static void storeTexturedRect(TextureVertex* vertices, const Rect& bounds, const Rect& texCoord) { 39 vertices[0] = { bounds.left, bounds.top, texCoord.left, texCoord.top }; 40 vertices[1] = { bounds.right, bounds.top, texCoord.right, texCoord.top }; 41 vertices[2] = { bounds.left, bounds.bottom, texCoord.left, texCoord.bottom }; 42 vertices[3] = { bounds.right, bounds.bottom, texCoord.right, texCoord.bottom }; 43} 44 45void BakedOpDispatcher::onMergedBitmapOps(BakedOpRenderer& renderer, 46 const MergedBakedOpList& opList) { 47 48 const BakedOpState& firstState = *(opList.states[0]); 49 const SkBitmap* bitmap = (static_cast<const BitmapOp*>(opList.states[0]->op))->bitmap; 50 51 AssetAtlas::Entry* entry = renderer.renderState().assetAtlas().getEntry(bitmap->pixelRef()); 52 Texture* texture = entry ? entry->texture : renderer.caches().textureCache.get(bitmap); 53 if (!texture) return; 54 const AutoTexture autoCleanup(texture); 55 56 TextureVertex vertices[opList.count * 4]; 57 Rect texCoords(0, 0, 1, 1); 58 if (entry) { 59 entry->uvMapper.map(texCoords); 60 } 61 for (size_t i = 0; i < opList.count; i++) { 62 const BakedOpState& state = *(opList.states[i]); 63 TextureVertex* rectVerts = &vertices[i * 4]; 64 65 // calculate unclipped bounds, since they'll determine texture coordinates 66 Rect opBounds = state.op->unmappedBounds; 67 state.computedState.transform.mapRect(opBounds); 68 if (CC_LIKELY(state.computedState.transform.isPureTranslate())) { 69 // pure translate, so snap (same behavior as onBitmapOp) 70 opBounds.snapToPixelBoundaries(); 71 } 72 storeTexturedRect(rectVerts, opBounds, texCoords); 73 renderer.dirtyRenderTarget(opBounds); 74 } 75 76 const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType) 77 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None; 78 Glop glop; 79 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 80 .setRoundRectClipState(firstState.roundRectClipState) 81 .setMeshTexturedIndexedQuads(vertices, opList.count * 6) 82 .setFillTexturePaint(*texture, textureFillFlags, firstState.op->paint, firstState.alpha) 83 .setTransform(Matrix4::identity(), TransformFlags::None) 84 .setModelViewIdentityEmptyBounds() 85 .build(); 86 ClipRect renderTargetClip(opList.clip); 87 const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr; 88 renderer.renderGlop(nullptr, clip, glop); 89} 90 91void BakedOpDispatcher::onMergedPatchOps(BakedOpRenderer& renderer, 92 const MergedBakedOpList& opList) { 93 const PatchOp& firstOp = *(static_cast<const PatchOp*>(opList.states[0]->op)); 94 const BakedOpState& firstState = *(opList.states[0]); 95 AssetAtlas::Entry* entry = renderer.renderState().assetAtlas().getEntry( 96 firstOp.bitmap->pixelRef()); 97 98 // Batches will usually contain a small number of items so it's 99 // worth performing a first iteration to count the exact number 100 // of vertices we need in the new mesh 101 uint32_t totalVertices = 0; 102 103 for (size_t i = 0; i < opList.count; i++) { 104 const PatchOp& op = *(static_cast<const PatchOp*>(opList.states[i]->op)); 105 106 // TODO: cache mesh lookups 107 const Patch* opMesh = renderer.caches().patchCache.get( 108 entry, op.bitmap->width(), op.bitmap->height(), 109 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch); 110 totalVertices += opMesh->verticesCount; 111 } 112 113 const bool dirtyRenderTarget = renderer.offscreenRenderTarget(); 114 115 uint32_t indexCount = 0; 116 117 TextureVertex vertices[totalVertices]; 118 TextureVertex* vertex = &vertices[0]; 119 // Create a mesh that contains the transformed vertices for all the 120 // 9-patch objects that are part of the batch. Note that onDefer() 121 // enforces ops drawn by this function to have a pure translate or 122 // identity matrix 123 for (size_t i = 0; i < opList.count; i++) { 124 const PatchOp& op = *(static_cast<const PatchOp*>(opList.states[i]->op)); 125 const BakedOpState& state = *opList.states[i]; 126 127 // TODO: cache mesh lookups 128 const Patch* opMesh = renderer.caches().patchCache.get( 129 entry, op.bitmap->width(), op.bitmap->height(), 130 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch); 131 132 133 uint32_t vertexCount = opMesh->verticesCount; 134 if (vertexCount == 0) continue; 135 136 // We use the bounds to know where to translate our vertices 137 // Using patchOp->state.mBounds wouldn't work because these 138 // bounds are clipped 139 const float tx = floorf(state.computedState.transform.getTranslateX() 140 + op.unmappedBounds.left + 0.5f); 141 const float ty = floorf(state.computedState.transform.getTranslateY() 142 + op.unmappedBounds.top + 0.5f); 143 144 // Copy & transform all the vertices for the current operation 145 TextureVertex* opVertices = opMesh->vertices.get(); 146 for (uint32_t j = 0; j < vertexCount; j++, opVertices++) { 147 TextureVertex::set(vertex++, 148 opVertices->x + tx, opVertices->y + ty, 149 opVertices->u, opVertices->v); 150 } 151 152 // Dirty the current layer if possible. When the 9-patch does not 153 // contain empty quads we can take a shortcut and simply set the 154 // dirty rect to the object's bounds. 155 if (dirtyRenderTarget) { 156 if (!opMesh->hasEmptyQuads) { 157 renderer.dirtyRenderTarget(Rect(tx, ty, 158 tx + op.unmappedBounds.getWidth(), ty + op.unmappedBounds.getHeight())); 159 } else { 160 const size_t count = opMesh->quads.size(); 161 for (size_t i = 0; i < count; i++) { 162 const Rect& quadBounds = opMesh->quads[i]; 163 const float x = tx + quadBounds.left; 164 const float y = ty + quadBounds.top; 165 renderer.dirtyRenderTarget(Rect(x, y, 166 x + quadBounds.getWidth(), y + quadBounds.getHeight())); 167 } 168 } 169 } 170 171 indexCount += opMesh->indexCount; 172 } 173 174 175 Texture* texture = entry ? entry->texture : renderer.caches().textureCache.get(firstOp.bitmap); 176 if (!texture) return; 177 const AutoTexture autoCleanup(texture); 178 179 // 9 patches are built for stretching - always filter 180 int textureFillFlags = TextureFillFlags::ForceFilter; 181 if (firstOp.bitmap->colorType() == kAlpha_8_SkColorType) { 182 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture; 183 } 184 Glop glop; 185 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 186 .setRoundRectClipState(firstState.roundRectClipState) 187 .setMeshTexturedIndexedQuads(vertices, indexCount) 188 .setFillTexturePaint(*texture, textureFillFlags, firstOp.paint, firstState.alpha) 189 .setTransform(Matrix4::identity(), TransformFlags::None) 190 .setModelViewIdentityEmptyBounds() 191 .build(); 192 ClipRect renderTargetClip(opList.clip); 193 const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr; 194 renderer.renderGlop(nullptr, clip, glop); 195} 196 197static void renderTextShadow(BakedOpRenderer& renderer, 198 const TextOp& op, const BakedOpState& textOpState) { 199 if (CC_LIKELY(!PaintUtils::hasTextShadow(op.paint))) return; 200 201 FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer(); 202 fontRenderer.setFont(op.paint, SkMatrix::I()); 203 renderer.caches().textureState().activateTexture(0); 204 205 PaintUtils::TextShadow textShadow; 206 if (!PaintUtils::getTextShadow(op.paint, &textShadow)) { 207 LOG_ALWAYS_FATAL("failed to query shadow attributes"); 208 } 209 210 renderer.caches().dropShadowCache.setFontRenderer(fontRenderer); 211 ShadowTexture* texture = renderer.caches().dropShadowCache.get( 212 op.paint, op.glyphs, op.glyphCount, textShadow.radius, op.positions); 213 // If the drop shadow exceeds the max texture size or couldn't be 214 // allocated, skip drawing 215 if (!texture) return; 216 const AutoTexture autoCleanup(texture); 217 218 const float sx = op.x - texture->left + textShadow.dx; 219 const float sy = op.y - texture->top + textShadow.dy; 220 221 Glop glop; 222 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 223 .setRoundRectClipState(textOpState.roundRectClipState) 224 .setMeshTexturedUnitQuad(nullptr) 225 .setFillShadowTexturePaint(*texture, textShadow.color, *op.paint, textOpState.alpha) 226 .setTransform(textOpState.computedState.transform, TransformFlags::None) 227 .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width(), sy + texture->height())) 228 .build(); 229 230 // Compute damage bounds and clip (since may differ from those in textOpState). 231 // Bounds should be same as text op, but with dx/dy offset and radius outset 232 // applied in local space. 233 auto& transform = textOpState.computedState.transform; 234 Rect shadowBounds = op.unmappedBounds; // STROKE 235 const bool expandForStroke = op.paint->getStyle() != SkPaint::kFill_Style; 236 if (expandForStroke) { 237 shadowBounds.outset(op.paint->getStrokeWidth() * 0.5f); 238 } 239 shadowBounds.translate(textShadow.dx, textShadow.dy); 240 shadowBounds.outset(textShadow.radius, textShadow.radius); 241 transform.mapRect(shadowBounds); 242 if (CC_UNLIKELY(expandForStroke && 243 (!transform.isPureTranslate() || op.paint->getStrokeWidth() < 1.0f))) { 244 shadowBounds.outset(0.5f); 245 } 246 247 auto clipState = textOpState.computedState.clipState; 248 if (clipState->mode != ClipMode::Rectangle 249 || !clipState->rect.contains(shadowBounds)) { 250 // need clip, so pass it and clip bounds 251 shadowBounds.doIntersect(clipState->rect); 252 } else { 253 // don't need clip, ignore 254 clipState = nullptr; 255 } 256 257 renderer.renderGlop(&shadowBounds, clipState, glop); 258} 259 260enum class TextRenderType { 261 Defer, 262 Flush 263}; 264 265static void renderText(BakedOpRenderer& renderer, const TextOp& op, const BakedOpState& state, 266 const ClipBase* renderClip, TextRenderType renderType) { 267 FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer(); 268 float x = op.x; 269 float y = op.y; 270 const Matrix4& transform = state.computedState.transform; 271 const bool pureTranslate = transform.isPureTranslate(); 272 if (CC_LIKELY(pureTranslate)) { 273 x = floorf(x + transform.getTranslateX() + 0.5f); 274 y = floorf(y + transform.getTranslateY() + 0.5f); 275 fontRenderer.setFont(op.paint, SkMatrix::I()); 276 fontRenderer.setTextureFiltering(false); 277 } else if (CC_UNLIKELY(transform.isPerspective())) { 278 fontRenderer.setFont(op.paint, SkMatrix::I()); 279 fontRenderer.setTextureFiltering(true); 280 } else { 281 // We only pass a partial transform to the font renderer. That partial 282 // matrix defines how glyphs are rasterized. Typically we want glyphs 283 // to be rasterized at their final size on screen, which means the partial 284 // matrix needs to take the scale factor into account. 285 // When a partial matrix is used to transform glyphs during rasterization, 286 // the mesh is generated with the inverse transform (in the case of scale, 287 // the mesh is generated at 1.0 / scale for instance.) This allows us to 288 // apply the full transform matrix at draw time in the vertex shader. 289 // Applying the full matrix in the shader is the easiest way to handle 290 // rotation and perspective and allows us to always generated quads in the 291 // font renderer which greatly simplifies the code, clipping in particular. 292 float sx, sy; 293 transform.decomposeScale(sx, sy); 294 fontRenderer.setFont(op.paint, SkMatrix::MakeScale( 295 roundf(std::max(1.0f, sx)), 296 roundf(std::max(1.0f, sy)))); 297 fontRenderer.setTextureFiltering(true); 298 } 299 Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 300 301 int alpha = PaintUtils::getAlphaDirect(op.paint) * state.alpha; 302 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(op.paint); 303 TextDrawFunctor functor(&renderer, &state, renderClip, 304 x, y, pureTranslate, alpha, mode, op.paint); 305 306 bool forceFinish = (renderType == TextRenderType::Flush); 307 bool mustDirtyRenderTarget = renderer.offscreenRenderTarget(); 308 const Rect* localOpClip = pureTranslate ? &state.computedState.clipRect() : nullptr; 309 fontRenderer.renderPosText(op.paint, localOpClip, op.glyphs, op.glyphCount, x, y, 310 op.positions, mustDirtyRenderTarget ? &layerBounds : nullptr, &functor, forceFinish); 311 312 if (mustDirtyRenderTarget) { 313 if (!pureTranslate) { 314 transform.mapRect(layerBounds); 315 } 316 renderer.dirtyRenderTarget(layerBounds); 317 } 318} 319 320void BakedOpDispatcher::onMergedTextOps(BakedOpRenderer& renderer, 321 const MergedBakedOpList& opList) { 322 for (size_t i = 0; i < opList.count; i++) { 323 const BakedOpState& state = *(opList.states[i]); 324 const TextOp& op = *(static_cast<const TextOp*>(state.op)); 325 renderTextShadow(renderer, op, state); 326 } 327 328 ClipRect renderTargetClip(opList.clip); 329 const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr; 330 for (size_t i = 0; i < opList.count; i++) { 331 const BakedOpState& state = *(opList.states[i]); 332 const TextOp& op = *(static_cast<const TextOp*>(state.op)); 333 TextRenderType renderType = (i + 1 == opList.count) 334 ? TextRenderType::Flush : TextRenderType::Defer; 335 renderText(renderer, op, state, clip, renderType); 336 } 337} 338 339namespace VertexBufferRenderFlags { 340 enum { 341 Offset = 0x1, 342 ShadowInterp = 0x2, 343 }; 344} 345 346static void renderVertexBuffer(BakedOpRenderer& renderer, const BakedOpState& state, 347 const VertexBuffer& vertexBuffer, float translateX, float translateY, 348 const SkPaint& paint, int vertexBufferRenderFlags) { 349 if (CC_LIKELY(vertexBuffer.getVertexCount())) { 350 bool shadowInterp = vertexBufferRenderFlags & VertexBufferRenderFlags::ShadowInterp; 351 const int transformFlags = vertexBufferRenderFlags & VertexBufferRenderFlags::Offset 352 ? TransformFlags::OffsetByFudgeFactor : 0; 353 Glop glop; 354 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 355 .setRoundRectClipState(state.roundRectClipState) 356 .setMeshVertexBuffer(vertexBuffer, shadowInterp) 357 .setFillPaint(paint, state.alpha) 358 .setTransform(state.computedState.transform, transformFlags) 359 .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds()) 360 .build(); 361 renderer.renderGlop(state, glop); 362 } 363} 364 365static void renderConvexPath(BakedOpRenderer& renderer, const BakedOpState& state, 366 const SkPath& path, const SkPaint& paint) { 367 VertexBuffer vertexBuffer; 368 // TODO: try clipping large paths to viewport 369 PathTessellator::tessellatePath(path, &paint, state.computedState.transform, vertexBuffer); 370 renderVertexBuffer(renderer, state, vertexBuffer, 0.0f, 0.0f, paint, 0); 371} 372 373static void renderPathTexture(BakedOpRenderer& renderer, const BakedOpState& state, 374 float xOffset, float yOffset, PathTexture& texture, const SkPaint& paint) { 375 Rect dest(texture.width(), texture.height()); 376 dest.translate(xOffset + texture.left - texture.offset, 377 yOffset + texture.top - texture.offset); 378 Glop glop; 379 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 380 .setRoundRectClipState(state.roundRectClipState) 381 .setMeshTexturedUnitQuad(nullptr) 382 .setFillPathTexturePaint(texture, paint, state.alpha) 383 .setTransform(state.computedState.transform, TransformFlags::None) 384 .setModelViewMapUnitToRect(dest) 385 .build(); 386 renderer.renderGlop(state, glop); 387} 388 389SkRect getBoundsOfFill(const RecordedOp& op) { 390 SkRect bounds = op.unmappedBounds.toSkRect(); 391 if (op.paint->getStyle() == SkPaint::kStrokeAndFill_Style) { 392 float outsetDistance = op.paint->getStrokeWidth() / 2; 393 bounds.outset(outsetDistance, outsetDistance); 394 } 395 return bounds; 396} 397 398void BakedOpDispatcher::onArcOp(BakedOpRenderer& renderer, const ArcOp& op, const BakedOpState& state) { 399 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180) 400 if (op.paint->getStyle() != SkPaint::kStroke_Style 401 || op.paint->getPathEffect() != nullptr 402 || op.useCenter) { 403 PathTexture* texture = renderer.caches().pathCache.getArc( 404 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), 405 op.startAngle, op.sweepAngle, op.useCenter, op.paint); 406 const AutoTexture holder(texture); 407 if (CC_LIKELY(holder.texture)) { 408 renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top, 409 *texture, *(op.paint)); 410 } 411 } else { 412 SkRect rect = getBoundsOfFill(op); 413 SkPath path; 414 if (op.useCenter) { 415 path.moveTo(rect.centerX(), rect.centerY()); 416 } 417 path.arcTo(rect, op.startAngle, op.sweepAngle, !op.useCenter); 418 if (op.useCenter) { 419 path.close(); 420 } 421 renderConvexPath(renderer, state, path, *(op.paint)); 422 } 423} 424 425void BakedOpDispatcher::onBitmapOp(BakedOpRenderer& renderer, const BitmapOp& op, const BakedOpState& state) { 426 Texture* texture = renderer.getTexture(op.bitmap); 427 if (!texture) return; 428 const AutoTexture autoCleanup(texture); 429 430 const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType) 431 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None; 432 Glop glop; 433 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 434 .setRoundRectClipState(state.roundRectClipState) 435 .setMeshTexturedUnitQuad(texture->uvMapper) 436 .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha) 437 .setTransform(state.computedState.transform, TransformFlags::None) 438 .setModelViewMapUnitToRectSnap(Rect(texture->width(), texture->height())) 439 .build(); 440 renderer.renderGlop(state, glop); 441} 442 443void BakedOpDispatcher::onBitmapMeshOp(BakedOpRenderer& renderer, const BitmapMeshOp& op, const BakedOpState& state) { 444 const static UvMapper defaultUvMapper; 445 const uint32_t elementCount = op.meshWidth * op.meshHeight * 6; 446 447 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]); 448 ColorTextureVertex* vertex = &mesh[0]; 449 450 const int* colors = op.colors; 451 std::unique_ptr<int[]> tempColors; 452 if (!colors) { 453 uint32_t colorsCount = (op.meshWidth + 1) * (op.meshHeight + 1); 454 tempColors.reset(new int[colorsCount]); 455 memset(tempColors.get(), 0xff, colorsCount * sizeof(int)); 456 colors = tempColors.get(); 457 } 458 459 Texture* texture = renderer.renderState().assetAtlas().getEntryTexture(op.bitmap->pixelRef()); 460 const UvMapper& mapper(texture && texture->uvMapper ? *texture->uvMapper : defaultUvMapper); 461 462 for (int32_t y = 0; y < op.meshHeight; y++) { 463 for (int32_t x = 0; x < op.meshWidth; x++) { 464 uint32_t i = (y * (op.meshWidth + 1) + x) * 2; 465 466 float u1 = float(x) / op.meshWidth; 467 float u2 = float(x + 1) / op.meshWidth; 468 float v1 = float(y) / op.meshHeight; 469 float v2 = float(y + 1) / op.meshHeight; 470 471 mapper.map(u1, v1, u2, v2); 472 473 int ax = i + (op.meshWidth + 1) * 2; 474 int ay = ax + 1; 475 int bx = i; 476 int by = bx + 1; 477 int cx = i + 2; 478 int cy = cx + 1; 479 int dx = i + (op.meshWidth + 1) * 2 + 2; 480 int dy = dx + 1; 481 482 const float* vertices = op.vertices; 483 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); 484 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]); 485 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); 486 487 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); 488 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); 489 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]); 490 } 491 } 492 493 if (!texture) { 494 texture = renderer.caches().textureCache.get(op.bitmap); 495 if (!texture) { 496 return; 497 } 498 } 499 const AutoTexture autoCleanup(texture); 500 501 /* 502 * TODO: handle alpha_8 textures correctly by applying paint color, but *not* 503 * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh. 504 */ 505 const int textureFillFlags = TextureFillFlags::None; 506 Glop glop; 507 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 508 .setRoundRectClipState(state.roundRectClipState) 509 .setMeshColoredTexturedMesh(mesh.get(), elementCount) 510 .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha) 511 .setTransform(state.computedState.transform, TransformFlags::None) 512 .setModelViewOffsetRect(0, 0, op.unmappedBounds) 513 .build(); 514 renderer.renderGlop(state, glop); 515} 516 517void BakedOpDispatcher::onBitmapRectOp(BakedOpRenderer& renderer, const BitmapRectOp& op, const BakedOpState& state) { 518 Texture* texture = renderer.getTexture(op.bitmap); 519 if (!texture) return; 520 const AutoTexture autoCleanup(texture); 521 522 Rect uv(std::max(0.0f, op.src.left / texture->width()), 523 std::max(0.0f, op.src.top / texture->height()), 524 std::min(1.0f, op.src.right / texture->width()), 525 std::min(1.0f, op.src.bottom / texture->height())); 526 527 const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType) 528 ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None; 529 const bool tryToSnap = MathUtils::areEqual(op.src.getWidth(), op.unmappedBounds.getWidth()) 530 && MathUtils::areEqual(op.src.getHeight(), op.unmappedBounds.getHeight()); 531 Glop glop; 532 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 533 .setRoundRectClipState(state.roundRectClipState) 534 .setMeshTexturedUvQuad(texture->uvMapper, uv) 535 .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha) 536 .setTransform(state.computedState.transform, TransformFlags::None) 537 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, op.unmappedBounds) 538 .build(); 539 renderer.renderGlop(state, glop); 540} 541 542void BakedOpDispatcher::onColorOp(BakedOpRenderer& renderer, const ColorOp& op, const BakedOpState& state) { 543 SkPaint paint; 544 paint.setColor(op.color); 545 paint.setXfermodeMode(op.mode); 546 547 Glop glop; 548 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 549 .setRoundRectClipState(state.roundRectClipState) 550 .setMeshUnitQuad() 551 .setFillPaint(paint, state.alpha) 552 .setTransform(Matrix4::identity(), TransformFlags::None) 553 .setModelViewMapUnitToRect(state.computedState.clipState->rect) 554 .build(); 555 renderer.renderGlop(state, glop); 556} 557 558void BakedOpDispatcher::onFunctorOp(BakedOpRenderer& renderer, const FunctorOp& op, const BakedOpState& state) { 559 renderer.renderFunctor(op, state); 560} 561 562void BakedOpDispatcher::onLinesOp(BakedOpRenderer& renderer, const LinesOp& op, const BakedOpState& state) { 563 VertexBuffer buffer; 564 PathTessellator::tessellateLines(op.points, op.floatCount, op.paint, 565 state.computedState.transform, buffer); 566 int displayFlags = op.paint->isAntiAlias() ? 0 : VertexBufferRenderFlags::Offset; 567 renderVertexBuffer(renderer, state, buffer, 0, 0, *(op.paint), displayFlags); 568} 569 570void BakedOpDispatcher::onOvalOp(BakedOpRenderer& renderer, const OvalOp& op, const BakedOpState& state) { 571 if (op.paint->getPathEffect() != nullptr) { 572 PathTexture* texture = renderer.caches().pathCache.getOval( 573 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.paint); 574 const AutoTexture holder(texture); 575 if (CC_LIKELY(holder.texture)) { 576 renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top, 577 *texture, *(op.paint)); 578 } 579 } else { 580 SkPath path; 581 SkRect rect = getBoundsOfFill(op); 582 path.addOval(rect); 583 584 if (state.computedState.localProjectionPathMask != nullptr) { 585 // Mask the ripple path by the local space projection mask in local space. 586 // Note that this can create CCW paths. 587 Op(path, *state.computedState.localProjectionPathMask, kIntersect_SkPathOp, &path); 588 } 589 renderConvexPath(renderer, state, path, *(op.paint)); 590 } 591} 592 593void BakedOpDispatcher::onPatchOp(BakedOpRenderer& renderer, const PatchOp& op, const BakedOpState& state) { 594 // 9 patches are built for stretching - always filter 595 int textureFillFlags = TextureFillFlags::ForceFilter; 596 if (op.bitmap->colorType() == kAlpha_8_SkColorType) { 597 textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture; 598 } 599 600 // TODO: avoid redoing the below work each frame: 601 AssetAtlas::Entry* entry = renderer.renderState().assetAtlas().getEntry(op.bitmap->pixelRef()); 602 const Patch* mesh = renderer.caches().patchCache.get( 603 entry, op.bitmap->width(), op.bitmap->height(), 604 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch); 605 606 Texture* texture = entry ? entry->texture : renderer.caches().textureCache.get(op.bitmap); 607 if (CC_LIKELY(texture)) { 608 const AutoTexture autoCleanup(texture); 609 Glop glop; 610 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 611 .setRoundRectClipState(state.roundRectClipState) 612 .setMeshPatchQuads(*mesh) 613 .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha) 614 .setTransform(state.computedState.transform, TransformFlags::None) 615 .setModelViewOffsetRectSnap(op.unmappedBounds.left, op.unmappedBounds.top, 616 Rect(op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight())) 617 .build(); 618 renderer.renderGlop(state, glop); 619 } 620} 621 622void BakedOpDispatcher::onPathOp(BakedOpRenderer& renderer, const PathOp& op, const BakedOpState& state) { 623 PathTexture* texture = renderer.caches().pathCache.get(op.path, op.paint); 624 const AutoTexture holder(texture); 625 if (CC_LIKELY(holder.texture)) { 626 // Unlike other callers to renderPathTexture, no offsets are used because PathOp doesn't 627 // have any translate built in, other than what's in the SkPath itself 628 renderPathTexture(renderer, state, 0, 0, *texture, *(op.paint)); 629 } 630} 631 632void BakedOpDispatcher::onPointsOp(BakedOpRenderer& renderer, const PointsOp& op, const BakedOpState& state) { 633 VertexBuffer buffer; 634 PathTessellator::tessellatePoints(op.points, op.floatCount, op.paint, 635 state.computedState.transform, buffer); 636 int displayFlags = op.paint->isAntiAlias() ? 0 : VertexBufferRenderFlags::Offset; 637 renderVertexBuffer(renderer, state, buffer, 0, 0, *(op.paint), displayFlags); 638} 639 640// See SkPaintDefaults.h 641#define SkPaintDefaults_MiterLimit SkIntToScalar(4) 642 643void BakedOpDispatcher::onRectOp(BakedOpRenderer& renderer, const RectOp& op, const BakedOpState& state) { 644 if (op.paint->getStyle() != SkPaint::kFill_Style) { 645 // only fill + default miter is supported by drawConvexPath, since others must handle joins 646 static_assert(SkPaintDefaults_MiterLimit == 4.0f, "Miter limit has changed"); 647 if (CC_UNLIKELY(op.paint->getPathEffect() != nullptr 648 || op.paint->getStrokeJoin() != SkPaint::kMiter_Join 649 || op.paint->getStrokeMiter() != SkPaintDefaults_MiterLimit)) { 650 PathTexture* texture = renderer.caches().pathCache.getRect( 651 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.paint); 652 const AutoTexture holder(texture); 653 if (CC_LIKELY(holder.texture)) { 654 renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top, 655 *texture, *(op.paint)); 656 } 657 } else { 658 SkPath path; 659 path.addRect(getBoundsOfFill(op)); 660 renderConvexPath(renderer, state, path, *(op.paint)); 661 } 662 } else { 663 if (op.paint->isAntiAlias() && !state.computedState.transform.isSimple()) { 664 SkPath path; 665 path.addRect(op.unmappedBounds.toSkRect()); 666 renderConvexPath(renderer, state, path, *(op.paint)); 667 } else { 668 // render simple unit quad, no tessellation required 669 Glop glop; 670 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 671 .setRoundRectClipState(state.roundRectClipState) 672 .setMeshUnitQuad() 673 .setFillPaint(*op.paint, state.alpha) 674 .setTransform(state.computedState.transform, TransformFlags::None) 675 .setModelViewMapUnitToRect(op.unmappedBounds) 676 .build(); 677 renderer.renderGlop(state, glop); 678 } 679 } 680} 681 682void BakedOpDispatcher::onRoundRectOp(BakedOpRenderer& renderer, const RoundRectOp& op, const BakedOpState& state) { 683 if (op.paint->getPathEffect() != nullptr) { 684 PathTexture* texture = renderer.caches().pathCache.getRoundRect( 685 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), 686 op.rx, op.ry, op.paint); 687 const AutoTexture holder(texture); 688 if (CC_LIKELY(holder.texture)) { 689 renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top, 690 *texture, *(op.paint)); 691 } 692 } else { 693 const VertexBuffer* buffer = renderer.caches().tessellationCache.getRoundRect( 694 state.computedState.transform, *(op.paint), 695 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry); 696 renderVertexBuffer(renderer, state, *buffer, 697 op.unmappedBounds.left, op.unmappedBounds.top, *(op.paint), 0); 698 } 699} 700 701static void renderShadow(BakedOpRenderer& renderer, const BakedOpState& state, float casterAlpha, 702 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) { 703 SkPaint paint; 704 paint.setAntiAlias(true); // want to use AlphaVertex 705 706 // The caller has made sure casterAlpha > 0. 707 uint8_t ambientShadowAlpha = renderer.getLightInfo().ambientShadowAlpha; 708 if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) { 709 ambientShadowAlpha = Properties::overrideAmbientShadowStrength; 710 } 711 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) { 712 paint.setAlpha((uint8_t)(casterAlpha * ambientShadowAlpha)); 713 renderVertexBuffer(renderer, state, *ambientShadowVertexBuffer, 0, 0, 714 paint, VertexBufferRenderFlags::ShadowInterp); 715 } 716 717 uint8_t spotShadowAlpha = renderer.getLightInfo().spotShadowAlpha; 718 if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) { 719 spotShadowAlpha = Properties::overrideSpotShadowStrength; 720 } 721 if (spotShadowVertexBuffer && spotShadowAlpha > 0) { 722 paint.setAlpha((uint8_t)(casterAlpha * spotShadowAlpha)); 723 renderVertexBuffer(renderer, state, *spotShadowVertexBuffer, 0, 0, 724 paint, VertexBufferRenderFlags::ShadowInterp); 725 } 726} 727 728void BakedOpDispatcher::onShadowOp(BakedOpRenderer& renderer, const ShadowOp& op, const BakedOpState& state) { 729 TessellationCache::vertexBuffer_pair_t buffers = op.shadowTask->getResult(); 730 renderShadow(renderer, state, op.casterAlpha, buffers.first, buffers.second); 731} 732 733void BakedOpDispatcher::onSimpleRectsOp(BakedOpRenderer& renderer, const SimpleRectsOp& op, const BakedOpState& state) { 734 Glop glop; 735 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 736 .setRoundRectClipState(state.roundRectClipState) 737 .setMeshIndexedQuads(&op.vertices[0], op.vertexCount / 4) 738 .setFillPaint(*op.paint, state.alpha) 739 .setTransform(state.computedState.transform, TransformFlags::None) 740 .setModelViewOffsetRect(0, 0, op.unmappedBounds) 741 .build(); 742 renderer.renderGlop(state, glop); 743} 744 745void BakedOpDispatcher::onTextOp(BakedOpRenderer& renderer, const TextOp& op, const BakedOpState& state) { 746 renderTextShadow(renderer, op, state); 747 renderText(renderer, op, state, state.computedState.getClipIfNeeded(), TextRenderType::Flush); 748} 749 750void BakedOpDispatcher::onTextOnPathOp(BakedOpRenderer& renderer, const TextOnPathOp& op, const BakedOpState& state) { 751 // Note: can't trust clipSideFlags since we record with unmappedBounds == clip. 752 // TODO: respect clipSideFlags, once we record with bounds 753 auto renderTargetClip = state.computedState.clipState; 754 755 FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer(); 756 fontRenderer.setFont(op.paint, SkMatrix::I()); 757 fontRenderer.setTextureFiltering(true); 758 759 Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 760 761 int alpha = PaintUtils::getAlphaDirect(op.paint) * state.alpha; 762 SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(op.paint); 763 TextDrawFunctor functor(&renderer, &state, renderTargetClip, 764 0.0f, 0.0f, false, alpha, mode, op.paint); 765 766 bool mustDirtyRenderTarget = renderer.offscreenRenderTarget(); 767 const Rect localSpaceClip = state.computedState.computeLocalSpaceClip(); 768 if (fontRenderer.renderTextOnPath(op.paint, &localSpaceClip, op.glyphs, op.glyphCount, 769 op.path, op.hOffset, op.vOffset, 770 mustDirtyRenderTarget ? &layerBounds : nullptr, &functor)) { 771 if (mustDirtyRenderTarget) { 772 // manually dirty render target, since TextDrawFunctor won't 773 state.computedState.transform.mapRect(layerBounds); 774 renderer.dirtyRenderTarget(layerBounds); 775 } 776 } 777} 778 779void BakedOpDispatcher::onTextureLayerOp(BakedOpRenderer& renderer, const TextureLayerOp& op, const BakedOpState& state) { 780 const bool tryToSnap = !op.layer->getForceFilter(); 781 float alpha = (op.layer->getAlpha() / 255.0f) * state.alpha; 782 Glop glop; 783 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 784 .setRoundRectClipState(state.roundRectClipState) 785 .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO 786 .setFillTextureLayer(*(op.layer), alpha) 787 .setTransform(state.computedState.transform, TransformFlags::None) 788 .setModelViewMapUnitToRectOptionalSnap(tryToSnap, Rect(op.layer->getWidth(), op.layer->getHeight())) 789 .build(); 790 renderer.renderGlop(state, glop); 791} 792 793void BakedOpDispatcher::onLayerOp(BakedOpRenderer& renderer, const LayerOp& op, const BakedOpState& state) { 794 // Note that we don't use op->paint in this function - it's never set on a LayerOp 795 OffscreenBuffer* buffer = *op.layerHandle; 796 797 if (CC_UNLIKELY(!buffer)) { 798 // Layer was not allocated, which can occur if there were no draw ops inside. We draw the 799 // equivalent by drawing a rect with the same layer properties (alpha/xfer/filter). 800 SkPaint paint; 801 paint.setAlpha(op.alpha * 255); 802 paint.setXfermodeMode(op.mode); 803 paint.setColorFilter(op.colorFilter); 804 RectOp rectOp(op.unmappedBounds, op.localMatrix, op.localClip, &paint); 805 BakedOpDispatcher::onRectOp(renderer, rectOp, state); 806 } else { 807 float layerAlpha = op.alpha * state.alpha; 808 Glop glop; 809 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 810 .setRoundRectClipState(state.roundRectClipState) 811 .setMeshTexturedIndexedVbo(buffer->vbo, buffer->elementCount) 812 .setFillLayer(buffer->texture, op.colorFilter, layerAlpha, op.mode, Blend::ModeOrderSwap::NoSwap) 813 .setTransform(state.computedState.transform, TransformFlags::None) 814 .setModelViewOffsetRectSnap(op.unmappedBounds.left, op.unmappedBounds.top, 815 Rect(op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight())) 816 .build(); 817 renderer.renderGlop(state, glop); 818 } 819} 820 821void BakedOpDispatcher::onCopyToLayerOp(BakedOpRenderer& renderer, const CopyToLayerOp& op, const BakedOpState& state) { 822 LOG_ALWAYS_FATAL_IF(*(op.layerHandle) != nullptr, "layer already exists!"); 823 *(op.layerHandle) = renderer.copyToLayer(state.computedState.clippedBounds); 824 LOG_ALWAYS_FATAL_IF(*op.layerHandle == nullptr, "layer copy failed"); 825} 826 827void BakedOpDispatcher::onCopyFromLayerOp(BakedOpRenderer& renderer, const CopyFromLayerOp& op, const BakedOpState& state) { 828 LOG_ALWAYS_FATAL_IF(*op.layerHandle == nullptr, "no layer to draw underneath!"); 829 if (!state.computedState.clippedBounds.isEmpty()) { 830 if (op.paint && op.paint->getAlpha() < 255) { 831 SkPaint layerPaint; 832 layerPaint.setAlpha(op.paint->getAlpha()); 833 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); 834 layerPaint.setColorFilter(op.paint->getColorFilter()); 835 RectOp rectOp(state.computedState.clippedBounds, Matrix4::identity(), nullptr, &layerPaint); 836 BakedOpDispatcher::onRectOp(renderer, rectOp, state); 837 } 838 839 OffscreenBuffer& layer = **(op.layerHandle); 840 auto mode = PaintUtils::getXfermodeDirect(op.paint); 841 Glop glop; 842 GlopBuilder(renderer.renderState(), renderer.caches(), &glop) 843 .setRoundRectClipState(state.roundRectClipState) 844 .setMeshTexturedUvQuad(nullptr, layer.getTextureCoordinates()) 845 .setFillLayer(layer.texture, nullptr, 1.0f, mode, Blend::ModeOrderSwap::Swap) 846 .setTransform(state.computedState.transform, TransformFlags::None) 847 .setModelViewMapUnitToRect(state.computedState.clippedBounds) 848 .build(); 849 renderer.renderGlop(state, glop); 850 } 851 renderer.renderState().layerPool().putOrDelete(*op.layerHandle); 852} 853 854} // namespace uirenderer 855} // namespace android 856