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. 4 5 #ifndef SKIA_EXT_SKIA_UTILS_MAC_H_ 6 #define SKIA_EXT_SKIA_UTILS_MAC_H_ 7 8 #include <ApplicationServices/ApplicationServices.h> 9 #include <vector> 10 11 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkColor.h" 13 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 24 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 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 } 50 51 // Matrix converters. 52 SK_API CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix); 53 54 // Rectangle converters. 55 SK_API SkRect CGRectToSkRect(const CGRect& rect); 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. 62 SK_API SkColor CGColorRefToSkColor(CGColorRef color); 63 64 // Converts ARGB to CGColorRef. 65 SK_API CGColorRef CGColorCreateFromSkColor(SkColor color); 66 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 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); 76 77 // Converts a CGImage to a SkBitmap. 78 SK_API SkBitmap CGImageToSkBitmap(CGImageRef image); 79 80 // Draws an NSImage with a given size into a SkBitmap. 81 SK_API SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque); 82 83 // Draws an NSImageRep with a given size into a SkBitmap. 84 SK_API SkBitmap NSImageRepToSkBitmap( 85 NSImageRep* image, NSSize size, bool is_opaque); 86 87 // Given an SkBitmap, return an autoreleased NSBitmapImageRep in the generic 88 // color space. 89 SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& image); 90 91 SK_API NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace( 92 const SkBitmap& skiaBitmap, 93 CGColorSpaceRef colorSpace); 94 95 // Given an SkBitmap and a color space, return an autoreleased NSImage. 96 SK_API NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& icon, 97 CGColorSpaceRef colorSpace); 98 99 // Given an SkBitmap, return an autoreleased NSImage in the generic color space. 100 // DEPRECATED, use SkBitmapToNSImageWithColorSpace() instead. 101 // TODO(thakis): Remove this -- http://crbug.com/69432 102 SK_API NSImage* SkBitmapToNSImage(const SkBitmap& icon); 103 104 // Converts a SkCanvas temporarily to a CGContext 105 class SK_API SkiaBitLocker { 106 public: 107 explicit SkiaBitLocker(SkCanvas* canvas); 108 ~SkiaBitLocker(); 109 CGContextRef cgContext(); 110 111 private: 112 void releaseIfNeeded(); 113 SkCanvas* canvas_; 114 CGContextRef cgContext_; 115 SkBitmap bitmap_; 116 bool useDeviceBits_; 117 }; 118 119 120 } // namespace gfx 121 122 #endif // SKIA_EXT_SKIA_UTILS_MAC_H_ 123