Bug 753021 - Favicon shortcuts should be centered with a white background. r=bbondy
authorParth Mudgal <artpar@gmail.com>
Sat, 21 Jul 2012 16:07:30 -0400
changeset 100302 37c915daed4a175daa70ec0afdcd7b1937296d34
parent 100301 aa003aa3a18f365fd805dca30f5aef3ebc584344
child 100303 3ff93386d03f590581cbe06ac3defe58e7f78404
push id12429
push userbbondy@mozilla.com
push dateTue, 24 Jul 2012 16:41:42 +0000
treeherdermozilla-inbound@37c915daed4a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbbondy
bugs753021
milestone17.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 753021 - Favicon shortcuts should be centered with a white background. r=bbondy
widget/windows/WinUtils.cpp
--- a/widget/windows/WinUtils.cpp
+++ b/widget/windows/WinUtils.cpp
@@ -19,16 +19,17 @@
 #include "nsNetUtil.h"
 #include "mozIAsyncFavicons.h"
  
 #include "nsIIconURI.h"
 #include "nsIDownloader.h"
 #include "nsINetUtil.h"
 #include "nsIChannel.h"
 #include "nsIObserver.h"
+#include "imgIEncoder.h"
 
 namespace mozilla {
 namespace widget {
 
   NS_IMPL_ISUPPORTS1(myDownloadObserver, nsIDownloadObserver)
   NS_IMPL_ISUPPORTS1(AsyncFaviconDataReady, nsIFaviconDataCallback)
   NS_IMPL_THREADSAFE_ISUPPORTS1(AsyncWriteIconToDisk, nsIRunnable)
   NS_IMPL_THREADSAFE_ISUPPORTS1(AsyncDeleteIconFromDisk, nsIRunnable)
@@ -453,23 +454,24 @@ AsyncFaviconDataReady::OnComplete(nsIURI
                                   PRUint32 aDataLen,
                                   const PRUint8 *aData, 
                                   const nsACString &aMimeType)
 {
   if (!aDataLen || !aData) {
     if (mURLShortcut) {
       OnFaviconDataNotAvailable();
     }
+    
     return NS_OK;
   }
 
   nsCOMPtr<nsIFile> icoFile;
   nsresult rv = FaviconHelper::GetOutputIconPath(mNewURI, icoFile, mURLShortcut);
-
   NS_ENSURE_SUCCESS(rv, rv);
+  
   nsAutoString path;
   rv = icoFile->GetPath(path);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Allocate a new buffer that we own and can use out of line in 
   // another thread.  Copy the favicon raw data into it.
   const fallible_t fallible = fallible_t();
   PRUint8 *data = new (fallible) PRUint8[aDataLen];
@@ -539,22 +541,48 @@ NS_IMETHODIMP AsyncWriteIconToDisk::Run(
     // Scale the image to the needed size and in ICO format
     mMimeTypeOfInputData.AssignLiteral("image/vnd.microsoft.icon");
     rv = imgtool->EncodeScaledImage(container, mMimeTypeOfInputData,
                                     systemIconWidth,
                                     systemIconHeight,
                                     EmptyString(),
                                     getter_AddRefs(iconStream));
     } else {
-    mMimeTypeOfInputData.AssignLiteral("image/vnd.microsoft.icon");
-    rv = imgtool->EncodeImage(container, 
-                              mMimeTypeOfInputData,
-                              NS_LITERAL_STRING("format=bmp;bpp=32"),
-                              getter_AddRefs(iconStream));
+      nsRefPtr<gfxASurface> s;
+      rv = container->GetFrame(imgIContainer::FRAME_FIRST, 0, getter_AddRefs(s));
+      NS_ENSURE_SUCCESS(rv, rv);
+
+      gfxImageSurface* surface =
+        new gfxImageSurface(gfxIntSize(48, 48),
+                            gfxImageSurface::ImageFormatARGB32);
+      gfxContext context(surface);
+      context.SetOperator(gfxContext::OPERATOR_SOURCE);
+      context.SetColor(gfxRGBA(1, 1, 1, 1));
+      context.Rectangle(gfxRect(0, 0, 48, 48));
+      context.Fill();
+
+      context.Translate(gfxPoint(16, 16));
+      context.SetOperator(gfxContext::OPERATOR_OVER);
+      context.DrawSurface(s,  gfxSize(16, 16));
+      gfxIntSize size = surface->GetSize();
+
+      nsRefPtr<imgIEncoder> encoder = 
+        do_CreateInstance("@mozilla.org/image/encoder;2?"
+                          "type=image/vnd.microsoft.icon");
+      NS_ENSURE_TRUE(encoder, NS_ERROR_FAILURE);
+      rv = encoder->InitFromData(surface->Data(), surface->Stride() * size.height,
+                            size.width, size.height, surface->Stride(),
+                            imgIEncoder::INPUT_FORMAT_HOSTARGB, EmptyString());
+      NS_ENSURE_SUCCESS(rv, rv);
+      CallQueryInterface(encoder.get(), getter_AddRefs(iconStream));
+      if (!iconStream) {
+        return NS_ERROR_FAILURE;
+      }
   }
+
   NS_ENSURE_SUCCESS(rv, rv);
   nsCOMPtr<nsIFile> icoFile
     = do_CreateInstance("@mozilla.org/file/local;1");
   NS_ENSURE_TRUE(icoFile, NS_ERROR_FAILURE);
   rv = icoFile->InitWithPath(mIconPath);
 
   // Setup the output stream for the ICO file on disk
   nsCOMPtr<nsIOutputStream> outputStream;