Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
AlternatingColorPatternBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
12#include "include/core/SkPath.h"
15
22
23static const struct ColorPatternData{
26 const char* fName;
27} gColorPatterns[] = {
28 // Keep this in same order as ColorPattern enum
29 { SK_ColorWHITE, false, "white" }, // kWhite_ColorPattern
30 { SK_ColorBLUE, false, "blue" }, // kBlue_ColorPattern
31 { SK_ColorWHITE, true, "obaqueBitMap" }, // kOpaqueBitmap_ColorPattern
32 { 0x10000000, true, "alphaBitmap" }, // kAlphaBitmap_ColorPattern
33};
34
39
40static void makebm(SkBitmap* bm, int w, int h) {
41 bm->allocN32Pixels(w, h);
43
44 SkCanvas canvas(*bm);
45 SkScalar s = SkIntToScalar(std::min(w, h));
46 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
47 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
48 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
49 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
50 static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
51
52
54
55 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, std::size(kColors0),
57 canvas.drawPaint(paint);
58 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, std::size(kColors1),
60 canvas.drawPaint(paint);
61}
62
63/**
64 * This bench draws a grid of either rects or filled paths, with two alternating color patterns.
65 * This color patterns are passed in as enums to the class. The options are:
66 * 1) solid white color
67 * 2) solid blue color
68 * 3) opaque bitmap
69 * 4) partial alpha bitmap
70 * The same color pattern can be set for both arguments to create a uniform pattern on all draws.
71 *
72 * The bench is used to test a few things. First it can test any optimizations made for a specific
73 * color pattern (for example drawing an opaque bitmap versus one with partial alpha). Also it can
74 * be used to test the cost of program switching and/or GrDrawOp combining when alternating between
75 * different patterns when on the gpu.
76 */
78public:
79 enum {
80 NX = 5,
81 NY = 5,
83 };
85
90
96
97
99 fPattern1 = gColorPatterns[pattern1];
100 fPattern2 = gColorPatterns[pattern2];
101 fName.printf("colorPattern_%s_%s_%s",
103 kRect_DrawType == drawType ? "rect" : "path");
104 fDrawType = drawType;
105 }
106
107protected:
108 const char* onGetName() override {
109 return fName.c_str();
110 }
111
112 void onDelayedSetup() override {
113 int w = 40;
114 int h = 40;
115 makebm(&fBmp, w, h);
118 int offset = 2;
119 int count = 0;
120 for (int j = 0; j < NY; ++j) {
121 for (int i = 0; i < NX; ++i) {
122 int x = (w + offset) * i;
123 int y = (h * offset) * j;
124 if (kRect_DrawType == fDrawType) {
127 } else {
132 }
133 if (0 == count % 2) {
136 } else {
139 }
140 ++count;
141 }
142 }
143 }
144
145 void onDraw(int loops, SkCanvas* canvas) override {
147 paint.setAntiAlias(false);
148
149 for (int i = 0; i < loops; ++i) {
150 for (int j = 0; j < NUM_DRAWS; ++j) {
151 paint.setColor(fColors[j]);
152 paint.setShader(fShaders[j]);
153 if (kRect_DrawType == fDrawType) {
154 canvas->drawRect(fRects[j], paint);
155 } else {
156 canvas->drawPath(fPaths[j], paint);
157 }
158 }
159 }
160 }
161
162private:
163 using INHERITED = Benchmark;
164};
165
175
185
static void makebm(SkBitmap *bm, int w, int h)
static const struct ColorPatternData gColorPatterns[]
#define DEF_BENCH(code)
Definition Benchmark.h:20
int count
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
void onDraw(int loops, SkCanvas *canvas) override
AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawPaint(const SkPaint &paint)
void drawPath(const SkPath &path, const SkPaint &paint)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
SkPath & moveTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:678
SkPath & rLineTo(SkScalar dx, SkScalar dy)
Definition SkPath.cpp:729
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
const char * c_str() const
Definition SkString.h:133
const Paint & paint
float SkScalar
Definition extension.cpp:12
struct MyStruct s
double y
double x
SkScalar w
SkScalar h
Point offset
void setXYWH(float x, float y, float width, float height)
Definition SkRect.h:931