blob: cb429cf76cd1a407c68db62fa6d7ad2dcdf5b193 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +00008#include "SkRecordOpts.h"
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +00009
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000010#include "SkRecordPattern.h"
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000011#include "SkRecords.h"
12#include "SkTDArray.h"
13
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000014using namespace SkRecords;
15
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000016void SkRecordOptimize(SkRecord* record) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000017 // TODO(mtklein): fuse independent optimizations to reduce number of passes?
commit-bot@chromium.org1e447302014-05-08 18:17:51 +000018 SkRecordNoopCulls(record);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000019 SkRecordNoopSaveRestores(record);
commit-bot@chromium.org90b5a2a2014-05-14 17:55:32 +000020 // TODO(mtklein): figure out why we draw differently and reenable
21 //SkRecordNoopSaveLayerDrawRestores(record);
commit-bot@chromium.org1e447302014-05-08 18:17:51 +000022
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000023 SkRecordAnnotateCullingPairs(record);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000024 SkRecordReduceDrawPosTextStrength(record); // Helpful to run this before BoundDrawPosTextH.
25 SkRecordBoundDrawPosTextH(record);
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000026}
27
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000028// Most of the optimizations in this file are pattern-based. These are all defined as structs with:
29// - a Pattern typedef
30// - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method,
31// which returns true if it made changes and false if not.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000032
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000033// Run a pattern-based optimization once across the SkRecord, returning true if it made any changes.
34// It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern,
35// record, and [begin,end) span of the commands that matched.
36template <typename Pass>
37static bool apply(Pass* pass, SkRecord* record) {
38 typename Pass::Pattern pattern;
39 bool changed = false;
40 unsigned begin, end = 0;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000041
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000042 while (pattern.search(record, &begin, &end)) {
43 changed |= pass->onMatch(record, &pattern, begin, end);
44 }
45 return changed;
46}
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000047
commit-bot@chromium.org1e447302014-05-08 18:17:51 +000048struct CullNooper {
49 typedef Pattern3<Is<PushCull>, Star<Is<NoOp> >, Is<PopCull> > Pattern;
50
51 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
52 record->replace<NoOp>(begin); // PushCull
53 record->replace<NoOp>(end-1); // PopCull
54 return true;
55 }
56};
57
58void SkRecordNoopCulls(SkRecord* record) {
59 CullNooper pass;
60 while (apply(&pass, record));
61}
62
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000063// Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into actual NoOps.
64struct SaveOnlyDrawsRestoreNooper {
65 typedef Pattern3<Is<Save>,
66 Star<Or<Is<NoOp>, IsDraw> >,
67 Is<Restore> >
68 Pattern;
69
70 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
71 record->replace<NoOp>(begin); // Save
72 record->replace<NoOp>(end-1); // Restore
73 return true;
74 }
75};
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000076// Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops.
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000077struct SaveNoDrawsRestoreNooper {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000078 // Star matches greedily, so we also have to exclude Save and Restore.
79 typedef Pattern3<Is<Save>,
80 Star<Not<Or3<Is<Save>,
81 Is<Restore>,
82 IsDraw> > >,
83 Is<Restore> >
84 Pattern;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000085
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000086 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000087 // The entire span between Save and Restore (inclusively) does nothing.
88 for (unsigned i = begin; i < end; i++) {
89 record->replace<NoOp>(i);
90 }
91 return true;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000092 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000093};
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000094void SkRecordNoopSaveRestores(SkRecord* record) {
commit-bot@chromium.org467705a2014-05-07 17:17:48 +000095 SaveOnlyDrawsRestoreNooper onlyDraws;
96 SaveNoDrawsRestoreNooper noDraws;
97
98 // Run until they stop changing things.
99 while (apply(&onlyDraws, record) || apply(&noDraws, record));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000100}
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000101
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000102// For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the
103// draw, and no-op the SaveLayer and Restore.
104struct SaveLayerDrawRestoreNooper {
105 typedef Pattern3<Is<SaveLayer>, IsDraw, Is<Restore> > Pattern;
106
107 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
108 SaveLayer* saveLayer = pattern->first<SaveLayer>();
109 if (saveLayer->bounds != NULL) {
110 // SaveLayer with bounds is too tricky for us.
111 return false;
112 }
113
114 SkPaint* layerPaint = saveLayer->paint;
115 if (NULL == layerPaint) {
116 // There wasn't really any point to this SaveLayer at all.
117 return KillSaveLayerAndRestore(record, begin);
118 }
119
120 SkPaint* drawPaint = pattern->second<SkPaint>();
121 if (drawPaint == NULL) {
122 // We can just give the draw the SaveLayer's paint.
123 // TODO(mtklein): figure out how to do this clearly
124 return false;
125 }
126
127 const uint32_t layerColor = layerPaint->getColor();
128 const uint32_t drawColor = drawPaint->getColor();
commit-bot@chromium.orgee7e23d2014-05-14 20:27:56 +0000129 if (!IsOnlyAlpha(layerColor) || !IsOpaque(drawColor) ||
130 HasAnyEffect(*layerPaint) || HasAnyEffect(*drawPaint)) {
commit-bot@chromium.orgf5bf3cf2014-05-07 14:47:44 +0000131 // Too fancy for us. Actually, as long as layerColor is just an alpha
132 // we can blend it into drawColor's alpha; drawColor doesn't strictly have to be opaque.
133 return false;
134 }
135
136 drawPaint->setColor(SkColorSetA(drawColor, SkColorGetA(layerColor)));
137 return KillSaveLayerAndRestore(record, begin);
138 }
139
140 static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerIndex) {
141 record->replace<NoOp>(saveLayerIndex); // SaveLayer
142 record->replace<NoOp>(saveLayerIndex+2); // Restore
143 return true;
144 }
145
146 static bool HasAnyEffect(const SkPaint& paint) {
147 return paint.getPathEffect() ||
148 paint.getShader() ||
149 paint.getXfermode() ||
150 paint.getMaskFilter() ||
151 paint.getColorFilter() ||
152 paint.getRasterizer() ||
153 paint.getLooper() ||
154 paint.getImageFilter();
155 }
156
157 static bool IsOpaque(SkColor color) {
158 return SkColorGetA(color) == SK_AlphaOPAQUE;
159 }
160 static bool IsOnlyAlpha(SkColor color) {
161 return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT);
162 }
163};
164void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) {
165 SaveLayerDrawRestoreNooper pass;
166 apply(&pass, record);
167}
168
169
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000170// Replaces DrawPosText with DrawPosTextH when all Y coordinates are equal.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000171struct StrengthReducer {
172 typedef Pattern1<Is<DrawPosText> > Pattern;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000173
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000174 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
175 SkASSERT(end == begin + 1);
176 DrawPosText* draw = pattern->first<DrawPosText>();
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000177
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000178 const unsigned points = draw->paint.countText(draw->text, draw->byteLength);
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000179 if (points == 0) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000180 return false; // No point (ha!).
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000181 }
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000182
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000183 const SkScalar firstY = draw->pos[0].fY;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000184 for (unsigned i = 1; i < points; i++) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000185 if (draw->pos[i].fY != firstY) {
186 return false; // Needs full power of DrawPosText.
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000187 }
188 }
189 // All ys are the same. We can replace DrawPosText with DrawPosTextH.
190
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000191 // draw->pos is points SkPoints, [(x,y),(x,y),(x,y),(x,y), ... ].
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000192 // We're going to squint and look at that as 2*points SkScalars, [x,y,x,y,x,y,x,y, ...].
193 // Then we'll rearrange things so all the xs are in order up front, clobbering the ys.
194 SK_COMPILE_ASSERT(sizeof(SkPoint) == 2 * sizeof(SkScalar), SquintingIsNotSafe);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000195 SkScalar* scalars = &draw->pos[0].fX;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000196 for (unsigned i = 0; i < 2*points; i += 2) {
197 scalars[i/2] = scalars[i];
198 }
199
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000200 // Extend lifetime of draw to the end of the loop so we can copy its paint.
201 Adopted<DrawPosText> adopted(draw);
202 SkNEW_PLACEMENT_ARGS(record->replace<DrawPosTextH>(begin, adopted),
203 DrawPosTextH,
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +0000204 (draw->paint, draw->text, draw->byteLength, scalars, firstY));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000205 return true;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000206 }
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000207};
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000208void SkRecordReduceDrawPosTextStrength(SkRecord* record) {
209 StrengthReducer pass;
210 apply(&pass, record);
211}
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000212
213// Tries to replace DrawPosTextH with BoundedDrawPosTextH, which knows conservative upper and lower
214// bounds to use with SkCanvas::quickRejectY.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000215struct TextBounder {
216 typedef Pattern1<Is<DrawPosTextH> > Pattern;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000217
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000218 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
219 SkASSERT(end == begin + 1);
220 DrawPosTextH* draw = pattern->first<DrawPosTextH>();
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000221
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000222 // If we're drawing vertical text, none of the checks we're about to do make any sense.
223 // We'll need to call SkPaint::computeFastBounds() later, so bail if that's not possible.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000224 if (draw->paint.isVerticalText() || !draw->paint.canComputeFastBounds()) {
225 return false;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000226 }
227
228 // Rather than checking the top and bottom font metrics, we guess. Actually looking up the
229 // top and bottom metrics is slow, and this overapproximation should be good enough.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000230 const SkScalar buffer = draw->paint.getTextSize() * 1.5f;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000231 SkDEBUGCODE(SkPaint::FontMetrics metrics;)
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000232 SkDEBUGCODE(draw->paint.getFontMetrics(&metrics);)
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000233 SkASSERT(-buffer <= metrics.fTop);
234 SkASSERT(+buffer >= metrics.fBottom);
235
236 // Let the paint adjust the text bounds. We don't care about left and right here, so we use
237 // 0 and 1 respectively just so the bounds rectangle isn't empty.
238 SkRect bounds;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000239 bounds.set(0, draw->y - buffer, SK_Scalar1, draw->y + buffer);
240 SkRect adjusted = draw->paint.computeFastBounds(bounds, &bounds);
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000241
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000242 Adopted<DrawPosTextH> adopted(draw);
243 SkNEW_PLACEMENT_ARGS(record->replace<BoundedDrawPosTextH>(begin, adopted),
244 BoundedDrawPosTextH,
245 (&adopted, adjusted.fTop, adjusted.fBottom));
246 return true;
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000247 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000248};
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000249void SkRecordBoundDrawPosTextH(SkRecord* record) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000250 TextBounder pass;
251 apply(&pass, record);
252}
253
254// Replaces PushCull with PairedPushCull, which lets us skip to the paired PopCull when the canvas
255// can quickReject the cull rect.
256// There's no efficient way (yet?) to express this one as a pattern, so we write a custom pass.
257class CullAnnotator {
258public:
259 // Do nothing to most ops.
260 template <typename T> void operator()(T*) {}
261
262 void operator()(PushCull* push) {
263 Pair pair = { fIndex, push };
264 fPushStack.push(pair);
265 }
266
267 void operator()(PopCull* pop) {
268 Pair push = fPushStack.top();
269 fPushStack.pop();
270
271 SkASSERT(fIndex > push.index);
272 unsigned skip = fIndex - push.index;
273
274 Adopted<PushCull> adopted(push.command);
275 SkNEW_PLACEMENT_ARGS(fRecord->replace<PairedPushCull>(push.index, adopted),
276 PairedPushCull, (&adopted, skip));
277 }
278
279 void apply(SkRecord* record) {
280 for (fRecord = record, fIndex = 0; fIndex < record->count(); fIndex++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +0000281 fRecord->mutate<void>(fIndex, *this);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000282 }
283 }
284
285private:
286 struct Pair {
287 unsigned index;
288 PushCull* command;
289 };
290
291 SkTDArray<Pair> fPushStack;
292 SkRecord* fRecord;
293 unsigned fIndex;
294};
295void SkRecordAnnotateCullingPairs(SkRecord* record) {
296 CullAnnotator pass;
297 pass.apply(record);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000298}