Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Side by Side Diff: skia/ext/skia_utils_mac.mm

Issue 7031006: Add utility for converting SkCanvas to CGContext (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "skia/ext/skia_utils_mac.h" 5 #include "skia/ext/skia_utils_mac.h"
6 6
7 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/mac/mac_util.h"
10 #include "base/mac/scoped_cftyperef.h" 11 #include "base/mac/scoped_cftyperef.h"
11 #include "base/memory/scoped_nsobject.h" 12 #include "base/memory/scoped_nsobject.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "skia/ext/bitmap_platform_device_mac.h" 14 #include "skia/ext/bitmap_platform_device_mac.h"
15 #include "third_party/skia/include/core/SkRegion.h"
14 #include "third_party/skia/include/utils/mac/SkCGUtils.h" 16 #include "third_party/skia/include/utils/mac/SkCGUtils.h"
15 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" 17 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
16 18
17 // 10.6 API that we use if available. 19 // 10.6 API that we use if available.
18 #if !defined(MAC_OS_X_VERSION_10_6) || \ 20 #if !defined(MAC_OS_X_VERSION_10_6) || \
19 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 21 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
20 22
21 @interface NSImageRep (NSObject) 23 @interface NSImageRep (NSObject)
22 24
23 - (BOOL)drawInRect:(NSRect)dstSpacePortionRect 25 - (BOOL)drawInRect:(NSRect)dstSpacePortionRect
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 279
278 [image setSize:min_size]; 280 [image setSize:min_size];
279 return [image.release() autorelease]; 281 return [image.release() autorelease];
280 } 282 }
281 283
282 SkBitmap AppplicationIconAtSize(int size) { 284 SkBitmap AppplicationIconAtSize(int size) {
283 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 285 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
284 return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); 286 return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true);
285 } 287 }
286 288
289
290 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas)
291 : m_canvas(canvas)
292 , m_cgContext(0) {
Nico 2011/05/16 15:13:00 , goes on previous line
_cary 2011/05/16 15:58:02 Done.
293 }
294
295 SkiaBitLocker::~SkiaBitLocker() {
296 releaseIfNeeded();
297 }
298
299 void SkiaBitLocker::releaseIfNeeded() {
300 if (!m_cgContext)
301 return;
302 m_canvas->getDevice()->accessBitmap(true).unlockPixels();
303 CGContextRelease(m_cgContext);
304 m_cgContext = 0;
305 }
306
307 CGContextRef SkiaBitLocker::cgContext() {
308 SkDevice* device = m_canvas->getDevice();
309 DCHECK(device);
310 if (!device)
311 return 0;
312 releaseIfNeeded();
313 const SkBitmap& bitmap = device->accessBitmap(true);
314 bitmap.lockPixels();
315 void* pixels = bitmap.getPixels();
316 m_cgContext = CGBitmapContextCreate(pixels, device->width(),
317 device->height(), 8, bitmap.rowBytes(), CGColorSpaceCreateDeviceRGB(),
318 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
319
320 // Apply device matrix.
321 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1);
322 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0,
323 -device->height());
324 CGContextConcatCTM(m_cgContext, contentsTransform);
325
326 // Apply clip in device coordinates.
327 CGMutablePathRef clipPath = CGPathCreateMutable();
328 SkRegion::Iterator iter(m_canvas->getTotalClip());
329 for (; !iter.done(); iter.next()) {
330 const SkIRect& skRect = iter.rect();
331 CGRect cgRect = SkIRectToCGRect(skRect);
332 CGPathAddRect(clipPath, 0, cgRect);
333 }
334 CGContextAddPath(m_cgContext, clipPath);
335 CGContextClip(m_cgContext);
336 CGPathRelease(clipPath);
337
338 // Apply content matrix.
339 const SkMatrix& skMatrix = m_canvas->getTotalMatrix();
340 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix);
341 CGContextConcatCTM(m_cgContext, affine);
342
343 return m_cgContext;
344 }
345
287 } // namespace gfx 346 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698