1 /*
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 "GrSamplePatternDictionary.h"
9 
operator ()(const SkTArray<SkPoint> & a,const SkTArray<SkPoint> & b) const10 bool GrSamplePatternDictionary::LessThan::operator()(
11         const SkTArray<SkPoint>& a, const SkTArray<SkPoint>& b) const {
12     if (a.count() != b.count()) {
13         return a.count() < b.count();
14     }
15     for (int i = 0; i < a.count(); ++i) {
16         // This doesn't have geometric meaning. We just need to define an ordering for std::map.
17         if (a[i].x() != b[i].x()) {
18             return a[i].x() < b[i].x();
19         }
20         if (a[i].y() != b[i].y()) {
21             return a[i].y() < b[i].y();
22         }
23     }
24     return false;  // Both sample patterns are equal, therefore, "a < b" is false.
25 }
26 
findOrAssignSamplePatternKey(const SkTArray<SkPoint> & sampleLocations)27 int GrSamplePatternDictionary::findOrAssignSamplePatternKey(
28         const SkTArray<SkPoint>& sampleLocations) {
29     if (std::numeric_limits<int>::max() == fSampleLocationsArray.count()) {
30         return 0;
31     }
32     const auto& insertResult = fSamplePatternKeyMap.insert(
33             {sampleLocations, fSampleLocationsArray.count()});
34     if (insertResult.second) {
35         // This means the "insert" call did not find the pattern in the key map already, and
36         // therefore an actual insertion took place. (We don't expect to see many unique sample
37         // patterns.)
38         const SkTArray<SkPoint>& sampleLocations = insertResult.first->first;
39         fSampleLocationsArray.push_back(&sampleLocations);
40     }
41     return insertResult.first->second;  // Return the new sample pattern key.
42 }
43