Replace old SkRecords RefBox with sk_sp.

The main differences are that RefBox's constructor reffed and sk_sp's doesn't,
and that RefBox auto-casts to the underlying pointer while sk_sp doesn't.

I've added a bunch of sk_ref_sp() and .get() to compensate.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2197243003

Review-Url: https://codereview.chromium.org/2197243003
This commit is contained in:
mtklein 2016-08-01 11:24:03 -07:00 committed by Commit bot
parent 2dad769dc9
commit da574d17f8
4 changed files with 42 additions and 50 deletions

View File

@ -86,22 +86,6 @@ enum Type { SK_RECORD_TYPES(ENUM) };
operator T*() const { return ptr; } \
T* operator->() const { return ptr; }
template <typename T>
class RefBox : SkNoncopyable {
public:
RefBox() {}
RefBox(T* obj) : fObj(SkSafeRef(obj)) {}
RefBox(RefBox&& o) : fObj(o.fObj) {
o.fObj = nullptr;
}
~RefBox() { SkSafeUnref(fObj); }
ACT_AS_PTR(fObj);
private:
T* fObj;
};
// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data.
template <typename T>
class Optional : SkNoncopyable {
@ -188,7 +172,7 @@ RECORD(Save, 0);
RECORD(SaveLayer, 0,
Optional<SkRect> bounds;
Optional<SkPaint> paint;
RefBox<const SkImageFilter> backdrop;
sk_sp<const SkImageFilter> backdrop;
SkCanvas::SaveLayerFlags saveLayerFlags);
RECORD(SetMatrix, 0,
@ -234,18 +218,18 @@ RECORD(DrawDrawable, kDraw_Tag,
int32_t index);
RECORD(DrawImage, kDraw_Tag|kHasImage_Tag,
Optional<SkPaint> paint;
RefBox<const SkImage> image;
sk_sp<const SkImage> image;
SkScalar left;
SkScalar top);
RECORD(DrawImageRect, kDraw_Tag|kHasImage_Tag,
Optional<SkPaint> paint;
RefBox<const SkImage> image;
sk_sp<const SkImage> image;
Optional<SkRect> src;
SkRect dst;
SkCanvas::SrcRectConstraint constraint);
RECORD(DrawImageNine, kDraw_Tag|kHasImage_Tag,
Optional<SkPaint> paint;
RefBox<const SkImage> image;
sk_sp<const SkImage> image;
SkIRect center;
SkRect dst);
RECORD(DrawOval, kDraw_Tag,
@ -258,11 +242,11 @@ RECORD(DrawPath, kDraw_Tag,
PreCachedPath path);
RECORD(DrawPicture, kDraw_Tag,
Optional<SkPaint> paint;
RefBox<const SkPicture> picture;
sk_sp<const SkPicture> picture;
TypedMatrix matrix);
RECORD(DrawShadowedPicture, kDraw_Tag,
Optional<SkPaint> paint;
RefBox<const SkPicture> picture;
sk_sp<const SkPicture> picture;
TypedMatrix matrix);
RECORD(DrawPoints, kDraw_Tag,
SkPaint paint;
@ -294,7 +278,7 @@ RECORD(DrawText, kDraw_Tag|kHasText_Tag,
SkScalar y);
RECORD(DrawTextBlob, kDraw_Tag|kHasText_Tag,
SkPaint paint;
RefBox<const SkTextBlob> blob;
sk_sp<const SkTextBlob> blob;
SkScalar x;
SkScalar y);
RECORD(DrawTextOnPath, kDraw_Tag|kHasText_Tag,
@ -314,10 +298,10 @@ RECORD(DrawPatch, kDraw_Tag,
PODArray<SkPoint> cubics;
PODArray<SkColor> colors;
PODArray<SkPoint> texCoords;
RefBox<SkXfermode> xmode);
sk_sp<SkXfermode> xmode);
RECORD(DrawAtlas, kDraw_Tag|kHasImage_Tag,
Optional<SkPaint> paint;
RefBox<const SkImage> atlas;
sk_sp<const SkImage> atlas;
PODArray<SkRSXform> xforms;
PODArray<SkRect> texs;
PODArray<SkColor> colors;
@ -331,13 +315,13 @@ RECORD(DrawVertices, kDraw_Tag,
PODArray<SkPoint> vertices;
PODArray<SkPoint> texs;
PODArray<SkColor> colors;
RefBox<SkXfermode> xmode;
sk_sp<SkXfermode> xmode;
PODArray<uint16_t> indices;
int indexCount);
RECORD(DrawAnnotation, 0,
SkRect rect;
SkString key;
RefBox<SkData> value);
sk_sp<SkData> value);
#undef RECORD
} // namespace SkRecords

View File

@ -79,7 +79,7 @@ bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
}
bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, const SkPaint& p) {
TRY_TO_STORE(DrawTextBlob, p, b, x, y);
TRY_TO_STORE(DrawTextBlob, p, sk_ref_sp(b), x, y);
}
#undef TRY_TO_STORE

View File

@ -77,7 +77,10 @@ template <> void Draw::draw(const NoOp&) {}
#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
DRAW(Restore, restore());
DRAW(Save, save());
DRAW(SaveLayer, saveLayer(SkCanvas::SaveLayerRec(r.bounds, r.paint, r.backdrop, r.saveLayerFlags)));
DRAW(SaveLayer, saveLayer(SkCanvas::SaveLayerRec(r.bounds,
r.paint,
r.backdrop.get(),
r.saveLayerFlags)));
DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
DRAW(Concat, concat(r.matrix));
@ -93,17 +96,17 @@ template <> void Draw::draw(const TranslateZ& r) { }
#endif
DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
DRAW(DrawImage, drawImage(r.image, r.left, r.top, r.paint));
DRAW(DrawImageRect, legacy_drawImageRect(r.image, r.src, r.dst, r.paint, r.constraint));
DRAW(DrawImageNine, drawImageNine(r.image, r.center, r.dst, r.paint));
DRAW(DrawImage, drawImage(r.image.get(), r.left, r.top, r.paint));
DRAW(DrawImageRect, legacy_drawImageRect(r.image.get(), r.src, r.dst, r.paint, r.constraint));
DRAW(DrawImageNine, drawImageNine(r.image.get(), r.center, r.dst, r.paint));
DRAW(DrawOval, drawOval(r.oval, r.paint));
DRAW(DrawPaint, drawPaint(r.paint));
DRAW(DrawPath, drawPath(r.path, r.paint));
DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode, r.paint));
DRAW(DrawPicture, drawPicture(r.picture, &r.matrix, r.paint));
DRAW(DrawPicture, drawPicture(r.picture.get(), &r.matrix, r.paint));
#ifdef SK_EXPERIMENTAL_SHADOWING
DRAW(DrawShadowedPicture, drawShadowedPicture(r.picture, &r.matrix, r.paint));
DRAW(DrawShadowedPicture, drawShadowedPicture(r.picture.get(), &r.matrix, r.paint));
#else
template <> void Draw::draw(const DrawShadowedPicture& r) { }
#endif
@ -114,13 +117,14 @@ DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
DRAW(DrawRect, drawRect(r.rect, r.paint));
DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
DRAW(DrawTextBlob, drawTextBlob(r.blob.get(), r.x, r.y, r.paint));
DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, &r.matrix, r.paint));
DRAW(DrawTextRSXform, drawTextRSXform(r.text, r.byteLength, r.xforms, r.cull, r.paint));
DRAW(DrawAtlas, drawAtlas(r.atlas, r.xforms, r.texs, r.colors, r.count, r.mode, r.cull, r.paint));
DRAW(DrawAtlas, drawAtlas(r.atlas.get(),
r.xforms, r.texs, r.colors, r.count, r.mode, r.cull, r.paint));
DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
r.xmode, r.indices, r.indexCount, r.paint));
DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value));
DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value.get()));
#undef DRAW
template <> void Draw::draw(const DrawDrawable& r) {
@ -403,7 +407,7 @@ private:
return this->adjustAndMap(op.outer.rect(), &op.paint);
}
Bounds bounds(const DrawImage& op) const {
const SkImage* image = op.image;
const SkImage* image = op.image.get();
SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height());
return this->adjustAndMap(rect, op.paint);

