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 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 25 ClipMaskBench(const char name[], const MaskMakerFunc maskMaker) 26 : fName(SkStringPrintf("clipmask_%s", name)) 27 , fClip(maskMaker(kSize)) {} 28 29 protected: 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 42 private: 43 static constexpr int kSize = 400; 44 45 SkString fName; 46 sk_sp<SkImage> fClip; 47 }; 48 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 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 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