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? 181e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org SkRecordNoopCulls(record); 1988c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkRecordNoopSaveRestores(record); 2090b5a2a653b312ff9bcd7102412da2dbeb52368ccommit-bot@chromium.org // TODO(mtklein): figure out why we draw differently and reenable 2190b5a2a653b312ff9bcd7102412da2dbeb52368ccommit-bot@chromium.org //SkRecordNoopSaveLayerDrawRestores(record); 221e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org 23ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org SkRecordAnnotateCullingPairs(record); 2488c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkRecordReduceDrawPosTextStrength(record); // Helpful to run this before BoundDrawPosTextH. 2588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkRecordBoundDrawPosTextH(record); 26ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org} 27ad8ce572f69633ffebe2fa486275d82a5e9a71fecommit-bot@chromium.org 2873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Most of the optimizations in this file are pattern-based. These are all defined as structs with: 2973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// - a Pattern typedef 3073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method, 3173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// which returns true if it made changes and false if not. 322e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 3373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Run a pattern-based optimization once across the SkRecord, returning true if it made any changes. 3473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern, 3573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// record, and [begin,end) span of the commands that matched. 3673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgtemplate <typename Pass> 3773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstatic bool apply(Pass* pass, SkRecord* record) { 3873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typename Pass::Pattern pattern; 3973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool changed = false; 4073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned begin, end = 0; 412e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 4273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org while (pattern.search(record, &begin, &end)) { 4373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org changed |= pass->onMatch(record, &pattern, begin, end); 442e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 4573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return changed; 4673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 472e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 481e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.orgstruct CullNooper { 491e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org typedef Pattern3<Is<PushCull>, Star<Is<NoOp> >, Is<PopCull> > Pattern; 501e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org 511e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 521e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org record->replace<NoOp>(begin); // PushCull 531e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org record->replace<NoOp>(end-1); // PopCull 541e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org return true; 551e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org } 561e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org}; 571e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org 581e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.orgvoid SkRecordNoopCulls(SkRecord* record) { 591e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org CullNooper pass; 601e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org while (apply(&pass, record)); 611e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org} 621e44730ade943bba928f289ce9f59430c50c71e5commit-bot@chromium.org 63467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org// Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into actual NoOps. 64467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.orgstruct SaveOnlyDrawsRestoreNooper { 65467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org typedef Pattern3<Is<Save>, 66467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org Star<Or<Is<NoOp>, IsDraw> >, 67467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org Is<Restore> > 68467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org Pattern; 69467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org 70467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 71467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org record->replace<NoOp>(begin); // Save 72467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org record->replace<NoOp>(end-1); // Restore 73467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org return true; 74467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org } 75467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org}; 7673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops. 77467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.orgstruct SaveNoDrawsRestoreNooper { 7873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // Star matches greedily, so we also have to exclude Save and Restore. 7973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typedef Pattern3<Is<Save>, 8073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Star<Not<Or3<Is<Save>, 8173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Is<Restore>, 8273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org IsDraw> > >, 8373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Is<Restore> > 8473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Pattern; 8573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org 8673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 8773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // If restore doesn't revert both matrix and clip, this isn't safe to noop away. 8873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org if (pattern->first<Save>()->flags != SkCanvas::kMatrixClip_SaveFlag) { 8973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; 902e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 912e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 9273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // The entire span between Save and Restore (inclusively) does nothing. 9373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org for (unsigned i = begin; i < end; i++) { 9473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org record->replace<NoOp>(i); 9573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 9673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return true; 972e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 9888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org}; 9973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordNoopSaveRestores(SkRecord* record) { 100467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org SaveOnlyDrawsRestoreNooper onlyDraws; 101467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org SaveNoDrawsRestoreNooper noDraws; 102467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org 103467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org // Run until they stop changing things. 104467705adf05ba99bbd9ccdf6a40eb463484a6fbfcommit-bot@chromium.org while (apply(&onlyDraws, record) || apply(&noDraws, record)); 10573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 10688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 107f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org// For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the 108f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org// draw, and no-op the SaveLayer and Restore. 109f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.orgstruct SaveLayerDrawRestoreNooper { 110f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org typedef Pattern3<Is<SaveLayer>, IsDraw, Is<Restore> > Pattern; 111f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 112f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 113f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SaveLayer* saveLayer = pattern->first<SaveLayer>(); 114f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (saveLayer->bounds != NULL) { 115f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // SaveLayer with bounds is too tricky for us. 116f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return false; 117f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 118f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 119f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SkPaint* layerPaint = saveLayer->paint; 120f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (NULL == layerPaint) { 121f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // There wasn't really any point to this SaveLayer at all. 122f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return KillSaveLayerAndRestore(record, begin); 123f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 124f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 125f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SkPaint* drawPaint = pattern->second<SkPaint>(); 126f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org if (drawPaint == NULL) { 127f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // We can just give the draw the SaveLayer's paint. 128f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // TODO(mtklein): figure out how to do this clearly 129f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return false; 130f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 131f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 132f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org const uint32_t layerColor = layerPaint->getColor(); 133f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org const uint32_t drawColor = drawPaint->getColor(); 134ee7e23d13f5400716715c1823ad6e41d9a5904cacommit-bot@chromium.org if (!IsOnlyAlpha(layerColor) || !IsOpaque(drawColor) || 135ee7e23d13f5400716715c1823ad6e41d9a5904cacommit-bot@chromium.org HasAnyEffect(*layerPaint) || HasAnyEffect(*drawPaint)) { 136f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // Too fancy for us. Actually, as long as layerColor is just an alpha 137f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org // we can blend it into drawColor's alpha; drawColor doesn't strictly have to be opaque. 138f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return false; 139f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 140f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 141f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org drawPaint->setColor(SkColorSetA(drawColor, SkColorGetA(layerColor))); 142f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return KillSaveLayerAndRestore(record, begin); 143f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 144f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 145f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerIndex) { 146f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org record->replace<NoOp>(saveLayerIndex); // SaveLayer 147f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org record->replace<NoOp>(saveLayerIndex+2); // Restore 148f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return true; 149f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 150f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 151f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool HasAnyEffect(const SkPaint& paint) { 152f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return paint.getPathEffect() || 153f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getShader() || 154f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getXfermode() || 155f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getMaskFilter() || 156f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getColorFilter() || 157f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getRasterizer() || 158f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getLooper() || 159f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org paint.getImageFilter(); 160f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 161f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 162f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool IsOpaque(SkColor color) { 163f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return SkColorGetA(color) == SK_AlphaOPAQUE; 164f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 165f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org static bool IsOnlyAlpha(SkColor color) { 166f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); 167f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org } 168f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org}; 169f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.orgvoid SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { 170f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org SaveLayerDrawRestoreNooper pass; 171f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org apply(&pass, record); 172f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org} 173f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 174f5bf3cf0257dc3d18932bde51f8eae33442e071fcommit-bot@chromium.org 17588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org// Replaces DrawPosText with DrawPosTextH when all Y coordinates are equal. 17673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstruct StrengthReducer { 17773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typedef Pattern1<Is<DrawPosText> > Pattern; 178506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 17973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 18073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkASSERT(end == begin + 1); 18173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org DrawPosText* draw = pattern->first<DrawPosText>(); 182506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 18373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org const unsigned points = draw->paint.countText(draw->text, draw->byteLength); 1842e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org if (points == 0) { 18573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; // No point (ha!). 18688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 18788c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 18873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org const SkScalar firstY = draw->pos[0].fY; 1892e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org for (unsigned i = 1; i < points; i++) { 19073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org if (draw->pos[i].fY != firstY) { 19173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; // Needs full power of DrawPosText. 1922e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 1932e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 1942e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // All ys are the same. We can replace DrawPosText with DrawPosTextH. 1952e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 19673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // draw->pos is points SkPoints, [(x,y),(x,y),(x,y),(x,y), ... ]. 1972e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // We're going to squint and look at that as 2*points SkScalars, [x,y,x,y,x,y,x,y, ...]. 1982e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // Then we'll rearrange things so all the xs are in order up front, clobbering the ys. 1992e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SK_COMPILE_ASSERT(sizeof(SkPoint) == 2 * sizeof(SkScalar), SquintingIsNotSafe); 20073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkScalar* scalars = &draw->pos[0].fX; 2012e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org for (unsigned i = 0; i < 2*points; i += 2) { 2022e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org scalars[i/2] = scalars[i]; 2032e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 204506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 20573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // Extend lifetime of draw to the end of the loop so we can copy its paint. 20673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Adopted<DrawPosText> adopted(draw); 20773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkNEW_PLACEMENT_ARGS(record->replace<DrawPosTextH>(begin, adopted), 20873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org DrawPosTextH, 20937f6e62f114b72d34bcd3140b16b3b30fe5750c8commit-bot@chromium.org (draw->paint, draw->text, draw->byteLength, scalars, firstY)); 21073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return true; 2112e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 2122e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org}; 21373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordReduceDrawPosTextStrength(SkRecord* record) { 21473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org StrengthReducer pass; 21573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org apply(&pass, record); 21673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 217506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 21888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org// Tries to replace DrawPosTextH with BoundedDrawPosTextH, which knows conservative upper and lower 21988c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org// bounds to use with SkCanvas::quickRejectY. 22073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgstruct TextBounder { 22173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org typedef Pattern1<Is<DrawPosTextH> > Pattern; 22288c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 22373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { 22473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkASSERT(end == begin + 1); 22573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org DrawPosTextH* draw = pattern->first<DrawPosTextH>(); 22688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 2272e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // If we're drawing vertical text, none of the checks we're about to do make any sense. 2282e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // We'll need to call SkPaint::computeFastBounds() later, so bail if that's not possible. 22973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org if (draw->paint.isVerticalText() || !draw->paint.canComputeFastBounds()) { 23073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return false; 2312e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org } 2322e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 2332e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // Rather than checking the top and bottom font metrics, we guess. Actually looking up the 2342e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // top and bottom metrics is slow, and this overapproximation should be good enough. 23573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org const SkScalar buffer = draw->paint.getTextSize() * 1.5f; 2362e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkDEBUGCODE(SkPaint::FontMetrics metrics;) 23773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkDEBUGCODE(draw->paint.getFontMetrics(&metrics);) 2382e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkASSERT(-buffer <= metrics.fTop); 2392e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkASSERT(+buffer >= metrics.fBottom); 2402e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org 2412e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // Let the paint adjust the text bounds. We don't care about left and right here, so we use 2422e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org // 0 and 1 respectively just so the bounds rectangle isn't empty. 2432e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org SkRect bounds; 24473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org bounds.set(0, draw->y - buffer, SK_Scalar1, draw->y + buffer); 24573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkRect adjusted = draw->paint.computeFastBounds(bounds, &bounds); 24673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org 24773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Adopted<DrawPosTextH> adopted(draw); 24873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkNEW_PLACEMENT_ARGS(record->replace<BoundedDrawPosTextH>(begin, adopted), 24973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org BoundedDrawPosTextH, 25073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org (&adopted, adjusted.fTop, adjusted.fBottom)); 25173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org return true; 25288c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 2532e0c32af0508a1e544c9953ea2fe128dbae7d429commit-bot@chromium.org}; 25473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordBoundDrawPosTextH(SkRecord* record) { 25573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org TextBounder pass; 25673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org apply(&pass, record); 25773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org} 25888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 25973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// Replaces PushCull with PairedPushCull, which lets us skip to the paired PopCull when the canvas 26073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// can quickReject the cull rect. 26173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org// There's no efficient way (yet?) to express this one as a pattern, so we write a custom pass. 26273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgclass CullAnnotator { 26373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgpublic: 26473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org // Do nothing to most ops. 26573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org template <typename T> void operator()(T*) {} 26688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 26773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org void operator()(PushCull* push) { 26873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Pair pair = { fIndex, push }; 26973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org fPushStack.push(pair); 27088c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 271506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 27273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org void operator()(PopCull* pop) { 27373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Pair push = fPushStack.top(); 27473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org fPushStack.pop(); 275506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 27673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkASSERT(fIndex > push.index); 27773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned skip = fIndex - push.index; 27888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 27973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org Adopted<PushCull> adopted(push.command); 28073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkNEW_PLACEMENT_ARGS(fRecord->replace<PairedPushCull>(push.index, adopted), 28173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org PairedPushCull, (&adopted, skip)); 28273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 28388c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 28473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org void apply(SkRecord* record) { 28573fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org for (fRecord = record, fIndex = 0; fIndex < record->count(); fIndex++) { 286c71da1f6ed3a5e1a3eb2dd860b8129e1b423f9f4commit-bot@chromium.org fRecord->mutate<void>(fIndex, *this); 28773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 28873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org } 289506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 29073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgprivate: 29173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org struct Pair { 29273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned index; 29373fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org PushCull* command; 29473fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org }; 29588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 29673fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkTDArray<Pair> fPushStack; 29773fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org SkRecord* fRecord; 29873fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org unsigned fIndex; 29973fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org}; 30073fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.orgvoid SkRecordAnnotateCullingPairs(SkRecord* record) { 30173fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org CullAnnotator pass; 30273fffeb83aab56bc8c2c5ce143ee9d132d64ac37commit-bot@chromium.org pass.apply(record); 30388c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org} 304