View File

@ -208,17 +208,17 @@ void SkRecorder::onDrawBitmapNine(const SkBitmap& bitmap,
void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
const SkPaint* paint) {
APPEND(DrawImage, this->copy(paint), image, left, top);
APPEND(DrawImage, this->copy(paint), sk_ref_sp(image), left, top);
}
void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
const SkPaint* paint, SrcRectConstraint constraint) {
APPEND(DrawImageRect, this->copy(paint), image, this->copy(src), dst, constraint);
APPEND(DrawImageRect, this->copy(paint), sk_ref_sp(image), this->copy(src), dst, constraint);
}
void SkRecorder::onDrawImageNine(const SkImage* image, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) {
APPEND(DrawImageNine, this->copy(paint), image, center, dst);
APPEND(DrawImageNine, this->copy(paint), sk_ref_sp(image), center, dst);
}
void SkRecorder::onDrawText(const void* text, size_t byteLength,
@ -271,13 +271,13 @@ void SkRecorder::onDrawTextRSXform(const void* text, size_t byteLength, const Sk
void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
const SkPaint& paint) {
TRY_MINIRECORDER(drawTextBlob, blob, x, y, paint);
APPEND(DrawTextBlob, paint, blob, x, y);
APPEND(DrawTextBlob, paint, sk_ref_sp(blob), x, y);
}
void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
if (fDrawPictureMode == Record_DrawPictureMode) {
fApproxBytesUsedBySubPictures += SkPictureUtils::ApproximateBytesUsed(pic);
APPEND(DrawPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
APPEND(DrawPicture, this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I());
} else {
SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect());
@ -290,7 +290,9 @@ void SkRecorder::onDrawShadowedPicture(const SkPicture* pic,
const SkPaint* paint) {
if (fDrawPictureMode == Record_DrawPictureMode) {
fApproxBytesUsedBySubPictures += SkPictureUtils::ApproximateBytesUsed(pic);
APPEND(DrawShadowedPicture, this->copy(paint), pic, matrix ? *matrix : SkMatrix::I());
APPEND(DrawShadowedPicture, this->copy(paint),
sk_ref_sp(pic),
matrix ? *matrix : SkMatrix::I());
} else {
SkASSERT(fDrawPictureMode == Playback_DrawPictureMode);
SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect());
@ -310,7 +312,7 @@ void SkRecorder::onDrawVertices(VertexMode vmode,
this->copy(vertices, vertexCount),
texs ? this->copy(texs, vertexCount) : nullptr,
colors ? this->copy(colors, vertexCount) : nullptr,
xmode,
sk_ref_sp(xmode),
this->copy(indices, indexCount),
indexCount);
}
@ -321,14 +323,14 @@ void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
xmode);
sk_ref_sp(xmode));
}
void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
const SkColor colors[], int count, SkXfermode::Mode mode,
const SkRect* cull, const SkPaint* paint) {
APPEND(DrawAtlas, this->copy(paint),
atlas,
sk_ref_sp(atlas),
this->copy(xform, count),
this->copy(tex, count),
this->copy(colors, count),
@ -338,7 +340,7 @@ void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], cons
}
void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
APPEND(DrawAnnotation, rect, SkString(key), value);
APPEND(DrawAnnotation, rect, SkString(key), sk_ref_sp(value));
}
void SkRecorder::willSave() {
@ -346,8 +348,10 @@ void SkRecorder::willSave() {
}
SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) {
APPEND(SaveLayer,
this->copy(rec.fBounds), this->copy(rec.fPaint), rec.fBackdrop, rec.fSaveLayerFlags);
APPEND(SaveLayer, this->copy(rec.fBounds)
, this->copy(rec.fPaint)
, sk_ref_sp(rec.fBackdrop)
, rec.fSaveLayerFlags);
return SkCanvas::kNoLayer_SaveLayerStrategy;
}