Pack and align SkStrokeRec to 4-byte boundary.

The new key for the distance field path cache will contain an
SkStrokeRec. This change guarantees that we don't have any hidden
padding that has garbage values, thereby preventing apparently
equal keys from hashing to two different values. This also has
the nice effect of reducing the size of SkStrokeRec from 24 bytes
to 16 bytes.

Review URL: https://codereview.chromium.org/1465773003
This commit is contained in:
jvanverth 2015-11-20 13:32:32 -08:00 committed by Commit bot
parent 179a8f5f7f
commit 897c993763
2 changed files with 15 additions and 9 deletions

View File

@ -35,8 +35,8 @@ public:
Style getStyle() const;
SkScalar getWidth() const { return fWidth; }
SkScalar getMiter() const { return fMiterLimit; }
SkPaint::Cap getCap() const { return fCap; }
SkPaint::Join getJoin() const { return fJoin; }
SkPaint::Cap getCap() const { return (SkPaint::Cap)fCap; }
SkPaint::Join getJoin() const { return (SkPaint::Join)fJoin; }
bool isHairlineStyle() const {
return kHairline_Style == this->getStyle();
@ -115,9 +115,15 @@ private:
SkScalar fResScale;
SkScalar fWidth;
SkScalar fMiterLimit;
SkPaint::Cap fCap;
SkPaint::Join fJoin;
bool fStrokeAndFill;
// The following three members are packed together into a single u32.
// This is to avoid unnecessary padding and ensure binary equality for
// hashing (because the padded areas might contain garbage values).
//
// fCap and fJoin are larger than needed to avoid having to initialize
// any pad values
uint32_t fCap : 16; // SkPaint::Cap
uint32_t fJoin : 15; // SkPaint::Join
uint32_t fStrokeAndFill : 1; // bool
};
#endif

View File

@ -108,8 +108,8 @@ bool SkStrokeRec::applyToPath(SkPath* dst, const SkPath& src) const {
}
SkStroke stroker;
stroker.setCap(fCap);
stroker.setJoin(fJoin);
stroker.setCap((SkPaint::Cap)fCap);
stroker.setJoin((SkPaint::Join)fJoin);
stroker.setMiterLimit(fMiterLimit);
stroker.setWidth(fWidth);
stroker.setDoFill(fStrokeAndFill);
@ -131,6 +131,6 @@ void SkStrokeRec::applyToPaint(SkPaint* paint) const {
paint->setStyle(fStrokeAndFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
paint->setStrokeWidth(fWidth);
paint->setStrokeMiter(fMiterLimit);
paint->setStrokeCap(fCap);
paint->setStrokeJoin(fJoin);
paint->setStrokeCap((SkPaint::Cap)fCap);
paint->setStrokeJoin((SkPaint::Join)fJoin);
}