blob: fc3cc16705d4ed7c6d869cf4d58eea0315d4de9e [file] [log] [blame]
Florin Malitac3b10a32017-05-02 11:09:01 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Benchmark.h"
9#include "sk_tool_utils.h"
10#include "SkCanvas.h"
11#include "SkColorSpace.h"
12#include "SkImage.h"
13#include "SkPictureRecorder.h"
14#include "SkString.h"
15#include "SkSurface.h"
16
17static void DrawMask(SkCanvas* canvas) {
18 sk_tool_utils::draw_checkerboard(canvas, SK_ColorTRANSPARENT, SK_ColorGREEN, 10);
19}
20
21class ClipMaskBench : public Benchmark {
22public:
23 using MaskMakerFunc = sk_sp<SkImage> (*)(int);
24
25 ClipMaskBench(const char name[], const MaskMakerFunc maskMaker)
26 : fName(SkStringPrintf("clipmask_%s", name))
27 , fClip(maskMaker(kSize)) {}
28
29protected:
30 const char* onGetName() override { return fName.c_str(); }
31
32 void onDraw(int loops, SkCanvas* canvas) override {
33 SkCanvas::SaveLayerRec rec(nullptr, nullptr, nullptr, fClip.get(), nullptr, 0);
34
35 for (int i = 0; i < loops; ++i) {
36 canvas->saveLayer(rec);
37 canvas->drawColor(SK_ColorBLUE);
38 canvas->restore();
39 }
40 }
41
42private:
43 static constexpr int kSize = 400;
44
45 SkString fName;
46 sk_sp<SkImage> fClip;
47};
48
49DEF_BENCH(return new ClipMaskBench("a8", [](int size) -> sk_sp<SkImage> {
50 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::MakeA8(size, size));
51 DrawMask(surface->getCanvas());
52 return surface->makeImageSnapshot();
53});)
54
55DEF_BENCH(return new ClipMaskBench("8888", [](int size) -> sk_sp<SkImage> {
56 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(size, size);
57 DrawMask(surface->getCanvas());
58 return surface->makeImageSnapshot();
59});)
60
61DEF_BENCH(return new ClipMaskBench("picture", [](int size) -> sk_sp<SkImage> {
62 SkPictureRecorder recorder;
63 DrawMask(recorder.beginRecording(size, size));
64 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(size, size),
65 nullptr, nullptr, SkImage::BitDepth::kU8,
66 SkColorSpace::MakeSRGB());
67});)
68