2016-10-13 10:04:15 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* table of images used in a document, for batch locking/unlocking and
|
|
|
|
* animating */
|
|
|
|
|
|
|
|
#include "ImageTracker.h"
|
|
|
|
|
2017-10-26 12:40:12 +03:00
|
|
|
#include "imgIRequest.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
2016-10-13 10:04:15 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
ImageTracker::ImageTracker() : mLocking(false), mAnimating(true) {}
|
|
|
|
|
|
|
|
ImageTracker::~ImageTracker() { SetLockingState(false); }
|
|
|
|
|
2016-10-13 10:04:19 +03:00
|
|
|
nsresult ImageTracker::Add(imgIRequest* aImage) {
|
2016-10-13 10:04:15 +03:00
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2017-06-14 02:03:38 +03:00
|
|
|
auto entry = mImages.LookupForAdd(aImage);
|
|
|
|
if (entry) {
|
|
|
|
// The image is already in the hashtable. Increment its count.
|
|
|
|
uint32_t oldCount = entry.Data();
|
|
|
|
MOZ_ASSERT(oldCount > 0, "Entry in the image tracker with count 0!");
|
|
|
|
entry.Data() = oldCount + 1;
|
|
|
|
} else {
|
|
|
|
// A new entry was inserted - set the count to 1.
|
|
|
|
entry.OrInsert([]() { return 1; });
|
|
|
|
|
|
|
|
// If we're locking images, lock this image too.
|
|
|
|
if (mLocking) {
|
|
|
|
rv = aImage->LockImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're animating images, request that this image be animated too.
|
|
|
|
if (mAnimating) {
|
|
|
|
nsresult rv2 = aImage->IncrementAnimationConsumers();
|
|
|
|
rv = NS_SUCCEEDED(rv) ? rv2 : rv;
|
|
|
|
}
|
2016-10-13 10:04:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-13 10:04:19 +03:00
|
|
|
nsresult ImageTracker::Remove(imgIRequest* aImage, uint32_t aFlags) {
|
2016-10-13 10:04:15 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aImage);
|
|
|
|
|
|
|
|
// Get the old count. It should exist and be > 0.
|
2017-06-18 18:07:54 +03:00
|
|
|
if (auto entry = mImages.Lookup(aImage)) {
|
|
|
|
MOZ_ASSERT(entry.Data() > 0, "Entry in the image tracker with count 0!");
|
|
|
|
// If the count becomes zero, remove it from the tracker.
|
|
|
|
if (--entry.Data() == 0) {
|
|
|
|
entry.Remove();
|
|
|
|
} else {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Removing image that wasn't in the tracker!");
|
2016-10-13 10:04:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Now that we're no longer tracking this image, unlock it if we'd
|
|
|
|
// previously locked it.
|
2016-10-13 10:04:19 +03:00
|
|
|
if (mLocking) {
|
2016-10-13 10:04:15 +03:00
|
|
|
rv = aImage->UnlockImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're animating images, remove our request to animate this one.
|
2016-10-13 10:04:19 +03:00
|
|
|
if (mAnimating) {
|
2016-10-13 10:04:15 +03:00
|
|
|
nsresult rv2 = aImage->DecrementAnimationConsumers();
|
|
|
|
rv = NS_SUCCEEDED(rv) ? rv2 : rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFlags & REQUEST_DISCARD) {
|
|
|
|
// Request that the image be discarded if nobody else holds a lock on it.
|
2016-10-13 10:04:19 +03:00
|
|
|
// Do this even if !mLocking, because even if we didn't just unlock
|
2016-10-13 10:04:15 +03:00
|
|
|
// this image, it might still be a candidate for discarding.
|
|
|
|
aImage->RequestDiscard();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-13 10:04:19 +03:00
|
|
|
nsresult ImageTracker::SetLockingState(bool aLocked) {
|
2016-10-13 10:04:15 +03:00
|
|
|
if (XRE_IsContentProcess() &&
|
|
|
|
!Preferences::GetBool("image.mem.allow_locking_in_content_processes",
|
|
|
|
true)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no change, there's nothing to do.
|
2016-10-13 10:04:19 +03:00
|
|
|
if (mLocking == aLocked) return NS_OK;
|
2016-10-13 10:04:15 +03:00
|
|
|
|
|
|
|
// Otherwise, iterate over our images and perform the appropriate action.
|
2016-10-13 10:04:19 +03:00
|
|
|
for (auto iter = mImages.Iter(); !iter.Done(); iter.Next()) {
|
2016-10-13 10:04:15 +03:00
|
|
|
imgIRequest* image = iter.Key();
|
|
|
|
if (aLocked) {
|
|
|
|
image->LockImage();
|
|
|
|
} else {
|
|
|
|
image->UnlockImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update state.
|
2016-10-13 10:04:19 +03:00
|
|
|
mLocking = aLocked;
|
2016-10-13 10:04:15 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-13 10:04:19 +03:00
|
|
|
void ImageTracker::SetAnimatingState(bool aAnimating) {
|
2016-10-13 10:04:15 +03:00
|
|
|
// If there's no change, there's nothing to do.
|
2016-10-13 10:04:19 +03:00
|
|
|
if (mAnimating == aAnimating) return;
|
2016-10-13 10:04:15 +03:00
|
|
|
|
|
|
|
// Otherwise, iterate over our images and perform the appropriate action.
|
2016-10-13 10:04:19 +03:00
|
|
|
for (auto iter = mImages.Iter(); !iter.Done(); iter.Next()) {
|
2016-10-13 10:04:15 +03:00
|
|
|
imgIRequest* image = iter.Key();
|
|
|
|
if (aAnimating) {
|
|
|
|
image->IncrementAnimationConsumers();
|
|
|
|
} else {
|
|
|
|
image->DecrementAnimationConsumers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update state.
|
2016-10-13 10:04:19 +03:00
|
|
|
mAnimating = aAnimating;
|
2016-10-13 10:04:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageTracker::RequestDiscardAll() {
|
2016-10-13 10:04:19 +03:00
|
|
|
for (auto iter = mImages.Iter(); !iter.Done(); iter.Next()) {
|
2016-10-13 10:04:15 +03:00
|
|
|
iter.Key()->RequestDiscard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|