| 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 bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 30 if (!bitmap.allocN32Pixels(size.width, size.height, is_opaque)) |
| 31 size.width, | |
| 32 size.height, | |
| 33 0, | |
| 34 is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); | |
| 35 | |
| 36 if (!bitmap.allocPixels()) | |
| 37 return bitmap; // Return |bitmap| which should respond true to isNull(). | 31 return bitmap; // Return |bitmap| which should respond true to isNull(). |
| 38 | 32 |
| 39 | 33 |
| 40 void* data = bitmap.getPixels(); | 34 void* data = bitmap.getPixels(); |
| 41 | 35 |
| 42 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple | 36 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
| 43 // recommends these flags for improved CG performance. | 37 // recommends these flags for improved CG performance. |
| 44 #define HAS_ARGB_SHIFTS(a, r, g, b) \ | 38 #define HAS_ARGB_SHIFTS(a, r, g, b) \ |
| 45 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ | 39 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ |
| 46 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) | 40 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 DCHECK(device); | 368 DCHECK(device); |
| 375 if (!device) | 369 if (!device) |
| 376 return 0; | 370 return 0; |
| 377 releaseIfNeeded(); // This flushes any prior bitmap use | 371 releaseIfNeeded(); // This flushes any prior bitmap use |
| 378 const SkBitmap& deviceBits = device->accessBitmap(true); | 372 const SkBitmap& deviceBits = device->accessBitmap(true); |
| 379 useDeviceBits_ = deviceBits.getPixels(); | 373 useDeviceBits_ = deviceBits.getPixels(); |
| 380 if (useDeviceBits_) { | 374 if (useDeviceBits_) { |
| 381 bitmap_ = deviceBits; | 375 bitmap_ = deviceBits; |
| 382 bitmap_.lockPixels(); | 376 bitmap_.lockPixels(); |
| 383 } else { | 377 } else { |
| 384 bitmap_.setConfig( | 378 if (!bitmap_.allocN32Pixels(deviceBits.width(), deviceBits.height())) |
| 385 SkBitmap::kARGB_8888_Config, deviceBits.width(), deviceBits.height()); | 379 return 0; |
| 386 bitmap_.allocPixels(); | |
| 387 bitmap_.eraseColor(0); | 380 bitmap_.eraseColor(0); |
| 388 } | 381 } |
| 389 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | 382 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
| 390 CGColorSpaceCreateDeviceRGB()); | 383 CGColorSpaceCreateDeviceRGB()); |
| 391 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), | 384 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), |
| 392 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, | 385 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, |
| 393 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); | 386 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); |
| 394 | 387 |
| 395 // Apply device matrix. | 388 // Apply device matrix. |
| 396 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); | 389 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 // Apply content matrix. | 421 // Apply content matrix. |
| 429 SkMatrix skMatrix = canvas_->getTotalMatrix(); | 422 SkMatrix skMatrix = canvas_->getTotalMatrix(); |
| 430 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); | 423 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); |
| 431 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); | 424 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); |
| 432 CGContextConcatCTM(cgContext_, affine); | 425 CGContextConcatCTM(cgContext_, affine); |
| 433 | 426 |
| 434 return cgContext_; | 427 return cgContext_; |
| 435 } | 428 } |
| 436 | 429 |
| 437 } // namespace gfx | 430 } // namespace gfx |
| OLD | NEW |