blob: e88c1100076be4033711eb53159ef629d15def3b [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"
tomhudson4a207e612015-11-05 23:06:3613#include "third_party/skia/include/core/SkPixmap.h"
[email protected]c43c6682009-05-19 14:51:4414
[email protected]247e2472008-08-20 19:34:3015struct SkIRect;
16struct SkPoint;
17struct SkRect;
[email protected]02c48b02011-05-17 23:06:1118class SkCanvas;
[email protected]227fcee2010-06-11 19:19:3719class SkMatrix;
[email protected]bcdab962010-05-14 20:57:5020#ifdef __LP64__
21typedef CGSize NSSize;
22#else
[email protected]09c6dec2009-06-16 19:57:2223typedef struct _NSSize NSSize;
[email protected]bcdab962010-05-14 20:57:5024#endif
[email protected]09c6dec2009-06-16 19:57:2225
26#ifdef __OBJC__
[email protected]35d53342012-05-16 21:30:1227@class NSBitmapImageRep;
[email protected]09c6dec2009-06-16 19:57:2228@class NSImage;
[email protected]7b9cf722011-04-27 20:50:5229@class NSImageRep;
[email protected]c3217be52011-06-07 21:14:0330@class NSColor;
[email protected]f6b503392010-10-21 14:41:3231#else
[email protected]35d53342012-05-16 21:30:1232class NSBitmapImageRep;
[email protected]f6b503392010-10-21 14:41:3233class NSImage;
[email protected]7b9cf722011-04-27 20:50:5234class NSImageRep;
[email protected]c3217be52011-06-07 21:14:0335class NSColor;
[email protected]09c6dec2009-06-16 19:57:2236#endif
[email protected]247e2472008-08-20 19:34:3037
scroggo66142aeb2015-12-10 19:39:2638namespace skia {
[email protected]dc4f63c2008-10-01 21:40:2839
40// Matrix converters.
[email protected]0dbe4a02012-03-09 20:45:2641SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix);
[email protected]24a14e82008-12-03 20:26:5142
[email protected]247e2472008-08-20 19:34:3043// Rectangle converters.
[email protected]19d7d702013-01-25 05:08:0944SK_API SkRect CGRectToSkRect(const CGRect& rect);
[email protected]247e2472008-08-20 19:34:3045
46// Converts a Skia rect to a CoreGraphics CGRect.
47CGRect SkIRectToCGRect(const SkIRect& rect);
48CGRect SkRectToCGRect(const SkRect& rect);
49
50// Converts CGColorRef to the ARGB layout Skia expects.
[email protected]04a07542012-07-23 16:51:2551SK_API SkColor CGColorRefToSkColor(CGColorRef color);
[email protected]247e2472008-08-20 19:34:3052
53// Converts ARGB to CGColorRef.
[email protected]7e7a0042012-07-23 21:17:3054SK_API CGColorRef CGColorCreateFromSkColor(SkColor color);
[email protected]247e2472008-08-20 19:34:3055
[email protected]da8543762012-03-20 08:52:2056// Converts NSColor to ARGB. Returns raw rgb values and does no colorspace
57// conversion. Only valid for colors in calibrated and device color spaces.
58SK_API SkColor NSDeviceColorToSkColor(NSColor* color);
59
[email protected]e5f893a2013-06-28 05:27:4360// Converts ARGB in the specified color space to NSColor.
61// Prefer sRGB over calibrated colors.
[email protected]41c685d2011-08-02 05:24:3762SK_API NSColor* SkColorToCalibratedNSColor(SkColor color);
[email protected]da8543762012-03-20 08:52:2063SK_API NSColor* SkColorToDeviceNSColor(SkColor color);
[email protected]e5f893a2013-06-28 05:27:4364SK_API NSColor* SkColorToSRGBNSColor(SkColor color);
[email protected]c3217be52011-06-07 21:14:0365
[email protected]09c6dec2009-06-16 19:57:2266// Converts a CGImage to a SkBitmap.
[email protected]41c685d2011-08-02 05:24:3767SK_API SkBitmap CGImageToSkBitmap(CGImageRef image);
[email protected]09c6dec2009-06-16 19:57:2268
[email protected]09c6dec2009-06-16 19:57:2269// Draws an NSImage with a given size into a SkBitmap.
[email protected]947e3362013-11-20 11:06:4770SK_API SkBitmap NSImageToSkBitmapWithColorSpace(NSImage* image,
71 bool is_opaque,
72 CGColorSpaceRef color_space);
[email protected]3194e1ed2009-06-25 18:16:3973
[email protected]7b9cf722011-04-27 20:50:5274// Draws an NSImageRep with a given size into a SkBitmap.
[email protected]947e3362013-11-20 11:06:4775SK_API SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image,
76 NSSize size,
77 bool is_opaque,
78 CGColorSpaceRef colorspace);
[email protected]7b9cf722011-04-27 20:50:5279
[email protected]35d53342012-05-16 21:30:1280// Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic
81// color space.
82SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image);
83
[email protected]bbfb4622012-08-21 17:59:0584SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace(
85 const SkBitmap& skiaBitmap,
86 CGColorSpaceRef colorSpace);
87
[email protected]695041b32011-03-03 04:17:0288// Given an SkBitmap and a color space, return an autoreleased NSImage.
[email protected]41c685d2011-08-02 05:24:3789SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon,
90 CGColorSpaceRef colorSpace);
[email protected]695041b32011-03-03 04:17:0291
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]41c685d2011-08-02 05:24:3795SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon);
[email protected]09c6dec2009-06-16 19:57:2296
[email protected]02c48b02011-05-17 23:06:1197// Converts a SkCanvas temporarily to a CGContext
[email protected]0dbe4a02012-03-09 20:45:2698class SK_API SkiaBitLocker {
[email protected]02c48b02011-05-17 23:06:1199 public:
tomhudsonad6f9da2016-06-08 19:17:29100 /**
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 */
ccameron74565e72014-09-30 19:59:16107 SkiaBitLocker(SkCanvas* canvas,
108 const SkIRect& userClipRect,
109 SkScalar bitmapScaleFactor = 1);
[email protected]02c48b02011-05-17 23:06:11110 ~SkiaBitLocker();
111 CGContextRef cgContext();
[email protected]b35234132014-08-21 23:05:46112 bool hasEmptyClipRegion() const;
[email protected]02c48b02011-05-17 23:06:11113
114 private:
115 void releaseIfNeeded();
[email protected]b35234132014-08-21 23:05:46116 SkIRect computeDirtyRect();
117
[email protected]02c48b02011-05-17 23:06:11118 SkCanvas* canvas_;
[email protected]b35234132014-08-21 23:05:46119
[email protected]02c48b02011-05-17 23:06:11120 CGContextRef cgContext_;
tomhudson4a207e612015-11-05 23:06:36121 // offscreen_ is only valid if useDeviceBits_ is false
122 SkBitmap offscreen_;
[email protected]60cbaaa12014-06-20 05:23:08123 SkIPoint bitmapOffset_;
ccameron74565e72014-09-30 19:59:16124 SkScalar bitmapScaleFactor_;
[email protected]b35234132014-08-21 23:05:46125
tomhudson4a207e612015-11-05 23:06:36126 // True if we are drawing to |canvas_|'s backing store directly.
127 // Otherwise, the bits in |bitmap_| are our allocation and need to
reed027a9fc2015-06-15 20:39:51128 // be copied over to |canvas_|.
129 bool useDeviceBits_;
130
[email protected]b35234132014-08-21 23:05:46131 // 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]02c48b02011-05-17 23:06:11136};
137
138
scroggo66142aeb2015-12-10 19:39:26139} // namespace skia
[email protected]247e2472008-08-20 19:34:30140
[email protected]24a14e82008-12-03 20:26:51141#endif // SKIA_EXT_SKIA_UTILS_MAC_H_