blob: a67d7e8a2ad3776d89d266c3437d11ff3732cf7d [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
mtkleinc3c61942015-11-19 07:23:49 -08008#include "RecordTestUtils.h"
tomhudsond9305112014-07-05 13:37:53 -07009#include "SkBitmap.h"
10#include "SkImageInfo.h"
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000011#include "SkRecord.h"
12#include "SkRecords.h"
mtkleinc3c61942015-11-19 07:23:49 -080013#include "SkShader.h"
14#include "Test.h"
15
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000016
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000017// Sums the area of any DrawRect command it sees.
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000018class AreaSummer {
19public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000020 AreaSummer() : fArea(0) {}
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000021
22 template <typename T> void operator()(const T&) { }
23
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000024 void operator()(const SkRecords::DrawRect& draw) {
25 fArea += (int)(draw.rect.width() * draw.rect.height());
26 }
27
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000028 int area() const { return fArea; }
29
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000030 void apply(const SkRecord& record) {
mtkleinc6ad06a2015-08-19 09:51:00 -070031 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070032 record.visit(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000033 }
34 }
35
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000036private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000037 int fArea;
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000038};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000039
40// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
41struct Stretch {
42 template <typename T> void operator()(T*) {}
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000043 void operator()(SkRecords::DrawRect* draw) {
44 draw->rect.fRight *= 2;
45 draw->rect.fBottom *= 2;
46 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000047
48 void apply(SkRecord* record) {
mtkleinc6ad06a2015-08-19 09:51:00 -070049 for (int i = 0; i < record->count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070050 record->mutate(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000051 }
52 }
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000053};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000054
mtklein449d9b72015-09-28 10:33:02 -070055#define APPEND(record, type, ...) new (record.append<type>()) type{__VA_ARGS__}
tomhudsond9305112014-07-05 13:37:53 -070056
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000057// 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;
tomhudsond9305112014-07-05 13:37:53 -070064 APPEND(record, SkRecords::DrawRect, paint, rect);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000065
66 // Its area should be 100.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000067 AreaSummer summer;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000068 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000069 REPORTER_ASSERT(r, summer.area() == 100);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000070
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000071 // Scale 2x.
72 Stretch stretch;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000073 stretch.apply(&record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000074
75 // Now its area should be 100 + 400.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000076 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000077 REPORTER_ASSERT(r, summer.area() == 500);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000078}
tomhudsond9305112014-07-05 13:37:53 -070079
mtkleinc3c61942015-11-19 07:23:49 -080080DEF_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
tomhudsond9305112014-07-05 13:37:53 -070099#undef APPEND
100
mtklein0209e952014-08-28 14:10:05 -0700101template <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;
mtklein0209e952014-08-28 14:10:05 -0700108 REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
mtklein0209e952014-08-28 14:10:05 -0700109 REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
110 REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
mtklein0209e952014-08-28 14:10:05 -0700111 REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
112
mtkleinf2950b12014-11-13 12:41:14 -0800113 // 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 }
mtklein0209e952014-08-28 14:10:05 -0700118}