diff --git a/src/record/SkRecordOpts.cpp b/src/record/SkRecordOpts.cpp index aaa611cf39..5b648f4215 100644 --- a/src/record/SkRecordOpts.cpp +++ b/src/record/SkRecordOpts.cpp @@ -16,6 +16,7 @@ using namespace SkRecords; void SkRecordOptimize(SkRecord* record) { // TODO(mtklein): fuse independent optimizations to reduce number of passes? SkRecordNoopSaveRestores(record); + SkRecordNoopSaveLayerDrawRestores(record); SkRecordAnnotateCullingPairs(record); SkRecordReduceDrawPosTextStrength(record); // Helpful to run this before BoundDrawPosTextH. SkRecordBoundDrawPosTextH(record); @@ -69,6 +70,73 @@ void SkRecordNoopSaveRestores(SkRecord* record) { while (apply(&pass, record)); // Run until it stops changing things. } +// For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the +// draw, and no-op the SaveLayer and Restore. +struct SaveLayerDrawRestoreNooper { + typedef Pattern3, IsDraw, Is > Pattern; + + bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) { + SaveLayer* saveLayer = pattern->first(); + if (saveLayer->bounds != NULL) { + // SaveLayer with bounds is too tricky for us. + return false; + } + + SkPaint* layerPaint = saveLayer->paint; + if (NULL == layerPaint) { + // There wasn't really any point to this SaveLayer at all. + return KillSaveLayerAndRestore(record, begin); + } + + SkPaint* drawPaint = pattern->second(); + if (drawPaint == NULL) { + // We can just give the draw the SaveLayer's paint. + // TODO(mtklein): figure out how to do this clearly + return false; + } + + const uint32_t layerColor = layerPaint->getColor(); + const uint32_t drawColor = drawPaint->getColor(); + if (!IsOnlyAlpha(layerColor) || !IsOpaque(drawColor) || HasAnyEffect(*layerPaint)) { + // Too fancy for us. Actually, as long as layerColor is just an alpha + // we can blend it into drawColor's alpha; drawColor doesn't strictly have to be opaque. + return false; + } + + drawPaint->setColor(SkColorSetA(drawColor, SkColorGetA(layerColor))); + return KillSaveLayerAndRestore(record, begin); + } + + static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerIndex) { + record->replace(saveLayerIndex); // SaveLayer + record->replace(saveLayerIndex+2); // Restore + return true; + } + + static bool HasAnyEffect(const SkPaint& paint) { + return paint.getPathEffect() || + paint.getShader() || + paint.getXfermode() || + paint.getMaskFilter() || + paint.getColorFilter() || + paint.getRasterizer() || + paint.getLooper() || + paint.getImageFilter(); + } + + static bool IsOpaque(SkColor color) { + return SkColorGetA(color) == SK_AlphaOPAQUE; + } + static bool IsOnlyAlpha(SkColor color) { + return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); + } +}; +void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { + SaveLayerDrawRestoreNooper pass; + apply(&pass, record); +} + + // Replaces DrawPosText with DrawPosTextH when all Y coordinates are equal. struct StrengthReducer { typedef Pattern1 > Pattern; diff --git a/src/record/SkRecordOpts.h b/src/record/SkRecordOpts.h index 6db7abca61..ad8c3bcfc7 100644 --- a/src/record/SkRecordOpts.h +++ b/src/record/SkRecordOpts.h @@ -17,6 +17,10 @@ void SkRecordOptimize(SkRecord*); // Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops. void SkRecordNoopSaveRestores(SkRecord*); +// For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the +// draw, and no-op the SaveLayer and Restore. +void SkRecordNoopSaveLayerDrawRestores(SkRecord*); + // Annotates PushCull commands with the relative offset of their paired PopCull. void SkRecordAnnotateCullingPairs(SkRecord*); diff --git a/tests/RecordOptsTest.cpp b/tests/RecordOptsTest.cpp index 260b97b3e8..90347eda1f 100644 --- a/tests/RecordOptsTest.cpp +++ b/tests/RecordOptsTest.cpp @@ -12,6 +12,8 @@ #include "SkRecorder.h" #include "SkRecords.h" +#include "SkXfermode.h" + static const int W = 1920, H = 1080; // If the command we're reading is a U, set ptr to it, otherwise set it to NULL. @@ -151,3 +153,76 @@ DEF_TEST(RecordOpts_NoopSaveRestores, r) { assert_type(r, record, 9); assert_type(r, record, 10); } + +static void assert_savelayer_restore(skiatest::Reporter* r, + SkRecord* record, + unsigned i, + bool shouldBeNoOped) { + SkRecordNoopSaveLayerDrawRestores(record); + if (shouldBeNoOped) { + assert_type(r, *record, i); + assert_type(r, *record, i+2); + } else { + assert_type(r, *record, i); + assert_type(r, *record, i+2); + } +} + +DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) { + SkRecord record; + SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); + + SkRect bounds = SkRect::MakeWH(100, 200); + SkRect draw = SkRect::MakeWH(50, 60); + + SkPaint goodLayerPaint, badLayerPaint, worseLayerPaint; + goodLayerPaint.setColor(0x03000000); // Only alpha. + badLayerPaint.setColor( 0x03040506); // Not only alpha. + worseLayerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); // Any effect will do. + + SkPaint goodDrawPaint, badDrawPaint; + goodDrawPaint.setColor(0xFF020202); // Opaque. + badDrawPaint.setColor( 0x0F020202); // Not opaque. + + // No change: optimization can't handle bounds. + recorder.saveLayer(&bounds, NULL); + recorder.drawRect(draw, goodDrawPaint); + recorder.restore(); + assert_savelayer_restore(r, &record, 0, false); + + // SaveLayer/Restore removed: no bounds + no paint = no point. + recorder.saveLayer(NULL, NULL); + recorder.drawRect(draw, goodDrawPaint); + recorder.restore(); + assert_savelayer_restore(r, &record, 3, true); + + // TODO(mtklein): test case with null draw paint + + // No change: layer paint isn't alpha-only. + recorder.saveLayer(NULL, &badLayerPaint); + recorder.drawRect(draw, goodDrawPaint); + recorder.restore(); + assert_savelayer_restore(r, &record, 6, false); + + // No change: layer paint has an effect. + recorder.saveLayer(NULL, &worseLayerPaint); + recorder.drawRect(draw, goodDrawPaint); + recorder.restore(); + assert_savelayer_restore(r, &record, 9, false); + + // No change: draw paint isn't opaque. + recorder.saveLayer(NULL, &goodLayerPaint); + recorder.drawRect(draw, badDrawPaint); + recorder.restore(); + assert_savelayer_restore(r, &record, 12, false); + + // SaveLayer/Restore removed: we can fold in the alpha! + recorder.saveLayer(NULL, &goodLayerPaint); + recorder.drawRect(draw, goodDrawPaint); + recorder.restore(); + assert_savelayer_restore(r, &record, 15, true); + + const SkRecords::DrawRect* drawRect = assert_type(r, record, 16); + REPORTER_ASSERT(r, drawRect != NULL); + REPORTER_ASSERT(r, drawRect->paint.getColor() == 0x03020202); +}