skia2/tests/RecordTest.cpp
commit-bot@chromium.org 37f6e62f11 Move paints to the front of draw structs.
The order of arguments in these structs is arbitrary, so we might as well arrange them to optimize something.  Putting the paints at the front means the logic to find the paint is a lot more concise: it's usually just ptr+0, or *(ptr+0) when the SkPaint is optional.

This considerably reduces the size of the jump table in IsDraw::operator().

BUG=skia:2378
R=fmalita@chromium.org, mtklein@google.com

Author: mtklein@chromium.org

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

git-svn-id: http://skia.googlecode.com/svn/trunk@14634 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-05-07 22:59:38 +00:00

73 lines
1.7 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkRecord.h"
#include "SkRecords.h"
// Sums the area of any DrawRect command it sees.
class AreaSummer {
public:
AreaSummer() : fArea(0) {}
template <typename T> void operator()(const T&) { }
void operator()(const SkRecords::DrawRect& draw) {
fArea += (int)(draw.rect.width() * draw.rect.height());
}
int area() const { return fArea; }
void apply(const SkRecord& record) {
for (unsigned i = 0; i < record.count(); i++) {
record.visit<void>(i, *this);
}
}
private:
int fArea;
};
// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
struct Stretch {
template <typename T> void operator()(T*) {}
void operator()(SkRecords::DrawRect* draw) {
draw->rect.fRight *= 2;
draw->rect.fBottom *= 2;
}
void apply(SkRecord* record) {
for (unsigned i = 0; i < record->count(); i++) {
record->mutate<void>(i, *this);
}
}
};
// Basic tests for the low-level SkRecord code.
DEF_TEST(Record, r) {
SkRecord record;
// Add a simple DrawRect command.
SkRect rect = SkRect::MakeWH(10, 10);
SkPaint paint;
SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRect, (paint, rect));
// Its area should be 100.
AreaSummer summer;
summer.apply(record);
REPORTER_ASSERT(r, summer.area() == 100);
// Scale 2x.
Stretch stretch;
stretch.apply(&record);
// Now its area should be 100 + 400.
summer.apply(record);
REPORTER_ASSERT(r, summer.area() == 500);
}