brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 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 | } |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 39 | auto srgb = SkColorSpace::MakeSRGB(); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 40 | SkImageInfo info = SkImageInfo::Make(fW, fH, kN32_SkColorType, kPremul_SkAlphaType, |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 41 | srgb); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 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); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 50 | paint.setColor(SK_ColorWHITE); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 51 | for (int i = 0; i < loops; i++) { |
| 52 | // Touch surface so mips are dirtied |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 53 | fSurface->getCanvas()->drawPoint(0, 0, paint); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 54 | |
| 55 | // Draw reduced version of surface to original canvas, to trigger mip generation |
| 56 | canvas->save(); |
| 57 | canvas->scale(0.1f, 0.1f); |
Yuqian Li | 06ee8e8 | 2017-03-17 20:50:06 +0000 | [diff] [blame] | 58 | canvas->drawImage(fSurface->makeImageSnapshot(SkBudgeted::kNo), 0, 0, &paint); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 59 | canvas->restore(); |
| 60 | } |
| 61 | } |
| 62 | |
brianosman | f7cfa92 | 2016-06-01 13:49:27 -0700 | [diff] [blame] | 63 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 64 | fSurface.reset(nullptr); |
| 65 | } |
| 66 | |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 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 | // |
brianosman | 2122c55 | 2016-06-02 06:50:40 -0700 | [diff] [blame] | 74 | DEF_BENCH( return new GrMipMapBench(511, 511); ) |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 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 |