Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrMipmapBench.cpp
Go to the documentation of this file.
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 "bench/Benchmark.h"
16
17class GrMipMapBench: public Benchmark {
18 sk_sp<SkSurface> fSurface;
19 SkString fName;
20 const int fW, fH;
21
22public:
23 GrMipMapBench(int w, int h) : fW(w), fH(h) {
24 fName.printf("gr_mipmap_build_%dx%d", w, h);
25 }
26
27protected:
28 bool isSuitableFor(Backend backend) override {
29 return Backend::kGanesh == backend;
30 }
31
32 const char* onGetName() override { return fName.c_str(); }
33
34 void onDraw(int loops, SkCanvas* canvas) override {
35 if (!fSurface) {
36 auto context = canvas->recordingContext();
37 if (!context) {
38 return;
39 }
40 auto srgb = SkColorSpace::MakeSRGB();
43 // We're benching the regeneration of the mip levels not the need to allocate them every
44 // frame. Thus we create the surface with mips to begin with.
45 fSurface = SkSurfaces::RenderTarget(context,
47 info,
48 /* sampleCount= */ 0,
50 /* surfaceProps= */ nullptr,
51 /* shouldCreateWithMips= */ true);
52 }
53
54 // Clear surface once:
55 fSurface->getCanvas()->clear(SK_ColorBLACK);
56
60 paint.setColor(SK_ColorWHITE);
61 for (int i = 0; i < loops; i++) {
62 // Touch surface so mips are dirtied
63 fSurface->getCanvas()->drawPoint(0, 0, paint);
64
65 // Draw reduced version of surface to original canvas, to trigger mip generation
66 canvas->save();
67 canvas->scale(0.1f, 0.1f);
68 canvas->drawImage(fSurface->makeImageSnapshot(), 0, 0, sampling, &paint);
69 canvas->restore();
70 }
71 }
72
73 void onPerCanvasPostDraw(SkCanvas*) override {
74 fSurface.reset(nullptr);
75 }
76
77private:
78 using INHERITED = Benchmark;
79};
80
81// Build variants that exercise the width and heights being even or odd at each level, as the
82// impl specializes on each of these.
83//
84DEF_BENCH( return new GrMipMapBench(511, 511); )
85DEF_BENCH( return new GrMipMapBench(512, 511); )
86DEF_BENCH( return new GrMipMapBench(511, 512); )
87DEF_BENCH( return new GrMipMapBench(512, 512); )
#define DEF_BENCH(code)
Definition Benchmark.h:20
const char * backend
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
const char * fName
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void onPerCanvasPostDraw(SkCanvas *) override
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
bool isSuitableFor(Backend backend) override
GrMipMapBench(int w, int h)
void restore()
Definition SkCanvas.cpp:465
virtual GrRecordingContext * recordingContext() const
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< SkColorSpace > MakeSRGB()
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
const Paint & paint
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
SkScalar w
SkScalar h
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)