blob: f1056c2d6da34a859ffad5563024d2fec951e9b6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkImageInfo.h"
10#include "include/core/SkShader.h"
11#include "src/core/SkRecord.h"
12#include "src/core/SkRecords.h"
13#include "tests/RecordTestUtils.h"
14#include "tests/Test.h"
mtkleinc3c61942015-11-19 07:23:49 -080015
Hal Canary8a001442018-09-19 11:31:27 -040016#include <new>
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000017
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000018// Sums the area of any DrawRect command it sees.
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000019class AreaSummer {
20public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000021 AreaSummer() : fArea(0) {}
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000022
23 template <typename T> void operator()(const T&) { }
24
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000025 void operator()(const SkRecords::DrawRect& draw) {
26 fArea += (int)(draw.rect.width() * draw.rect.height());
27 }
28
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000029 int area() const { return fArea; }
30
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000031 void apply(const SkRecord& record) {
mtkleinc6ad06a2015-08-19 09:51:00 -070032 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070033 record.visit(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000034 }
35 }
36
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000037private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000038 int fArea;
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000039};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000040
41// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
42struct Stretch {
43 template <typename T> void operator()(T*) {}
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000044 void operator()(SkRecords::DrawRect* draw) {
45 draw->rect.fRight *= 2;
46 draw->rect.fBottom *= 2;
47 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000048
49 void apply(SkRecord* record) {
mtkleinc6ad06a2015-08-19 09:51:00 -070050 for (int i = 0; i < record->count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070051 record->mutate(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000052 }
53 }
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000054};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000055
mtklein449d9b72015-09-28 10:33:02 -070056#define APPEND(record, type, ...) new (record.append<type>()) type{__VA_ARGS__}
tomhudsond9305112014-07-05 13:37:53 -070057
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000058// Basic tests for the low-level SkRecord code.
59DEF_TEST(Record, r) {
60 SkRecord record;
61
62 // Add a simple DrawRect command.
63 SkRect rect = SkRect::MakeWH(10, 10);
64 SkPaint paint;
tomhudsond9305112014-07-05 13:37:53 -070065 APPEND(record, SkRecords::DrawRect, paint, rect);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000066
67 // Its area should be 100.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000068 AreaSummer summer;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000069 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000070 REPORTER_ASSERT(r, summer.area() == 100);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000071
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000072 // Scale 2x.
73 Stretch stretch;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000074 stretch.apply(&record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000075
76 // Now its area should be 100 + 400.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000077 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000078 REPORTER_ASSERT(r, summer.area() == 500);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000079}
tomhudsond9305112014-07-05 13:37:53 -070080
mtkleinc3c61942015-11-19 07:23:49 -080081DEF_TEST(Record_defrag, r) {
82 SkRecord record;
83 APPEND(record, SkRecords::Save);
84 APPEND(record, SkRecords::ClipRect);
85 APPEND(record, SkRecords::NoOp);
86 APPEND(record, SkRecords::DrawRect);
87 APPEND(record, SkRecords::NoOp);
88 APPEND(record, SkRecords::NoOp);
89 APPEND(record, SkRecords::Restore);
90 REPORTER_ASSERT(r, record.count() == 7);
91
92 record.defrag();
93 REPORTER_ASSERT(r, record.count() == 4);
94 assert_type<SkRecords::Save >(r, record, 0);
95 assert_type<SkRecords::ClipRect>(r, record, 1);
96 assert_type<SkRecords::DrawRect>(r, record, 2);
97 assert_type<SkRecords::Restore >(r, record, 3);
98}
99
tomhudsond9305112014-07-05 13:37:53 -0700100#undef APPEND
101
mtklein0209e952014-08-28 14:10:05 -0700102template <typename T>
103static bool is_aligned(const T* p) {
104 return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
105}
106
107DEF_TEST(Record_Alignment, r) {
108 SkRecord record;
mtklein0209e952014-08-28 14:10:05 -0700109 REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
mtklein0209e952014-08-28 14:10:05 -0700110 REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
111 REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
mtklein0209e952014-08-28 14:10:05 -0700112 REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
113
mtkleinf2950b12014-11-13 12:41:14 -0800114 // It's not clear if we care that 8-byte values are aligned on 32-bit machines.
115 if (sizeof(void*) == 8) {
116 REPORTER_ASSERT(r, is_aligned(record.alloc<double>()));
117 REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>()));
118 }
mtklein0209e952014-08-28 14:10:05 -0700119}