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 #include "skia/ext/skia_utils_mac.h"
7 #import <AppKit/AppKit.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "skia/ext/bitmap_platform_device_mac.h"
14 #include "third_party/skia/include/core/SkRegion.h"
15 #include "third_party/skia/include/utils/mac/SkCGUtils.h"
19 // Draws an NSImage or an NSImageRep with a given size into a SkBitmap.
20 SkBitmap NSImageOrNSImageRepToSkBitmapWithColorSpace(
22 NSImageRep* image_rep,
25 CGColorSpaceRef color_space) {
26 // Only image or image_rep should be provided, not both.
27 DCHECK((image != 0) ^ (image_rep != 0));
30 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
34 is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
36 if (!bitmap.allocPixels())
37 return bitmap; // Return |bitmap| which should respond true to isNull().
40 void* data = bitmap.getPixels();
42 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
43 // recommends these flags for improved CG performance.
44 #define HAS_ARGB_SHIFTS(a, r, g, b) \
45 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
46 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
47 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
48 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
55 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
57 #error We require that Skia's and CoreGraphics's recommended \
58 image memory layout match.
60 #undef HAS_ARGB_SHIFTS
62 // Something went really wrong. Best guess is that the bitmap data is invalid.
65 [NSGraphicsContext saveGraphicsState];
67 NSGraphicsContext* context_cocoa =
68 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
69 [NSGraphicsContext setCurrentContext:context_cocoa];
71 NSRect drawRect = NSMakeRect(0, 0, size.width, size.height);
73 [image drawInRect:drawRect
75 operation:NSCompositeCopy
78 [image_rep drawInRect:drawRect
80 operation:NSCompositeCopy
86 [NSGraphicsContext restoreGraphicsState];
95 CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) {
96 // CGAffineTransforms don't support perspective transforms, so make sure
97 // we don't get those.
98 DCHECK(matrix[SkMatrix::kMPersp0] == 0.0f);
99 DCHECK(matrix[SkMatrix::kMPersp1] == 0.0f);
100 DCHECK(matrix[SkMatrix::kMPersp2] == 1.0f);
102 return CGAffineTransformMake(matrix[SkMatrix::kMScaleX],
103 matrix[SkMatrix::kMSkewY],
104 matrix[SkMatrix::kMSkewX],
105 matrix[SkMatrix::kMScaleY],
106 matrix[SkMatrix::kMTransX],
107 matrix[SkMatrix::kMTransY]);
110 SkRect CGRectToSkRect(const CGRect& rect) {
112 rect.origin.x, rect.origin.y, CGRectGetMaxX(rect), CGRectGetMaxY(rect)
117 CGRect SkIRectToCGRect(const SkIRect& rect) {
119 { rect.fLeft, rect.fTop },
120 { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop }
125 CGRect SkRectToCGRect(const SkRect& rect) {
127 { rect.fLeft, rect.fTop },
128 { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop }
133 // Converts CGColorRef to the ARGB layout Skia expects.
134 SkColor CGColorRefToSkColor(CGColorRef color) {
135 DCHECK(CGColorGetNumberOfComponents(color) == 4);
136 const CGFloat* components = CGColorGetComponents(color);
137 return SkColorSetARGB(SkScalarRoundToInt(255.0 * components[3]), // alpha
138 SkScalarRoundToInt(255.0 * components[0]), // red
139 SkScalarRoundToInt(255.0 * components[1]), // green
140 SkScalarRoundToInt(255.0 * components[2])); // blue
143 // Converts ARGB to CGColorRef.
144 CGColorRef CGColorCreateFromSkColor(SkColor color) {
145 return CGColorCreateGenericRGB(SkColorGetR(color) / 255.0,
146 SkColorGetG(color) / 255.0,
147 SkColorGetB(color) / 255.0,
148 SkColorGetA(color) / 255.0);
151 // Converts NSColor to ARGB
152 SkColor NSDeviceColorToSkColor(NSColor* color) {
153 DCHECK([color colorSpace] == [NSColorSpace genericRGBColorSpace] ||
154 [color colorSpace] == [NSColorSpace deviceRGBColorSpace]);
155 CGFloat red, green, blue, alpha;
156 color = [color colorUsingColorSpace:[NSColorSpace deviceRGBColorSpace]];
157 [color getRed:&red green:&green blue:&blue alpha:&alpha];
158 return SkColorSetARGB(SkScalarRoundToInt(255.0 * alpha),
159 SkScalarRoundToInt(255.0 * red),
160 SkScalarRoundToInt(255.0 * green),
161 SkScalarRoundToInt(255.0 * blue));
164 // Converts ARGB to NSColor.
165 NSColor* SkColorToCalibratedNSColor(SkColor color) {
166 return [NSColor colorWithCalibratedRed:SkColorGetR(color) / 255.0
167 green:SkColorGetG(color) / 255.0
168 blue:SkColorGetB(color) / 255.0
169 alpha:SkColorGetA(color) / 255.0];
172 NSColor* SkColorToDeviceNSColor(SkColor color) {
173 return [NSColor colorWithDeviceRed:SkColorGetR(color) / 255.0
174 green:SkColorGetG(color) / 255.0
175 blue:SkColorGetB(color) / 255.0
176 alpha:SkColorGetA(color) / 255.0];
179 NSColor* SkColorToSRGBNSColor(SkColor color) {
180 const CGFloat components[] = {
181 SkColorGetR(color) / 255.0,
182 SkColorGetG(color) / 255.0,
183 SkColorGetB(color) / 255.0,
184 SkColorGetA(color) / 255.0
186 return [NSColor colorWithColorSpace:[NSColorSpace sRGBColorSpace]
187 components:components
191 SkBitmap CGImageToSkBitmap(CGImageRef image) {
195 int width = CGImageGetWidth(image);
196 int height = CGImageGetHeight(image);
198 scoped_ptr<SkBaseDevice> device(
199 skia::BitmapPlatformDevice::Create(NULL, width, height, false));
201 CGContextRef context = skia::GetBitmapContext(device.get());
203 // We need to invert the y-axis of the canvas so that Core Graphics drawing
204 // happens right-side up. Skia has an upper-left origin and CG has a lower-
206 CGContextScaleCTM(context, 1.0, -1.0);
207 CGContextTranslateCTM(context, 0, -height);
209 // We want to copy transparent pixels from |image|, instead of blending it
210 // onto uninitialized pixels.
211 CGContextSetBlendMode(context, kCGBlendModeCopy);
213 CGRect rect = CGRectMake(0, 0, width, height);
214 CGContextDrawImage(context, rect, image);
216 // Because |device| will be cleaned up and will take its pixels with it, we
217 // copy it to the stack and return it.
218 SkBitmap bitmap = device->accessBitmap(false);
223 SkBitmap NSImageToSkBitmapWithColorSpace(
224 NSImage* image, bool is_opaque, CGColorSpaceRef color_space) {
225 return NSImageOrNSImageRepToSkBitmapWithColorSpace(
226 image, nil, [image size], is_opaque, color_space);
229 SkBitmap NSImageRepToSkBitmapWithColorSpace(NSImageRep* image_rep,
232 CGColorSpaceRef color_space) {
233 return NSImageOrNSImageRepToSkBitmapWithColorSpace(
234 nil, image_rep, size, is_opaque, color_space);
237 NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& skiaBitmap) {
238 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
239 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
240 return SkBitmapToNSBitmapImageRepWithColorSpace(skiaBitmap, color_space);
243 NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace(
244 const SkBitmap& skiaBitmap,
245 CGColorSpaceRef colorSpace) {
246 // First convert SkBitmap to CGImageRef.
247 base::ScopedCFTypeRef<CGImageRef> cgimage(
248 SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace));
250 // Now convert to NSBitmapImageRep.
251 base::scoped_nsobject<NSBitmapImageRep> bitmap(
252 [[NSBitmapImageRep alloc] initWithCGImage:cgimage]);
253 return [bitmap.release() autorelease];
256 NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap,
257 CGColorSpaceRef colorSpace) {
258 if (skiaBitmap.isNull())
261 base::scoped_nsobject<NSImage> image([[NSImage alloc] init]);
262 [image addRepresentation:
263 SkBitmapToNSBitmapImageRepWithColorSpace(skiaBitmap, colorSpace)];
264 [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())];
265 return [image.release() autorelease];
268 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) {
269 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
270 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
271 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get());
274 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas)
279 SkiaBitLocker::~SkiaBitLocker() {
283 // This must be called to balance calls to cgContext
284 void SkiaBitLocker::releaseIfNeeded() {
287 if (useDeviceBits_) {
288 bitmap_.unlockPixels();
290 // Find the bits that were drawn to.
291 SkAutoLockPixels lockedPixels(bitmap_);
292 const uint32_t* pixelBase
293 = reinterpret_cast<uint32_t*>(bitmap_.getPixels());
294 int rowPixels = bitmap_.rowBytesAsPixels();
295 int width = bitmap_.width();
296 int height = bitmap_.height();
301 const uint32_t* pixels = pixelBase;
302 while (++y < height) {
303 for (x = 0; x < width; ++x) {
312 bounds.fBottom = height;
314 pixels = pixelBase + rowPixels * (y - 1);
315 while (--y > bounds.fTop) {
316 for (x = 0; x < width; ++x) {
318 bounds.fBottom = y + 1;
327 while (++x < width) {
328 pixels = pixelBase + rowPixels * bounds.fTop;
329 for (y = bounds.fTop; y < bounds.fBottom; ++y) {
338 bounds.fRight = width;
340 while (--x > bounds.fLeft) {
341 pixels = pixelBase + rowPixels * bounds.fTop;
342 for (y = bounds.fTop; y < bounds.fBottom; ++y) {
344 bounds.fRight = x + 1;
352 if (!bitmap_.extractSubset(&subset, bounds)) {
355 // Neutralize the global matrix by concatenating the inverse. In the
356 // future, Skia may provide some mechanism to set the device portion of
357 // the matrix to identity without clobbering any hosting matrix (e.g., the
358 // picture's matrix).
359 const SkMatrix& skMatrix = canvas_->getTotalMatrix();
361 if (!skMatrix.invert(&inverse))
364 canvas_->concat(inverse);
365 canvas_->drawBitmap(subset, bounds.fLeft, bounds.fTop);
368 CGContextRelease(cgContext_);
372 CGContextRef SkiaBitLocker::cgContext() {
373 SkBaseDevice* device = canvas_->getTopDevice();
377 releaseIfNeeded(); // This flushes any prior bitmap use
378 const SkBitmap& deviceBits = device->accessBitmap(true);
379 useDeviceBits_ = deviceBits.getPixels();
380 if (useDeviceBits_) {
381 bitmap_ = deviceBits;
382 bitmap_.lockPixels();
385 SkBitmap::kARGB_8888_Config, deviceBits.width(), deviceBits.height());
386 bitmap_.allocPixels();
387 bitmap_.eraseColor(0);
389 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
390 CGColorSpaceCreateDeviceRGB());
391 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(),
392 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace,
393 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
395 // Apply device matrix.
396 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1);
397 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0,
399 CGContextConcatCTM(cgContext_, contentsTransform);
401 const SkIPoint& pt = device->getOrigin();
402 // Skip applying the clip when not writing directly to device.
403 // They're applied in the offscreen case when the bitmap is drawn.
404 if (useDeviceBits_) {
405 // Apply clip in device coordinates.
406 CGMutablePathRef clipPath = CGPathCreateMutable();
407 const SkRegion& clipRgn = canvas_->getTotalClip();
408 if (clipRgn.isEmpty()) {
409 // CoreGraphics does not consider a newly created path to be empty.
410 // Explicitly set it to empty so the subsequent drawing is clipped out.
411 // It would be better to make the CGContext hidden if there was a CG
412 // call that does that.
413 CGPathAddRect(clipPath, 0, CGRectMake(0, 0, 0, 0));
415 SkRegion::Iterator iter(clipRgn);
416 const SkIPoint& pt = device->getOrigin();
417 for (; !iter.done(); iter.next()) {
418 SkIRect skRect = iter.rect();
420 CGRect cgRect = SkIRectToCGRect(skRect);
421 CGPathAddRect(clipPath, 0, cgRect);
423 CGContextAddPath(cgContext_, clipPath);
424 CGContextClip(cgContext_);
425 CGPathRelease(clipPath);
428 // Apply content matrix.
429 SkMatrix skMatrix = canvas_->getTotalMatrix();
430 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY));
431 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix);
432 CGContextConcatCTM(cgContext_, affine);