GrSamplePatternDictionary.cpp 1.6 KB

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