FrameTimeout.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef mozilla_image_FrameTimeout_h
  7. #define mozilla_image_FrameTimeout_h
  8. #include <stdint.h>
  9. #include "mozilla/Assertions.h"
  10. namespace mozilla {
  11. namespace image {
  12. /**
  13. * FrameTimeout wraps a frame timeout value (measured in milliseconds) after
  14. * first normalizing it. This normalization is necessary because some tools
  15. * generate incorrect frame timeout values which we nevertheless have to
  16. * support. For this reason, code that deals with frame timeouts should always
  17. * use a FrameTimeout value rather than the raw value from the image header.
  18. */
  19. struct FrameTimeout
  20. {
  21. /**
  22. * @return a FrameTimeout of zero. This should be used only for math
  23. * involving FrameTimeout values. You can't obtain a zero FrameTimeout from
  24. * FromRawMilliseconds().
  25. */
  26. static FrameTimeout Zero() { return FrameTimeout(0); }
  27. /// @return an infinite FrameTimeout.
  28. static FrameTimeout Forever() { return FrameTimeout(-1); }
  29. /// @return a FrameTimeout obtained by normalizing a raw timeout value.
  30. static FrameTimeout FromRawMilliseconds(int32_t aRawMilliseconds)
  31. {
  32. // Normalize all infinite timeouts to the same value.
  33. if (aRawMilliseconds < 0) {
  34. return FrameTimeout::Forever();
  35. }
  36. // Very small timeout values are problematic for two reasons: we don't want
  37. // to burn energy redrawing animated images extremely fast, and broken tools
  38. // generate these values when they actually want a "default" value, so such
  39. // images won't play back right without normalization. For some context,
  40. // see bug 890743, bug 125137, bug 139677, and bug 207059. The historical
  41. // behavior of IE and Opera was:
  42. // IE 6/Win:
  43. // 10 - 50ms is normalized to 100ms.
  44. // >50ms is used unnormalized.
  45. // Opera 7 final/Win:
  46. // 10ms is normalized to 100ms.
  47. // >10ms is used unnormalized.
  48. if (aRawMilliseconds >= 0 && aRawMilliseconds <= 10 ) {
  49. return FrameTimeout(100);
  50. }
  51. // The provided timeout value is OK as-is.
  52. return FrameTimeout(aRawMilliseconds);
  53. }
  54. bool operator==(const FrameTimeout& aOther) const
  55. {
  56. return mTimeout == aOther.mTimeout;
  57. }
  58. bool operator!=(const FrameTimeout& aOther) const { return !(*this == aOther); }
  59. FrameTimeout operator+(const FrameTimeout& aOther)
  60. {
  61. if (*this == Forever() || aOther == Forever()) {
  62. return Forever();
  63. }
  64. return FrameTimeout(mTimeout + aOther.mTimeout);
  65. }
  66. FrameTimeout& operator+=(const FrameTimeout& aOther)
  67. {
  68. *this = *this + aOther;
  69. return *this;
  70. }
  71. /**
  72. * @return this FrameTimeout's value in milliseconds. Illegal to call on a
  73. * an infinite FrameTimeout value.
  74. */
  75. uint32_t AsMilliseconds() const
  76. {
  77. if (*this == Forever()) {
  78. MOZ_ASSERT_UNREACHABLE("Calling AsMilliseconds() on an infinite FrameTimeout");
  79. return 100; // Fail to something sane.
  80. }
  81. return uint32_t(mTimeout);
  82. }
  83. /**
  84. * @return this FrameTimeout value encoded so that non-negative values
  85. * represent a timeout in milliseconds, and -1 represents an infinite
  86. * timeout.
  87. *
  88. * XXX(seth): This is a backwards compatibility hack that should be removed.
  89. */
  90. int32_t AsEncodedValueDeprecated() const { return mTimeout; }
  91. private:
  92. explicit FrameTimeout(int32_t aTimeout)
  93. : mTimeout(aTimeout)
  94. { }
  95. int32_t mTimeout;
  96. };
  97. } // namespace image
  98. } // namespace mozilla
  99. #endif // mozilla_image_FrameTimeout_h