Bug 786449 - Add a place to explicitly initialize RasterImage on the main thread at startup. r=jlebar
authorJoe Drew <joe@drew.ca>
Wed, 26 Sep 2012 11:33:06 -0400
changeset 108625 e7381ac9e30a172cc129070e57bcca3ff59a8cd5
parent 108624 51644b3ee33365d9464c1ca058d3773f2e7f6889
child 108626 b2dba2108722f301dd8865fa2f0a16551f873db7
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersjlebar
bugs786449
milestone18.0a1
Bug 786449 - Add a place to explicitly initialize RasterImage on the main thread at startup. r=jlebar
image/build/nsImageModule.cpp
image/src/RasterImage.cpp
image/src/RasterImage.h
--- a/image/build/nsImageModule.cpp
+++ b/image/build/nsImageModule.cpp
@@ -91,16 +91,17 @@ static const mozilla::Module::CategoryEn
   { "content-sniffing-services", "@mozilla.org/image/loader;1", "@mozilla.org/image/loader;1" },
   { NULL }
 };
 
 static nsresult
 imglib_Initialize()
 {
   mozilla::image::DiscardTracker::Initialize();
+  mozilla::image::RasterImage::Initialize();
   imgLoader::GlobalInit();
   return NS_OK;
 }
 
 static void
 imglib_Shutdown()
 {
   imgLoader::Shutdown();
--- a/image/src/RasterImage.cpp
+++ b/image/src/RasterImage.cpp
@@ -48,28 +48,26 @@ using namespace mozilla::layers;
 static PRLogModuleInfo *gCompressedImageAccountingLog = PR_NewLogModule ("CompressedImageAccounting");
 #else
 #define gCompressedImageAccountingLog
 #endif
 
 // Tweakable progressive decoding parameters.  These are initialized to 0 here
 // because otherwise, we have to initialize them in a static initializer, which
 // makes us slower to start up.
-static bool gInitializedPrefCaches = false;
 static uint32_t gDecodeBytesAtATime = 0;
 static uint32_t gMaxMSBeforeYield = 0;
 
 static void
 InitPrefCaches()
 {
   Preferences::AddUintVarCache(&gDecodeBytesAtATime,
                                "image.mem.decode_bytes_at_a_time", 200000);
   Preferences::AddUintVarCache(&gMaxMSBeforeYield,
                                "image.mem.max_ms_before_yield", 400);
-  gInitializedPrefCaches = true;
 }
 
 /* We define our own error checking macros here for 2 reasons:
  *
  * 1) Most of the failures we encounter here will (hopefully) be
  * the result of decoding failures (ie, bad data) and not code
  * failures. As such, we don't want to clutter up debug consoles
  * with spurious messages about NS_ENSURE_SUCCESS failures.
@@ -176,20 +174,16 @@ RasterImage::RasterImage(imgStatusTracke
 {
   // Set up the discard tracker node.
   mDiscardTrackerNode.img = this;
   Telemetry::GetHistogramById(Telemetry::IMAGE_DECODE_COUNT)->Add(0);
 
   // Statistics
   num_containers++;
 
-  // Register our pref observers if we haven't yet.
-  if (NS_UNLIKELY(!gInitializedPrefCaches)) {
-    InitPrefCaches();
-  }
 }
 
 //******************************************************************************
 RasterImage::~RasterImage()
 {
   delete mAnim;
 
   for (unsigned int i = 0; i < mFrames.Length(); ++i)
@@ -219,16 +213,26 @@ RasterImage::~RasterImage()
       NS_WARNING("Failed to shut down decoder in destructor!");
   }
 
   // Total statistics
   num_containers--;
   total_source_bytes -= mSourceData.Length();
 }
 
+void
+RasterImage::Initialize()
+{
+  InitPrefCaches();
+
+  // Create our singletons now, so we don't have to worry about what thread
+  // they're created on.
+  DecodeWorker::Singleton();
+}
+
 nsresult
 RasterImage::Init(imgIDecoderObserver *aObserver,
                   const char* aMimeType,
                   const char* aURIString,
                   uint32_t aFlags)
 {
   // We don't support re-initialization
   if (mInitialized)
--- a/image/src/RasterImage.h
+++ b/image/src/RasterImage.h
@@ -303,16 +303,19 @@ public:
     kDisposeNotSpecified,   // Leave frame, let new frame draw on top
     kDisposeKeep,           // Leave frame, let new frame draw on top
     kDisposeClear,          // Clear the frame's area, revealing bg
     kDisposeRestorePrevious // Restore the previous (composited) frame
   };
 
   const char* GetURIString() { return mURIString.get();}
 
+  // Called from module startup. Sets up RasterImage to be used.
+  static void Initialize();
+
 private:
   struct Anim
   {
     //! Area of the first frame that needs to be redrawn on subsequent loops.
     nsIntRect                  firstFrameRefreshArea;
     uint32_t                   currentAnimationFrameIndex; // 0 to numFrames-1
 
     // the time that the animation advanced to the current frame