Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: skia/ext/skia_utils_mac.mm

Issue 6849030: Add support for multi resolution icons (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "skia/ext/skia_utils_mac.h" 5 #include "skia/ext/skia_utils_mac.h"
6 6
7 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/mac/scoped_cftyperef.h" 10 #include "base/mac/scoped_cftyperef.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 fromRect:NSZeroRect 160 fromRect:NSZeroRect
161 operation:NSCompositeCopy 161 operation:NSCompositeCopy
162 fraction:1.0]; 162 fraction:1.0];
163 163
164 // Done drawing, restore context. 164 // Done drawing, restore context.
165 [NSGraphicsContext restoreGraphicsState]; 165 [NSGraphicsContext restoreGraphicsState];
166 166
167 return bitmap; 167 return bitmap;
168 } 168 }
169 169
170 SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) {
171 SkBitmap bitmap;
172 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height);
173 if (bitmap.allocPixels() != true)
Robert Sesek 2011/04/14 18:42:44 if (!bitmap.allocPixels())
174 return bitmap; // Return |bitmap| which should respond true to isNull().
175
176 bitmap.setIsOpaque(is_opaque);
177
178 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space(
179 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
Robert Sesek 2011/04/14 18:42:44 nit: indent 4
180 void* data = bitmap.getPixels();
181
182 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
183 // recommends these flags for improved CG performance.
184 #define HAS_ARGB_SHIFTS(a, r, g, b) \
185 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
186 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
187 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
188 base::mac::ScopedCFTypeRef<CGContextRef> context(
189 CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4,
190 color_space,
191 kCGImageAlphaPremultipliedFirst |
192 kCGBitmapByteOrder32Host));
193 #else
194 #error We require that Skia's and CoreGraphics's recommended \
195 image memory layout match.
196 #endif
197 #undef HAS_ARGB_SHIFTS
198
199 // Something went really wrong. Best guess is that the bitmap data is invalid.
200 DCHECK(context != NULL);
Robert Sesek 2011/04/14 18:42:44 DHCECK(context)
201
202 // Save the current graphics context so that we can restore it later.
203 [NSGraphicsContext saveGraphicsState];
204
205 // Dummy context that we will draw into.
206 NSGraphicsContext* context_cocoa =
207 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
Robert Sesek 2011/04/14 18:42:44 nit: indent 4
208 [NSGraphicsContext setCurrentContext:context_cocoa];
209
210 // This will stretch any images to |size| if it does not fit or is non-square.
211 [image drawInRect:NSMakeRect(0, 0, size.width, size.height)];
212
213 // Done drawing, restore context.
214 [NSGraphicsContext restoreGraphicsState];
215
216 return bitmap;
217 }
218
170 NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap, 219 NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap,
171 CGColorSpaceRef colorSpace) { 220 CGColorSpaceRef colorSpace) {
172 if (skiaBitmap.isNull()) 221 if (skiaBitmap.isNull())
173 return nil; 222 return nil;
174 223
175 // First convert SkBitmap to CGImageRef. 224 // First convert SkBitmap to CGImageRef.
176 CGImageRef cgimage = 225 CGImageRef cgimage =
177 SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace); 226 SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace);
178 227
179 // Now convert to NSImage. 228 // Now convert to NSImage.
180 NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc] 229 NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc]
181 initWithCGImage:cgimage] autorelease]; 230 initWithCGImage:cgimage] autorelease];
182 CFRelease(cgimage); 231 CFRelease(cgimage);
183 NSImage* image = [[[NSImage alloc] init] autorelease]; 232 NSImage* image = [[[NSImage alloc] init] autorelease];
184 [image addRepresentation:bitmap]; 233 [image addRepresentation:bitmap];
185 [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())]; 234 [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())];
186 return image; 235 return image;
187 } 236 }
188 237
189 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { 238 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) {
190 base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( 239 base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
191 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); 240 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
192 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); 241 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get());
193 } 242 }
194 243
244 NSImage* SkBitmapsToNSImage(const std::vector<SkBitmap*>& bitmaps) {
245 if (bitmaps.empty())
246 return nil;
247
248 base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
249 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
250 NSImage* image = [[[NSImage alloc] init] autorelease];
251 NSSize minSize = NSZeroSize;
252
253 for (std::vector<SkBitmap*>::const_iterator it = bitmaps.begin();
254 it != bitmaps.end(); ++it) {
255 const SkBitmap& skiaBitmap = **it;
256 // First convert SkBitmap to CGImageRef.
257 CGImageRef cgimage =
258 SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace);
Robert Sesek 2011/04/14 18:42:44 nit: indent 4
259
260 // Now convert to NSImage.
261 NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc]
Robert Sesek 2011/04/14 18:42:44 I'd use scoped_nsobject<> here so you control the
262 initWithCGImage:cgimage] autorelease];
263 CFRelease(cgimage);
264 [image addRepresentation:bitmap];
Robert Sesek 2011/04/14 18:42:44 nit: missing a space
265
266 if (minSize.width == 0 || minSize.width < skiaBitmap.width()) {
267 minSize.width = skiaBitmap.width();
268 minSize.height = skiaBitmap.height();
269 }
270 }
271
272 [image setSize:minSize];
273 return image;
274 }
275
195 SkBitmap AppplicationIconAtSize(int size) { 276 SkBitmap AppplicationIconAtSize(int size) {
196 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 277 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
197 return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); 278 return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true);
198 } 279 }
199 280
200 } // namespace gfx 281 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698