2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkImageInfo.h"
14 #include "SkRecords.h"
16 // Sums the area of any DrawRect command it sees.
19 AreaSummer() : fArea(0) {}
21 template <typename T> void operator()(const T&) { }
23 void operator()(const SkRecords::DrawRect& draw) {
24 fArea += (int)(draw.rect.width() * draw.rect.height());
27 int area() const { return fArea; }
29 void apply(const SkRecord& record) {
30 for (unsigned i = 0; i < record.count(); i++) {
31 record.visit<void>(i, *this);
39 // Scales out the bottom-right corner of any DrawRect command it sees by 2x.
41 template <typename T> void operator()(T*) {}
42 void operator()(SkRecords::DrawRect* draw) {
43 draw->rect.fRight *= 2;
44 draw->rect.fBottom *= 2;
47 void apply(SkRecord* record) {
48 for (unsigned i = 0; i < record->count(); i++) {
49 record->mutate<void>(i, *this);
54 #define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__))
56 // Basic tests for the low-level SkRecord code.
60 // Add a simple DrawRect command.
61 SkRect rect = SkRect::MakeWH(10, 10);
63 APPEND(record, SkRecords::DrawRect, paint, rect);
65 // Its area should be 100.
68 REPORTER_ASSERT(r, summer.area() == 100);
72 stretch.apply(&record);
74 // Now its area should be 100 + 400.
76 REPORTER_ASSERT(r, summer.area() == 500);
82 static bool is_aligned(const T* p) {
83 return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
86 DEF_TEST(Record_Alignment, r) {
89 // Of course a byte's always aligned.
90 REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
92 // (If packed tightly, the rest below here would be off by one.)
94 // It happens that the first implementation always aligned to 4 bytes,
95 // so these two were always correct.
96 REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
97 REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
99 // These two are regression tests (void* only on 64-bit machines).
100 REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>()));
101 REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
103 // We're not testing beyond sizeof(void*), which is where the current implementation will break.