Flutter Engine
The Flutter Engine
BlendmodeBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
10#include "include/core/SkFont.h"
14#include "src/base/SkRandom.h"
16#include "tools/DecodeUtils.h"
17#include "tools/Resources.h"
19
20namespace {
21enum Type {
22 kText,
23 kRect,
24 kSprite,
25};
26}
27
28const char* gTypeNames[] = {
29 "mask", "rect", "sprite",
30};
31
32// Benchmark that draws non-AA rects or AA text with an SkBlendMode.
33class XfermodeBench : public Benchmark {
34public:
36 fType = t;
37 fName.printf("blendmicro_%s_%s", gTypeNames[t], SkBlendMode_Name(mode));
38 }
39
40protected:
41 const char* onGetName() override { return fName.c_str(); }
42
43 void onDelayedSetup() override {
44 if (fType == kSprite) {
45 fImage = ToolUtils::GetResourceAsImage("images/color_wheel.png");
46 }
47 }
48
49 void onDraw(int loops, SkCanvas* canvas) override {
50 const char* text = "Hamburgefons";
51 size_t len = strlen(text);
52 SkISize size = canvas->getBaseLayerSize();
53 SkRandom random;
54 while (loops > 0) {
56 paint.setBlendMode(fBlendMode);
57 paint.setColor(random.nextU());
58 switch (fType) {
59 case kText: {
60 // Draw text to exercise AA code paths.
62 font.setSize(random.nextRangeScalar(12, 96));
63 SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth),
64 y = random.nextRangeScalar(0, (SkScalar)size.fHeight);
66 int iterations = std::min(1000, loops);
67 for (int j = 0; j < iterations; ++j) {
68 canvas->drawTextBlob(blob, x, y, paint);
69 }
70 loops -= iterations;
71 } break;
72 case kRect: {
73 // Draw rects to exercise non-AA code paths.
74 SkScalar w = random.nextRangeScalar(50, 100);
75 SkScalar h = random.nextRangeScalar(50, 100);
77 random.nextUScalar1() * (size.fWidth - w),
78 random.nextUScalar1() * (size.fHeight - h),
79 w,
80 h
81 );
82 int iterations = std::min(1000, loops);
83 for (int j = 0; j < iterations; ++j) {
84 canvas->drawRect(rect, paint);
85 }
86 loops -= iterations;
87 } break;
88 case kSprite:
89 paint.setAlphaf(1.0f);
90 for (int i = 0; i < 10; ++i) {
91 canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
92 }
93 loops -= 1;
94 break;
95 }
96 }
97 }
98
99private:
100 SkBlendMode fBlendMode;
101 SkString fName;
102 sk_sp<SkImage> fImage;
103 Type fType;
104
105 using INHERITED = Benchmark;
106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110#define BENCH(mode) \
111 DEF_BENCH( return new XfermodeBench(mode, kText); ) \
112 DEF_BENCH( return new XfermodeBench(mode, kRect); ) \
113 DEF_BENCH( return new XfermodeBench(mode, kSprite); )
114
127
131
142
#define BENCH(mode)
const char * gTypeNames[]
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kSaturation
saturation of source with hue and luminosity of destination
@ kColorBurn
darken destination to reflect source
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kHue
hue of source with saturation and luminosity of destination
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kColorDodge
brighten destination to reflect source
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kSoftLight
lighten or darken, depending on source
@ kDifference
rc = s + d - 2*(min(s*da, d*sa)), ra = kSrcOver
@ kOverlay
multiply or screen, depending on destination
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
@ kSrcIn
r = s * da
@ kClear
r = 0
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
virtual SkISize getBaseLayerSize() const
Definition: SkCanvas.cpp:369
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
Definition: SkFont.h:35
uint32_t nextU()
Definition: SkRandom.h:42
SkScalar nextUScalar1()
Definition: SkRandom.h:101
SkScalar nextRangeScalar(SkScalar min, SkScalar max)
Definition: SkRandom.h:106
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
const char * c_str() const
Definition: SkString.h:133
static sk_sp< SkTextBlob > MakeFromText(const void *text, size_t byteLength, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: SkTextBlob.cpp:788
void onDelayedSetup() override
XfermodeBench(SkBlendMode mode, Type t)
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
constexpr char kText[]
Definition: glyph_pos.cpp:28
static float min(float r, float g, float b)
Definition: hsl.cpp:48
std::u16string text
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
SkFont DefaultFont()
constexpr std::array< std::array< float, 2 >, 2 > kRect
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
SkScalar w
SkScalar h
Definition: SkSize.h:16
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659