Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / skia / ext / skia_utils_mac.h
blobb60f9e8ad534d4601dd61deb7548df9f7e884ff3
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
3 // found in the LICENSE file.
5 #ifndef SKIA_EXT_SKIA_UTILS_MAC_H_
6 #define SKIA_EXT_SKIA_UTILS_MAC_H_
8 #include <ApplicationServices/ApplicationServices.h>
9 #include <vector>
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkColor.h"
14 struct SkIRect;
15 struct SkPoint;
16 struct SkRect;
17 class SkCanvas;
18 class SkMatrix;
19 #ifdef __LP64__
20 typedef CGSize NSSize;
21 #else
22 typedef struct _NSSize NSSize;
23 #endif
25 #ifdef __OBJC__
26 @class NSBitmapImageRep;
27 @class NSImage;
28 @class NSImageRep;
29 @class NSColor;
30 #else
31 class NSBitmapImageRep;
32 class NSImage;
33 class NSImageRep;
34 class NSColor;
35 #endif
37 namespace gfx {
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);
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);
51 // Matrix converters.
52 SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix);
54 // Rectangle converters.
55 SK_API SkRect CGRectToSkRect(const CGRect& rect);
57 // Converts a Skia rect to a CoreGraphics CGRect.
58 CGRect SkIRectToCGRect(const SkIRect& rect);
59 CGRect SkRectToCGRect(const SkRect& rect);
61 // Converts CGColorRef to the ARGB layout Skia expects.
62 SK_API SkColor CGColorRefToSkColor(CGColorRef color);
64 // Converts ARGB to CGColorRef.
65 SK_API CGColorRef CGColorCreateFromSkColor(SkColor color);
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);
71 // Converts ARGB in the specified color space to NSColor.
72 // Prefer sRGB over calibrated colors.
73 SK_API NSColor* SkColorToCalibratedNSColor(SkColor color);
74 SK_API NSColor* SkColorToDeviceNSColor(SkColor color);
75 SK_API NSColor* SkColorToSRGBNSColor(SkColor color);
77 // Converts a CGImage to a SkBitmap.
78 SK_API SkBitmap CGImageToSkBitmap(CGImageRef image);
80 // Draws an NSImage with a given size into a SkBitmap.
81 SK_API SkBitmap NSImageToSkBitmapWithColorSpace(NSImage* image,
82 bool is_opaque,
83 CGColorSpaceRef color_space);
85 // Draws an NSImageRep with a given size into a SkBitmap.
86 SK_API SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image,
87 NSSize size,
88 bool is_opaque,
89 CGColorSpaceRef colorspace);
91 // Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic
92 // color space.
93 SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image);
95 SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace(
96 const SkBitmap& skiaBitmap,
97 CGColorSpaceRef colorSpace);
99 // Given an SkBitmap and a color space, return an autoreleased NSImage.
100 SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon,
101 CGColorSpaceRef colorSpace);
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
106 SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon);
108 // Converts a SkCanvas temporarily to a CGContext
109 class SK_API SkiaBitLocker {
110 public:
111 // TODO(ccameron): delete this constructor
112 explicit SkiaBitLocker(SkCanvas* canvas);
113 SkiaBitLocker(SkCanvas* canvas,
114 const SkIRect& userClipRect,
115 SkScalar bitmapScaleFactor = 1);
116 ~SkiaBitLocker();
117 CGContextRef cgContext();
118 bool hasEmptyClipRegion() const;
120 private:
121 void releaseIfNeeded();
122 SkIRect computeDirtyRect();
124 SkCanvas* canvas_;
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_;
131 CGContextRef cgContext_;
132 SkBitmap bitmap_;
133 SkIPoint bitmapOffset_;
134 SkScalar bitmapScaleFactor_;
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_|.
139 bool useDeviceBits_;
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_;
149 } // namespace gfx
151 #endif // SKIA_EXT_SKIA_UTILS_MAC_H_