Index: src/effects/SkLayerDrawLooper.cpp |
diff --git a/src/effects/SkLayerDrawLooper.cpp b/src/effects/SkLayerDrawLooper.cpp |
index bca65ef77a2c43cac9860b9815ff272510de85e6..7f1c81c52ebdfa2aa592677a1992f070d0be8d4a 100644 |
--- a/src/effects/SkLayerDrawLooper.cpp |
+++ b/src/effects/SkLayerDrawLooper.cpp |
@@ -7,6 +7,7 @@ |
*/ |
#include "SkCanvas.h" |
#include "SkColor.h" |
+#include "SkPaint.h" |
#include "SkReadBuffer.h" |
#include "SkWriteBuffer.h" |
#include "SkLayerDrawLooper.h" |
@@ -21,19 +22,16 @@ SkLayerDrawLooper::LayerInfo::LayerInfo() { |
fPostTranslate = false; |
} |
-SkLayerDrawLooper::SkLayerDrawLooper() |
- : fRecs(nullptr), |
- fCount(0) { |
-} |
+SkLayerDrawLooper::SkLayerDrawLooper() : fRecs() {} |
-SkLayerDrawLooper::~SkLayerDrawLooper() { |
- Rec* rec = fRecs; |
- while (rec) { |
- Rec* next = rec->fNext; |
- delete rec; |
- rec = next; |
- } |
-} |
+SkLayerDrawLooper::~SkLayerDrawLooper() {} |
+ |
+struct SkLayerDrawLooper::Rec { |
+ explicit Rec(const LayerInfo& info) : fPaint(), fInfo(info) {} |
+ |
+ SkPaint fPaint; |
+ LayerInfo fInfo; |
+}; |
SkLayerDrawLooper::Context* SkLayerDrawLooper::createContext(SkCanvas* canvas, void* storage) const { |
canvas->save(); |
@@ -128,35 +126,36 @@ static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) { |
} |
SkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext( |
- const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {} |
+ const SkLayerDrawLooper* looper) |
+ : fCurr(looper->fRecs.begin()), fEnd(looper->fRecs.end()) {} |
bool SkLayerDrawLooper::LayerDrawLooperContext::next(SkCanvas* canvas, |
SkPaint* paint) { |
canvas->restore(); |
- if (nullptr == fCurrRec) { |
+ if (fEnd == fCurr) { |
return false; |
} |
- ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo); |
+ const Rec& rec = *fCurr++; |
+ ApplyInfo(paint, rec.fPaint, rec.fInfo); |
canvas->save(); |
- if (fCurrRec->fInfo.fPostTranslate) { |
- postTranslate(canvas, fCurrRec->fInfo.fOffset.fX, |
- fCurrRec->fInfo.fOffset.fY); |
+ if (rec.fInfo.fPostTranslate) { |
+ postTranslate(canvas, rec.fInfo.fOffset.fX, |
+ rec.fInfo.fOffset.fY); |
} else { |
- canvas->translate(fCurrRec->fInfo.fOffset.fX, |
- fCurrRec->fInfo.fOffset.fY); |
+ canvas->translate(rec.fInfo.fOffset.fX, |
+ rec.fInfo.fOffset.fY); |
} |
- fCurrRec = fCurrRec->fNext; |
return true; |
} |
bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const { |
- if (fCount != 2) { |
+ if (fRecs.size() != 2) { |
return false; |
} |
- const Rec* rec = fRecs; |
+ const Rec* rec = &fRecs[0]; |
// bottom layer needs to be just blur(maskfilter) |
if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) { |
@@ -174,7 +173,7 @@ bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const { |
return false; |
} |
- rec = rec->fNext; |
+ rec = &fRecs[1]; |
// top layer needs to be "plain" |
if (rec->fInfo.fPaintBits) { |
return false; |
@@ -188,8 +187,8 @@ bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const { |
if (bsRec) { |
bsRec->fSigma = maskBlur.fSigma; |
- bsRec->fOffset = fRecs->fInfo.fOffset; |
- bsRec->fColor = fRecs->fPaint.getColor(); |
+ bsRec->fOffset = fRecs[0].fInfo.fOffset; |
+ bsRec->fColor = fRecs[0].fPaint.getColor(); |
bsRec->fStyle = maskBlur.fStyle; |
bsRec->fQuality = maskBlur.fQuality; |
} |
@@ -199,19 +198,17 @@ bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const { |
/////////////////////////////////////////////////////////////////////////////// |
void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const { |
- buffer.writeInt(fCount); |
+ buffer.writeInt(fRecs.size()); |
- Rec* rec = fRecs; |
- for (int i = 0; i < fCount; i++) { |
+ for (const Rec& rec : fRecs) { |
// Legacy "flagsmask" field -- now ignored, remove when we bump version |
buffer.writeInt(0); |
- buffer.writeInt(rec->fInfo.fPaintBits); |
- buffer.writeInt(rec->fInfo.fColorMode); |
- buffer.writePoint(rec->fInfo.fOffset); |
- buffer.writeBool(rec->fInfo.fPostTranslate); |
- buffer.writePaint(rec->fPaint); |
- rec = rec->fNext; |
+ buffer.writeInt(rec.fInfo.fPaintBits); |
+ buffer.writeInt(rec.fInfo.fColorMode); |
+ buffer.writePoint(rec.fInfo.fOffset); |
+ buffer.writeBool(rec.fInfo.fPostTranslate); |
+ buffer.writePaint(rec.fPaint); |
} |
} |
@@ -219,6 +216,7 @@ SkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) { |
int count = buffer.readInt(); |
Builder builder; |
+ builder.reserve(count); |
for (int i = 0; i < count; i++) { |
LayerInfo info; |
// Legacy "flagsmask" field -- now ignored, remove when we bump version |
@@ -235,30 +233,30 @@ SkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) { |
#ifndef SK_IGNORE_TO_STRING |
void SkLayerDrawLooper::toString(SkString* str) const { |
- str->appendf("SkLayerDrawLooper (%d): ", fCount); |
+ str->appendf("SkLayerDrawLooper (%zu): ", fRecs.size()); |
- Rec* rec = fRecs; |
- for (int i = 0; i < fCount; i++) { |
- str->appendf("%d: paintBits: (", i); |
- if (0 == rec->fInfo.fPaintBits) { |
+ for (size_t i = 0; i < fRecs.size(); i++) { |
+ const Rec& rec = fRecs[i]; |
+ str->appendf("%zu: paintBits: (", i); |
+ if (0 == rec.fInfo.fPaintBits) { |
str->append("None"); |
- } else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) { |
+ } else if (kEntirePaint_Bits == rec.fInfo.fPaintBits) { |
str->append("EntirePaint"); |
} else { |
bool needSeparator = false; |
- SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style", |
+ SkAddFlagToString(str, SkToBool(kStyle_Bit & rec.fInfo.fPaintBits), "Style", |
&needSeparator); |
- SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX", |
+ SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec.fInfo.fPaintBits), "TextSkewX", |
&needSeparator); |
- SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect", |
+ SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec.fInfo.fPaintBits), "PathEffect", |
&needSeparator); |
- SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter", |
+ SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec.fInfo.fPaintBits), "MaskFilter", |
&needSeparator); |
- SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader", |
+ SkAddFlagToString(str, SkToBool(kShader_Bit & rec.fInfo.fPaintBits), "Shader", |
&needSeparator); |
- SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter", |
+ SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec.fInfo.fPaintBits), "ColorFilter", |
&needSeparator); |
- SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode", |
+ SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec.fInfo.fPaintBits), "Xfermode", |
&needSeparator); |
} |
str->append(") "); |
@@ -270,88 +268,48 @@ void SkLayerDrawLooper::toString(SkString* str) const { |
"kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion" |
}; |
- str->appendf("mode: %s ", gModeStrings[rec->fInfo.fColorMode]); |
+ str->appendf("mode: %s ", gModeStrings[rec.fInfo.fColorMode]); |
str->append("offset: ("); |
- str->appendScalar(rec->fInfo.fOffset.fX); |
+ str->appendScalar(rec.fInfo.fOffset.fX); |
str->append(", "); |
- str->appendScalar(rec->fInfo.fOffset.fY); |
+ str->appendScalar(rec.fInfo.fOffset.fY); |
str->append(") "); |
str->append("postTranslate: "); |
- if (rec->fInfo.fPostTranslate) { |
+ if (rec.fInfo.fPostTranslate) { |
str->append("true "); |
} else { |
str->append("false "); |
} |
- rec->fPaint.toString(str); |
- rec = rec->fNext; |
+ rec.fPaint.toString(str); |
} |
} |
#endif |
SkLayerDrawLooper::Builder::Builder() |
- : fRecs(nullptr), |
- fTopRec(nullptr), |
- fCount(0) { |
-} |
+ : fRecs() {} |
-SkLayerDrawLooper::Builder::~Builder() { |
- Rec* rec = fRecs; |
- while (rec) { |
- Rec* next = rec->fNext; |
- delete rec; |
- rec = next; |
- } |
-} |
- |
-SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) { |
- fCount += 1; |
+SkLayerDrawLooper::Builder::~Builder() {} |
- Rec* rec = new Rec; |
- rec->fNext = fRecs; |
- rec->fInfo = info; |
- fRecs = rec; |
- if (nullptr == fTopRec) { |
- fTopRec = rec; |
- } |
+void SkLayerDrawLooper::Builder::reserve(size_t n) { |
+ fRecs.reserve(n); |
+} |
- return &rec->fPaint; |
+SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) { |
+ fRecs.push_back(Rec(info)); |
+ return &fRecs.back().fPaint; |
} |
-void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) { |
+void SkLayerDrawLooper::Builder::addLayerOnTop(SkScalar dx, SkScalar dy) { |
LayerInfo info; |
- |
info.fOffset.set(dx, dy); |
- (void)this->addLayer(info); |
-} |
- |
-SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) { |
- fCount += 1; |
- |
- Rec* rec = new Rec; |
- rec->fNext = nullptr; |
- rec->fInfo = info; |
- if (nullptr == fRecs) { |
- fRecs = rec; |
- } else { |
- SkASSERT(fTopRec); |
- fTopRec->fNext = rec; |
- } |
- fTopRec = rec; |
- |
- return &rec->fPaint; |
+ this->addLayerOnTop(info); |
} |
SkLayerDrawLooper* SkLayerDrawLooper::Builder::detachLooper() { |
SkLayerDrawLooper* looper = new SkLayerDrawLooper; |
- looper->fCount = fCount; |
- looper->fRecs = fRecs; |
- |
- fCount = 0; |
- fRecs = nullptr; |
- fTopRec = nullptr; |
- |
+ fRecs.swap(looper->fRecs); |
return looper; |
} |