blob: 66b9b68575fdbbe0ec7d4bc6236ae7ebe9886157 [file] [log] [blame]
Chris Daltond7291ba2019-03-07 14:17:03 -07001/*
2 * Copyright 2019 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
8#include "SkTypes.h"
9#include "SkPoint.h"
10#include "SkRandom.h"
11#include "Test.h"
12#include <vector>
13
14#if SK_SUPPORT_GPU
15
16#include "GrSamplePatternDictionary.h"
17
18static SkTArray<SkPoint> make_sample_pattern(const std::vector<SkPoint>& sampleLocations) {
19 return SkTArray<SkPoint>(sampleLocations.data(), sampleLocations.size());
20}
21
22static SkTArray<SkPoint> make_random_sample_pattern(SkRandom* rand) {
23 SkTArray<SkPoint> pattern;
24 int count = rand->nextULessThan(20) + 1;
25 pattern.reset(count);
26 for (int i = 0; i < count; ++i) {
27 pattern[i] = SkPoint::Make(rand->nextF(), rand->nextF());
28 }
29 return pattern;
30}
31
32// This test ensures that the sample pattern dictionary caches and retrieves patterns correctly.
33DEF_TEST(SamplePatternDictionary, reporter) {
34 SkTArray<SkTArray<SkPoint>> testPatterns;
35 testPatterns.push_back() = make_sample_pattern({ // Intel on mac, msaa8, offscreen.
36 {0.562500, 0.312500},
37 {0.437500, 0.687500},
38 {0.812500, 0.562500},
39 {0.312500, 0.187500},
40 {0.187500, 0.812500},
41 {0.062500, 0.437500},
42 {0.687500, 0.937500},
43 {0.937500, 0.062500}
44 });
45
46 testPatterns.push_back() = make_sample_pattern({ // Intel on mac, msaa8, on-screen.
47 {0.562500, 0.687500},
48 {0.437500, 0.312500},
49 {0.812500, 0.437500},
50 {0.312500, 0.812500},
51 {0.187500, 0.187500},
52 {0.062500, 0.562500},
53 {0.687500, 0.062500},
54 {0.937500, 0.937500}
55 });
56
57 testPatterns.push_back() = make_sample_pattern({ // NVIDIA, msaa16.
58 {0.062500, 0.000000},
59 {0.250000, 0.125000},
60 {0.187500, 0.375000},
61 {0.437500, 0.312500},
62 {0.500000, 0.062500},
63 {0.687500, 0.187500},
64 {0.750000, 0.437500},
65 {0.937500, 0.250000},
66 {0.000000, 0.500000},
67 {0.312500, 0.625000},
68 {0.125000, 0.750000},
69 {0.375000, 0.875000},
70 {0.562500, 0.562500},
71 {0.812500, 0.687500},
72 {0.625000, 0.812500},
73 {0.875000, 0.937500}
74 });
75
76 testPatterns.push_back() = make_sample_pattern({ // NVIDIA, mixed samples, 16:1.
77 {0.250000, 0.125000},
78 {0.625000, 0.812500},
79 {0.500000, 0.062500},
80 {0.812500, 0.687500},
81 {0.187500, 0.375000},
82 {0.875000, 0.937500},
83 {0.125000, 0.750000},
84 {0.750000, 0.437500},
85 {0.937500, 0.250000},
86 {0.312500, 0.625000},
87 {0.437500, 0.312500},
88 {0.000000, 0.500000},
89 {0.375000, 0.875000},
90 {0.687500, 0.187500},
91 {0.062500, 0.000000},
92 {0.562500, 0.562500}
93 });
94
95 SkRandom rand;
96 for (int i = 0; i < 23; ++i) {
97 testPatterns.push_back(make_random_sample_pattern(&rand));
98 }
99
100 // Duplicate the initial 4 patterns, with slight differences.
101 testPatterns.push_back(testPatterns[0]);
102 testPatterns.back().back().fX += 0.001f;
103
104 testPatterns.push_back(testPatterns[1]);
105 testPatterns.back().back().fY -= 0.002f;
106
107 testPatterns.push_back(testPatterns[2]);
108 testPatterns.back().push_back(SkPoint::Make(.5f, .5f));
109
110 testPatterns.push_back(testPatterns[3]);
111 testPatterns.back().pop_back();
112
113 for (int i = 0; i < 13; ++i) {
114 testPatterns.push_back(make_random_sample_pattern(&rand));
115 }
116
117 GrSamplePatternDictionary dict;
118 for (int i = 0; i < 2; ++i) {
119 for (int j = 0; j < testPatterns.count(); ++j) {
120 for (int k = 0; k < 3; ++k) {
121 const SkTArray<SkPoint>& pattern = testPatterns[testPatterns.count() - j - 1];
122 REPORTER_ASSERT(reporter, j == dict.findOrAssignSamplePatternKey(pattern));
123 }
124 }
125 }
126 for (int j = 0; j < testPatterns.count(); ++j) {
127 const SkTArray<SkPoint>& pattern = testPatterns[testPatterns.count() - j - 1];
128 REPORTER_ASSERT(reporter, dict.retrieveSampleLocations(j) == pattern);
129 }
130}
131
132#endif