skia2/bench/ClipMaskBench.cpp
Florin Malita c3b10a3a78 Reland "Add a clip mask bench"
Change-Id: Iefbfd1ecec38d3db5bac4370c75e6701aa03076f
TBR=reed@google.com
Reviewed-on: https://skia-review.googlesource.com/15102
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
2017-05-02 15:33:06 +00:00

69 lines
2.1 KiB
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "sk_tool_utils.h"
#include "SkCanvas.h"
#include "SkColorSpace.h"
#include "SkImage.h"
#include "SkPictureRecorder.h"
#include "SkString.h"
#include "SkSurface.h"
static void DrawMask(SkCanvas* canvas) {
sk_tool_utils::draw_checkerboard(canvas, SK_ColorTRANSPARENT, SK_ColorGREEN, 10);
}
class ClipMaskBench : public Benchmark {
public:
using MaskMakerFunc = sk_sp<SkImage> (*)(int);
ClipMaskBench(const char name[], const MaskMakerFunc maskMaker)
: fName(SkStringPrintf("clipmask_%s", name))
, fClip(maskMaker(kSize)) {}
protected:
const char* onGetName() override { return fName.c_str(); }
void onDraw(int loops, SkCanvas* canvas) override {
SkCanvas::SaveLayerRec rec(nullptr, nullptr, nullptr, fClip.get(), nullptr, 0);
for (int i = 0; i < loops; ++i) {
canvas->saveLayer(rec);
canvas->drawColor(SK_ColorBLUE);
canvas->restore();
}
}
private:
static constexpr int kSize = 400;
SkString fName;
sk_sp<SkImage> fClip;
};
DEF_BENCH(return new ClipMaskBench("a8", [](int size) -> sk_sp<SkImage> {
sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::MakeA8(size, size));
DrawMask(surface->getCanvas());
return surface->makeImageSnapshot();
});)
DEF_BENCH(return new ClipMaskBench("8888", [](int size) -> sk_sp<SkImage> {
sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(size, size);
DrawMask(surface->getCanvas());
return surface->makeImageSnapshot();
});)
DEF_BENCH(return new ClipMaskBench("picture", [](int size) -> sk_sp<SkImage> {
SkPictureRecorder recorder;
DrawMask(recorder.beginRecording(size, size));
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(size, size),
nullptr, nullptr, SkImage::BitDepth::kU8,
SkColorSpace::MakeSRGB());
});)