blob: e58ef10ddac457153e272529427cab383361f787 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +00008#include "Test.h"
9
10#include "SkRecord.h"
11#include "SkRecords.h"
12
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000013// Sums the area of any DrawRect command it sees.
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000014class AreaSummer {
15public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000016 AreaSummer() : fArea(0) {}
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000017
18 template <typename T> void operator()(const T&) { }
19
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000020 int area() const { return fArea; }
21
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000022 void apply(const SkRecord& record) {
23 for (unsigned i = 0; i < record.count(); i++) {
24 record.visit(i, *this);
25 }
26 }
27
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000028private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000029 int fArea;
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000030};
31template <> void AreaSummer::operator()(const SkRecords::DrawRect& record) {
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000032 fArea += (int) (record.rect.width() * record.rect.height());
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000033}
34
35// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
36struct Stretch {
37 template <typename T> void operator()(T*) {}
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000038
39 void apply(SkRecord* record) {
40 for (unsigned i = 0; i < record->count(); i++) {
41 record->mutate(i, *this);
42 }
43 }
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000044};
45template <> void Stretch::operator()(SkRecords::DrawRect* record) {
46 record->rect.fRight *= 2;
47 record->rect.fBottom *= 2;
48}
49
50// Basic tests for the low-level SkRecord code.
51DEF_TEST(Record, r) {
52 SkRecord record;
53
54 // Add a simple DrawRect command.
55 SkRect rect = SkRect::MakeWH(10, 10);
56 SkPaint paint;
57 SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRect, (rect, paint));
58
59 // Its area should be 100.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000060 AreaSummer summer;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000061 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000062 REPORTER_ASSERT(r, summer.area() == 100);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000063
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000064 // Scale 2x.
65 Stretch stretch;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000066 stretch.apply(&record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000067
68 // Now its area should be 100 + 400.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000069 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000070 REPORTER_ASSERT(r, summer.area() == 500);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000071}