blob: b16c23d3bc735bea931731c8cefc0e4af2d1fafb [file] [log] [blame]
brianosmanfc299062016-06-01 12:40:15 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
brianosmanfc299062016-06-01 12:40:15 -070013
brianosmanfc299062016-06-01 12:40:15 -070014class GrMipMapBench: public Benchmark {
15 sk_sp<SkSurface> fSurface;
16 SkString fName;
17 const int fW, fH;
18
19public:
20 GrMipMapBench(int w, int h) : fW(w), fH(h) {
21 fName.printf("gr_mipmap_build_%dx%d", w, h);
22 }
23
24protected:
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 Phillips16bf7d32020-07-07 10:20:27 -040033 auto context = canvas->recordingContext();
34 if (!context) {
brianosmanfc299062016-06-01 12:40:15 -070035 return;
36 }
Matt Sarett77a7a1b2017-02-07 13:56:11 -050037 auto srgb = SkColorSpace::MakeSRGB();
Brian Salomon8fe24272017-07-07 12:56:11 -040038 SkImageInfo info =
39 SkImageInfo::Make(fW, fH, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb);
Greg Daniel09c94002018-06-08 22:11:51 +000040 // 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
brianosmanfc299062016-06-01 12:40:15 -070045 }
46
47 // Clear surface once:
48 fSurface->getCanvas()->clear(SK_ColorBLACK);
49
50 SkPaint paint;
51 paint.setFilterQuality(kMedium_SkFilterQuality);
Mike Reed3661bc92017-02-22 13:21:42 -050052 paint.setColor(SK_ColorWHITE);
brianosmanfc299062016-06-01 12:40:15 -070053 for (int i = 0; i < loops; i++) {
54 // Touch surface so mips are dirtied
Mike Reed3661bc92017-02-22 13:21:42 -050055 fSurface->getCanvas()->drawPoint(0, 0, paint);
brianosmanfc299062016-06-01 12:40:15 -070056
57 // Draw reduced version of surface to original canvas, to trigger mip generation
58 canvas->save();
59 canvas->scale(0.1f, 0.1f);
Mike Reede02d7f82021-01-21 22:25:21 -050060 canvas->drawImage(fSurface->makeImageSnapshot(), 0, 0, SkSamplingOptions(), &paint);
brianosmanfc299062016-06-01 12:40:15 -070061 canvas->restore();
62 }
63 }
64
brianosmanf7cfa922016-06-01 13:49:27 -070065 void onPerCanvasPostDraw(SkCanvas*) override {
66 fSurface.reset(nullptr);
67 }
68
brianosmanfc299062016-06-01 12:40:15 -070069private:
John Stiles7571f9e2020-09-02 22:42:33 -040070 using INHERITED = Benchmark;
brianosmanfc299062016-06-01 12:40:15 -070071};
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//
brianosman2122c552016-06-02 06:50:40 -070076DEF_BENCH( return new GrMipMapBench(511, 511); )
brianosmanfc299062016-06-01 12:40:15 -070077DEF_BENCH( return new GrMipMapBench(512, 511); )
78DEF_BENCH( return new GrMipMapBench(511, 512); )
79DEF_BENCH( return new GrMipMapBench(512, 512); )