• 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 "SkBitmap.h"
11 #include "SkImageInfo.h"
12 #include "SkShader.h"
13 #include "SkRecord.h"
14 #include "SkRecords.h"
15 
16 // Sums the area of any DrawRect command it sees.
17 class AreaSummer {
18 public:
AreaSummer()19     AreaSummer() : fArea(0) {}
20 
operator ()(const T &)21     template <typename T> void operator()(const T&) { }
22 
operator ()(const SkRecords::DrawRect & draw)23     void operator()(const SkRecords::DrawRect& draw) {
24         fArea += (int)(draw.rect.width() * draw.rect.height());
25     }
26 
area() const27     int area() const { return fArea; }
28 
apply(const SkRecord & record)29     void apply(const SkRecord& record) {
30         for (unsigned i = 0; i < record.count(); i++) {
31             record.visit<void>(i, *this);
32         }
33     }
34 
35 private:
36     int fArea;
37 };
38 
39 // Scales out the bottom-right corner of any DrawRect command it sees by 2x.
40 struct Stretch {
operator ()Stretch41     template <typename T> void operator()(T*) {}
operator ()Stretch42     void operator()(SkRecords::DrawRect* draw) {
43         draw->rect.fRight *= 2;
44         draw->rect.fBottom *= 2;
45     }
46 
applyStretch47     void apply(SkRecord* record) {
48         for (unsigned i = 0; i < record->count(); i++) {
49             record->mutate<void>(i, *this);
50         }
51     }
52 };
53 
54 #define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__))
55 
56 // Basic tests for the low-level SkRecord code.
DEF_TEST(Record,r)57 DEF_TEST(Record, r) {
58     SkRecord record;
59 
60     // Add a simple DrawRect command.
61     SkRect rect = SkRect::MakeWH(10, 10);
62     SkPaint paint;
63     APPEND(record, SkRecords::DrawRect, paint, rect);
64 
65     // Its area should be 100.
66     AreaSummer summer;
67     summer.apply(record);
68     REPORTER_ASSERT(r, summer.area() == 100);
69 
70     // Scale 2x.
71     Stretch stretch;
72     stretch.apply(&record);
73 
74     // Now its area should be 100 + 400.
75     summer.apply(record);
76     REPORTER_ASSERT(r, summer.area() == 500);
77 }
78 
79 #undef APPEND
80 
81 template <typename T>
is_aligned(const T * p)82 static bool is_aligned(const T* p) {
83     return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
84 }
85 
DEF_TEST(Record_Alignment,r)86 DEF_TEST(Record_Alignment, r) {
87     SkRecord record;
88     REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
89     REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
90     REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
91     REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
92 
93     // It's not clear if we care that 8-byte values are aligned on 32-bit machines.
94     if (sizeof(void*) == 8) {
95         REPORTER_ASSERT(r, is_aligned(record.alloc<double>()));
96         REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>()));
97     }
98 }
99 
100