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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 13 | |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 14 | class GrMipMapBench: public Benchmark { |
| 15 | sk_sp<SkSurface> fSurface; |
| 16 | SkString fName; |
| 17 | const int fW, fH; |
| 18 | |
| 19 | public: |
| 20 | GrMipMapBench(int w, int h) : fW(w), fH(h) { |
| 21 | fName.printf("gr_mipmap_build_%dx%d", w, h); |
| 22 | } |
| 23 | |
| 24 | protected: |
| 25 | bool isSuitableFor(Backend backend) override { |
| 26 | return kGPU_Backend == backend; |
| 27 | } |
| 28 | |
| 29 | const char* onGetName() override { return fName.c_str(); } |
| 30 | |
| 31 | void onDraw(int loops, SkCanvas* canvas) override { |
| 32 | if (!fSurface) { |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 33 | auto context = canvas->recordingContext(); |
| 34 | if (!context) { |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 35 | return; |
| 36 | } |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 37 | auto srgb = SkColorSpace::MakeSRGB(); |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 38 | SkImageInfo info = |
| 39 | SkImageInfo::Make(fW, fH, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 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 | |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Clear surface once: |
| 48 | fSurface->getCanvas()->clear(SK_ColorBLACK); |
| 49 | |
| 50 | SkPaint paint; |
| 51 | paint.setFilterQuality(kMedium_SkFilterQuality); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 52 | paint.setColor(SK_ColorWHITE); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 53 | for (int i = 0; i < loops; i++) { |
| 54 | // Touch surface so mips are dirtied |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 55 | fSurface->getCanvas()->drawPoint(0, 0, paint); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 56 | |
| 57 | // Draw reduced version of surface to original canvas, to trigger mip generation |
| 58 | canvas->save(); |
| 59 | canvas->scale(0.1f, 0.1f); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame^] | 60 | canvas->drawImage(fSurface->makeImageSnapshot(), 0, 0, SkSamplingOptions(), &paint); |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 61 | canvas->restore(); |
| 62 | } |
| 63 | } |
| 64 | |
brianosman | f7cfa92 | 2016-06-01 13:49:27 -0700 | [diff] [blame] | 65 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 66 | fSurface.reset(nullptr); |
| 67 | } |
| 68 | |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 69 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 70 | using INHERITED = Benchmark; |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Build variants that exercise the width and heights being even or odd at each level, as the |
| 74 | // impl specializes on each of these. |
| 75 | // |
brianosman | 2122c55 | 2016-06-02 06:50:40 -0700 | [diff] [blame] | 76 | DEF_BENCH( return new GrMipMapBench(511, 511); ) |
brianosman | fc29906 | 2016-06-01 12:40:15 -0700 | [diff] [blame] | 77 | DEF_BENCH( return new GrMipMapBench(512, 511); ) |
| 78 | DEF_BENCH( return new GrMipMapBench(511, 512); ) |
| 79 | DEF_BENCH( return new GrMipMapBench(512, 512); ) |