skia_gold_matching_algorithm.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_BASE_TEST_SKIA_GOLD_MATCHING_ALGORITHM_H_
  5. #define UI_BASE_TEST_SKIA_GOLD_MATCHING_ALGORITHM_H_
  6. #include <string>
  7. namespace base {
  8. class CommandLine;
  9. }
  10. namespace ui {
  11. namespace test {
  12. // Describes what algorithm to use to match golden images.
  13. // There are three algorithms (exact, fuzzy, sobel), and the latter two have
  14. // adjustable parameters:
  15. //
  16. // Fuzzy has the max number of different pixels, the max per-channel delta sum
  17. // (i.e. how much a pixel can differ by and still be considered the same), and
  18. // how many border pixels to ignore. The number of ignored border pixels is
  19. // mainly to make the Sobel filter work better, as if an edge goes all the way
  20. // to the border of the image, the pixels along the border won't be blacked
  21. // out due to the Sobel filter.
  22. //
  23. // Sobel has the parameters of fuzzy + the edge threshold, which determines how
  24. // much of the image should be blacked out based on the generated Sobel filter.
  25. //
  26. // In general, it works like this:
  27. // 1. Try an exact comparison check against all approved images for the test,
  28. // returning success if a hash matches.
  29. // 2. Get the most recent image for the trace (test + any reported JSON keys,
  30. // e.g. OS/hardware information)
  31. // 3. If the algorithm is Sobel, generate a Sobel filter and black out parts
  32. // of the image based on the provided edge threshold.
  33. // 4. Do a fuzzy comparison of the images.
  34. //
  35. // To determine the suitable parameter values using historical data for a test:
  36. // https://cs.chromium.org/chromium/src/content/test/gpu/gold_inexact_matching/determine_gold_inexact_parameters.py
  37. class SkiaGoldMatchingAlgorithm {
  38. public:
  39. SkiaGoldMatchingAlgorithm();
  40. virtual ~SkiaGoldMatchingAlgorithm();
  41. // Append the algorithm parameter to |cmd|.
  42. virtual void AppendAlgorithmToCmdline(base::CommandLine& cmd) const;
  43. protected:
  44. // The algorithm name for commandline.
  45. virtual std::string GetCommandLineSwitchName() const = 0;
  46. };
  47. class ExactSkiaGoldMatchingAlgorithm : public SkiaGoldMatchingAlgorithm {
  48. public:
  49. ExactSkiaGoldMatchingAlgorithm();
  50. ~ExactSkiaGoldMatchingAlgorithm() override;
  51. void AppendAlgorithmToCmdline(base::CommandLine& cmd) const override;
  52. protected:
  53. std::string GetCommandLineSwitchName() const override;
  54. };
  55. class FuzzySkiaGoldMatchingAlgorithm : public SkiaGoldMatchingAlgorithm {
  56. public:
  57. FuzzySkiaGoldMatchingAlgorithm(int max_different_pixels,
  58. int pixel_delta_threshold,
  59. int ignored_border_thickness = 0);
  60. ~FuzzySkiaGoldMatchingAlgorithm() override;
  61. void AppendAlgorithmToCmdline(base::CommandLine& cmd) const override;
  62. protected:
  63. std::string GetCommandLineSwitchName() const override;
  64. private:
  65. int max_different_pixels_{0};
  66. int pixel_delta_threshold_{0};
  67. int ignored_border_thickness_{0};
  68. };
  69. class SobelSkiaGoldMatchingAlgorithm : public FuzzySkiaGoldMatchingAlgorithm {
  70. public:
  71. SobelSkiaGoldMatchingAlgorithm(int max_different_pixels,
  72. int pixel_delta_threshold,
  73. int edge_threshold,
  74. int ignored_border_thickness = 0);
  75. ~SobelSkiaGoldMatchingAlgorithm() override;
  76. void AppendAlgorithmToCmdline(base::CommandLine& cmd) const override;
  77. protected:
  78. std::string GetCommandLineSwitchName() const override;
  79. private:
  80. int edge_threshold_{0};
  81. };
  82. } // namespace test
  83. } // namespace ui
  84. #endif // UI_BASE_TEST_SKIA_GOLD_MATCHING_ALGORITHM_H_