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/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "skia/ext/bitmap_platform_device_mac.h" | 13 #include "skia/ext/bitmap_platform_device_mac.h" |
14 #include "third_party/skia/include/core/SkRegion.h" | 14 #include "third_party/skia/include/core/SkRegion.h" |
15 #include "third_party/skia/include/utils/mac/SkCGUtils.h" | 15 #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // 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. |
20 SkBitmap NSImageOrNSImageRepToSkBitmapWithColorSpace( | 20 SkBitmap NSImageOrNSImageRepToSkBitmapWithColorSpace( |
21 NSImage* image, | 21 NSImage* image, |
22 NSImageRep* image_rep, | 22 NSImageRep* image_rep, |
23 NSSize size, | 23 NSSize size, |
24 bool is_opaque, | 24 bool is_opaque, |
25 CGColorSpaceRef color_space) { | 25 CGColorSpaceRef color_space) { |
26 // Only image or image_rep should be provided, not both. | 26 // Only image or image_rep should be provided, not both. |
27 DCHECK((image != 0) ^ (image_rep != 0)); | 27 DCHECK((image != 0) ^ (image_rep != 0)); |
28 | 28 |
29 SkBitmap bitmap; | 29 SkBitmap bitmap; |
30 if (!bitmap.tryAllocN32Pixels(size.width, size.height, is_opaque)) | 30 if (!bitmap.allocN32Pixels(size.width, size.height, is_opaque)) |
31 return bitmap; // Return |bitmap| which should respond true to isNull(). | 31 return bitmap; // Return |bitmap| which should respond true to isNull(). |
32 | 32 |
33 | 33 |
34 void* data = bitmap.getPixels(); | 34 void* data = bitmap.getPixels(); |
35 | 35 |
36 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple | 36 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
37 // recommends these flags for improved CG performance. | 37 // recommends these flags for improved CG performance. |
38 #define HAS_ARGB_SHIFTS(a, r, g, b) \ | 38 #define HAS_ARGB_SHIFTS(a, r, g, b) \ |
39 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ | 39 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ |
40 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) | 40 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 useDeviceBits_ = deviceBits.getPixels() && | 423 useDeviceBits_ = deviceBits.getPixels() && |
424 canvas_->isClipRect() && | 424 canvas_->isClipRect() && |
425 !bitmapIsDummy_; | 425 !bitmapIsDummy_; |
426 if (useDeviceBits_) { | 426 if (useDeviceBits_) { |
427 bool result = deviceBits.extractSubset(&bitmap_, clip_bounds); | 427 bool result = deviceBits.extractSubset(&bitmap_, clip_bounds); |
428 DCHECK(result); | 428 DCHECK(result); |
429 if (!result) | 429 if (!result) |
430 return 0; | 430 return 0; |
431 bitmap_.lockPixels(); | 431 bitmap_.lockPixels(); |
432 } else { | 432 } else { |
433 bool result = bitmap_.tryAllocN32Pixels( | 433 bool result = bitmap_.allocN32Pixels( |
434 clip_bounds.width(), clip_bounds.height()); | 434 clip_bounds.width(), clip_bounds.height()); |
435 DCHECK(result); | 435 DCHECK(result); |
436 if (!result) | 436 if (!result) |
437 return 0; | 437 return 0; |
438 bitmap_.eraseColor(0); | 438 bitmap_.eraseColor(0); |
439 } | 439 } |
440 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 440 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
441 CGColorSpaceCreateDeviceRGB()); | 441 CGColorSpaceCreateDeviceRGB()); |
442 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 442 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), |
443 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 443 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, |
444 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 444 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
445 DCHECK(cgContext_); | 445 DCHECK(cgContext_); |
446 | 446 |
447 SkMatrix matrix = canvas_->getTotalMatrix(); | 447 SkMatrix matrix = canvas_->getTotalMatrix(); |
448 matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()), | 448 matrix.postTranslate(-SkIntToScalar(bitmapOffset_.x()), |
449 -SkIntToScalar(bitmapOffset_.y())); | 449 -SkIntToScalar(bitmapOffset_.y())); |
450 matrix.postScale(1, -1); | 450 matrix.postScale(1, -1); |
451 matrix.postTranslate(0, SkIntToScalar(bitmap_.height())); | 451 matrix.postTranslate(0, SkIntToScalar(bitmap_.height())); |
452 | 452 |
453 CGContextConcatCTM(cgContext_, SkMatrixToCGAffineTransform(matrix)); | 453 CGContextConcatCTM(cgContext_, SkMatrixToCGAffineTransform(matrix)); |
454 | 454 |
455 return cgContext_; | 455 return cgContext_; |
456 } | 456 } |
457 | 457 |
458 bool SkiaBitLocker::hasEmptyClipRegion() const { | 458 bool SkiaBitLocker::hasEmptyClipRegion() const { |
459 return canvas_->isClipEmpty(); | 459 return canvas_->isClipEmpty(); |
460 } | 460 } |
461 | 461 |
462 } // namespace gfx | 462 } // namespace gfx |
OLD | NEW |