Mac: Explicitly set the colorspace on SkBitmap -> CGImageRef conversions.

The color space was hardcoded as "generic rgb" in skia. This is not always correct, also skia is changing this color space around a lot currently. To protect us from their unreliable default, hardcode "generic rgb" as default on our side for now, but make it possible for clients to provide their own color space.

Use this to let tabpose and the favicon code pass in the device colorspace.

BUG=24267,50307
TEST=Open tabpose. Delayed thumbnails should look like backing-store backed thumbnails. The colors of favicons should now match other browsers.

Review URL: http://codereview.chromium.org/6117006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71208 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/ui/cocoa/tab_strip_controller.mm b/chrome/browser/ui/cocoa/tab_strip_controller.mm
index d9fb6fc..3165dcc 100644
--- a/chrome/browser/ui/cocoa/tab_strip_controller.mm
+++ b/chrome/browser/ui/cocoa/tab_strip_controller.mm
@@ -12,6 +12,7 @@
 #include "app/l10n_util.h"
 #include "app/mac/nsimage_cache.h"
 #include "app/resource_bundle.h"
+#include "base/mac/mac_util.h"
 #include "base/sys_string_conversions.h"
 #include "chrome/app/chrome_command_ids.h"
 #include "chrome/browser/autocomplete/autocomplete.h"
@@ -1209,12 +1210,16 @@
 - (NSImageView*)iconImageViewForContents:(TabContents*)contents {
   BOOL isApp = contents->is_app();
   NSImage* image = nil;
+  // Favicons come from the renderer, and the renderer draws everything in the
+  // system color space.
+  CGColorSpaceRef colorSpace = base::mac::GetSystemColorSpace();
   if (isApp) {
     SkBitmap* icon = contents->GetExtensionAppIcon();
     if (icon)
-      image = gfx::SkBitmapToNSImage(*icon);
+      image = gfx::SkBitmapToNSImageWithColorSpace(*icon, colorSpace);
   } else {
-    image = gfx::SkBitmapToNSImage(contents->GetFavIcon());
+    image = gfx::SkBitmapToNSImageWithColorSpace(contents->GetFavIcon(),
+                                                 colorSpace);
   }
 
   // Either we don't have a valid favicon or there was some issue converting it
diff --git a/chrome/browser/ui/cocoa/tabpose_window.mm b/chrome/browser/ui/cocoa/tabpose_window.mm
index b9e4a3d..98d8611 100644
--- a/chrome/browser/ui/cocoa/tabpose_window.mm
+++ b/chrome/browser/ui/cocoa/tabpose_window.mm
@@ -177,8 +177,9 @@
 
 - (void)setThumbnail:(const SkBitmap&)bitmap {
   // SkCreateCGImageRef() holds on to |bitmaps|'s memory, so this doesn't
-  // create a copy.
-  thumbnail_.reset(SkCreateCGImageRef(bitmap));
+  // create a copy. The renderer always draws data in the system colorspace.
+  thumbnail_.reset(SkCreateCGImageRefWithColorspace(
+      bitmap, base::mac::GetSystemColorSpace()));
   loader_ = NULL;
   [self setNeedsDisplay];
 }
diff --git a/skia/ext/skia_utils_mac.h b/skia/ext/skia_utils_mac.h
index 9a1636e..0551643 100644
--- a/skia/ext/skia_utils_mac.h
+++ b/skia/ext/skia_utils_mac.h
@@ -64,7 +64,13 @@
 // Draws an NSImage with a given size into a SkBitmap.
 SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque);
 
-// Given an SkBitmap, return an autoreleased NSImage.
+// Given an SkBitmap and a color space, return an autoreleased NSImage.
+NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon,
+                                         CGColorSpaceRef colorSpace);
+
+// Given an SkBitmap, return an autoreleased NSImage in the generic color space.
+// DEPRECATED, use SkBitmapToNSImageWithColorSpace() instead.
+// TODO(thakis): Remove this -- http://crbug.com/69432
 NSImage* SkBitmapToNSImage(const SkBitmap& icon);
 
 // Returns |[NSImage imageNamed:@"NSApplicationIcon"]| as SkBitmap.
diff --git a/skia/ext/skia_utils_mac.mm b/skia/ext/skia_utils_mac.mm
index f52709b..171d3299 100644
--- a/skia/ext/skia_utils_mac.mm
+++ b/skia/ext/skia_utils_mac.mm
@@ -167,12 +167,14 @@
   return bitmap;
 }
 
-NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) {
+NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap,
+                                         CGColorSpaceRef colorSpace) {
   if (skiaBitmap.isNull())
     return nil;
 
   // First convert SkBitmap to CGImageRef.
-  CGImageRef cgimage = SkCreateCGImageRef(skiaBitmap);
+  CGImageRef cgimage =
+    SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace);
 
   // Now convert to NSImage.
   NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc]
@@ -184,6 +186,12 @@
   return image;
 }
 
+NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) {
+  base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
+      CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
+  return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get());
+}
+
 SkBitmap AppplicationIconAtSize(int size) {
   NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
   return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true);