• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
DrawMask(SkCanvas * canvas)17 static void DrawMask(SkCanvas* canvas) {
18     sk_tool_utils::draw_checkerboard(canvas, SK_ColorTRANSPARENT, SK_ColorGREEN, 10);
19 }
20 
21 class ClipMaskBench : public Benchmark {
22 public:
23     using MaskMakerFunc = sk_sp<SkImage> (*)(int);
24 
ClipMaskBench(const char name[],const MaskMakerFunc maskMaker)25     ClipMaskBench(const char name[], const MaskMakerFunc maskMaker)
26         : fName(SkStringPrintf("clipmask_%s", name))
27         , fClip(maskMaker(kSize)) {}
28 
29 protected:
onGetName()30     const char* onGetName() override { return fName.c_str(); }
31 
onDraw(int loops,SkCanvas * canvas)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 
42 private:
43     static constexpr int kSize = 400;
44 
45     SkString       fName;
46     sk_sp<SkImage> fClip;
47 };
48 
__anon0c1184da0102(int size) 49 DEF_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 
__anon0c1184da0202(int size) 55 DEF_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 
__anon0c1184da0302(int size) 61 DEF_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 
69 /////////
70 #include "SkSurface.h"
71 #include "SkPath.h"
72 
73 class RasterTileBench : public Benchmark {
74     sk_sp<SkSurface> fSurf;
75     SkPath           fPath;
76     SkString       fName;
77 public:
RasterTileBench()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 
87 protected:
onGetName()88     const char* onGetName() override { return fName.c_str(); }
89 
onDraw(int loops,SkCanvas * canvas)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 
103 private:
104 };
105 DEF_BENCH(return new RasterTileBench;)
106