Skip to content

Commit

Permalink
ImageSkia is 1:1 to NSImage. This patch adds conversions between Imag…
Browse files Browse the repository at this point in the history
…eSkia and NSImage instead of SkBitmap wherever possible

Renamed image_mac to image_skia_util_mac.
Moved AppplicationIconAtSize from skia_utils_mac to image_skia_util_mac

BUG=132327
TEST=Chromium task manager icon shows up not blurry when chromium is run in High DPI mode on Mac.

Review URL: https://chromiumcodereview.appspot.com/10532102

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142153 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
pkotwicz@chromium.org committed Jun 14, 2012
1 parent 5553254 commit 0c40064
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#include "v8/include/v8.h"

#if defined(OS_MACOSX)
#include "skia/ext/skia_utils_mac.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#endif
#if defined(OS_WIN)
#include "chrome/browser/app_icon_win.h"
Expand Down Expand Up @@ -1387,8 +1387,9 @@ TaskManagerBrowserProcessResource::TaskManagerBrowserProcessResource()
#elif defined(OS_MACOSX)
if (!default_icon_) {
// IDR_PRODUCT_LOGO_16 doesn't quite look like chrome/mac's icns icon. Load
// the real app icon (requires a nsimage->skbitmap->nsimage conversion :-().
default_icon_ = new gfx::ImageSkia(gfx::AppplicationIconAtSize(16));
// the real app icon (requires a nsimage->image_skia->nsimage
// conversion :-().
default_icon_ = new gfx::ImageSkia(gfx::ApplicationIconAtSize(16));
}
#else
// TODO(port): Port icon code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
#include "content/public/browser/notification_source.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"
#import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
#import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#include "third_party/apple_sample_code/ImageAndTextCell.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"

namespace {
// Colors for the infobar.
Expand Down Expand Up @@ -415,7 +414,7 @@ new CookiesTreeModel(blocked_lsos.cookies()->Clone(),
icons_.reset([[NSMutableArray alloc] init]);
for (std::vector<gfx::ImageSkia>::iterator it = skiaIcons.begin();
it != skiaIcons.end(); ++it) {
[icons_ addObject:gfx::SkBitmapToNSImage(*it->bitmap())];
[icons_ addObject:gfx::NSImageFromImageSkia(*it)];
}

// Default icon will be the last item in the array.
Expand Down
7 changes: 3 additions & 4 deletions chrome/browser/ui/cocoa/menu_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
#include "base/logging.h"
#include "base/sys_string_conversions.h"
#import "chrome/browser/ui/cocoa/event_utils.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/accelerators/accelerator_cocoa.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"

@interface MenuController (Private)
- (void)addSeparatorToMenu:(NSMenu*)menu
Expand Down Expand Up @@ -109,7 +108,7 @@ - (void)addItemToMenu:(NSMenu*)menu
// If the menu item has an icon, set it.
gfx::ImageSkia skiaIcon;
if (model->GetIconAt(modelIndex, &skiaIcon) && !skiaIcon.isNull()) {
NSImage* icon = gfx::SkBitmapToNSImage(*skiaIcon.bitmap());
NSImage* icon = gfx::NSImageFromImageSkia(skiaIcon);
if (icon) {
[item setImage:icon];
}
Expand Down Expand Up @@ -169,7 +168,7 @@ - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
gfx::ImageSkia skiaIcon;
NSImage* icon = nil;
if (model->GetIconAt(modelIndex, &skiaIcon) && !skiaIcon.isNull())
icon = gfx::SkBitmapToNSImage(*skiaIcon.bitmap());
icon = gfx::NSImageFromImageSkia(skiaIcon);
[(id)item setImage:icon];
}
return model->IsEnabledAt(modelIndex);
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/ui/cocoa/speech_recognition_bubble_cocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#import "chrome/browser/ui/cocoa/speech_recognition_window_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/gfx/image/image_skia_util_mac.h"

using content::WebContents;

Expand Down Expand Up @@ -56,7 +56,7 @@

void SpeechRecognitionBubbleImpl::UpdateImage() {
if (window_.get())
[window_.get() setImage:gfx::SkBitmapToNSImage(icon_image())];
[window_.get() setImage:gfx::NSImageFromImageSkia(icon_image())];
}

void SpeechRecognitionBubbleImpl::Show() {
Expand Down Expand Up @@ -121,7 +121,7 @@

[window_.get() updateLayout:display_mode()
messageText:message_text()
iconImage:gfx::SkBitmapToNSImage(icon_image())];
iconImage:gfx::NSImageFromImageSkia(icon_image())];
}

} // namespace
Expand Down
12 changes: 6 additions & 6 deletions chrome/browser/ui/cocoa/table_row_nsimage_cache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "chrome/browser/ui/cocoa/table_row_nsimage_cache.h"

