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