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-05-07 21:16:09 +00:00
|
|
|
void operator()(const SkRecords::DrawRect& draw) {
|
|
|
|
fArea += (int)(draw.rect.width() * draw.rect.height());
|
|
|
|
}
|
|
|
|
|
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++) {
|
2014-05-07 21:16:09 +00:00
|
|
|
record.visit<void>(i, *this);
|
2014-04-22 16:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
|
|
|
|
struct Stretch {
|
|
|
|
template <typename T> void operator()(T*) {}
|
2014-05-07 21:16:09 +00:00
|
|
|
void operator()(SkRecords::DrawRect* draw) {
|
|
|
|
draw->rect.fRight *= 2;
|
|
|
|
draw->rect.fBottom *= 2;
|
|
|
|
}
|
2014-04-22 16:57:20 +00:00
|
|
|
|
|
|
|
void apply(SkRecord* record) {
|
|
|
|
for (unsigned i = 0; i < record->count(); i++) {
|
2014-05-07 21:16:09 +00:00
|
|
|
record->mutate<void>(i, *this);
|
2014-04-22 16:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-08 17:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
2014-05-07 22:59:38 +00:00
|
|
|
SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRect, (paint, rect));
|
2014-04-08 17:31:08 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|