#include "base/logging.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"

TableRowNSImageCache::TableRowNSImageCache(Table* model)
: model_(model),
Expand Down Expand Up @@ -66,12 +66,12 @@
DCHECK_LT(row, static_cast<int>([icon_images_ count]));
NSImage* image = static_cast<NSImage*>([icon_images_ pointerAtIndex:row]);
if (!image) {
const gfx::ImageSkia bitmap_icon =
const gfx::ImageSkia image_skia_icon =
model_->GetIcon(row);
// This means GetIcon() will get called until it returns a non-empty bitmap.
// Empty bitmaps are intentionally not cached.
if (!bitmap_icon.isNull()) {
image = gfx::SkBitmapToNSImage(bitmap_icon);
// This means GetIcon() will get called until it returns a non-empty image.
// Empty images are intentionally not cached.
if (!image_skia_icon.isNull()) {
image = gfx::NSImageFromImageSkia(image_skia_icon);
DCHECK(image);
[icon_images_ replacePointerAtIndex:row withPointer:image];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#import "chrome/browser/ui/cocoa/event_utils.h"
#import "chrome/browser/ui/cocoa/menu_button.h"
#include "chrome/browser/ui/toolbar/back_forward_menu_model.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"

using base::SysUTF16ToNSString;
using gfx::SkBitmapToNSImage;
using gfx::NSImageFromImageSkia;

@implementation BackForwardMenuController

Expand Down Expand Up @@ -75,7 +74,7 @@ - (void)menuNeedsUpdate:(NSMenu*)menu {
gfx::ImageSkia icon;
// Icon (if it has one).
if (model_->GetIconAt(menuID, &icon))
[menuItem setImage:SkBitmapToNSImage(*icon.bitmap())];
[menuItem setImage:NSImageFromImageSkia(icon)];

// This will make it call our |-executeMenuItem:| method. We store the
// |menuID| (or |menu_id|) in the tag.
Expand Down
3 changes: 0 additions & 3 deletions skia/ext/skia_utils_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon,
// TODO(thakis): Remove this -- http://crbug.com/69432
SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon);

// Returns |[NSImage imageNamed:@"NSApplicationIcon"]| as SkBitmap.
SK_API SkBitmap AppplicationIconAtSize(int size);

// Converts a SkCanvas temporarily to a CGContext
class SK_API SkiaBitLocker {
public:
Expand Down
5 changes: 0 additions & 5 deletions skia/ext/skia_utils_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,6 @@ SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) {
return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get());
}

SkBitmap AppplicationIconAtSize(int size) {
NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true);
}

SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas)
: canvas_(canvas),
cgContext_(0) {
Expand Down
17 changes: 6 additions & 11 deletions ui/gfx/image/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@
#include "ui/gfx/image/cairo_cached_surface.h"
#elif defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#endif

namespace gfx {

namespace internal {

#if defined(OS_MACOSX)
ImageSkia NSImageToImageSkia(NSImage* image);
NSImage* ImageSkiaToNSImage(const ImageSkia* image);
#endif

#if defined(TOOLKIT_GTK)
const ImageSkia GdkPixbufToImageSkia(GdkPixbuf* pixbuf) {
const ImageSkia ImageSkiaFromGdkPixbuf(GdkPixbuf* pixbuf) {
CHECK(pixbuf);
gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf),
gdk_pixbuf_get_height(pixbuf)), false);
Expand Down Expand Up @@ -375,15 +370,15 @@ internal::ImageRep* Image::GetRepresentation(
if (storage_->default_representation_type() == Image::kImageRepGdk) {
internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk();
rep = new internal::ImageRepSkia(new ImageSkia(
internal::GdkPixbufToImageSkia(pixbuf_rep->pixbuf())));
internal::ImageSkiaFromGdkPixbuf(pixbuf_rep->pixbuf())));
}
// We don't do conversions from CairoCachedSurfaces to Skia because the
// data lives on the display server and we'll always have a GdkPixbuf if we
// have a CairoCachedSurface.
#elif defined(OS_MACOSX)
if (storage_->default_representation_type() == Image::kImageRepCocoa) {
internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa();
ImageSkia image_skia = internal::NSImageToImageSkia(nsimage_rep->image());
ImageSkia image_skia = ImageSkiaFromNSImage(nsimage_rep->image());
rep = new internal::ImageRepSkia(new ImageSkia(image_skia));
}
#endif
Expand Down Expand Up @@ -418,8 +413,8 @@ internal::ImageRep* Image::GetRepresentation(
}
#elif defined(OS_MACOSX)
if (rep_type == Image::kImageRepCocoa) {
NSImage* image = internal::ImageSkiaToNSImage(
default_rep->AsImageRepSkia()->image());
NSImage* image = NSImageFromImageSkia(
*default_rep->AsImageRepSkia()->image());
base::mac::NSObjectRetain(image);
native_rep = new internal::ImageRepCocoa(image);
}
Expand Down
48 changes: 0 additions & 48 deletions ui/gfx/image/image_mac.mm

This file was deleted.

42 changes: 42 additions & 0 deletions ui/gfx/image/image_skia_util_mac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_IMAGE_IMAGE_SKIA_UTIL_MAC_H_
#define UI_GFX_IMAGE_IMAGE_SKIA_UTIL_MAC_H_
#pragma once

#include "ui/base/ui_export.h"

#ifdef __LP64__
typedef CGSize NSSize;
#else
typedef struct _NSSize NSSize;
#endif

#ifdef __OBJC__
@class NSImage;
#else
class NSImage;
#endif

namespace gfx {
class ImageSkia;

// Converts to ImageSkia from NSImage.
UI_EXPORT gfx::ImageSkia ImageSkiaFromNSImage(NSImage* image);

// Resizes NSImage to |size| DIP and then converts to ImageSkia.
UI_EXPORT gfx::ImageSkia ImageSkiaFromResizedNSImage(NSImage* image,
NSSize size);

// Resizes |[NSImage imageNamed:@NSApplicationIcon]| to have edge width of
// |size| DIP and returns result as ImageSkia.
UI_EXPORT gfx::ImageSkia ApplicationIconAtSize(int size);

// Converts to NSImage from ImageSkia.
UI_EXPORT NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia);

} // namespace gfx

