[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" |
tomhudson | 4a207e61 | 2015-11-05 23:06:36 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkPixmap.h" |
[email protected] | c43c668 | 2009-05-19 14:51:44 | [diff] [blame] | 14 | |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 15 | struct SkIRect; |
| 16 | struct SkPoint; |
| 17 | struct SkRect; |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 18 | class SkCanvas; |
[email protected] | 227fcee | 2010-06-11 19:19:37 | [diff] [blame] | 19 | class SkMatrix; |
[email protected] | bcdab96 | 2010-05-14 20:57:50 | [diff] [blame] | 20 | #ifdef __LP64__ |
| 21 | typedef CGSize NSSize; |
| 22 | #else |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 23 | typedef struct _NSSize NSSize; |
[email protected] | bcdab96 | 2010-05-14 20:57:50 | [diff] [blame] | 24 | #endif |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 25 | |
| 26 | #ifdef __OBJC__ |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 27 | @class NSBitmapImageRep; |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 28 | @class NSImage; |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 29 | @class NSImageRep; |
[email protected] | c3217be5 | 2011-06-07 21:14:03 | [diff] [blame] | 30 | @class NSColor; |
[email protected] | f6b50339 | 2010-10-21 14:41:32 | [diff] [blame] | 31 | #else |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 32 | class NSBitmapImageRep; |
[email protected] | f6b50339 | 2010-10-21 14:41:32 | [diff] [blame] | 33 | class NSImage; |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 34 | class NSImageRep; |
[email protected] | c3217be5 | 2011-06-07 21:14:03 | [diff] [blame] | 35 | class NSColor; |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 36 | #endif |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 37 | |
scroggo | 66142aeb | 2015-12-10 19:39:26 | [diff] [blame] | 38 | namespace skia { |
[email protected] | dc4f63c | 2008-10-01 21:40:28 | [diff] [blame] | 39 | |
| 40 | // Matrix converters. |
[email protected] | 0dbe4a0 | 2012-03-09 20:45:26 | [diff] [blame] | 41 | SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix); |
[email protected] | 24a14e8 | 2008-12-03 20:26:51 | [diff] [blame] | 42 | |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 43 | // Rectangle converters. |
[email protected] | 19d7d70 | 2013-01-25 05:08:09 | [diff] [blame] | 44 | SK_API SkRect CGRectToSkRect(const CGRect& rect); |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 45 | |
| 46 | // Converts a Skia rect to a CoreGraphics CGRect. |
| 47 | CGRect SkIRectToCGRect(const SkIRect& rect); |
| 48 | CGRect SkRectToCGRect(const SkRect& rect); |
| 49 | |
| 50 | // Converts CGColorRef to the ARGB layout Skia expects. |
[email protected] | 04a0754 | 2012-07-23 16:51:25 | [diff] [blame] | 51 | SK_API SkColor CGColorRefToSkColor(CGColorRef color); |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 52 | |
| 53 | // Converts ARGB to CGColorRef. |
[email protected] | 7e7a004 | 2012-07-23 21:17:30 | [diff] [blame] | 54 | SK_API CGColorRef CGColorCreateFromSkColor(SkColor color); |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 55 | |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 56 | // Converts NSColor to ARGB. Returns raw rgb values and does no colorspace |
| 57 | // conversion. Only valid for colors in calibrated and device color spaces. |
| 58 | SK_API SkColor NSDeviceColorToSkColor(NSColor* color); |
| 59 | |
[email protected] | e5f893a | 2013-06-28 05:27:43 | [diff] [blame] | 60 | // Converts ARGB in the specified color space to NSColor. |
| 61 | // Prefer sRGB over calibrated colors. |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 62 | SK_API NSColor* SkColorToCalibratedNSColor(SkColor color); |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 63 | SK_API NSColor* SkColorToDeviceNSColor(SkColor color); |
[email protected] | e5f893a | 2013-06-28 05:27:43 | [diff] [blame] | 64 | SK_API NSColor* SkColorToSRGBNSColor(SkColor color); |
[email protected] | c3217be5 | 2011-06-07 21:14:03 | [diff] [blame] | 65 | |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 66 | // Converts a CGImage to a SkBitmap. |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 67 | SK_API SkBitmap CGImageToSkBitmap(CGImageRef image); |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 68 | |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 69 | // Draws an NSImage with a given size into a SkBitmap. |
[email protected] | 947e336 | 2013-11-20 11:06:47 | [diff] [blame] | 70 | SK_API SkBitmap NSImageToSkBitmapWithColorSpace(NSImage* image, |
| 71 | bool is_opaque, |
| 72 | CGColorSpaceRef color_space); |
[email protected] | 3194e1ed | 2009-06-25 18:16:39 | [diff] [blame] | 73 | |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 74 | // Draws an NSImageRep with a given size into a SkBitmap. |
[email protected] | 947e336 | 2013-11-20 11:06:47 | [diff] [blame] | 75 | SK_API SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image, |
| 76 | NSSize size, |
| 77 | bool is_opaque, |
| 78 | CGColorSpaceRef colorspace); |
[email protected] | 7b9cf72 | 2011-04-27 20:50:52 | [diff] [blame] | 79 | |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 80 | // Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic |
| 81 | // color space. |
| 82 | SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image); |
| 83 | |
[email protected] | bbfb462 | 2012-08-21 17:59:05 | [diff] [blame] | 84 | SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace( |
| 85 | const SkBitmap& skiaBitmap, |
| 86 | CGColorSpaceRef colorSpace); |
| 87 | |
[email protected] | 695041b3 | 2011-03-03 04:17:02 | [diff] [blame] | 88 | // Given an SkBitmap and a color space, return an autoreleased NSImage. |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 89 | SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, |
| 90 | CGColorSpaceRef colorSpace); |
[email protected] | 695041b3 | 2011-03-03 04:17:02 | [diff] [blame] | 91 | |
| 92 | // Given an SkBitmap, return an autoreleased NSImage in the generic color space. |
| 93 | // DEPRECATED, use SkBitmapToNSImageWithColorSpace() instead. |
| 94 | // TODO(thakis): Remove this -- http://crbug.com/69432 |
[email protected] | 41c685d | 2011-08-02 05:24:37 | [diff] [blame] | 95 | SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon); |
[email protected] | 09c6dec | 2009-06-16 19:57:22 | [diff] [blame] | 96 | |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 97 | // Converts a SkCanvas temporarily to a CGContext |
[email protected] | 0dbe4a0 | 2012-03-09 20:45:26 | [diff] [blame] | 98 | class SK_API SkiaBitLocker { |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 99 | public: |
tomhudson | ad6f9da | 2016-06-08 19:17:29 | [diff] [blame] | 100 | /** |
| 101 | User clip rect is an *additional* clip to be applied in addition to the |
| 102 | current state of the canvas, in *local* rather than device coordinates. |
| 103 | If no additional clipping is desired, pass in |
| 104 | SkIRect::MakeSize(canvas->getBaseLayerSize()) transformed by the inverse |
| 105 | CTM. |
| 106 | */ |
ccameron | 74565e7 | 2014-09-30 19:59:16 | [diff] [blame] | 107 | SkiaBitLocker(SkCanvas* canvas, |
| 108 | const SkIRect& userClipRect, |
| 109 | SkScalar bitmapScaleFactor = 1); |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 110 | ~SkiaBitLocker(); |
| 111 | CGContextRef cgContext(); |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 112 | bool hasEmptyClipRegion() const; |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | void releaseIfNeeded(); |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 116 | SkIRect computeDirtyRect(); |
| 117 | |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 118 | SkCanvas* canvas_; |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 119 | |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 120 | CGContextRef cgContext_; |
tomhudson | 4a207e61 | 2015-11-05 23:06:36 | [diff] [blame] | 121 | // offscreen_ is only valid if useDeviceBits_ is false |
| 122 | SkBitmap offscreen_; |
[email protected] | 60cbaaa1 | 2014-06-20 05:23:08 | [diff] [blame] | 123 | SkIPoint bitmapOffset_; |
ccameron | 74565e7 | 2014-09-30 19:59:16 | [diff] [blame] | 124 | SkScalar bitmapScaleFactor_; |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 125 | |
tomhudson | 4a207e61 | 2015-11-05 23:06:36 | [diff] [blame] | 126 | // True if we are drawing to |canvas_|'s backing store directly. |
| 127 | // Otherwise, the bits in |bitmap_| are our allocation and need to |
reed | 027a9fc | 2015-06-15 20:39:51 | [diff] [blame] | 128 | // be copied over to |canvas_|. |
| 129 | bool useDeviceBits_; |
| 130 | |
[email protected] | b3523413 | 2014-08-21 23:05:46 | [diff] [blame] | 131 | // True if |bitmap_| is a dummy 1x1 bitmap allocated for the sake of creating |
| 132 | // a non-NULL CGContext (it is invalid to use a NULL CGContext), and will not |
| 133 | // be copied to |canvas_|. This will happen if |canvas_|'s clip region is |
| 134 | // empty. |
| 135 | bool bitmapIsDummy_; |
[email protected] | 02c48b0 | 2011-05-17 23:06:11 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | |
scroggo | 66142aeb | 2015-12-10 19:39:26 | [diff] [blame] | 139 | } // namespace skia |
[email protected] | 247e247 | 2008-08-20 19:34:30 | [diff] [blame] | 140 | |
[email protected] | 24a14e8 | 2008-12-03 20:26:51 | [diff] [blame] | 141 | #endif // SKIA_EXT_SKIA_UTILS_MAC_H_ |