Bug 899334 - Don't decrease the resolution of icons in native Mac menus. r=smichaud
authorMarkus Stange <mstange@themasta.com>
Sun, 20 Jul 2014 16:31:37 +0200
changeset 217117 333460ebc613fb2cac809e8ea8a85a9eff0a515b
parent 217116 4515815020907e84660259ea993287bd84058855
child 217118 6fb5b8d67c005acef313d3f91f2cf1d6414976e5
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmichaud
bugs899334
milestone33.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 899334 - Don't decrease the resolution of icons in native Mac menus. r=smichaud
widget/cocoa/nsMenuItemIconX.mm
--- a/widget/cocoa/nsMenuItemIconX.mm
+++ b/widget/cocoa/nsMenuItemIconX.mm
@@ -384,91 +384,61 @@ nsMenuItemIconX::OnStopFrame(imgIRequest
   }
 
   if (mImageRegionRect.IsEmpty()) {
     mImageRegionRect.SetRect(0, 0, origWidth, origHeight);
   }
 
   RefPtr<SourceSurface> surface =
     imageContainer->GetFrame(imgIContainer::FRAME_CURRENT,
-                             imgIContainer::FLAG_NONE);
+                             imgIContainer::FLAG_SYNC_DECODE);
   if (!surface) {
     [mNativeMenuItem setImage:nil];
     return NS_ERROR_FAILURE;
   }
 
   CGImageRef origImage = NULL;
   nsresult rv = nsCocoaUtils::CreateCGImageFromSurface(surface, &origImage);
   if (NS_FAILED(rv) || !origImage) {
     [mNativeMenuItem setImage:nil];
     return NS_ERROR_FAILURE;
   }
 
   bool createSubImage = !(mImageRegionRect.x == 0 && mImageRegionRect.y == 0 &&
                             mImageRegionRect.width == origWidth && mImageRegionRect.height == origHeight);
   
-  CGImageRef finalImage = NULL;
+  CGImageRef finalImage = origImage;
   if (createSubImage) {
     // if mImageRegionRect is set using CSS, we need to slice a piece out of the overall 
     // image to use as the icon
     finalImage = ::CGImageCreateWithImageInRect(origImage, 
                                                 ::CGRectMake(mImageRegionRect.x, 
                                                 mImageRegionRect.y,
                                                 mImageRegionRect.width,
                                                 mImageRegionRect.height));
     ::CGImageRelease(origImage);
     if (!finalImage) {
       [mNativeMenuItem setImage:nil];
       return NS_ERROR_FAILURE;  
     }
-  } else {
-    finalImage = origImage;
   }
-  // The image may not be the right size for a menu icon (16x16).
-  // Create a new CGImage for the menu item.
-  uint8_t* bitmap = (uint8_t*)malloc(kIconBytes);
-
-  CGColorSpaceRef colorSpace = ::CGColorSpaceCreateDeviceRGB();
-
-  CGContextRef bitmapContext = ::CGBitmapContextCreate(bitmap, kIconWidth, kIconHeight,
-                                                       kIconBitsPerComponent,
-                                                       kIconBytesPerRow,
-                                                       colorSpace,
-                                                       kCGImageAlphaPremultipliedLast);
-  ::CGColorSpaceRelease(colorSpace);
-  if (!bitmapContext) {
-    ::CGImageRelease(finalImage);
-    free(bitmap);
-    ::CGColorSpaceRelease(colorSpace);
-    return NS_ERROR_FAILURE;
-  }
-  CGRect iconRect = ::CGRectMake(0, 0, kIconWidth, kIconHeight);
-  ::CGContextClearRect(bitmapContext, iconRect);
-  ::CGContextDrawImage(bitmapContext, iconRect, finalImage);
-  
-  CGImageRef iconImage = ::CGBitmapContextCreateImage(bitmapContext);
-
-  ::CGImageRelease(finalImage);
-  ::CGContextRelease(bitmapContext);
-  free(bitmap);
- 
-  if (!iconImage) return NS_ERROR_FAILURE;
 
   NSImage *newImage = nil;
-  rv = nsCocoaUtils::CreateNSImageFromCGImage(iconImage, &newImage);
+  rv = nsCocoaUtils::CreateNSImageFromCGImage(finalImage, &newImage);
   if (NS_FAILED(rv) || !newImage) {    
     [mNativeMenuItem setImage:nil];
-    ::CGImageRelease(iconImage);
+    ::CGImageRelease(finalImage);
     return NS_ERROR_FAILURE;
   }
 
+  [newImage setSize:NSMakeSize(kIconWidth, kIconHeight)];
   [mNativeMenuItem setImage:newImage];
   
   [newImage release];
-  ::CGImageRelease(iconImage);
+  ::CGImageRelease(finalImage);
 
   mLoadedIcon = true;
   mSetIcon = true;
 
   if (mMenuObject) {
     mMenuObject->IconUpdated();
   }