Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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:
35 XfermodeBench(SkBlendMode mode, Type t) : fBlendMode(mode) {
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
constexpr SkRect kRect
void drawRect(const SkRect &rect, const SkPaint &paint)
virtual SkISize getBaseLayerSize() const
Definition SkCanvas.cpp:373
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
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)
void onDelayedSetup() override
XfermodeBench(SkBlendMode mode, Type t)
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
const Paint & paint
float SkScalar
Definition extension.cpp:12
constexpr char kText[]
Definition glyph_pos.cpp:28
std::u16string text
double y
double x
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
SkFont DefaultFont()
SkScalar w
SkScalar h
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659