2014-04-11 18:33:31 +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.
|
|
|
|
*/
|
|
|
|
|
2014-04-08 17:31:08 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#include "SkRecord.h"
|
|
|
|
#include "SkRecords.h"
|
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
// Sums the area of any DrawRect command it sees.
|
2014-04-08 17:31:08 +00:00
|
|
|
class AreaSummer {
|
|
|
|
public:
|
2014-04-08 23:31:35 +00:00
|
|
|
AreaSummer() : fArea(0) {}
|
2014-04-08 17:31:08 +00:00
|
|
|
|
|
|
|
template <typename T> void operator()(const T&) { }
|
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
int area() const { return fArea; }
|
|
|
|
|
2014-04-22 16:57:20 +00:00
|
|
|
void apply(const SkRecord& record) {
|
|
|
|
for (unsigned i = 0; i < record.count(); i++) {
|
|
|
|
record.visit(i, *this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 17:31:08 +00:00
|
|
|
private:
|
2014-04-08 23:31:35 +00:00
|
|
|
int fArea;
|
2014-04-08 17:31:08 +00:00
|
|
|
};
|
|
|
|
template <> void AreaSummer::operator()(const SkRecords::DrawRect& record) {
|
2014-04-08 23:31:35 +00:00
|
|
|
fArea += (int) (record.rect.width() * record.rect.height());
|
2014-04-08 17:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
|
|
|
|
struct Stretch {
|
|
|
|
template <typename T> void operator()(T*) {}
|
2014-04-22 16:57:20 +00:00
|
|
|
|
|
|
|
void apply(SkRecord* record) {
|
|
|
|
for (unsigned i = 0; i < record->count(); i++) {
|
|
|
|
record->mutate(i, *this);
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 17:31:08 +00:00
|
|
|
};
|
|
|
|
template <> void Stretch::operator()(SkRecords::DrawRect* record) {
|
|
|
|
record->rect.fRight *= 2;
|
|
|
|
record->rect.fBottom *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, (rect, paint));
|
|
|
|
|
|
|
|
// Its area should be 100.
|
2014-04-08 23:31:35 +00:00
|
|
|
AreaSummer summer;
|
2014-04-22 16:57:20 +00:00
|
|
|
summer.apply(record);
|
2014-04-08 23:31:35 +00:00
|
|
|
REPORTER_ASSERT(r, summer.area() == 100);
|
|
|
|
|
|
|
|
// Scale 2x.
|
|
|
|
Stretch stretch;
|
2014-04-22 16:57:20 +00:00
|
|
|
stretch.apply(&record);
|
2014-04-08 23:31:35 +00:00
|
|
|
|
|
|
|
// Now its area should be 100 + 400.
|
2014-04-22 16:57:20 +00:00
|
|
|
summer.apply(record);
|
2014-04-08 23:31:35 +00:00
|
|
|
REPORTER_ASSERT(r, summer.area() == 500);
|
2014-04-08 17:31:08 +00:00
|
|
|
}
|