From 76e9485ffd718cc32bbf479cf02cdb9f6787c1f8 Mon Sep 17 00:00:00 2001 From: Hal Canary Date: Mon, 7 May 2018 09:36:14 -0400 Subject: [PATCH] SkRecorder: templates are better than macros Change-Id: I594da617a0fc8970a01c2165e41cdd803173ffc5 Reviewed-on: https://skia-review.googlesource.com/126420 Reviewed-by: Mike Klein Commit-Queue: Hal Canary --- src/core/SkRecorder.cpp | 86 +++++++++++++++++++++-------------------- src/core/SkRecorder.h | 3 ++ 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/core/SkRecorder.cpp b/src/core/SkRecorder.cpp index 41441864ce..271300065e 100644 --- a/src/core/SkRecorder.cpp +++ b/src/core/SkRecorder.cpp @@ -66,11 +66,13 @@ void SkRecorder::forgetRecord() { } // To make appending to fRecord a little less verbose. -#define APPEND(T, ...) \ - if (fMiniRecorder) { \ - this->flushMiniRecorder(); \ - } \ - new (fRecord->append()) SkRecords::T{__VA_ARGS__} +template +void SkRecorder::append(Args&&... args) { + if (fMiniRecorder) { + this->flushMiniRecorder(); + } + new (fRecord->append()) T{std::forward(args)...}; +} #define TRY_MINIRECORDER(method, ...) \ if (fMiniRecorder && fMiniRecorder->method(__VA_ARGS__)) { return; } @@ -130,40 +132,40 @@ void SkRecorder::flushMiniRecorder() { } void SkRecorder::onDrawPaint(const SkPaint& paint) { - APPEND(DrawPaint, paint); + this->append(paint); } void SkRecorder::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) { - APPEND(DrawPoints, paint, mode, SkToUInt(count), this->copy(pts, count)); + this->append(paint, mode, SkToUInt(count), this->copy(pts, count)); } void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) { TRY_MINIRECORDER(drawRect, rect, paint); - APPEND(DrawRect, paint, rect); + this->append(paint, rect); } void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) { - APPEND(DrawRegion, paint, region); + this->append(paint, region); } void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) { - APPEND(DrawOval, paint, oval); + this->append(paint, oval); } void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint& paint) { - APPEND(DrawArc, paint, oval, startAngle, sweepAngle, useCenter); + this->append(paint, oval, startAngle, sweepAngle, useCenter); } void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { - APPEND(DrawRRect, paint, rrect); + this->append(paint, rrect); } void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { - APPEND(DrawDRRect, paint, outer, inner); + this->append(paint, outer, inner); } void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) { @@ -172,7 +174,7 @@ void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) { fDrawableList.reset(new SkDrawableList); } fDrawableList->append(drawable); - APPEND(DrawDrawable, this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1); + this->append(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1); } else { SkASSERT(fDrawPictureMode == Playback_DrawPictureMode); drawable->draw(this, matrix); @@ -181,7 +183,7 @@ void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) { void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) { TRY_MINIRECORDER(drawPath, path, paint); - APPEND(DrawPath, paint, path); + this->append(paint, path); } void SkRecorder::onDrawBitmap(const SkBitmap& bitmap, @@ -223,24 +225,24 @@ void SkRecorder::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& latt void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint) { - APPEND(DrawImage, this->copy(paint), sk_ref_sp(image), left, top); + this->append(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), sk_ref_sp(image), this->copy(src), dst, constraint); + this->append(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), sk_ref_sp(image), center, dst); + this->append(this->copy(paint), sk_ref_sp(image), center, dst); } void SkRecorder::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, const SkPaint* paint) { int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0; SkASSERT(lattice.fBounds); - APPEND(DrawImageLattice, this->copy(paint), sk_ref_sp(image), + this->append(this->copy(paint), sk_ref_sp(image), lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount), lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount), flagCount, this->copy(lattice.fRectTypes, flagCount), @@ -249,14 +251,14 @@ void SkRecorder::onDrawImageLattice(const SkImage* image, const Lattice& lattice void SkRecorder::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, const SkPaint& paint) { - APPEND(DrawText, + this->append( paint, this->copy((const char*)text, byteLength), byteLength, x, y); } void SkRecorder::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], const SkPaint& paint) { const int points = paint.countText(text, byteLength); - APPEND(DrawPosText, + this->append( paint, this->copy((const char*)text, byteLength), byteLength, @@ -266,7 +268,7 @@ void SkRecorder::onDrawPosText(const void* text, size_t byteLength, void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY, const SkPaint& paint) { const int points = paint.countText(text, byteLength); - APPEND(DrawPosTextH, + this->append( paint, this->copy((const char*)text, byteLength), SkToUInt(byteLength), @@ -276,7 +278,7 @@ void SkRecorder::onDrawPosTextH(const void* text, size_t byteLength, void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { - APPEND(DrawTextOnPath, + this->append( paint, this->copy((const char*)text, byteLength), byteLength, @@ -286,7 +288,7 @@ void SkRecorder::onDrawTextOnPath(const void* text, size_t byteLength, const SkP void SkRecorder::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], const SkRect* cull, const SkPaint& paint) { - APPEND(DrawTextRSXform, + this->append( paint, this->copy((const char*)text, byteLength), byteLength, @@ -297,13 +299,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, sk_ref_sp(blob), x, y); + this->append(paint, sk_ref_sp(blob), x, y); } void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) { if (fDrawPictureMode == Record_DrawPictureMode) { fApproxBytesUsedBySubPictures += pic->approximateBytesUsed(); - APPEND(DrawPicture, this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I()); + this->append(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I()); } else { SkASSERT(fDrawPictureMode == Playback_DrawPictureMode); SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect()); @@ -313,13 +315,13 @@ void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, con void SkRecorder::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode, const SkPaint& paint) { - APPEND(DrawVertices, paint, sk_ref_sp(const_cast(vertices)), bmode); + this->append(paint, sk_ref_sp(const_cast(vertices)), bmode); } void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) { - APPEND(DrawPatch, paint, + this->append(paint, cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr, colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr, texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr, @@ -329,7 +331,7 @@ void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkRect* cull, const SkPaint* paint) { - APPEND(DrawAtlas, this->copy(paint), + this->append(this->copy(paint), sk_ref_sp(atlas), this->copy(xform, count), this->copy(tex, count), @@ -340,23 +342,23 @@ void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], cons } void SkRecorder::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) { - APPEND(DrawShadowRec, path, rec); + this->append(path, rec); } void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { - APPEND(DrawAnnotation, rect, SkString(key), sk_ref_sp(value)); + this->append(rect, SkString(key), sk_ref_sp(value)); } void SkRecorder::onFlush() { - APPEND(Flush); + this->append(); } void SkRecorder::willSave() { - APPEND(Save); + this->append(); } SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) { - APPEND(SaveLayer, this->copy(rec.fBounds) + this->append(this->copy(rec.fBounds) , this->copy(rec.fPaint) , sk_ref_sp(rec.fBackdrop) , sk_ref_sp(rec.fClipMask) @@ -366,42 +368,42 @@ SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& } void SkRecorder::didRestore() { - APPEND(Restore, this->getTotalMatrix()); + this->append(this->getTotalMatrix()); } void SkRecorder::didConcat(const SkMatrix& matrix) { - APPEND(Concat, matrix); + this->append(matrix); } void SkRecorder::didSetMatrix(const SkMatrix& matrix) { - APPEND(SetMatrix, matrix); + this->append(matrix); } void SkRecorder::didTranslate(SkScalar dx, SkScalar dy) { - APPEND(Translate, dx, dy); + this->append(dx, dy); } void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) { INHERITED(onClipRect, rect, op, edgeStyle); SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle); - APPEND(ClipRect, rect, opAA); + this->append(rect, opAA); } void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) { INHERITED(onClipRRect, rrect, op, edgeStyle); SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle); - APPEND(ClipRRect, rrect, opAA); + this->append(rrect, opAA); } void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) { INHERITED(onClipPath, path, op, edgeStyle); SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle); - APPEND(ClipPath, path, opAA); + this->append(path, opAA); } void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) { INHERITED(onClipRegion, deviceRgn, op); - APPEND(ClipRegion, deviceRgn, op); + this->append(deviceRgn, op); } sk_sp SkRecorder::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { diff --git a/src/core/SkRecorder.h b/src/core/SkRecorder.h index 9328d08424..6a7a83bdc2 100644 --- a/src/core/SkRecorder.h +++ b/src/core/SkRecorder.h @@ -146,6 +146,9 @@ private: template T* copy(const T[], size_t count); + template + void append(Args&&...); + DrawPictureMode fDrawPictureMode; size_t fApproxBytesUsedBySubPictures; SkRecord* fRecord;