blob: b3b98f2fed8b12dc4b08b07ae024019d783f3139 [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
Mike Reed2dc50cc2018-04-12 12:20:25 -040069/////////
70#include "SkSurface.h"
71#include "SkPath.h"
72
73class RasterTileBench : public Benchmark {
74 sk_sp<SkSurface> fSurf;
75 SkPath fPath;
76 SkString fName;
77public:
78 RasterTileBench() : fName("rastertile") {
79 int W = 2014 * 20;
80 int H = 20;
81 fSurf = SkSurface::MakeRasterN32Premul(W, H);
82
83 fPath.moveTo(0, 0);
84 fPath.cubicTo(20, 10, 10, 15, 30, 5);
85 }
86
87protected:
88 const char* onGetName() override { return fName.c_str(); }
89
90 void onDraw(int loops, SkCanvas* canvas) override {
91 SkPaint paint;
92 paint.setStyle(SkPaint::kStroke_Style);
93 paint.setStrokeWidth(1.1f);
94 paint.setAntiAlias(true);
95
96 for (int i = 0; i < loops; ++i) {
97 for (int j = 0; j < 1000; ++j) {
98 fSurf->getCanvas()->drawPath(fPath, paint);
99 }
100 }
101 }
102
103private:
104};
105DEF_BENCH(return new RasterTileBench;)