| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/mac_util.h" | |
| 11 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/memory/scoped_nsobject.h" | 11 #include "base/memory/scoped_nsobject.h" |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "skia/ext/bitmap_platform_device_mac.h" | 13 #include "skia/ext/bitmap_platform_device_mac.h" |
| 15 #include "third_party/skia/include/core/SkRegion.h" | 14 #include "third_party/skia/include/core/SkRegion.h" |
| 16 #include "third_party/skia/include/utils/mac/SkCGUtils.h" | 15 #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // Draws an NSImage or an NSImageRep with a given size into a SkBitmap. | 19 // Draws an NSImage or an NSImageRep with a given size into a SkBitmap. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 50 kCGBitmapByteOrder32Host)); | 49 kCGBitmapByteOrder32Host)); |
| 51 #else | 50 #else |
| 52 #error We require that Skia's and CoreGraphics's recommended \ | 51 #error We require that Skia's and CoreGraphics's recommended \ |
| 53 image memory layout match. | 52 image memory layout match. |
| 54 #endif | 53 #endif |
| 55 #undef HAS_ARGB_SHIFTS | 54 #undef HAS_ARGB_SHIFTS |
| 56 | 55 |
| 57 // Something went really wrong. Best guess is that the bitmap data is invalid. | 56 // Something went really wrong. Best guess is that the bitmap data is invalid. |
| 58 DCHECK(context); | 57 DCHECK(context); |
| 59 | 58 |
| 60 // Save the current graphics context so that we can restore it later. | |
| 61 NSGraphicsContext* old_context = [NSGraphicsContext currentContext]; | |
| 62 [NSGraphicsContext saveGraphicsState]; | 59 [NSGraphicsContext saveGraphicsState]; |
| 63 | 60 |
| 64 // Dummy context that we will draw into. | |
| 65 NSGraphicsContext* context_cocoa = | 61 NSGraphicsContext* context_cocoa = |
| 66 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; | 62 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; |
| 67 [NSGraphicsContext setCurrentContext:context_cocoa]; | 63 [NSGraphicsContext setCurrentContext:context_cocoa]; |
| 68 | 64 |
| 69 // This will stretch any images to |size| if it does not fit or is non-square. | |
| 70 NSRect drawRect = NSMakeRect(0, 0, size.width, size.height); | 65 NSRect drawRect = NSMakeRect(0, 0, size.width, size.height); |
| 71 | |
| 72 // NSImage does caching such that subsequent drawing is much faster (on my | |
| 73 // machine, about 4x faster). Unfortunately on 10.5 NSImageRep doesn't do | |
| 74 // caching. For this reason we draw using an NSImage if available. Once | |
| 75 // 10.5 is no longer supported we can drop this and always use NSImageRep. | |
| 76 if (image) { | 66 if (image) { |
| 77 [image drawInRect:drawRect | 67 [image drawInRect:drawRect |
| 78 fromRect:NSZeroRect | 68 fromRect:NSZeroRect |
| 79 operation:NSCompositeCopy | 69 operation:NSCompositeCopy |
| 80 fraction:1.0]; | 70 fraction:1.0]; |
| 81 } else { | 71 } else { |
| 82 // Use NSCompositeCopy if available, it's slightly faster. | 72 [image_rep drawInRect:drawRect |
| 83 if ([image_rep respondsToSelector:@selector( | 73 fromRect:NSZeroRect |
| 84 drawInRect:fromRect:operation:fraction:respectFlipped:hints:)]) { | 74 operation:NSCompositeCopy |
| 85 [image_rep drawInRect:drawRect | 75 fraction:1.0 |
| 86 fromRect:NSZeroRect | 76 respectFlipped:NO |
| 87 operation:NSCompositeCopy | 77 hints:NO]; |
| 88 fraction:1.0 | |
| 89 respectFlipped:NO | |
| 90 hints:NO]; | |
| 91 } else { | |
| 92 [image_rep drawInRect:drawRect]; | |
| 93 } | |
| 94 } | 78 } |
| 95 | 79 |
| 96 [NSGraphicsContext restoreGraphicsState]; | 80 [NSGraphicsContext restoreGraphicsState]; |
| 97 if (!old_context && base::mac::IsOSLeopardOrEarlier()) | |
| 98 [NSGraphicsContext setCurrentContext:nil]; | |
| 99 | 81 |
| 100 return bitmap; | 82 return bitmap; |
| 101 } | 83 } |
| 102 | 84 |
| 103 } // namespace | 85 } // namespace |
| 104 | 86 |
| 105 namespace gfx { | 87 namespace gfx { |
| 106 | 88 |
| 107 CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) { | 89 CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) { |
| 108 // CGAffineTransforms don't support perspective transforms, so make sure | 90 // CGAffineTransforms don't support perspective transforms, so make sure |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 // copy it to the stack and return it. | 199 // copy it to the stack and return it. |
| 218 SkBitmap bitmap = device->accessBitmap(false); | 200 SkBitmap bitmap = device->accessBitmap(false); |
| 219 | 201 |
| 220 return bitmap; | 202 return bitmap; |
| 221 } | 203 } |
| 222 | 204 |
| 223 SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { | 205 SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { |
| 224 return NSImageOrNSImageRepToSkBitmap(image, nil, size, is_opaque); | 206 return NSImageOrNSImageRepToSkBitmap(image, nil, size, is_opaque); |
| 225 } | 207 } |
| 226 | 208 |
| 227 SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) { | 209 SkBitmap NSImageRepToSkBitmap( |
| 228 return NSImageOrNSImageRepToSkBitmap(nil, image, size, is_opaque); | 210 NSImageRep* image_rep, NSSize size, bool is_opaque) { |
| 211 return NSImageOrNSImageRepToSkBitmap(nil, image_rep, size, is_opaque); |
| 229 } | 212 } |
| 230 | 213 |
| 231 NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& skiaBitmap) { | 214 NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& skiaBitmap) { |
| 232 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( | 215 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
| 233 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | 216 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| 234 | 217 |
| 235 // First convert SkBitmap to CGImageRef. | 218 // First convert SkBitmap to CGImageRef. |
| 236 base::mac::ScopedCFTypeRef<CGImageRef> cgimage( | 219 base::mac::ScopedCFTypeRef<CGImageRef> cgimage( |
| 237 SkCreateCGImageRefWithColorspace(skiaBitmap, color_space)); | 220 SkCreateCGImageRefWithColorspace(skiaBitmap, color_space)); |
| 238 | 221 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 // Apply content matrix. | 404 // Apply content matrix. |
| 422 SkMatrix skMatrix = canvas_->getTotalMatrix(); | 405 SkMatrix skMatrix = canvas_->getTotalMatrix(); |
| 423 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | 406 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); |
| 424 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | 407 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
| 425 CGContextConcatCTM(cgContext_, affine); | 408 CGContextConcatCTM(cgContext_, affine); |
| 426 | 409 |
| 427 return cgContext_; | 410 return cgContext_; |
| 428 } | 411 } |
| 429 | 412 |
| 430 } // namespace gfx | 413 } // namespace gfx |
| OLD | NEW |