SkRecordOpts.cpp revision f5bf3cf0257dc3d18932bde51f8eae33442e071f
1c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org/* 2c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * Copyright 2014 Google Inc. 3c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * 4c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be 5c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * found in the LICENSE file. 6c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org */ 7c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org 8ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org#include "SkRecordOpts.h" 9506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 1073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org#include "SkRecordPattern.h" 11506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org#include "SkRecords.h" 12506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org#include "SkTDArray.h" 13506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 1473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgusing namespace SkRecords; 1573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org 16ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.orgvoid SkRecordOptimize(SkRecord* record) { 1788c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // TODO(mtklein): fuse independent optimizations to reduce number of passes? 1888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkRecordNoopSaveRestores(record); 19f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SkRecordNoopSaveLayerDrawRestores(record); 20ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org SkRecordAnnotateCullingPairs(record); 2188c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkRecordReduceDrawPosTextStrength(record); // Helpful to run this before BoundDrawPosTextH. 2288c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkRecordBoundDrawPosTextH(record); 23ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org} 24ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org 2573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Most of the optimizations in this file are pattern-based. These are all defined as structs with: 2673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// - a Pattern typedef 2773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method, 2873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// which returns true if it made changes and false if not. 292e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 3073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Run a pattern-based optimization once across the SkRecord, returning true if it made any changes. 3173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern, 3273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// record, and [begin,end) span of the commands that matched. 3373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgtemplate <typename Pass> 3473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstatic bool apply(Pass* pass, SkRecord* record) { 3573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typename Pass::Pattern pattern; 3673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool changed = false; 3773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned begin, end = 0; 382e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 3973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org while (pattern.search(record, &begin, &end)) { 4073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org changed |= pass->onMatch(record, &pattern, begin, end); 412e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 4273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return changed; 4373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 442e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 4573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops. 4673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstruct SaveRestoreNooper { 4773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // Star matches greedily, so we also have to exclude Save and Restore. 4873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typedef Pattern3<Is<Save>, 4973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Star<Not<Or3<Is<Save>, 5073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Is<Restore>, 5173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org IsDraw> > >, 5273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Is<Restore> > 5373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Pattern; 5473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org 5573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 5673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // If restore doesn't revert both matrix and clip, this isn't safe to noop away. 5773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org if (pattern->first<Save>()->flags != SkCanvas::kMatrixClip_SaveFlag) { 5873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; 592e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 602e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 6173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // The entire span between Save and Restore (inclusively) does nothing. 6273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org for (unsigned i = begin; i < end; i++) { 6373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org record->replace<NoOp>(i); 6473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 6573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return true; 662e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 6788c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org}; 6873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordNoopSaveRestores(SkRecord* record) { 6973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SaveRestoreNooper pass; 7073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org while (apply(&pass, record)); // Run until it stops changing things. 7173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 7288c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 73f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org// For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the 74f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org// draw, and no-op the SaveLayer and Restore. 75f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.orgstruct SaveLayerDrawRestoreNooper { 76f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org typedef Pattern3<Is<SaveLayer>, IsDraw, Is<Restore> > Pattern; 77f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 78f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 79f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SaveLayer* saveLayer = pattern->first<SaveLayer>(); 80f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (saveLayer->bounds != NULL) { 81f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // SaveLayer with bounds is too tricky for us. 82f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return false; 83f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 84f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 85f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SkPaint* layerPaint = saveLayer->paint; 86f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (NULL == layerPaint) { 87f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // There wasn't really any point to this SaveLayer at all. 88f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return KillSaveLayerAndRestore(record, begin); 89f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 90f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 91f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SkPaint* drawPaint = pattern->second<SkPaint>(); 92f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (drawPaint == NULL) { 93f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // We can just give the draw the SaveLayer's paint. 94f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // TODO(mtklein): figure out how to do this clearly 95f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return false; 96f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 97f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 98f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org const uint32_t layerColor = layerPaint->getColor(); 99f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org const uint32_t drawColor = drawPaint->getColor(); 100f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (!IsOnlyAlpha(layerColor) || !IsOpaque(drawColor) || HasAnyEffect(*layerPaint)) { 101f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // Too fancy for us. Actually, as long as layerColor is just an alpha 102f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // we can blend it into drawColor's alpha; drawColor doesn't strictly have to be opaque. 103f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return false; 104f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 105f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 106f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org drawPaint->setColor(SkColorSetA(drawColor, SkColorGetA(layerColor))); 107f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return KillSaveLayerAndRestore(record, begin); 108f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 109f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 110f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerIndex) { 111f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org record->replace<NoOp>(saveLayerIndex); // SaveLayer 112f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org record->replace<NoOp>(saveLayerIndex+2); // Restore 113f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return true; 114f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 115f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 116f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool HasAnyEffect(const SkPaint& paint) { 117f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return paint.getPathEffect() || 118f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getShader() || 119f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getXfermode() || 120f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getMaskFilter() || 121f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getColorFilter() || 122f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getRasterizer() || 123f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getLooper() || 124f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getImageFilter(); 125f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 126f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 127f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool IsOpaque(SkColor color) { 128f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return SkColorGetA(color) == SK_AlphaOPAQUE; 129f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 130f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool IsOnlyAlpha(SkColor color) { 131f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); 132f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 133f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org}; 134f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.orgvoid SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { 135f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SaveLayerDrawRestoreNooper pass; 136f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org apply(&pass, record); 137f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org} 138f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 139f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 14088c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org// Replaces DrawPosText with DrawPosTextH when all Y coordinates are equal. 14173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstruct StrengthReducer { 14273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typedef Pattern1<Is<DrawPosText> > Pattern; 143506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 14473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 14573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkASSERT(end == begin + 1); 14673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org DrawPosText* draw = pattern->first<DrawPosText>(); 147506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 14873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org const unsigned points = draw->paint.countText(draw->text, draw->byteLength); 1492e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org if (points == 0) { 15073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; // No point (ha!). 15188c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 15288c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 15373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org const SkScalar firstY = draw->pos[0].fY; 1542e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org for (unsigned i = 1; i < points; i++) { 15573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org if (draw->pos[i].fY != firstY) { 15673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; // Needs full power of DrawPosText. 1572e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 1582e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 1592e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // All ys are the same. We can replace DrawPosText with DrawPosTextH. 1602e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 16173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // draw->pos is points SkPoints, [(x,y),(x,y),(x,y),(x,y), ... ]. 1622e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // We're going to squint and look at that as 2*points SkScalars, [x,y,x,y,x,y,x,y, ...]. 1632e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // Then we'll rearrange things so all the xs are in order up front, clobbering the ys. 1642e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SK_COMPILE_ASSERT(sizeof(SkPoint) == 2 * sizeof(SkScalar), SquintingIsNotSafe); 16573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkScalar* scalars = &draw->pos[0].fX; 1662e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org for (unsigned i = 0; i < 2*points; i += 2) { 1672e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org scalars[i/2] = scalars[i]; 1682e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 169506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 17073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // Extend lifetime of draw to the end of the loop so we can copy its paint. 17173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Adopted<DrawPosText> adopted(draw); 17273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkNEW_PLACEMENT_ARGS(record->replace<DrawPosTextH>(begin, adopted), 17373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org DrawPosTextH, 17473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org (draw->text, draw->byteLength, scalars, firstY, draw->paint)); 17573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return true; 1762e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 1772e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org}; 17873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordReduceDrawPosTextStrength(SkRecord* record) { 17973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org StrengthReducer pass; 18073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org apply(&pass, record); 18173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 182506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 18388c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org// Tries to replace DrawPosTextH with BoundedDrawPosTextH, which knows conservative upper and lower 18488c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org// bounds to use with SkCanvas::quickRejectY. 18573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstruct TextBounder { 18673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typedef Pattern1<Is<DrawPosTextH> > Pattern; 18788c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 18873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 18973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkASSERT(end == begin + 1); 19073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org DrawPosTextH* draw = pattern->first<DrawPosTextH>(); 19188c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 1922e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // If we're drawing vertical text, none of the checks we're about to do make any sense. 1932e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // We'll need to call SkPaint::computeFastBounds() later, so bail if that's not possible. 19473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org if (draw->paint.isVerticalText() || !draw->paint.canComputeFastBounds()) { 19573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; 1962e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 1972e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 1982e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // Rather than checking the top and bottom font metrics, we guess. Actually looking up the 1992e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // top and bottom metrics is slow, and this overapproximation should be good enough. 20073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org const SkScalar buffer = draw->paint.getTextSize() * 1.5f; 2012e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkDEBUGCODE(SkPaint::FontMetrics metrics;) 20273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkDEBUGCODE(draw->paint.getFontMetrics(&metrics);) 2032e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkASSERT(-buffer <= metrics.fTop); 2042e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkASSERT(+buffer >= metrics.fBottom); 2052e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 2062e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // Let the paint adjust the text bounds. We don't care about left and right here, so we use 2072e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // 0 and 1 respectively just so the bounds rectangle isn't empty. 2082e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkRect bounds; 20973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bounds.set(0, draw->y - buffer, SK_Scalar1, draw->y + buffer); 21073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkRect adjusted = draw->paint.computeFastBounds(bounds, &bounds); 21173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org 21273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Adopted<DrawPosTextH> adopted(draw); 21373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkNEW_PLACEMENT_ARGS(record->replace<BoundedDrawPosTextH>(begin, adopted), 21473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org BoundedDrawPosTextH, 21573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org (&adopted, adjusted.fTop, adjusted.fBottom)); 21673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return true; 21788c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 2182e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org}; 21973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordBoundDrawPosTextH(SkRecord* record) { 22073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org TextBounder pass; 22173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org apply(&pass, record); 22273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 22388c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 22473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Replaces PushCull with PairedPushCull, which lets us skip to the paired PopCull when the canvas 22573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// can quickReject the cull rect. 22673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// There's no efficient way (yet?) to express this one as a pattern, so we write a custom pass. 22773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgclass CullAnnotator { 22873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgpublic: 22973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // Do nothing to most ops. 23073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org template <typename T> void operator()(T*) {} 23188c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 23273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org void operator()(PushCull* push) { 23373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Pair pair = { fIndex, push }; 23473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org fPushStack.push(pair); 23588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 236506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 23773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org void operator()(PopCull* pop) { 23873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Pair push = fPushStack.top(); 23973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org fPushStack.pop(); 240506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 24173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkASSERT(fIndex > push.index); 24273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned skip = fIndex - push.index; 24388c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 24473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Adopted<PushCull> adopted(push.command); 24573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkNEW_PLACEMENT_ARGS(fRecord->replace<PairedPushCull>(push.index, adopted), 24673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org PairedPushCull, (&adopted, skip)); 24773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 24888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 24973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org void apply(SkRecord* record) { 25073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org for (fRecord = record, fIndex = 0; fIndex < record->count(); fIndex++) { 25173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org fRecord->mutate(fIndex, *this); 25273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 25373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 254506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 25573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgprivate: 25673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org struct Pair { 25773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned index; 25873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org PushCull* command; 25973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org }; 26088c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 26173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkTDArray<Pair> fPushStack; 26273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkRecord* fRecord; 26373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned fIndex; 26473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org}; 26573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordAnnotateCullingPairs(SkRecord* record) { 26673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org CullAnnotator pass; 26773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org pass.apply(record); 26888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org} 269