blob: 49efc28d2cc92c5967f7674ee50508bccfa16f99 [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
tomhudsond9305112014-07-05 13:37:53 -070010#include "SkBitmap.h"
11#include "SkImageInfo.h"
12#include "SkShader.h"
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000013#include "SkRecord.h"
14#include "SkRecords.h"
15
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000016// Sums the area of any DrawRect command it sees.
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000017class AreaSummer {
18public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000019 AreaSummer() : fArea(0) {}
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000020
21 template <typename T> void operator()(const T&) { }
22
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000023 void operator()(const SkRecords::DrawRect& draw) {
24 fArea += (int)(draw.rect.width() * draw.rect.height());
25 }
26
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000027 int area() const { return fArea; }
28
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000029 void apply(const SkRecord& record) {
30 for (unsigned i = 0; i < record.count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000031 record.visit<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000032 }
33 }
34
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000035private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000036 int fArea;
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000037};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000038
39// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
40struct Stretch {
41 template <typename T> void operator()(T*) {}
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000042 void operator()(SkRecords::DrawRect* draw) {
43 draw->rect.fRight *= 2;
44 draw->rect.fBottom *= 2;
45 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000046
47 void apply(SkRecord* record) {
48 for (unsigned i = 0; i < record->count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000049 record->mutate<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000050 }
51 }
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000052};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000053
tomhudsond9305112014-07-05 13:37:53 -070054#define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__))
55
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000056// Basic tests for the low-level SkRecord code.
57DEF_TEST(Record, r) {
58 SkRecord record;
59
60 // Add a simple DrawRect command.
61 SkRect rect = SkRect::MakeWH(10, 10);
62 SkPaint paint;
tomhudsond9305112014-07-05 13:37:53 -070063 APPEND(record, SkRecords::DrawRect, paint, rect);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000064
65 // Its area should be 100.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000066 AreaSummer summer;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000067 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000068 REPORTER_ASSERT(r, summer.area() == 100);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000069
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000070 // Scale 2x.
71 Stretch stretch;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000072 stretch.apply(&record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000073
74 // Now its area should be 100 + 400.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000075 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000076 REPORTER_ASSERT(r, summer.area() == 500);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000077}
tomhudsond9305112014-07-05 13:37:53 -070078
tomhudsond9305112014-07-05 13:37:53 -070079#undef APPEND
80
mtklein0209e952014-08-28 14:10:05 -070081template <typename T>
82static bool is_aligned(const T* p) {
83 return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
84}
85
86DEF_TEST(Record_Alignment, r) {
87 SkRecord record;
mtklein0209e952014-08-28 14:10:05 -070088 REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
mtklein0209e952014-08-28 14:10:05 -070089 REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
90 REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
mtklein0209e952014-08-28 14:10:05 -070091 REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
92
mtkleinf2950b12014-11-13 12:41:14 -080093 // 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 }
mtklein0209e952014-08-28 14:10:05 -070098}
99