jitter_gms.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2018 Google LLC.
  2. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  3. // Jitter GMs
  4. //
  5. // Re-execute rendering tests with slight translational changes and see if
  6. // there is a significant change. Print `1` if the named test has no
  7. // significant change, `0` otherwise
  8. #include "gm/gm.h"
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkExecutor.h"
  13. #include "include/core/SkGraphics.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkTypes.h"
  17. #include "include/private/SkSemaphore.h"
  18. #include "tools/Registry.h"
  19. #include "tools/skqp/src/skqp.h"
  20. #include "tools/skqp/src/skqp_model.h"
  21. #include <math.h>
  22. #include <algorithm>
  23. #include <cstdio>
  24. #include <fstream>
  25. #include <memory>
  26. #include <mutex>
  27. #include <string>
  28. #include <vector>
  29. // Error tolerance distance in 8888 color space with Manhattan metric on color channel.
  30. static constexpr uint8_t kSkiaSkqpGlobalErrorTolerance = 8;
  31. // Number of times to jitter the canvas.
  32. static constexpr int kNumberOfJitters = 7;
  33. // Distance to translate the canvas in each jitter (direction will be different each time).
  34. static constexpr float kJitterMagnitude = 0.03125f;
  35. // The `kNumberOfJitters` different runs will each go in a different direction.
  36. // this is the angle (in radians) for the first one.
  37. static constexpr float kPhase = 0.3f;
  38. static void do_gm(SkBitmap* bm, skiagm::GM* gm, SkPoint jitter) {
  39. SkASSERT(bm);
  40. SkASSERT(gm);
  41. SkASSERT(bm->dimensions() == gm->getISize());
  42. SkCanvas canvas(*bm);
  43. SkAutoCanvasRestore autoCanvasRestore(&canvas, true);
  44. canvas.clear(SK_ColorWHITE);
  45. canvas.translate(jitter.x(), jitter.y());
  46. gm->draw(&canvas);
  47. canvas.flush();
  48. }
  49. // Return true if passes jitter test.
  50. static bool test_jitter(skiagm::GM* gm) {
  51. SkASSERT(gm);
  52. SkISize size = gm->getISize();
  53. SkBitmap control, experimental;
  54. control.allocN32Pixels(size.width(), size.height());
  55. experimental.allocN32Pixels(size.width(), size.height());
  56. do_gm(&control, gm, {0, 0});
  57. for (int i = 0; i < kNumberOfJitters; ++i) {
  58. float angle = i * (6.2831853f / kNumberOfJitters) + kPhase;
  59. do_gm(&experimental, gm, SkPoint{kJitterMagnitude * cosf(angle),
  60. kJitterMagnitude * sinf(angle)});
  61. SkQP::RenderOutcome result = skqp::Check(
  62. control.pixmap(), control.pixmap(), experimental.pixmap(),
  63. kSkiaSkqpGlobalErrorTolerance, nullptr);
  64. if (result.fTotalError > 0) {
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. static bool do_this_test(const char* name,
  71. const std::vector<std::string>& doNotRun,
  72. const std::vector<std::string>& testOnlyThese) {
  73. for (const std::string& bad : doNotRun) {
  74. if (bad == name) {
  75. return false;
  76. }
  77. }
  78. for (const std::string& good : testOnlyThese) {
  79. if (good == name) {
  80. return true;
  81. }
  82. }
  83. return testOnlyThese.empty();
  84. }
  85. int main(int argc, char** argv) {
  86. std::vector<std::string> doNotRun;
  87. std::vector<std::string> testOnlyThese;
  88. if (argc > 1) {
  89. std::ifstream ifs(argv[1]);
  90. if (ifs.is_open()) {
  91. std::string str;
  92. while (std::getline(ifs, str)) {
  93. doNotRun.push_back(str);
  94. }
  95. }
  96. }
  97. if (argc > 2) {
  98. for (int i = 2; i < argc; ++i) {
  99. testOnlyThese.emplace_back(argv[i]);
  100. }
  101. }
  102. SkGraphics::Init();
  103. std::mutex mutex;
  104. std::vector<std::string> goodResults;
  105. std::vector<std::string> badResults;
  106. int total = 0;
  107. SkSemaphore semaphore;
  108. auto executor = SkExecutor::MakeFIFOThreadPool();
  109. for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
  110. ++total;
  111. executor->add([factory, &mutex, &goodResults, &badResults,
  112. &semaphore, &doNotRun, &testOnlyThese](){
  113. std::unique_ptr<skiagm::GM> gm(factory(nullptr));
  114. const char* name = gm->getName();
  115. if (do_this_test(name, doNotRun, testOnlyThese)) {
  116. bool success = test_jitter(gm.get());
  117. std::lock_guard<std::mutex> lock(mutex);
  118. if (success) {
  119. goodResults.emplace_back(name);
  120. } else {
  121. badResults.emplace_back(name);
  122. }
  123. fputc('.', stderr);
  124. fflush(stderr);
  125. }
  126. semaphore.signal();
  127. });
  128. }
  129. while (total-- > 0) { semaphore.wait(); }
  130. fputc('\n', stderr);
  131. fflush(stderr);
  132. std::sort(goodResults.begin(), goodResults.end());
  133. std::sort(badResults.begin(), badResults.end());
  134. std::ofstream good("good.txt");
  135. std::ofstream bad("bad.txt");
  136. for (const std::string& s : goodResults) { good << s << '\n'; }
  137. for (const std::string& s : badResults) { bad << s << '\n'; }
  138. fprintf(stderr, "good = %u\nbad = %u\n\n",
  139. (unsigned)goodResults.size(), (unsigned)badResults.size());
  140. }