[email protected] | 0dbe4a0 | 2012-03-09 20:45:26 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 4 | |
[email protected] | 24a14e8 | 2008-12-03 20:26:51 | [diff] [blame] | 5 | #ifndef SKIA_EXT_SKIA_UTILS_MAC_H_ |
| 6 | #define SKIA_EXT_SKIA_UTILS_MAC_H_ |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 7 | |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 8 | #include <ApplicationServices/ApplicationServices.h> |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 9 | #include <vector> |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 10 | |
[email protected] | b8c84c0 | 2012-04-11 17:37:17 | [diff] [blame] | 11 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | c43c668 | 2009-05-19 14:51:44 | [diff] [blame] | 12 | #include "third_party/skia/include/core/SkColor.h" |
| 13 | |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 14 | struct SkIRect; |
| 15 | struct SkPoint; |
| 16 | struct SkRect; |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 17 | class SkCanvas; |
[email protected] | 227fcee | 2010-06-11 19:19:37 | [diff] [blame] | 18 | class SkMatrix; |
[email protected] | bcdab96 | 2010-05-14 20:57:50 | [diff] [blame] | 19 | #ifdef __LP64__ |
| 20 | typedef CGSize NSSize; |
| 21 | #else |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 22 | typedef struct _NSSize NSSize; |
[email protected] | bcdab96 | 2010-05-14 20:57:50 | [diff] [blame] | 23 | #endif |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 24 | |
| 25 | #ifdef __OBJC__ |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 26 | @class NSBitmapImageRep; |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 27 | @class NSImage; |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 28 | @class NSImageRep; |
[email protected] | c3217be5 | 2011-06-07 21:14:03 | [diff] [blame] | 29 | @class NSColor; |
[email protected] | f6b50339 | 2010-10-21 14:41:32 | [diff] [blame] | 30 | #else |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 31 | class NSBitmapImageRep; |
[email protected] | f6b50339 | 2010-10-21 14:41:32 | [diff] [blame] | 32 | class NSImage; |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 33 | class NSImageRep; |
[email protected] | c3217be5 | 2011-06-07 21:14:03 | [diff] [blame] | 34 | class NSColor; |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 35 | #endif |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 36 | |
| 37 | namespace gfx { |
| 38 | |
| 39 | // Converts a Skia point to a CoreGraphics CGPoint. |
| 40 | // Both use same in-memory format. |
| 41 | inline const CGPoint& SkPointToCGPoint(const SkPoint& point) { |
| 42 | return reinterpret_cast<const CGPoint&>(point); |
| 43 | } |
| 44 | |
| 45 | // Converts a CoreGraphics point to a Skia CGPoint. |
| 46 | // Both use same in-memory format. |
| 47 | inline const SkPoint& CGPointToSkPoint(const CGPoint& point) { |
| 48 | return reinterpret_cast<const SkPoint&>(point); |
| 49 | } |
[email protected] | dc4f63c | 2008-10-01 21:40:28 | [diff] [blame] | 50 | |
| 51 | // Matrix converters. |
[email protected] | 0dbe4a0 | 2012-03-09 20:45:26 | [diff] [blame] | 52 | SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix); |
[email protected] | 24a14e8 | 2008-12-03 20:26:51 | [diff] [blame] | 53 | |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 54 | // Rectangle converters. |
[email protected] | 19d7d70 | 2013-01-25 05:08:09 | [diff] [blame] | 55 | SK_API SkRect CGRectToSkRect(const CGRect& rect); |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 56 | |
| 57 | // Converts a Skia rect to a CoreGraphics CGRect. |
| 58 | CGRect SkIRectToCGRect(const SkIRect& rect); |
| 59 | CGRect SkRectToCGRect(const SkRect& rect); |
| 60 | |
| 61 | // Converts CGColorRef to the ARGB layout Skia expects. |
[email protected] | 04a0754 | 2012-07-23 16:51:25 | [diff] [blame] | 62 | SK_API SkColor CGColorRefToSkColor(CGColorRef color); |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 63 | |
| 64 | // Converts ARGB to CGColorRef. |
[email protected] | 7e7a004 | 2012-07-23 21:17:30 | [diff] [blame] | 65 | SK_API CGColorRef CGColorCreateFromSkColor(SkColor color); |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 66 | |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 67 | // Converts NSColor to ARGB. Returns raw rgb values and does no colorspace |
| 68 | // conversion. Only valid for colors in calibrated and device color spaces. |
| 69 | SK_API SkColor NSDeviceColorToSkColor(NSColor* color); |
| 70 | |
[email protected] | e5f893a | 2013-06-28 05:27:43 | [diff] [blame] | 71 | // Converts ARGB in the specified color space to NSColor. |
| 72 | // Prefer sRGB over calibrated colors. |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 73 | SK_API NSColor* SkColorToCalibratedNSColor(SkColor color); |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 74 | SK_API NSColor* SkColorToDeviceNSColor(SkColor color); |
[email protected] | e5f893a | 2013-06-28 05:27:43 | [diff] [blame] | 75 | SK_API NSColor* SkColorToSRGBNSColor(SkColor color); |
[email protected] | c3217be5 | 2011-06-07 21:14:03 | [diff] [blame] | 76 | |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 77 | // Converts a CGImage to a SkBitmap. |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 78 | SK_API SkBitmap CGImageToSkBitmap(CGImageRef image); |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 79 | |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 80 | // Draws an NSImage with a given size into a SkBitmap. |
[email protected] | 947e336 | 2013-11-20 11:06:47 | [diff] [blame] | 81 | SK_API SkBitmap NSImageToSkBitmapWithColorSpace(NSImage* image, |
| 82 | bool is_opaque, |
| 83 | CGColorSpaceRef color_space); |
[email protected] | 3194e1ed | 2009-06-25 18:16:39 | [diff] [blame] | 84 | |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 85 | // Draws an NSImageRep with a given size into a SkBitmap. |
[email protected] | 947e336 | 2013-11-20 11:06:47 | [diff] [blame] | 86 | SK_API SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image, |
| 87 | NSSize size, |
| 88 | bool is_opaque, |
| 89 | CGColorSpaceRef colorspace); |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 90 | |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 91 | // Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic |
| 92 | // color space. |
| 93 | SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image); |
| 94 | |
[email protected] | bbfb462 | 2012-08-21 17:59:05 | [diff] [blame] | 95 | SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace( |
| 96 | const SkBitmap& skiaBitmap, |
| 97 | CGColorSpaceRef colorSpace); |
| 98 | |
[email protected] | 695041b3 | 2011-03-03 04:17:02 | [diff] [blame] | 99 | // Given an SkBitmap and a color space, return an autoreleased NSImage. |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 100 | SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, |
| 101 | CGColorSpaceRef colorSpace); |
[email protected] | 695041b3 | 2011-03-03 04:17:02 | [diff] [blame] | 102 | |
| 103 | // Given an SkBitmap, return an autoreleased NSImage in the generic color space. |
| 104 | // DEPRECATED, use SkBitmapToNSImageWithColorSpace() instead. |
| 105 | // TODO(thakis): Remove this -- http://crbug.com/69432 |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 106 | SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon); |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 107 | |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 108 | // Converts a SkCanvas temporarily to a CGContext |
[email protected] | 0dbe4a0 | 2012-03-09 20:45:26 | [diff] [blame] | 109 | class SK_API SkiaBitLocker { |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 110 | public: |
ccameron | 74565e7 | 2014-09-30 19:59:16 | [diff] [blame^] | 111 | // TODO(ccameron): delete this constructor |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 112 | explicit SkiaBitLocker(SkCanvas* canvas); |
ccameron | 74565e7 | 2014-09-30 19:59:16 | [diff] [blame^] | 113 | SkiaBitLocker(SkCanvas* canvas, |
| 114 | const SkIRect& userClipRect, |
| 115 | SkScalar bitmapScaleFactor = 1); |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 116 | ~SkiaBitLocker(); |
| 117 | CGContextRef cgContext(); |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 118 | bool hasEmptyClipRegion() const; |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | void releaseIfNeeded(); |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 122 | SkIRect computeDirtyRect(); |
| 123 | |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 124 | SkCanvas* canvas_; |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 125 | |
| 126 | // If the user specified a clip rect it would draw into then the locker may |
| 127 | // skip the step of searching for a rect bounding the pixels that the user |
| 128 | // has drawn into. |
| 129 | bool userClipRectSpecified_; |
| 130 | |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 131 | CGContextRef cgContext_; |
[email protected] | b8c84c0 | 2012-04-11 17:37:17 | [diff] [blame] | 132 | SkBitmap bitmap_; |
[email protected] | 60cbaaa1 | 2014-06-20 05:23:08 | [diff] [blame] | 133 | SkIPoint bitmapOffset_; |
ccameron | 74565e7 | 2014-09-30 19:59:16 | [diff] [blame^] | 134 | SkScalar bitmapScaleFactor_; |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 135 | |
| 136 | // True if we are drawing to |canvas_|'s SkBaseDevice's bits directly through |
| 137 | // |bitmap_|. Otherwise, the bits in |bitmap_| are our allocation and need to |
| 138 | // be copied over to |canvas_|. |
[email protected] | 2960adf | 2014-05-13 01:53:50 | [diff] [blame] | 139 | bool useDeviceBits_; |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 140 | |
| 141 | // True if |bitmap_| is a dummy 1x1 bitmap allocated for the sake of creating |
| 142 | // a non-NULL CGContext (it is invalid to use a NULL CGContext), and will not |
| 143 | // be copied to |canvas_|. This will happen if |canvas_|'s clip region is |
| 144 | // empty. |
| 145 | bool bitmapIsDummy_; |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 149 | } // namespace gfx |
| 150 | |
[email protected] | 24a14e8 | 2008-12-03 20:26:51 | [diff] [blame] | 151 | #endif // SKIA_EXT_SKIA_UTILS_MAC_H_ |