Bug 1291045 (Part 6) - Add DecodedSurfaceProvider to handle both decoding and surface ownership for single-frame images. r=dholbert,edwin
authorSeth Fowler <mark.seth.fowler@gmail.com>
Mon, 01 Aug 2016 15:18:54 -0700
changeset 308466 a6e140998c35958e3d05031afacd9e123356b2d3
parent 308465 7466366ce986deea1a21f47b998216a1527c528d
child 308467 b5418ecab2a98fa13e349d0be2405126634d2824
push id30537
push userphilringnalda@gmail.com
push dateSun, 07 Aug 2016 06:34:58 +0000
treeherdermozilla-central@70948f120f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdholbert, edwin
bugs1291045
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1291045 (Part 6) - Add DecodedSurfaceProvider to handle both decoding and surface ownership for single-frame images. r=dholbert,edwin
image/DecodedSurfaceProvider.cpp
image/DecodedSurfaceProvider.h
image/moz.build
new file mode 100644
--- /dev/null
+++ b/image/DecodedSurfaceProvider.cpp
@@ -0,0 +1,216 @@
+/* -*- 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 "DecodedSurfaceProvider.h"
+
+#include "gfxPrefs.h"
+#include "nsProxyRelease.h"
+
+#include "Decoder.h"
+
+using namespace mozilla::gfx;
+
+namespace mozilla {
+namespace image {
+
+DecodedSurfaceProvider::DecodedSurfaceProvider(NotNull<RasterImage*> aImage,
+                                               NotNull<Decoder*> aDecoder,
+                                               const SurfaceKey& aSurfaceKey)
+  : ISurfaceProvider(AvailabilityState::StartAsPlaceholder())
+  , mImage(aImage.get())
+  , mDecoder(aDecoder.get())
+  , mSurfaceKey(aSurfaceKey)
+{
+  MOZ_ASSERT(!mDecoder->IsMetadataDecode(),
+             "Use MetadataDecodingTask for metadata decodes");
+  MOZ_ASSERT(mDecoder->IsFirstFrameDecode(),
+             "Use AnimationDecodingTask for animation decodes");
+}
+
+DecodedSurfaceProvider::~DecodedSurfaceProvider()
+{
+  DropImageReference();
+}
+
+void
+DecodedSurfaceProvider::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
+DecodedSurfaceProvider::DrawableRef()
+{
+  // We depend on SurfaceCache::SurfaceAvailable() to provide synchronization
+  // for methods that touch |mSurface|; after SurfaceAvailable() is called,
+  // |mSurface| should be non-null and shouldn't be mutated further until we get
+  // destroyed. That means that the assertions below are very important; we'll
+  // end up with data races if these assumptions are violated.
+  if (Availability().IsPlaceholder()) {
+    MOZ_ASSERT_UNREACHABLE("Calling DrawableRef() on a placeholder");
+    return DrawableFrameRef();
+  }
+
+  if (!mSurface) {
+    MOZ_ASSERT_UNREACHABLE("Calling DrawableRef() when we have no surface");
+    return DrawableFrameRef();
+  }
+
+  return mSurface->DrawableRef();
+}
+
+bool
+DecodedSurfaceProvider::IsFinished() const
+{
+  // See DrawableRef() for commentary on these assertions.
+  if (Availability().IsPlaceholder()) {
+    MOZ_ASSERT_UNREACHABLE("Calling IsFinished() on a placeholder");
+    return false;
+  }
+
+  if (!mSurface) {
+    MOZ_ASSERT_UNREACHABLE("Calling IsFinished() when we have no surface");
+    return false;
+  }
+
+  return mSurface->IsFinished();
+}
+
+void
+DecodedSurfaceProvider::SetLocked(bool aLocked)
+{
+  // See DrawableRef() for commentary on these assertions.
+  if (Availability().IsPlaceholder()) {
+    MOZ_ASSERT_UNREACHABLE("Calling SetLocked() on a placeholder");
+    return;
+  }
+
+  if (!mSurface) {
+    MOZ_ASSERT_UNREACHABLE("Calling SetLocked() when we have no surface");
+    return;
+  }
+
+  if (aLocked == IsLocked()) {
+    return;  // Nothing to do.
+  }
+
+  // If we're locked, hold a DrawableFrameRef to |mSurface|, which will keep any
+  // volatile buffer it owns in memory.
+  mLockRef = aLocked ? mSurface->DrawableRef()
+                     : DrawableFrameRef();
+}
+
+size_t
+DecodedSurfaceProvider::LogicalSizeInBytes() const
+{
+  // Single frame images are always 32bpp.
+  IntSize size = mSurfaceKey.Size();
+  return size.width * size.height * sizeof(uint32_t);
+}
+
+void
+DecodedSurfaceProvider::Run()
+{
+  if (!mDecoder || !mImage) {
+    MOZ_ASSERT_UNREACHABLE("Running after decoding finished?");
+    return;
+  }
+
+  // Run the decoder.
+  LexerResult result = mDecoder->Decode(WrapNotNull(this));
+
+  // If there's a new surface available, announce it to the surface cache.
+  CheckForNewSurface();
+
+  if (result.is<TerminalState>()) {
+    FinishDecoding();
+    return;  // We're done.
+  }
+
+  // Notify for the progress we've made so far.
+  if (mDecoder->HasProgress()) {
+    NotifyProgress(WrapNotNull(mImage), WrapNotNull(mDecoder));
+  }
+
+  MOZ_ASSERT(result.is<Yield>());
+
+  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;
+  }
+
+  // Single-frame images shouldn't yield for any reason except NEED_MORE_DATA.
+  MOZ_ASSERT_UNREACHABLE("Unexpected yield for single-frame image");
+  mDecoder->TerminateFailure();
+  FinishDecoding();
+}
+
+void
+DecodedSurfaceProvider::CheckForNewSurface()
+{
+  if (mSurface) {
+    // Single-frame images should produce no more than one surface, so if we
+    // have one, it should be the same one the decoder is working on.
+    MOZ_ASSERT(mSurface.get() == mDecoder->GetCurrentFrameRef().get(),
+               "DecodedSurfaceProvider and Decoder have different surfaces?");
+    return;
+  }
+
+  // We don't have a surface yet; try to get one from the decoder.
+  mSurface = mDecoder->GetCurrentFrameRef().get();
+  if (!mSurface) {
+    return;  // No surface yet.
+  }
+
+  // We just got a surface for the first time; let the surface cache know.
+  MOZ_ASSERT(mImage);
+  SurfaceCache::SurfaceAvailable(WrapNotNull(this),
+                                 ImageKey(mImage.get()),
+                                 mSurfaceKey);
+}
+
+void
+DecodedSurfaceProvider::FinishDecoding()
+{
+  MOZ_ASSERT(mImage);
+  MOZ_ASSERT(mDecoder);
+
+  // Send notifications.
+  NotifyDecodeComplete(WrapNotNull(mImage), WrapNotNull(mDecoder));
+
+  // Destroy our decoder; we don't need it anymore. (And if we don't destroy it,
+  // our surface can never be optimized, because the decoder has a
+  // RawAccessFrameRef to it.)
+  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
+DecodedSurfaceProvider::ShouldPreferSyncRun() const
+{
+  return mDecoder->ShouldSyncDecode(gfxPrefs::ImageMemDecodeBytesAtATime());
+}
+
+} // namespace image
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/image/DecodedSurfaceProvider.h
@@ -0,0 +1,89 @@
+/* -*- 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/. */
+
+/**
+ * An ISurfaceProvider implemented for single-frame decoded surfaces.
+ */
+
+#ifndef mozilla_image_DecodedSurfaceProvider_h
+#define mozilla_image_DecodedSurfaceProvider_h
+
+#include "IDecodingTask.h"
+#include "ISurfaceProvider.h"
+#include "SurfaceCache.h"
+
+namespace mozilla {
+namespace image {
+
+/**
+ * An ISurfaceProvider that manages the decoding of a single-frame image and
+ * stores the resulting surface.
+ */
+class DecodedSurfaceProvider final
+  : public ISurfaceProvider
+  , public IDecodingTask
+{
+public:
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DecodedSurfaceProvider, override)
+
+  DecodedSurfaceProvider(NotNull<RasterImage*> aImage,
+                         NotNull<Decoder*> aDecoder,
+                         const SurfaceKey& aSurfaceKey);
+
+
+  //////////////////////////////////////////////////////////////////////////////
+  // ISurfaceProvider implementation.
+  //////////////////////////////////////////////////////////////////////////////
+
+public:
+  DrawableFrameRef DrawableRef() override;
+  bool IsFinished() const override;
+  size_t LogicalSizeInBytes() const override;
+
+protected:
+  bool IsLocked() const override { return bool(mLockRef); }
+  void SetLocked(bool aLocked) override;
+
+
+  //////////////////////////////////////////////////////////////////////////////
+  // IDecodingTask implementation.
+  //////////////////////////////////////////////////////////////////////////////
+
+public:
+  void Run() override;
+  bool ShouldPreferSyncRun() const override;
+
+  // Full decodes are low priority compared to metadata decodes because they
+  // don't block layout or page load.
+  TaskPriority Priority() const override { return TaskPriority::eLow; }
+
+
+private:
+  virtual ~DecodedSurfaceProvider();
+
+  void DropImageReference();
+  void CheckForNewSurface();
+  void FinishDecoding();
+
+  /// The image associated with our decoder. Dropped after decoding.
+  RefPtr<RasterImage> mImage;
+
+  /// The decoder that will generate our surface. Dropped after decoding.
+  RefPtr<Decoder> mDecoder;
+
+  /// Our surface. Initially null until it's generated by the decoder.
+  RefPtr<imgFrame> mSurface;
+
+  /// A drawable reference to our service; used for locking.
+  DrawableFrameRef mLockRef;
+
+  /// The key under which we're stored as a cache entry in the surface cache.
+  SurfaceKey mSurfaceKey;
+};
+
+} // namespace image
+} // namespace mozilla
+
+#endif // mozilla_image_DecodedSurfaceProvider_h
--- a/image/moz.build
+++ b/image/moz.build
@@ -47,16 +47,17 @@ EXPORTS += [
     'IProgressObserver.h',
     'Orientation.h',
     'SurfaceCache.h',
     'SurfaceFlags.h',
 ]
 
 UNIFIED_SOURCES += [
     'ClippedImage.cpp',
+    'DecodedSurfaceProvider.cpp',
     'DecodePool.cpp',
     'Decoder.cpp',
     'DecoderFactory.cpp',
     'DynamicImage.cpp',
     'FrameAnimator.cpp',
     'FrozenImage.cpp',
     'IDecodingTask.cpp',
     'Image.cpp',