blob: e151fe519f8945e5489b66fb0eeb24254f22c8df [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
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
16class GrMipMapBench: public Benchmark {
17 sk_sp<SkSurface> fSurface;
18 SkString fName;
19 const int fW, fH;
20
21public:
22 GrMipMapBench(int w, int h) : fW(w), fH(h) {
23 fName.printf("gr_mipmap_build_%dx%d", w, h);
24 }
25
26protected:
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 Sarett77a7a1b2017-02-07 13:56:11 -050039 auto srgb = SkColorSpace::MakeSRGB();
Brian Salomon8fe24272017-07-07 12:56:11 -040040 SkImageInfo info =
41 SkImageInfo::Make(fW, fH, kRGBA_8888_SkColorType, kPremul_SkAlphaType, srgb);
brianosmanfc299062016-06-01 12:40:15 -070042 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 Reed3661bc92017-02-22 13:21:42 -050050 paint.setColor(SK_ColorWHITE);
brianosmanfc299062016-06-01 12:40:15 -070051 for (int i = 0; i < loops; i++) {
52 // Touch surface so mips are dirtied
Mike Reed3661bc92017-02-22 13:21:42 -050053 fSurface->getCanvas()->drawPoint(0, 0, paint);
brianosmanfc299062016-06-01 12:40:15 -070054
55 // Draw reduced version of surface to original canvas, to trigger mip generation
56 canvas->save();
57 canvas->scale(0.1f, 0.1f);
Robert Phillipsac6b1fa2017-03-20 08:38:50 -040058 canvas->drawImage(fSurface->makeImageSnapshot(), 0, 0, &paint);
brianosmanfc299062016-06-01 12:40:15 -070059 canvas->restore();
60 }
61 }
62
brianosmanf7cfa922016-06-01 13:49:27 -070063 void onPerCanvasPostDraw(SkCanvas*) override {
64 fSurface.reset(nullptr);
65 }
66
brianosmanfc299062016-06-01 12:40:15 -070067private:
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//
brianosman2122c552016-06-02 06:50:40 -070074DEF_BENCH( return new GrMipMapBench(511, 511); )
brianosmanfc299062016-06-01 12:40:15 -070075DEF_BENCH( return new GrMipMapBench(512, 511); )
76DEF_BENCH( return new GrMipMapBench(511, 512); )
77DEF_BENCH( return new GrMipMapBench(512, 512); )
78
79#endif