Image.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "Image.h"
  6. #include "nsRefreshDriver.h"
  7. #include "mozilla/TimeStamp.h"
  8. namespace mozilla {
  9. namespace image {
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // Memory Reporting
  12. ///////////////////////////////////////////////////////////////////////////////
  13. ImageMemoryCounter::ImageMemoryCounter(Image* aImage,
  14. MallocSizeOf aMallocSizeOf,
  15. bool aIsUsed)
  16. : mIsUsed(aIsUsed)
  17. {
  18. MOZ_ASSERT(aImage);
  19. // Extract metadata about the image.
  20. RefPtr<ImageURL> imageURL(aImage->GetURI());
  21. if (imageURL) {
  22. imageURL->GetSpec(mURI);
  23. }
  24. int32_t width = 0;
  25. int32_t height = 0;
  26. aImage->GetWidth(&width);
  27. aImage->GetHeight(&height);
  28. mIntrinsicSize.SizeTo(width, height);
  29. mType = aImage->GetType();
  30. // Populate memory counters for source and decoded data.
  31. mValues.SetSource(aImage->SizeOfSourceWithComputedFallback(aMallocSizeOf));
  32. aImage->CollectSizeOfSurfaces(mSurfaces, aMallocSizeOf);
  33. // Compute totals.
  34. for (const SurfaceMemoryCounter& surfaceCounter : mSurfaces) {
  35. mValues += surfaceCounter.Values();
  36. }
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // Image Base Types
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // Constructor
  42. ImageResource::ImageResource(ImageURL* aURI) :
  43. mURI(aURI),
  44. mInnerWindowId(0),
  45. mAnimationConsumers(0),
  46. mAnimationMode(kNormalAnimMode),
  47. mInitialized(false),
  48. mAnimating(false),
  49. mError(false)
  50. { }
  51. ImageResource::~ImageResource()
  52. {
  53. // Ask our ProgressTracker to drop its weak reference to us.
  54. mProgressTracker->ResetImage();
  55. }
  56. void
  57. ImageResource::IncrementAnimationConsumers()
  58. {
  59. MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
  60. "with DecrementAnimationConsumers");
  61. mAnimationConsumers++;
  62. }
  63. void
  64. ImageResource::DecrementAnimationConsumers()
  65. {
  66. MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
  67. "with IncrementAnimationConsumers");
  68. MOZ_ASSERT(mAnimationConsumers >= 1,
  69. "Invalid no. of animation consumers!");
  70. mAnimationConsumers--;
  71. }
  72. nsresult
  73. ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode)
  74. {
  75. if (mError) {
  76. return NS_ERROR_FAILURE;
  77. }
  78. NS_ENSURE_ARG_POINTER(aAnimationMode);
  79. *aAnimationMode = mAnimationMode;
  80. return NS_OK;
  81. }
  82. nsresult
  83. ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode)
  84. {
  85. if (mError) {
  86. return NS_ERROR_FAILURE;
  87. }
  88. NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
  89. aAnimationMode == kDontAnimMode ||
  90. aAnimationMode == kLoopOnceAnimMode,
  91. "Wrong Animation Mode is being set!");
  92. mAnimationMode = aAnimationMode;
  93. return NS_OK;
  94. }
  95. bool
  96. ImageResource::HadRecentRefresh(const TimeStamp& aTime)
  97. {
  98. // Our threshold for "recent" is 1/2 of the default refresh-driver interval.
  99. // This ensures that we allow for frame rates at least as fast as the
  100. // refresh driver's default rate.
  101. static TimeDuration recentThreshold =
  102. TimeDuration::FromMilliseconds(nsRefreshDriver::DefaultInterval() / 2.0);
  103. if (!mLastRefreshTime.IsNull() &&
  104. aTime - mLastRefreshTime < recentThreshold) {
  105. return true;
  106. }
  107. // else, we can proceed with a refresh.
  108. // But first, update our last refresh time:
  109. mLastRefreshTime = aTime;
  110. return false;
  111. }
  112. void
  113. ImageResource::EvaluateAnimation()
  114. {
  115. if (!mAnimating && ShouldAnimate()) {
  116. nsresult rv = StartAnimation();
  117. mAnimating = NS_SUCCEEDED(rv);
  118. } else if (mAnimating && !ShouldAnimate()) {
  119. StopAnimation();
  120. }
  121. }
  122. } // namespace image
  123. } // namespace mozilla