1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "AnimationSurfaceProvider.h"
#include "gfxPrefs.h"
#include "nsProxyRelease.h"
#include "Decoder.h"
using namespace mozilla::gfx;
namespace mozilla {
namespace image {
AnimationSurfaceProvider::AnimationSurfaceProvider(NotNull<RasterImage*> aImage,
const SurfaceKey& aSurfaceKey,
NotNull<Decoder*> aDecoder)
: ISurfaceProvider(ImageKey(aImage.get()), aSurfaceKey,
AvailabilityState::StartAsPlaceholder())
, mImage(aImage.get())
, mDecodingMutex("AnimationSurfaceProvider::mDecoder")
, mDecoder(aDecoder.get())
, mFramesMutex("AnimationSurfaceProvider::mFrames")
{
MOZ_ASSERT(!mDecoder->IsMetadataDecode(),
"Use MetadataDecodingTask for metadata decodes");
MOZ_ASSERT(!mDecoder->IsFirstFrameDecode(),
"Use DecodedSurfaceProvider for single-frame image decodes");
}
AnimationSurfaceProvider::~AnimationSurfaceProvider()
{
DropImageReference();
}
void
AnimationSurfaceProvider::DropImageReference()
{
if (!mImage) {
return; // Nothing to do.
}
// RasterImage objects need to be destroyed on the main thread. We also need
// to destroy them asynchronously, because if our surface cache entry is
// destroyed and we were the only thing keeping |mImage| alive, RasterImage's
// destructor may call into the surface cache while whatever code caused us to
// get evicted is holding the surface cache lock, causing deadlock.
RefPtr<RasterImage> image = mImage;
mImage = nullptr;
NS_ReleaseOnMainThread(image.forget(), /* aAlwaysProxy = */ true);
}
DrawableFrameRef
AnimationSurfaceProvider::DrawableRef(size_t aFrame)
{
MutexAutoLock lock(mFramesMutex);
if (Availability().IsPlaceholder()) {
MOZ_ASSERT_UNREACHABLE("Calling DrawableRef() on a placeholder");
return DrawableFrameRef();
}
if (mFrames.IsEmpty()) {
MOZ_ASSERT_UNREACHABLE("Calling DrawableRef() when we have no frames");
return DrawableFrameRef();
}
// If we don't have that frame, return an empty frame ref.
if (aFrame >= mFrames.Length()) {
return DrawableFrameRef();
}
// We've got the requested frame. Return it.
MOZ_ASSERT(mFrames[aFrame]);
return mFrames[aFrame]->DrawableRef();
}
bool
AnimationSurfaceProvider::IsFinished() const
{
MutexAutoLock lock(mFramesMutex);
if (Availability().IsPlaceholder()) {
MOZ_ASSERT_UNREACHABLE("Calling IsFinished() on a placeholder");
return false;
}
if (mFrames.IsEmpty()) {
MOZ_ASSERT_UNREACHABLE("Calling IsFinished() when we have no frames");
return false;
}
// As long as we have at least one finished frame, we're finished.
return mFrames[0]->IsFinished();
}
size_t
AnimationSurfaceProvider::LogicalSizeInBytes() const
{
// When decoding animated images, we need at most three live surfaces: the
// composited surface, the previous composited surface for
// DisposalMethod::RESTORE_PREVIOUS, and the surface we're currently decoding
// into. The composited surfaces are always BGRA. Although the surface we're
// decoding into may be paletted, and may be smaller than the real size of the
// image, we assume the worst case here.
// XXX(seth): Note that this is actually not accurate yet; we're storing the
// full sequence of frames, not just the three live surfaces mentioned above.
// Unfortunately there's no way to know in advance how many frames an
// animation has, so we really can't do better here. This will become correct
// once bug 1289954 is complete.
IntSize size = GetSurfaceKey().Size();
return 3 * size.width * size.height * sizeof(uint32_t);
}
void
AnimationSurfaceProvider::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
size_t& aHeapSizeOut,
size_t& aNonHeapSizeOut)
{
// Note that the surface cache lock is already held here, and then we acquire
// mFramesMutex. For this method, this ordering is unavoidable, which means
// that we must be careful to always use the same ordering elsewhere.
MutexAutoLock lock(mFramesMutex);
for (const RawAccessFrameRef& frame : mFrames) {
frame->AddSizeOfExcludingThis(aMallocSizeOf, aHeapSizeOut, aNonHeapSizeOut);
}
}
void
AnimationSurfaceProvider::Run()
{
MutexAutoLock lock(mDecodingMutex);
if (!mDecoder || !mImage) {
MOZ_ASSERT_UNREACHABLE("Running after decoding finished?");
return;
}
while (true) {
// Run the decoder.
LexerResult result = mDecoder->Decode(WrapNotNull(this));
if (result.is<TerminalState>()) {
// We may have a new frame now, but it's not guaranteed - a decoding
// failure or truncated data may mean that no new frame got produced.
// Since we're not sure, rather than call CheckForNewFrameAtYield() here
// we call CheckForNewFrameAtTerminalState(), which handles both of these
// possibilities.
CheckForNewFrameAtTerminalState();
// We're done!
FinishDecoding();
return;
}
// Notify for the progress we've made so far.
if (mDecoder->HasProgress()) {
NotifyProgress(WrapNotNull(mImage), WrapNotNull(mDecoder));
}
if (result == LexerResult(Yield::NEED_MORE_DATA)) {
// We can't make any more progress right now. The decoder itself will ensure
// that we get reenqueued when more data is available; just return for now.
return;
}
// There's new output available - a new frame! Grab it.
MOZ_ASSERT(result == LexerResult(Yield::OUTPUT_AVAILABLE));
CheckForNewFrameAtYield();
}
}
void
AnimationSurfaceProvider::CheckForNewFrameAtYield()
{
mDecodingMutex.AssertCurrentThreadOwns();
MOZ_ASSERT(mDecoder);
bool justGotFirstFrame = false;
{
MutexAutoLock lock(mFramesMutex);
// Try to get the new frame from the decoder.
RawAccessFrameRef frame = mDecoder->GetCurrentFrameRef();
if (!frame) {
MOZ_ASSERT_UNREACHABLE("Decoder yielded but didn't produce a frame?");
return;
}
// We should've gotten a different frame than last time.
MOZ_ASSERT_IF(!mFrames.IsEmpty(),
mFrames.LastElement().get() != frame.get());
// Append the new frame to the list.
mFrames.AppendElement(Move(frame));
if (mFrames.Length() == 1) {
justGotFirstFrame = true;
}
}
if (justGotFirstFrame) {
AnnounceSurfaceAvailable();
}
}
void
AnimationSurfaceProvider::CheckForNewFrameAtTerminalState()
{
mDecodingMutex.AssertCurrentThreadOwns();
MOZ_ASSERT(mDecoder);
bool justGotFirstFrame = false;
{
MutexAutoLock lock(mFramesMutex);
RawAccessFrameRef frame = mDecoder->GetCurrentFrameRef();
if (!frame) {
return;
}
if (!mFrames.IsEmpty() && mFrames.LastElement().get() == frame.get()) {
return; // We already have this one.
}
// Append the new frame to the list.
mFrames.AppendElement(Move(frame));
if (mFrames.Length() == 1) {
justGotFirstFrame = true;
}
}
if (justGotFirstFrame) {
AnnounceSurfaceAvailable();
}
}
void
AnimationSurfaceProvider::AnnounceSurfaceAvailable()
{
mFramesMutex.AssertNotCurrentThreadOwns();
MOZ_ASSERT(mImage);
// We just got the first frame; let the surface cache know. We deliberately do
// this outside of mFramesMutex to avoid a potential deadlock with
// AddSizeOfExcludingThis(), since otherwise we'd be acquiring mFramesMutex
// and then the surface cache lock, while the memory reporting code would
// acquire the surface cache lock and then mFramesMutex.
SurfaceCache::SurfaceAvailable(WrapNotNull(this));
}
void
AnimationSurfaceProvider::FinishDecoding()
{
mDecodingMutex.AssertCurrentThreadOwns();
MOZ_ASSERT(mImage);
MOZ_ASSERT(mDecoder);
// Send notifications.
NotifyDecodeComplete(WrapNotNull(mImage), WrapNotNull(mDecoder));
// Destroy our decoder; we don't need it anymore.
mDecoder = nullptr;
// We don't need a reference to our image anymore, either, and we don't want
// one. We may be stored in the surface cache for a long time after decoding
// finishes. If we don't drop our reference to the image, we'll end up
// keeping it alive as long as we remain in the surface cache, which could
// greatly extend the image's lifetime - in fact, if the image isn't
// discardable, it'd result in a leak!
DropImageReference();
}
bool
AnimationSurfaceProvider::ShouldPreferSyncRun() const
{
MutexAutoLock lock(mDecodingMutex);
MOZ_ASSERT(mDecoder);
return mDecoder->ShouldSyncDecode(gfxPrefs::ImageMemDecodeBytesAtATime());
}
} // namespace image
} // namespace mozilla
|