jitter_calculator.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2022 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 ASH_AMBIENT_UI_JITTER_CALCULATOR_H_
  5. #define ASH_AMBIENT_UI_JITTER_CALCULATOR_H_
  6. #include "ash/ash_export.h"
  7. #include "base/callback.h"
  8. #include "ui/gfx/geometry/vector2d.h"
  9. namespace ash {
  10. // Calculates jitter to apply to a UI with the ultimate goal of preventing
  11. // screen burn. The methodology is:
  12. // * Each time jitter is calculated, move by a fixed "step size" (in pixels) in
  13. // both x and y directions. Possible incremental offsets are -step_size, 0,
  14. // and step_size. This is decided randomly.
  15. // * There are limits to the total amount that the jitter can displace the UI
  16. // from its original unshifted position.
  17. // The caller is responsible for calling Calculate() at the desired frequency.
  18. // The recommendation is approximately every 1-2 minutes, but this is specific
  19. // to each use case.
  20. class ASH_EXPORT JitterCalculator {
  21. public:
  22. struct Config {
  23. static constexpr int kDefaultStepSize = 2;
  24. static constexpr int kDefaultMaxAbsTranslation = 10;
  25. int step_size = kDefaultStepSize;
  26. // Largest the UI can be globally displaced from its original position in
  27. // both x and y directions. Bounds are inclusive. Requirements:
  28. // * Range (max_translation - min_translation) must be >= the |step_size|.
  29. // * Range must include 0 (the original unshifted position),
  30. int x_min_translation = -kDefaultMaxAbsTranslation;
  31. int x_max_translation = kDefaultMaxAbsTranslation;
  32. int y_min_translation = -kDefaultMaxAbsTranslation;
  33. int y_max_translation = kDefaultMaxAbsTranslation;
  34. };
  35. // Must return either 0 or 1.
  36. using RandomBinaryGenerator = base::RepeatingCallback<int()>;
  37. explicit JitterCalculator(Config config);
  38. // Constructor exposed for testing purposes to allow injecting a custom
  39. // random number generator.
  40. JitterCalculator(Config config,
  41. RandomBinaryGenerator random_binary_generator);
  42. JitterCalculator(const JitterCalculator& other) = delete;
  43. JitterCalculator& operator=(const JitterCalculator& other) = delete;
  44. ~JitterCalculator();
  45. // Returns the new total translation to apply from the UI's original unshifted
  46. // position (0, 0).
  47. gfx::Vector2d Calculate();
  48. private:
  49. void AssetCurrentTranslationWithinBounds() const;
  50. const Config config_;
  51. const RandomBinaryGenerator random_binary_generator_;
  52. // Current total translation from the original unshifted position.
  53. gfx::Vector2d current_translation_;
  54. // The direction to translate for the x/y coordinates. `1` means positive
  55. // translate, `-1` negative. Initial values are arbitrary as they are
  56. // continuously updated as the jitter is calculated.
  57. int translate_x_direction = 1;
  58. int translate_y_direction = -1;
  59. };
  60. } // namespace ash
  61. #endif // ASH_AMBIENT_UI_JITTER_CALCULATOR_H_