skia2/bench/PathIterBench.cpp
commit-bot@chromium.org 644629c1c7 Implement a benchmark for GrResourceCache
Adds "grresourcecache_add" and "grresourcecache_find" bench tests to test
GrResourceCache::add and GrResourceCache::find. The tests work only
with GPU backends, since GrResourceCache needs an GrGpu.

Modifies bench tests to override SkBenchmark::isSuitableFor(Backend)
function that specifies what kind of backend the test is inteded
for. This replaces the previous "fIsRendering" flag that would
indicate test that did no rendering.

Adds SkCanvas::getGrContext() call to get the GrContext that the
canvas ends up drawing to. The member function solves a common
use-case that is also used in the benchmark added here.

R=mtklein@google.com, bsalomon@google.com

Author: kkinnunen@nvidia.com

Review URL: https://codereview.chromium.org/73643005

git-svn-id: http://skia.googlecode.com/svn/trunk@12334 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-11-21 06:21:58 +00:00

96 lines
2.4 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBenchmark.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
#include "SkPaint.h"
#include "SkRandom.h"
#include "SkShader.h"
#include "SkString.h"
static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
int n = rand.nextU() & 3;
n += 1;
for (int i = 0; i < n; ++i) {
pts[i].fX = rand.nextSScalar1();
pts[i].fY = rand.nextSScalar1();
}
return n;
}
class PathIterBench : public SkBenchmark {
SkString fName;
SkPath fPath;
bool fRaw;
public:
PathIterBench(bool raw) {
fName.printf("pathiter_%s", raw ? "raw" : "consume");
fRaw = raw;
SkRandom rand;
for (int i = 0; i < 1000; ++i) {
SkPoint pts[4];
int n = rand_pts(rand, pts);
switch (n) {
case 1:
fPath.moveTo(pts[0]);
break;
case 2:
fPath.lineTo(pts[1]);
break;
case 3:
fPath.quadTo(pts[1], pts[2]);
break;
case 4:
fPath.cubicTo(pts[1], pts[2], pts[3]);
break;
}
}
}
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
return backend == kNonRendering_Backend;
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(SkCanvas*) SK_OVERRIDE {
if (fRaw) {
for (int i = 0; i < this->getLoops(); ++i) {
SkPath::RawIter iter(fPath);
SkPath::Verb verb;
SkPoint pts[4];
while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
}
} else {
for (int i = 0; i < this->getLoops(); ++i) {
SkPath::Iter iter(fPath, false);
SkPath::Verb verb;
SkPoint pts[4];
while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
}
}
}
private:
typedef SkBenchmark INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
DEF_BENCH( return new PathIterBench(false); )
DEF_BENCH( return new PathIterBench(true); )