blob: 96f3ad41e1a427c9c33e15708a73d057a97bb573 [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.orgc71da1f2014-05-07 21:16:09 +000020 void operator()(const SkRecords::DrawRect& draw) {
21 fArea += (int)(draw.rect.width() * draw.rect.height());
22 }
23
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000024 int area() const { return fArea; }
25
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000026 void apply(const SkRecord& record) {
27 for (unsigned i = 0; i < record.count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000028 record.visit<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000029 }
30 }
31
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000032private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000033 int fArea;
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000034};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000035
36// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
37struct Stretch {
38 template <typename T> void operator()(T*) {}
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000039 void operator()(SkRecords::DrawRect* draw) {
40 draw->rect.fRight *= 2;
41 draw->rect.fBottom *= 2;
42 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000043
44 void apply(SkRecord* record) {
45 for (unsigned i = 0; i < record->count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000046 record->mutate<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000047 }
48 }
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000049};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000050
51// Basic tests for the low-level SkRecord code.
52DEF_TEST(Record, r) {
53 SkRecord record;
54
55 // Add a simple DrawRect command.
56 SkRect rect = SkRect::MakeWH(10, 10);
57 SkPaint paint;
commit-bot@chromium.org37f6e622014-05-07 22:59:38 +000058 SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRect, (paint, rect));
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000059
60 // Its area should be 100.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000061 AreaSummer summer;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000062 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000063 REPORTER_ASSERT(r, summer.area() == 100);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000064
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000065 // Scale 2x.
66 Stretch stretch;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000067 stretch.apply(&record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000068
69 // Now its area should be 100 + 400.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000070 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000071 REPORTER_ASSERT(r, summer.area() == 500);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000072}