blob: 2984baded3cf332b27af9d88fac8b58480414cf5 [file] [log] [blame]
mtkleinc6ad06a2015-08-19 09:51:00 -07001/*
2 * Copyright 2015 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.org0a98d872014-05-19 15:15:24 +00008#ifndef RecordTestUtils_DEFINED
9#define RecordTestUtils_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkRecord.h"
12#include "src/core/SkRecords.h"
13#include "tests/Test.h"
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000014
halcanary96fcdcc2015-08-27 07:41:13 -070015// If the command we're reading is a U, set ptr to it, otherwise set it to nullptr.
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000016template <typename U>
17struct ReadAs {
halcanary96fcdcc2015-08-27 07:41:13 -070018 ReadAs() : ptr(nullptr), type(SkRecords::Type(~0)) {}
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000019
20 const U* ptr;
21 SkRecords::Type type;
22
23 void operator()(const U& r) { ptr = &r; type = U::kType; }
24
25 template <typename T>
26 void operator()(const T&) { type = U::kType; }
27};
28
29// Assert that the ith command in record is of type T, and return it.
30template <typename T>
mtkleinc6ad06a2015-08-19 09:51:00 -070031static const T* assert_type(skiatest::Reporter* r, const SkRecord& record, int index) {
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000032 ReadAs<T> reader;
mtklein343a63d2016-03-22 11:46:53 -070033 record.visit(index, reader);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000034 REPORTER_ASSERT(r, T::kType == reader.type);
bsalomon49f085d2014-09-05 13:34:00 -070035 REPORTER_ASSERT(r, SkToBool(reader.ptr));
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000036 return reader.ptr;
37}
38
reed2ff1fce2014-12-11 07:07:37 -080039template <typename DrawT> struct MatchType {
40 template <typename T> int operator()(const T&) { return 0; }
41 int operator()(const DrawT&) { return 1; }
42};
43
44template <typename DrawT> int count_instances_of_type(const SkRecord& record) {
45 MatchType<DrawT> matcher;
46 int counter = 0;
mtkleinc6ad06a2015-08-19 09:51:00 -070047 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070048 counter += record.visit(i, matcher);
reed2ff1fce2014-12-11 07:07:37 -080049 }
50 return counter;
51}
52
53template <typename DrawT> int find_first_instances_of_type(const SkRecord& record) {
54 MatchType<DrawT> matcher;
mtkleinc6ad06a2015-08-19 09:51:00 -070055 for (int i = 0; i < record.count(); i++) {
mtklein343a63d2016-03-22 11:46:53 -070056 if (record.visit(i, matcher)) {
reed2ff1fce2014-12-11 07:07:37 -080057 return i;
58 }
59 }
60 return -1;
61}
62
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000063#endif//RecordTestUtils_DEFINED