#endif // UI_GFX_IMAGE_IMAGE_SKIA_UTIL_MAC_H_
65 changes: 65 additions & 0 deletions ui/gfx/image/image_skia_util_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/image/image_skia_util_mac.h"

#import <AppKit/AppKit.h>

#include "base/mac/mac_util.h"
#include "base/memory/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image_skia.h"

namespace gfx {

gfx::ImageSkia ImageSkiaFromNSImage(NSImage* image) {
return ImageSkiaFromResizedNSImage(image, [image size]);
}

gfx::ImageSkia ImageSkiaFromResizedNSImage(NSImage* image,
NSSize desired_size) {
// Resize and convert to ImageSkia simultaneously to save on computation.
// TODO(pkotwicz): Separate resizing NSImage and converting to ImageSkia.
float resize_scale_x = desired_size.width / [image size].width;
float resize_scale_y = desired_size.height / [image size].height;

gfx::ImageSkia image_skia;
for (NSImageRep* image_rep in [image representations]) {
NSSize image_rep_size = NSMakeSize([image_rep pixelsWide] * resize_scale_x,
[image_rep pixelsHigh] * resize_scale_y);
SkBitmap bitmap(gfx::NSImageRepToSkBitmap(image_rep, image_rep_size,
false));
if (!bitmap.isNull() && !bitmap.empty()) {
float scale_factor = image_rep_size.width / desired_size.width;
image_skia.AddBitmapForScale(bitmap, scale_factor);
}
}
return image_skia;
}

gfx::ImageSkia ApplicationIconAtSize(int desired_size) {
NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
return ImageSkiaFromResizedNSImage(image,
NSMakeSize(desired_size, desired_size));
}

NSImage* NSImageFromImageSkia(const gfx::ImageSkia& image_skia) {
if (image_skia.empty())
return nil;

scoped_nsobject<NSImage> image([[NSImage alloc] init]);

const std::vector<SkBitmap> bitmaps = image_skia.bitmaps();
for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin();
it != bitmaps.end(); ++it) {
[image addRepresentation:gfx::SkBitmapToNSBitmapImageRep(*it)];
}

[image setSize:NSMakeSize(image_skia.width(), image_skia.height())];
return [image.release() autorelease];
}

} // namespace gfx
Loading

0 comments on commit 0c40064

Please sign in to comment.