SamplePatternDictionaryTest.cpp 4.0 KB

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