• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "Test.h"
9 
10 #include "SkRecord.h"
11 #include "SkRecords.h"
12 
13 // Sums the area of any DrawRect command it sees.
14 class AreaSummer {
15 public:
AreaSummer()16     AreaSummer() : fArea(0) {}
17 
operator ()(const T &)18     template <typename T> void operator()(const T&) { }
19 
operator ()(const SkRecords::DrawRect & draw)20     void operator()(const SkRecords::DrawRect& draw) {
21         fArea += (int)(draw.rect.width() * draw.rect.height());
22     }
23 
area() const24     int area() const { return fArea; }
25 
apply(const SkRecord & record)26     void apply(const SkRecord& record) {
27         for (unsigned i = 0; i < record.count(); i++) {
28             record.visit<void>(i, *this);
29         }
30     }
31 
32 private:
33     int fArea;
34 };
35 
36 // Scales out the bottom-right corner of any DrawRect command it sees by 2x.
37 struct Stretch {
operator ()Stretch38     template <typename T> void operator()(T*) {}
operator ()Stretch39     void operator()(SkRecords::DrawRect* draw) {
40         draw->rect.fRight *= 2;
41         draw->rect.fBottom *= 2;
42     }
43 
applyStretch44     void apply(SkRecord* record) {
45         for (unsigned i = 0; i < record->count(); i++) {
46             record->mutate<void>(i, *this);
47         }
48     }
49 };
50 
51 // Basic tests for the low-level SkRecord code.
DEF_TEST(Record,r)52 DEF_TEST(Record, r) {
53     SkRecord record;
54 
55     // Add a simple DrawRect command.
56     SkRect rect = SkRect::MakeWH(10, 10);
57     SkPaint paint;
58     SkNEW_PLACEMENT_ARGS(record.append<SkRecords::DrawRect>(), SkRecords::DrawRect, (paint, rect));
59 
60     // Its area should be 100.
61     AreaSummer summer;
62     summer.apply(record);
63     REPORTER_ASSERT(r, summer.area() == 100);
64 
65     // Scale 2x.
66     Stretch stretch;
67     stretch.apply(&record);
68 
69     // Now its area should be 100 + 400.
70     summer.apply(record);
71     REPORTER_ASSERT(r, summer.area() == 500);
72 }
73