blob: e4421fab5265961826cefdbd1a60a4756a6be0bc [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
11#include "SkRecord.h"
12#include "SkRecords.h"
13
halcanary96fcdcc2015-08-27 07:41:13 -070014// 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 +000015template <typename U>
16struct ReadAs {
halcanary96fcdcc2015-08-27 07:41:13 -070017 ReadAs() : ptr(nullptr), type(SkRecords::Type(~0)) {}
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000018
19 const U* ptr;
20 SkRecords::Type type;
21
22 void operator()(const U& r) { ptr = &r; type = U::kType; }
23
24 template <typename T>
25 void operator()(const T&) { type = U::kType; }
26};
27
28// Assert that the ith command in record is of type T, and return it.
29template <typename T>
mtkleinc6ad06a2015-08-19 09:51:00 -070030static const T* assert_type(skiatest::Reporter* r, const SkRecord& record, int index) {
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000031 ReadAs<T> reader;
32 record.visit<void>(index, reader);
33 REPORTER_ASSERT(r, T::kType == reader.type);
bsalomon49f085d2014-09-05 13:34:00 -070034 REPORTER_ASSERT(r, SkToBool(reader.ptr));
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000035 return reader.ptr;
36}
37
reed2ff1fce2014-12-11 07:07:37 -080038template <typename DrawT> struct MatchType {
39 template <typename T> int operator()(const T&) { return 0; }
40 int operator()(const DrawT&) { return 1; }
41};
42
43template <typename DrawT> int count_instances_of_type(const SkRecord& record) {
44 MatchType<DrawT> matcher;
45 int counter = 0;
mtkleinc6ad06a2015-08-19 09:51:00 -070046 for (int i = 0; i < record.count(); i++) {
reed2ff1fce2014-12-11 07:07:37 -080047 counter += record.visit<int>(i, matcher);
48 }
49 return counter;
50}
51
52template <typename DrawT> int find_first_instances_of_type(const SkRecord& record) {
53 MatchType<DrawT> matcher;
mtkleinc6ad06a2015-08-19 09:51:00 -070054 for (int i = 0; i < record.count(); i++) {
reed2ff1fce2014-12-11 07:07:37 -080055 if (record.visit<int>(i, matcher)) {
56 return i;
57 }
58 }
59 return -1;
60}
61
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000062#endif//RecordTestUtils_DEFINED