2014-07-30 15:58:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "src/core/SkBlendModePriv.h"
|
2014-07-30 15:58:20 +00:00
|
|
|
|
2014-08-08 14:43:29 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2014-07-30 15:58:20 +00:00
|
|
|
/** This benchmark tests rendering rotated rectangles. It can optionally apply AA and/or change the
|
2014-08-08 14:43:29 +00:00
|
|
|
paint color between each rect in different ways using the ColorType enum. The xfermode used can
|
2016-03-29 16:03:52 +00:00
|
|
|
be specified as well.
|
2014-08-08 14:43:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum ColorType {
|
|
|
|
kConstantOpaque_ColorType,
|
|
|
|
kConstantTransparent_ColorType,
|
|
|
|
kChangingOpaque_ColorType,
|
|
|
|
kChangingTransparent_ColorType,
|
|
|
|
kAlternatingOpaqueAndTransparent_ColorType,
|
2018-11-20 19:58:23 +00:00
|
|
|
kShaderOpaque_ColorType
|
2014-08-08 14:43:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline SkColor start_color(ColorType ct) {
|
|
|
|
switch (ct) {
|
|
|
|
case kConstantOpaque_ColorType:
|
|
|
|
case kChangingOpaque_ColorType:
|
|
|
|
case kAlternatingOpaqueAndTransparent_ColorType:
|
|
|
|
return 0xFFA07040;
|
|
|
|
case kConstantTransparent_ColorType:
|
|
|
|
case kChangingTransparent_ColorType:
|
|
|
|
return 0x80A07040;
|
2018-11-20 19:58:23 +00:00
|
|
|
case kShaderOpaque_ColorType:
|
|
|
|
return SK_ColorWHITE;
|
2014-08-08 14:43:29 +00:00
|
|
|
}
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("Shouldn't reach here.");
|
2014-08-08 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline SkColor advance_color(SkColor old, ColorType ct, int step) {
|
|
|
|
if (kAlternatingOpaqueAndTransparent_ColorType == ct) {
|
|
|
|
ct = (step & 0x1) ? kChangingOpaque_ColorType : kChangingTransparent_ColorType ;
|
|
|
|
}
|
|
|
|
switch (ct) {
|
|
|
|
case kConstantOpaque_ColorType:
|
|
|
|
case kConstantTransparent_ColorType:
|
2018-11-20 19:58:23 +00:00
|
|
|
case kShaderOpaque_ColorType:
|
2014-08-08 14:43:29 +00:00
|
|
|
return old;
|
|
|
|
case kChangingOpaque_ColorType:
|
|
|
|
return 0xFF000000 | (old + 0x00010307);
|
|
|
|
case kChangingTransparent_ColorType:
|
|
|
|
return (0x00FFFFFF & (old + 0x00010307)) | 0x80000000;
|
|
|
|
case kAlternatingOpaqueAndTransparent_ColorType:
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("Can't get here");
|
2014-08-08 14:43:29 +00:00
|
|
|
}
|
2017-08-16 14:53:04 +00:00
|
|
|
SK_ABORT("Shouldn't reach here.");
|
2014-08-08 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkString to_lower(const char* str) {
|
|
|
|
SkString lower(str);
|
|
|
|
for (size_t i = 0; i < lower.size(); i++) {
|
|
|
|
lower[i] = tolower(lower[i]);
|
|
|
|
}
|
|
|
|
return lower;
|
|
|
|
}
|
|
|
|
|
2014-07-30 15:58:20 +00:00
|
|
|
class RotRectBench: public Benchmark {
|
|
|
|
public:
|
2018-11-20 19:58:23 +00:00
|
|
|
RotRectBench(bool aa, ColorType ct, SkBlendMode mode, bool perspective = false)
|
2014-07-30 15:58:20 +00:00
|
|
|
: fAA(aa)
|
2018-11-20 19:58:23 +00:00
|
|
|
, fPerspective(perspective)
|
2014-08-08 14:43:29 +00:00
|
|
|
, fColorType(ct)
|
|
|
|
, fMode(mode) {
|
2014-07-30 15:58:20 +00:00
|
|
|
this->makeName();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override { return fName.c_str(); }
|
2014-07-30 15:58:20 +00:00
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2014-07-30 15:58:20 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(fAA);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(fMode);
|
2014-08-08 14:43:29 +00:00
|
|
|
SkColor color = start_color(fColorType);
|
2014-07-30 15:58:20 +00:00
|
|
|
|
2015-03-23 14:46:13 +00:00
|
|
|
int w = this->getSize().x();
|
|
|
|
int h = this->getSize().y();
|
2014-07-30 15:58:20 +00:00
|
|
|
|
|
|
|
static const SkScalar kRectW = 25.1f;
|
|
|
|
static const SkScalar kRectH = 25.9f;
|
|
|
|
|
2018-11-20 19:58:23 +00:00
|
|
|
if (fColorType == kShaderOpaque_ColorType) {
|
|
|
|
// The only requirement for the shader is that it requires local coordinates
|
|
|
|
SkPoint pts[2] = { {0.0f, 0.0f}, {kRectW, kRectH} };
|
|
|
|
SkColor colors[] = { color, SK_ColorBLUE };
|
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2018-11-20 19:58:23 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 15:58:20 +00:00
|
|
|
SkMatrix rotate;
|
|
|
|
// This value was chosen so that we frequently hit the axis-aligned case.
|
|
|
|
rotate.setRotate(30.f, kRectW / 2, kRectH / 2);
|
|
|
|
SkMatrix m = rotate;
|
|
|
|
|
|
|
|
SkScalar tx = 0, ty = 0;
|
|
|
|
|
2018-11-20 19:58:23 +00:00
|
|
|
if (fPerspective) {
|
|
|
|
// Apply some fixed perspective to change how ops may draw the rects
|
|
|
|
SkMatrix perspective;
|
|
|
|
perspective.setIdentity();
|
|
|
|
perspective.setPerspX(1e-4f);
|
|
|
|
perspective.setPerspY(1e-3f);
|
|
|
|
perspective.setSkewX(0.1f);
|
|
|
|
canvas->concat(perspective);
|
|
|
|
}
|
|
|
|
|
2014-07-30 15:58:20 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(tx, ty);
|
|
|
|
canvas->concat(m);
|
|
|
|
paint.setColor(color);
|
2014-08-08 14:43:29 +00:00
|
|
|
color = advance_color(color, fColorType, i);
|
|
|
|
|
2014-07-30 15:58:20 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(kRectW, kRectH), paint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
tx += kRectW + 2;
|
|
|
|
if (tx > w) {
|
|
|
|
tx = 0;
|
|
|
|
ty += kRectH + 2;
|
|
|
|
if (ty > h) {
|
|
|
|
ty = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.postConcat(rotate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void makeName() {
|
|
|
|
fName = "rotated_rects";
|
|
|
|
if (fAA) {
|
|
|
|
fName.append("_aa");
|
|
|
|
} else {
|
|
|
|
fName.append("_bw");
|
|
|
|
}
|
2018-11-20 19:58:23 +00:00
|
|
|
if (fPerspective) {
|
|
|
|
fName.append("_persp");
|
|
|
|
}
|
2014-08-08 14:43:29 +00:00
|
|
|
switch (fColorType) {
|
|
|
|
case kConstantOpaque_ColorType:
|
|
|
|
fName.append("_same_opaque");
|
|
|
|
break;
|
|
|
|
case kConstantTransparent_ColorType:
|
|
|
|
fName.append("_same_transparent");
|
|
|
|
break;
|
|
|
|
case kChangingOpaque_ColorType:
|
|
|
|
fName.append("_changing_opaque");
|
|
|
|
break;
|
|
|
|
case kChangingTransparent_ColorType:
|
|
|
|
fName.append("_changing_transparent");
|
|
|
|
break;
|
|
|
|
case kAlternatingOpaqueAndTransparent_ColorType:
|
|
|
|
fName.append("_alternating_transparent_and_opaque");
|
|
|
|
break;
|
2018-11-20 19:58:23 +00:00
|
|
|
case kShaderOpaque_ColorType:
|
|
|
|
fName.append("_shader_opaque");
|
|
|
|
break;
|
2014-07-30 15:58:20 +00:00
|
|
|
}
|
2016-11-15 21:44:34 +00:00
|
|
|
fName.appendf("_%s", to_lower(SkBlendMode_Name(fMode)).c_str());
|
2014-07-30 15:58:20 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
bool fAA;
|
2018-11-20 19:58:23 +00:00
|
|
|
bool fPerspective;
|
2016-10-06 00:33:02 +00:00
|
|
|
ColorType fColorType;
|
|
|
|
SkBlendMode fMode;
|
|
|
|
SkString fName;
|
2014-07-30 15:58:20 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2014-07-30 15:58:20 +00:00
|
|
|
};
|
|
|
|
|
2018-11-20 19:58:23 +00:00
|
|
|
#define DEF_FOR_COLOR_TYPES(aa, blend) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kConstantOpaque_ColorType, blend);) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kConstantTransparent_ColorType, blend);) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kChangingOpaque_ColorType, blend);) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kChangingTransparent_ColorType, blend);) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kAlternatingOpaqueAndTransparent_ColorType, blend);) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kShaderOpaque_ColorType, blend);)
|
|
|
|
#define DEF_FOR_AA_MODES(blend) \
|
|
|
|
DEF_FOR_COLOR_TYPES(true, blend) \
|
|
|
|
DEF_FOR_COLOR_TYPES(false, blend)
|
|
|
|
|
2014-08-08 14:43:29 +00:00
|
|
|
// Choose kSrcOver because it always allows coverage and alpha to be conflated. kSrc only allows
|
|
|
|
// conflation when opaque, and kDarken because it isn't possilbe with standard GL blending.
|
2018-11-20 19:58:23 +00:00
|
|
|
DEF_FOR_AA_MODES(SkBlendMode::kSrcOver)
|
|
|
|
DEF_FOR_AA_MODES(SkBlendMode::kSrc)
|
|
|
|
DEF_FOR_AA_MODES(SkBlendMode::kDarken)
|
|
|
|
|
|
|
|
// Only do a limited run of perspective tests
|
|
|
|
#define DEF_FOR_PERSP_MODES(aa) \
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kConstantOpaque_ColorType, SkBlendMode::kSrcOver, true);)\
|
|
|
|
DEF_BENCH(return new RotRectBench(aa, kShaderOpaque_ColorType, SkBlendMode::kSrcOver, true);)
|
|
|
|
DEF_FOR_PERSP_MODES(true)
|
|
|
|
DEF_FOR_PERSP_MODES(false)
|