1 /* 2 * Copyright 2016 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 "bench/Benchmark.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkImage.h" 11 #include "include/core/SkPaint.h" 12 #include "include/core/SkSurface.h" 13 14 class GrMipMapBench: public Benchmark { 15 sk_sp<SkSurface> fSurface; 16 SkString fName; 17 const int fW, fH; 18 19 public: GrMipMapBench(int w,int h)20 GrMipMapBench(int w, int h) : fW(w), fH(h) { 21 fName.printf("gr_mipmap_build_%dx%d", w, h); 22 } 23 24 protected: isSuitableFor(Backend backend)25 bool isSuitableFor(Backend backend) override { 26 return kGPU_Backend == backend; 27 } 28 onGetName()29 const char* onGetName() override { return fName.c_str(); } 30 onDraw(int loops,SkCanvas * canvas)31 void onDraw(int loops, SkCanvas* canvas) override { 32 if (!fSurface) { 33 auto context = canvas->recordingContext(); 34 if (!context) { 35 return; 36 } 37 auto srgb = SkColorSpace::MakeSRGB(); 38 SkImageInfo info = 39 SkImageInfo::Make(fW, fH, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb); 40 // We're benching the regeneration of the mip levels not the need to allocate them every 41 // frame. Thus we create the surface with mips to begin with. 42 fSurface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, 43 kBottomLeft_GrSurfaceOrigin, nullptr, true); 44 45 } 46 47 // Clear surface once: 48 fSurface->getCanvas()->clear(SK_ColorBLACK); 49 50 SkSamplingOptions sampling(SkFilterMode::kLinear, 51 SkMipmapMode::kLinear); 52 SkPaint paint; 53 paint.setColor(SK_ColorWHITE); 54 for (int i = 0; i < loops; i++) { 55 // Touch surface so mips are dirtied 56 fSurface->getCanvas()->drawPoint(0, 0, paint); 57 58 // Draw reduced version of surface to original canvas, to trigger mip generation 59 canvas->save(); 60 canvas->scale(0.1f, 0.1f); 61 canvas->drawImage(fSurface->makeImageSnapshot(), 0, 0, sampling, &paint); 62 canvas->restore(); 63 } 64 } 65 onPerCanvasPostDraw(SkCanvas *)66 void onPerCanvasPostDraw(SkCanvas*) override { 67 fSurface.reset(nullptr); 68 } 69 70 private: 71 using INHERITED = Benchmark; 72 }; 73 74 // Build variants that exercise the width and heights being even or odd at each level, as the 75 // impl specializes on each of these. 76 // 77 DEF_BENCH( return new GrMipMapBench(511, 511); ) 78 DEF_BENCH( return new GrMipMapBench(512, 511); ) 79 DEF_BENCH( return new GrMipMapBench(511, 512); ) 80 DEF_BENCH( return new GrMipMapBench(512, 512); ) 81