2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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/SkRect.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkFloatBits.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2011-06-06 15:16:31 +00:00
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class ScalarBench : public Benchmark {
|
2011-06-06 15:16:31 +00:00
|
|
|
SkString fName;
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
ScalarBench(const char name[]) {
|
2011-06-06 15:16:31 +00:00
|
|
|
fName.printf("scalar_%s", name);
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2011-06-06 15:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void performTest() = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual int mulLoopCount() const { return 1; }
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2011-06-06 15:16:31 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2011-06-06 15:16:31 +00:00
|
|
|
this->performTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2011-06-06 15:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// having unknown values in our arrays can throw off the timing a lot, perhaps
|
2020-07-28 14:20:58 +00:00
|
|
|
// handling NaN values is a lot slower. Anyway, this is just meant to put
|
2011-06-06 15:16:31 +00:00
|
|
|
// reasonable values in our arrays.
|
|
|
|
template <typename T> void init9(T array[9]) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-06-06 15:16:31 +00:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
array[i] = rand.nextSScalar1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FloatComparisonBench : public ScalarBench {
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
FloatComparisonBench() : INHERITED("compare_float") {
|
2011-06-06 15:16:31 +00:00
|
|
|
init9(fArray);
|
|
|
|
}
|
|
|
|
protected:
|
2020-07-21 21:03:56 +00:00
|
|
|
int mulLoopCount() const override { return 4; }
|
|
|
|
void performTest() override {
|
2014-06-02 19:41:07 +00:00
|
|
|
// xoring into a volatile prevents the compiler from optimizing these checks away.
|
2021-08-05 16:16:07 +00:00
|
|
|
SK_MAYBE_UNUSED volatile bool junk = false;
|
2014-06-02 19:41:07 +00:00
|
|
|
junk ^= (fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
|
|
|
|
junk ^= (fArray[2] != 0.0f || fArray[5] != 0.0f);
|
2011-06-06 15:16:31 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
float fArray[9];
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = ScalarBench;
|
2011-06-06 15:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ForcedIntComparisonBench : public ScalarBench {
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
ForcedIntComparisonBench()
|
|
|
|
: INHERITED("compare_forced_int") {
|
2011-06-06 15:16:31 +00:00
|
|
|
init9(fArray);
|
|
|
|
}
|
|
|
|
protected:
|
2020-07-21 21:03:56 +00:00
|
|
|
int mulLoopCount() const override { return 4; }
|
|
|
|
void performTest() override {
|
2014-06-02 19:41:07 +00:00
|
|
|
// xoring into a volatile prevents the compiler from optimizing these checks away.
|
2021-08-05 16:16:07 +00:00
|
|
|
SK_MAYBE_UNUSED volatile int32_t junk = 0;
|
2014-06-02 19:41:07 +00:00
|
|
|
junk ^= (SkScalarAs2sCompliment(fArray[6]) |
|
|
|
|
SkScalarAs2sCompliment(fArray[7]) |
|
|
|
|
(SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
|
|
|
|
junk ^= (SkScalarAs2sCompliment(fArray[2]) |
|
|
|
|
SkScalarAs2sCompliment(fArray[5]));
|
2011-06-06 15:16:31 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
static const int32_t kPersp1Int = 0x3f800000;
|
2011-06-06 17:55:11 +00:00
|
|
|
SkScalar fArray[9];
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = ScalarBench;
|
2011-06-06 15:16:31 +00:00
|
|
|
};
|
|
|
|
|
2012-06-13 12:30:35 +00:00
|
|
|
class IsFiniteScalarBench : public ScalarBench {
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
IsFiniteScalarBench() : INHERITED("isfinite") {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2012-06-13 12:30:35 +00:00
|
|
|
for (size_t i = 0; i < ARRAY_N; ++i) {
|
|
|
|
fArray[i] = rand.nextSScalar1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
int mulLoopCount() const override { return 1; }
|
|
|
|
void performTest() override {
|
2012-06-13 12:30:35 +00:00
|
|
|
int sum = 0;
|
|
|
|
for (size_t i = 0; i < ARRAY_N; ++i) {
|
|
|
|
// We pass -fArray[i], so the compiler can't cheat and treat the
|
|
|
|
// value as an int (even though we tell it that it is a float)
|
|
|
|
sum += SkScalarIsFinite(-fArray[i]);
|
|
|
|
}
|
|
|
|
// we do this so the compiler won't optimize our loop away...
|
|
|
|
this->doSomething(fArray, sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void doSomething(SkScalar array[], int sum) {}
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
ARRAY_N = 64
|
|
|
|
};
|
|
|
|
SkScalar fArray[ARRAY_N];
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = ScalarBench;
|
2012-06-13 12:30:35 +00:00
|
|
|
};
|
|
|
|
|
2012-05-15 14:14:04 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class RectBoundsBench : public Benchmark {
|
2012-05-15 14:14:04 +00:00
|
|
|
enum {
|
|
|
|
PTS = 100,
|
|
|
|
};
|
|
|
|
SkPoint fPts[PTS];
|
|
|
|
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
RectBoundsBench() {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2012-05-15 14:14:04 +00:00
|
|
|
for (int i = 0; i < PTS; ++i) {
|
|
|
|
fPts[i].fX = rand.nextSScalar1();
|
|
|
|
fPts[i].fY = rand.nextSScalar1();
|
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-15 14:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2012-05-15 14:14:04 +00:00
|
|
|
return "rect_bounds";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2012-05-15 14:14:04 +00:00
|
|
|
SkRect r;
|
2021-08-13 02:27:39 +00:00
|
|
|
for (int loop = 0; loop < loops; ++loop) {
|
2015-03-25 21:30:49 +00:00
|
|
|
for (int i = 0; i < 1000; ++i) {
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setBounds(fPts, PTS);
|
2015-03-25 21:30:49 +00:00
|
|
|
}
|
2012-05-15 14:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2012-05-15 14:14:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new FloatComparisonBench(); )
|
|
|
|
DEF_BENCH( return new ForcedIntComparisonBench(); )
|
|
|
|
DEF_BENCH( return new RectBoundsBench(); )
|
|
|
|
DEF_BENCH( return new IsFiniteScalarBench(); )
|