blob: b60f9e8ad534d4601dd61deb7548df9f7e884ff3 [file] [log] [blame]
[email protected]0dbe4a02012-03-09 20:45:261// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]247e2472008-08-20 19:34:304
[email protected]24a14e82008-12-03 20:26:515#ifndef SKIA_EXT_SKIA_UTILS_MAC_H_
6#define SKIA_EXT_SKIA_UTILS_MAC_H_
[email protected]247e2472008-08-20 19:34:307
[email protected]dc454a7c2011-08-12 22:01:138#include <ApplicationServices/ApplicationServices.h>
[email protected]7b9cf722011-04-27 20:50:529#include <vector>
[email protected]247e2472008-08-20 19:34:3010
[email protected]b8c84c02012-04-11 17:37:1711#include "third_party/skia/include/core/SkBitmap.h"
[email protected]c43c6682009-05-19 14:51:4412#include "third_party/skia/include/core/SkColor.h"
13
[email protected]247e2472008-08-20 19:34:3014struct SkIRect;
15struct SkPoint;
16struct SkRect;
[email protected]02c48b02011-05-17 23:06:1117class SkCanvas;
[email protected]227fcee2010-06-11 19:19:3718class SkMatrix;
[email protected]bcdab962010-05-14 20:57:5019#ifdef __LP64__
20typedef CGSize NSSize;
21#else
[email protected]09c6dec2009-06-16 19:57:2222typedef struct _NSSize NSSize;
[email protected]bcdab962010-05-14 20:57:5023#endif
[email protected]09c6dec2009-06-16 19:57:2224
25#ifdef __OBJC__
[email protected]35d53342012-05-16 21:30:1226@class NSBitmapImageRep;
[email protected]09c6dec2009-06-16 19:57:2227@class NSImage;
[email protected]7b9cf722011-04-27 20:50:5228@class NSImageRep;
[email protected]c3217be52011-06-07 21:14:0329@class NSColor;
[email protected]f6b503392010-10-21 14:41:3230#else
[email protected]35d53342012-05-16 21:30:1231class NSBitmapImageRep;
[email protected]f6b503392010-10-21 14:41:3232class NSImage;
[email protected]7b9cf722011-04-27 20:50:5233class NSImageRep;
[email protected]c3217be52011-06-07 21:14:0334class NSColor;
[email protected]09c6dec2009-06-16 19:57:2235#endif
[email protected]247e2472008-08-20 19:34:3036
37namespace gfx {
38
39// Converts a Skia point to a CoreGraphics CGPoint.
40// Both use same in-memory format.
41inline 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.
47inline const SkPoint& CGPointToSkPoint(const CGPoint& point) {
48 return reinterpret_cast<const SkPoint&>(point);
49}
[email protected]dc4f63c2008-10-01 21:40:2850
51// Matrix converters.
[email protected]0dbe4a02012-03-09 20:45:2652SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix);
[email protected]24a14e82008-12-03 20:26:5153
[email protected]247e2472008-08-20 19:34:3054// Rectangle converters.
[email protected]19d7d702013-01-25 05:08:0955SK_API SkRect CGRectToSkRect(const CGRect& rect);
[email protected]247e2472008-08-20 19:34:3056
57// Converts a Skia rect to a CoreGraphics CGRect.
58CGRect SkIRectToCGRect(const SkIRect& rect);
59CGRect SkRectToCGRect(const SkRect& rect);
60
61// Converts CGColorRef to the ARGB layout Skia expects.
[email protected]04a07542012-07-23 16:51:2562SK_API SkColor CGColorRefToSkColor(CGColorRef color);
[email protected]247e2472008-08-20 19:34:3063
64// Converts ARGB to CGColorRef.
[email protected]7e7a0042012-07-23 21:17:3065SK_API CGColorRef CGColorCreateFromSkColor(SkColor color);
[email protected]247e2472008-08-20 19:34:3066
[email protected]da8543762012-03-20 08:52:2067// Converts NSColor to ARGB. Returns raw rgb values and does no colorspace
68// conversion. Only valid for colors in calibrated and device color spaces.
69SK_API SkColor NSDeviceColorToSkColor(NSColor* color);
70
[email protected]e5f893a2013-06-28 05:27:4371// Converts ARGB in the specified color space to NSColor.
72// Prefer sRGB over calibrated colors.
[email protected]41c685d2011-08-02 05:24:3773SK_API NSColor* SkColorToCalibratedNSColor(SkColor color);
[email protected]da8543762012-03-20 08:52:2074SK_API NSColor* SkColorToDeviceNSColor(SkColor color);
[email protected]e5f893a2013-06-28 05:27:4375SK_API NSColor* SkColorToSRGBNSColor(SkColor color);
[email protected]c3217be52011-06-07 21:14:0376
[email protected]09c6dec2009-06-16 19:57:2277// Converts a CGImage to a SkBitmap.
[email protected]41c685d2011-08-02 05:24:3778SK_API SkBitmap CGImageToSkBitmap(CGImageRef image);
[email protected]09c6dec2009-06-16 19:57:2279
[email protected]09c6dec2009-06-16 19:57:2280// Draws an NSImage with a given size into a SkBitmap.
[email protected]947e3362013-11-20 11:06:4781SK_API SkBitmap NSImageToSkBitmapWithColorSpace(NSImage* image,
82 bool is_opaque,
83 CGColorSpaceRef color_space);
[email protected]3194e1ed2009-06-25 18:16:3984
[email protected]7b9cf722011-04-27 20:50:5285// Draws an NSImageRep with a given size into a SkBitmap.
[email protected]947e3362013-11-20 11:06:4786SK_API SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image,
87 NSSize size,
88 bool is_opaque,
89 CGColorSpaceRef colorspace);
[email protected]7b9cf722011-04-27 20:50:5290
[email protected]35d53342012-05-16 21:30:1291// Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic
92// color space.
93SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image);
94
[email protected]bbfb4622012-08-21 17:59:0595SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace(
96 const SkBitmap& skiaBitmap,
97 CGColorSpaceRef colorSpace);
98
[email protected]695041b32011-03-03 04:17:0299// Given an SkBitmap and a color space, return an autoreleased NSImage.
[email protected]41c685d2011-08-02 05:24:37100SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon,
101 CGColorSpaceRef colorSpace);
[email protected]695041b32011-03-03 04:17:02102
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]41c685d2011-08-02 05:24:37106SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon);
[email protected]09c6dec2009-06-16 19:57:22107
[email protected]02c48b02011-05-17 23:06:11108// Converts a SkCanvas temporarily to a CGContext
[email protected]0dbe4a02012-03-09 20:45:26109class SK_API SkiaBitLocker {
[email protected]02c48b02011-05-17 23:06:11110 public:
ccameron74565e72014-09-30 19:59:16111 // TODO(ccameron): delete this constructor
[email protected]02c48b02011-05-17 23:06:11112 explicit SkiaBitLocker(SkCanvas* canvas);
ccameron74565e72014-09-30 19:59:16113 SkiaBitLocker(SkCanvas* canvas,
114 const SkIRect& userClipRect,
115 SkScalar bitmapScaleFactor = 1);
[email protected]02c48b02011-05-17 23:06:11116 ~SkiaBitLocker();
117 CGContextRef cgContext();
[email protected]b35234132014-08-21 23:05:46118 bool hasEmptyClipRegion() const;
[email protected]02c48b02011-05-17 23:06:11119
120 private:
121 void releaseIfNeeded();
[email protected]b35234132014-08-21 23:05:46122 SkIRect computeDirtyRect();
123
[email protected]02c48b02011-05-17 23:06:11124 SkCanvas* canvas_;
[email protected]b35234132014-08-21 23:05:46125
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]02c48b02011-05-17 23:06:11131 CGContextRef cgContext_;
[email protected]b8c84c02012-04-11 17:37:17132 SkBitmap bitmap_;
[email protected]60cbaaa12014-06-20 05:23:08133 SkIPoint bitmapOffset_;
ccameron74565e72014-09-30 19:59:16134 SkScalar bitmapScaleFactor_;
[email protected]b35234132014-08-21 23:05:46135
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]2960adf2014-05-13 01:53:50139 bool useDeviceBits_;
[email protected]b35234132014-08-21 23:05:46140
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]02c48b02011-05-17 23:06:11146};
147
148
[email protected]247e2472008-08-20 19:34:30149} // namespace gfx
150
[email protected]24a14e82008-12-03 20:26:51151#endif // SKIA_EXT_SKIA_UTILS_MAC_H_