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