OLD | NEW |
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/mac_util.h" |
10 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/scoped_nsobject.h" | 12 #include "base/memory/scoped_nsobject.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #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" |
14 #include "third_party/skia/include/utils/mac/SkCGUtils.h" | 16 #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
15 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 17 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
16 | 18 |
17 // 10.6 API that we use if available. | 19 // 10.6 API that we use if available. |
18 #if !defined(MAC_OS_X_VERSION_10_6) || \ | 20 #if !defined(MAC_OS_X_VERSION_10_6) || \ |
19 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 | 21 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
20 | 22 |
21 @interface NSImageRep (NSObject) | 23 @interface NSImageRep (NSObject) |
22 | 24 |
23 - (BOOL)drawInRect:(NSRect)dstSpacePortionRect | 25 - (BOOL)drawInRect:(NSRect)dstSpacePortionRect |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 | 279 |
278 [image setSize:min_size]; | 280 [image setSize:min_size]; |
279 return [image.release() autorelease]; | 281 return [image.release() autorelease]; |
280 } | 282 } |
281 | 283 |
282 SkBitmap AppplicationIconAtSize(int size) { | 284 SkBitmap AppplicationIconAtSize(int size) { |
283 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | 285 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
284 return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); | 286 return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); |
285 } | 287 } |
286 | 288 |
| 289 |
| 290 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas) |
| 291 : canvas_(canvas), |
| 292 cgContext_(0) { |
| 293 } |
| 294 |
| 295 SkiaBitLocker::~SkiaBitLocker() { |
| 296 releaseIfNeeded(); |
| 297 } |
| 298 |
| 299 void SkiaBitLocker::releaseIfNeeded() { |
| 300 if (!cgContext_) |
| 301 return; |
| 302 canvas_->getDevice()->accessBitmap(true).unlockPixels(); |
| 303 CGContextRelease(cgContext_); |
| 304 cgContext_ = 0; |
| 305 } |
| 306 |
| 307 CGContextRef SkiaBitLocker::cgContext() { |
| 308 SkDevice* device = canvas_->getDevice(); |
| 309 DCHECK(device); |
| 310 if (!device) |
| 311 return 0; |
| 312 releaseIfNeeded(); |
| 313 const SkBitmap& bitmap = device->accessBitmap(true); |
| 314 bitmap.lockPixels(); |
| 315 void* pixels = bitmap.getPixels(); |
| 316 cgContext_ = CGBitmapContextCreate(pixels, device->width(), |
| 317 device->height(), 8, bitmap.rowBytes(), CGColorSpaceCreateDeviceRGB(), |
| 318 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
| 319 |
| 320 // Apply device matrix. |
| 321 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); |
| 322 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, |
| 323 -device->height()); |
| 324 CGContextConcatCTM(cgContext_, contentsTransform); |
| 325 |
| 326 // Apply clip in device coordinates. |
| 327 CGMutablePathRef clipPath = CGPathCreateMutable(); |
| 328 SkRegion::Iterator iter(canvas_->getTotalClip()); |
| 329 for (; !iter.done(); iter.next()) { |
| 330 const SkIRect& skRect = iter.rect(); |
| 331 CGRect cgRect = SkIRectToCGRect(skRect); |
| 332 CGPathAddRect(clipPath, 0, cgRect); |
| 333 } |
| 334 CGContextAddPath(cgContext_, clipPath); |
| 335 CGContextClip(cgContext_); |
| 336 CGPathRelease(clipPath); |
| 337 |
| 338 // Apply content matrix. |
| 339 const SkMatrix& skMatrix = canvas_->getTotalMatrix(); |
| 340 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
| 341 CGContextConcatCTM(cgContext_, affine); |
| 342 |
| 343 return cgContext_; |
| 344 } |
| 345 |
287 } // namespace gfx | 346 } // namespace gfx |
OLD | NEW |