2011-10-31 14:33:35 +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/SkCanvas.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkAAClip.h"
|
|
|
|
#include "src/core/SkClipOpPriv.h"
|
2012-05-07 18:07:36 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls
|
2014-06-19 19:32:29 +00:00
|
|
|
class AAClipBench : public Benchmark {
|
2012-05-07 18:07:36 +00:00
|
|
|
SkString fName;
|
|
|
|
SkPath fClipPath;
|
|
|
|
SkRect fClipRect;
|
|
|
|
SkRect fDrawRect;
|
|
|
|
bool fDoPath;
|
|
|
|
bool fDoAA;
|
|
|
|
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
AAClipBench(bool doPath, bool doAA)
|
|
|
|
: fDoPath(doPath)
|
2012-05-09 15:48:31 +00:00
|
|
|
, fDoAA(doAA) {
|
2012-05-07 18:07:36 +00:00
|
|
|
|
|
|
|
fName.printf("aaclip_%s_%s",
|
|
|
|
doPath ? "path" : "rect",
|
|
|
|
doAA ? "AA" : "BW");
|
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
fClipRect.setLTRB(10.5f, 10.5f, 50.5f, 50.5f);
|
2019-09-14 19:13:23 +00:00
|
|
|
fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
|
2019-08-24 23:39:13 +00:00
|
|
|
fDrawRect.setWH(100, 100);
|
2012-05-07 18:07:36 +00:00
|
|
|
|
|
|
|
SkASSERT(fClipPath.isConvex());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-07-21 21:03:56 +00:00
|
|
|
const char* onGetName() override { return fName.c_str(); }
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2012-05-07 18:07:36 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
this->setupPaint(&paint);
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-07 18:07:36 +00:00
|
|
|
// jostle the clip regions each time to prevent caching
|
|
|
|
fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
|
|
|
|
fClipPath.reset();
|
2019-09-14 19:13:23 +00:00
|
|
|
fClipPath.addRoundRect(fClipRect,
|
|
|
|
SkIntToScalar(5), SkIntToScalar(5));
|
2012-05-07 18:07:36 +00:00
|
|
|
SkASSERT(fClipPath.isConvex());
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
#if 1
|
|
|
|
if (fDoPath) {
|
2020-09-16 17:55:05 +00:00
|
|
|
canvas->clipPath(fClipPath, SkClipOp::kIntersect, fDoAA);
|
2012-05-07 18:07:36 +00:00
|
|
|
} else {
|
2020-09-16 17:55:05 +00:00
|
|
|
canvas->clipRect(fClipRect, SkClipOp::kIntersect, fDoAA);
|
2012-05-07 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas->drawRect(fDrawRect, paint);
|
|
|
|
#else
|
|
|
|
// this path tests out directly draw the clip primitive
|
|
|
|
// use it to comparing just drawing the clip vs. drawing using
|
|
|
|
// the clip
|
|
|
|
if (fDoPath) {
|
|
|
|
canvas->drawPath(fClipPath, paint);
|
|
|
|
} else {
|
|
|
|
canvas->drawRect(fClipRect, paint);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2012-05-07 18:07:36 +00:00
|
|
|
};
|
|
|
|
|
2012-05-09 15:48:31 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// This bench tests out nested clip stacks. It is intended to simulate
|
|
|
|
// how WebKit nests clips.
|
2014-06-19 19:32:29 +00:00
|
|
|
class NestedAAClipBench : public Benchmark {
|
2012-05-09 15:48:31 +00:00
|
|
|
SkString fName;
|
|
|
|
bool fDoAA;
|
|
|
|
SkRect fDrawRect;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom fRandom;
|
2012-05-09 15:48:31 +00:00
|
|
|
|
2012-05-09 16:27:10 +00:00
|
|
|
static const int kNestingDepth = 3;
|
2012-05-09 15:48:31 +00:00
|
|
|
static const int kImageSize = 400;
|
|
|
|
|
|
|
|
SkPoint fSizes[kNestingDepth+1];
|
|
|
|
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
NestedAAClipBench(bool doAA) : fDoAA(doAA) {
|
2012-05-09 15:48:31 +00:00
|
|
|
fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW");
|
|
|
|
|
2012-08-23 18:09:54 +00:00
|
|
|
fDrawRect = SkRect::MakeLTRB(0, 0,
|
|
|
|
SkIntToScalar(kImageSize),
|
2012-05-09 15:48:31 +00:00
|
|
|
SkIntToScalar(kImageSize));
|
|
|
|
|
|
|
|
fSizes[0].set(SkIntToScalar(kImageSize), SkIntToScalar(kImageSize));
|
|
|
|
|
|
|
|
for (int i = 1; i < kNestingDepth+1; ++i) {
|
|
|
|
fSizes[i].set(fSizes[i-1].fX/2, fSizes[i-1].fY/2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-07-21 21:03:56 +00:00
|
|
|
const char* onGetName() override { return fName.c_str(); }
|
2012-05-09 15:48:31 +00:00
|
|
|
|
|
|
|
|
2012-08-23 18:09:54 +00:00
|
|
|
void recurse(SkCanvas* canvas,
|
2012-05-09 15:48:31 +00:00
|
|
|
int depth,
|
|
|
|
const SkPoint& offset) {
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
2012-08-23 18:09:54 +00:00
|
|
|
SkRect temp = SkRect::MakeLTRB(0, 0,
|
2012-05-09 15:48:31 +00:00
|
|
|
fSizes[depth].fX, fSizes[depth].fY);
|
|
|
|
temp.offset(offset);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.addRoundRect(temp, SkIntToScalar(3), SkIntToScalar(3));
|
|
|
|
SkASSERT(path.isConvex());
|
|
|
|
|
2020-09-16 17:55:05 +00:00
|
|
|
canvas->clipPath(path, SkClipOp::kIntersect, fDoAA);
|
2012-05-09 15:48:31 +00:00
|
|
|
|
|
|
|
if (kNestingDepth == depth) {
|
|
|
|
// we only draw the draw rect at the lowest nesting level
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xff000000 | fRandom.nextU());
|
|
|
|
canvas->drawRect(fDrawRect, paint);
|
|
|
|
} else {
|
|
|
|
SkPoint childOffset = offset;
|
|
|
|
this->recurse(canvas, depth+1, childOffset);
|
|
|
|
|
|
|
|
childOffset += fSizes[depth+1];
|
|
|
|
this->recurse(canvas, depth+1, childOffset);
|
|
|
|
|
|
|
|
childOffset.fX = offset.fX + fSizes[depth+1].fX;
|
|
|
|
childOffset.fY = offset.fY;
|
|
|
|
this->recurse(canvas, depth+1, childOffset);
|
|
|
|
|
|
|
|
childOffset.fX = offset.fX;
|
|
|
|
childOffset.fY = offset.fY + fSizes[depth+1].fY;
|
|
|
|
this->recurse(canvas, depth+1, childOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:03:56 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2012-05-09 15:48:31 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-09 15:48:31 +00:00
|
|
|
SkPoint offset = SkPoint::Make(0, 0);
|
|
|
|
this->recurse(canvas, 0, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2012-05-09 15:48:31 +00:00
|
|
|
};
|
|
|
|
|
2012-05-07 18:07:36 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-06-19 19:32:29 +00:00
|
|
|
class AAClipBuilderBench : public Benchmark {
|
2011-10-31 14:33:35 +00:00
|
|
|
SkString fName;
|
|
|
|
SkPath fPath;
|
|
|
|
SkRect fRect;
|
|
|
|
SkRegion fRegion;
|
|
|
|
bool fDoPath;
|
|
|
|
bool fDoAA;
|
|
|
|
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
AAClipBuilderBench(bool doPath, bool doAA) {
|
2011-10-31 14:33:35 +00:00
|
|
|
fDoPath = doPath;
|
|
|
|
fDoAA = doAA;
|
|
|
|
|
|
|
|
fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
|
|
|
|
doAA ? "AA" : "BW");
|
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
fRegion.setRect({0, 0, 640, 480});
|
2011-10-31 14:33:35 +00:00
|
|
|
fRect.set(fRegion.getBounds());
|
|
|
|
fRect.inset(SK_Scalar1/4, SK_Scalar1/4);
|
|
|
|
fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-07-21 21:03:56 +00:00
|
|
|
const char* onGetName() override { return fName.c_str(); }
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2011-10-31 14:33:35 +00:00
|
|
|
SkPaint paint;
|
|
|
|
this->setupPaint(&paint);
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2011-10-31 14:33:35 +00:00
|
|
|
SkAAClip clip;
|
|
|
|
if (fDoPath) {
|
|
|
|
clip.setPath(fPath, &fRegion, fDoAA);
|
|
|
|
} else {
|
|
|
|
clip.setRect(fRect, fDoAA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2011-10-31 14:33:35 +00:00
|
|
|
};
|
|
|
|
|
2012-05-07 18:07:36 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-06-19 19:32:29 +00:00
|
|
|
class AAClipRegionBench : public Benchmark {
|
2011-11-28 19:54:56 +00:00
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
AAClipRegionBench() {
|
2011-11-28 19:54:56 +00:00
|
|
|
SkPath path;
|
|
|
|
// test conversion of a complex clip to a aaclip
|
|
|
|
path.addCircle(0, 0, SkIntToScalar(200));
|
|
|
|
path.addCircle(0, 0, SkIntToScalar(180));
|
|
|
|
// evenodd means we've constructed basically a stroked circle
|
2019-11-26 17:17:17 +00:00
|
|
|
path.setFillType(SkPathFillType::kEvenOdd);
|
2011-11-28 19:54:56 +00:00
|
|
|
|
|
|
|
SkIRect bounds;
|
|
|
|
path.getBounds().roundOut(&bounds);
|
|
|
|
fRegion.setPath(path, SkRegion(bounds));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-07-21 21:03:56 +00:00
|
|
|
const char* onGetName() override { return "aaclip_setregion"; }
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2011-11-28 19:54:56 +00:00
|
|
|
SkAAClip clip;
|
|
|
|
clip.setRegion(fRegion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkRegion fRegion;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2011-11-28 19:54:56 +00:00
|
|
|
};
|
|
|
|
|
2012-05-07 18:07:36 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2011-10-31 14:33:35 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new AAClipBuilderBench(false, false);)
|
|
|
|
DEF_BENCH(return new AAClipBuilderBench(false, true);)
|
|
|
|
DEF_BENCH(return new AAClipBuilderBench(true, false);)
|
|
|
|
DEF_BENCH(return new AAClipBuilderBench(true, true);)
|
|
|
|
DEF_BENCH(return new AAClipRegionBench();)
|
|
|
|
DEF_BENCH(return new AAClipBench(false, false);)
|
|
|
|
DEF_BENCH(return new AAClipBench(false, true);)
|
|
|
|
DEF_BENCH(return new AAClipBench(true, false);)
|
|
|
|
DEF_BENCH(return new AAClipBench(true, true);)
|
|
|
|
DEF_BENCH(return new NestedAAClipBench(false);)
|
|
|
|
DEF_BENCH(return new NestedAAClipBench(true);)
|