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" | 10 #include "base/mac/mac_util.h" |
11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
12 #include "base/memory/scoped_nsobject.h" | 12 #include "base/memory/scoped_nsobject.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "skia/ext/bitmap_platform_device_mac.h" | 14 #include "skia/ext/bitmap_platform_device_mac.h" |
15 #include "third_party/skia/include/core/SkRegion.h" | 15 #include "third_party/skia/include/core/SkRegion.h" |
16 #include "third_party/skia/include/utils/mac/SkCGUtils.h" | 16 #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
17 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | |
18 | 17 |
19 // 10.6 API that we use if available. | 18 // 10.6 API that we use if available. |
20 #if !defined(MAC_OS_X_VERSION_10_6) || \ | 19 #if !defined(MAC_OS_X_VERSION_10_6) || \ |
21 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 | 20 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
22 | 21 |
23 @interface NSImageRep (NSObject) | 22 @interface NSImageRep (NSObject) |
24 | 23 |
25 - (BOOL)drawInRect:(NSRect)dstSpacePortionRect | 24 - (BOOL)drawInRect:(NSRect)dstSpacePortionRect |
26 fromRect:(NSRect)srcSpacePortionRect | 25 fromRect:(NSRect)srcSpacePortionRect |
27 operation:(NSCompositingOperation)op | 26 operation:(NSCompositingOperation)op |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 #else | 68 #else |
70 #error We require that Skia's and CoreGraphics's recommended \ | 69 #error We require that Skia's and CoreGraphics's recommended \ |
71 image memory layout match. | 70 image memory layout match. |
72 #endif | 71 #endif |
73 #undef HAS_ARGB_SHIFTS | 72 #undef HAS_ARGB_SHIFTS |
74 | 73 |
75 // Something went really wrong. Best guess is that the bitmap data is invalid. | 74 // Something went really wrong. Best guess is that the bitmap data is invalid. |
76 DCHECK(context); | 75 DCHECK(context); |
77 | 76 |
78 // Save the current graphics context so that we can restore it later. | 77 // Save the current graphics context so that we can restore it later. |
79 gfx::ScopedNSGraphicsContextSaveGState scoped_g_state; | 78 NSGraphicsContext* old_context = [NSGraphicsContext currentContext]; |
79 [NSGraphicsContext saveGraphicsState]; | |
80 | 80 |
81 // Dummy context that we will draw into. | 81 // Dummy context that we will draw into. |
82 NSGraphicsContext* context_cocoa = | 82 NSGraphicsContext* context_cocoa = |
83 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; | 83 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; |
84 [NSGraphicsContext setCurrentContext:context_cocoa]; | 84 [NSGraphicsContext setCurrentContext:context_cocoa]; |
85 | 85 |
86 // This will stretch any images to |size| if it does not fit or is non-square. | 86 // This will stretch any images to |size| if it does not fit or is non-square. |
87 NSRect drawRect = NSMakeRect(0, 0, size.width, size.height); | 87 NSRect drawRect = NSMakeRect(0, 0, size.width, size.height); |
88 | 88 |
89 // NSImage does caching such that subsequent drawing is much faster (on my | 89 // NSImage does caching such that subsequent drawing is much faster (on my |
(...skipping 13 matching lines...) Expand all Loading... | |
103 fromRect:NSZeroRect | 103 fromRect:NSZeroRect |
104 operation:NSCompositeCopy | 104 operation:NSCompositeCopy |
105 fraction:1.0 | 105 fraction:1.0 |
106 respectFlipped:NO | 106 respectFlipped:NO |
107 hints:NO]; | 107 hints:NO]; |
108 } else { | 108 } else { |
109 [image_rep drawInRect:drawRect]; | 109 [image_rep drawInRect:drawRect]; |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 [NSGraphicsContext restoreGraphicsState]; | |
114 if (!old_context && base::mac::IsOSLeopardOrEarlier()) | |
Robert Sesek
2012/03/05 15:49:46
Remove this condition; it's logic that would only
Nico
2012/03/05 15:58:35
Why's that? -currentContext could return nil, coul
Robert Sesek
2012/03/05 17:23:58
Because we're in a drawing context. The scoper is
Nico
2012/03/05 17:33:00
I don't understand, can you expand a bit? Why is -
Robert Sesek
2012/03/05 17:40:04
DAMN YOU RIETEVELD. I thought this was in |-drawRe
| |
115 [NSGraphicsContext setCurrentContext:nil]; | |
116 | |
113 return bitmap; | 117 return bitmap; |
114 } | 118 } |
115 | 119 |
116 } // namespace | 120 } // namespace |
117 | 121 |
118 namespace gfx { | 122 namespace gfx { |
119 | 123 |
120 CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) { | 124 CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) { |
121 // CGAffineTransforms don't support perspective transforms, so make sure | 125 // CGAffineTransforms don't support perspective transforms, so make sure |
122 // we don't get those. | 126 // we don't get those. |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 // Apply content matrix. | 360 // Apply content matrix. |
357 SkMatrix skMatrix = canvas_->getTotalMatrix(); | 361 SkMatrix skMatrix = canvas_->getTotalMatrix(); |
358 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | 362 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); |
359 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | 363 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
360 CGContextConcatCTM(cgContext_, affine); | 364 CGContextConcatCTM(cgContext_, affine); |
361 | 365 |
362 return cgContext_; | 366 return cgContext_; |
363 } | 367 } |
364 | 368 |
365 } // namespace gfx | 369 } // namespace gfx |
OLD | NEW |