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

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

Issue 16917011: mac: Replace base::mac::ScopedCFTypeRef with base::ScopedCFTypeRef. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: with fixed off-by-1 in git-clang-format Created 7 years, 6 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
« no previous file with comments | « skia/ext/skia_utils_ios.mm ('k') | sync/util/get_session_name_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/scoped_cftyperef.h" 10 #include "base/mac/scoped_cftyperef.h"
(...skipping 14 matching lines...) Expand all
25 // Only image or image_rep should be provided, not both. 25 // Only image or image_rep should be provided, not both.
26 DCHECK((image != 0) ^ (image_rep != 0)); 26 DCHECK((image != 0) ^ (image_rep != 0));
27 27
28 SkBitmap bitmap; 28 SkBitmap bitmap;
29 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height); 29 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height);
30 if (!bitmap.allocPixels()) 30 if (!bitmap.allocPixels())
31 return bitmap; // Return |bitmap| which should respond true to isNull(). 31 return bitmap; // Return |bitmap| which should respond true to isNull().
32 32
33 bitmap.setIsOpaque(is_opaque); 33 bitmap.setIsOpaque(is_opaque);
34 34
35 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( 35 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
36 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); 36 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
37 void* data = bitmap.getPixels(); 37 void* data = bitmap.getPixels();
38 38
39 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple 39 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
40 // recommends these flags for improved CG performance. 40 // recommends these flags for improved CG performance.
41 #define HAS_ARGB_SHIFTS(a, r, g, b) \ 41 #define HAS_ARGB_SHIFTS(a, r, g, b) \
42 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \ 42 (SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
43 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b)) 43 && SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
44 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0) 44 #if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
45 base::mac::ScopedCFTypeRef<CGContextRef> context( 45 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
46 CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4, 46 data,
47 color_space, 47 size.width,
48 kCGImageAlphaPremultipliedFirst | 48 size.height,
49 kCGBitmapByteOrder32Host)); 49 8,
50 size.width * 4,
51 color_space,
52 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
50 #else 53 #else
51 #error We require that Skia's and CoreGraphics's recommended \ 54 #error We require that Skia's and CoreGraphics's recommended \
52 image memory layout match. 55 image memory layout match.
53 #endif 56 #endif
54 #undef HAS_ARGB_SHIFTS 57 #undef HAS_ARGB_SHIFTS
55 58
56 // Something went really wrong. Best guess is that the bitmap data is invalid. 59 // Something went really wrong. Best guess is that the bitmap data is invalid.
57 DCHECK(context); 60 DCHECK(context);
58 61
59 [NSGraphicsContext saveGraphicsState]; 62 [NSGraphicsContext saveGraphicsState];
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { 208 SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) {
206 return NSImageOrNSImageRepToSkBitmap(image, nil, size, is_opaque); 209 return NSImageOrNSImageRepToSkBitmap(image, nil, size, is_opaque);
207 } 210 }
208 211
209 SkBitmap NSImageRepToSkBitmap( 212 SkBitmap NSImageRepToSkBitmap(
210 NSImageRep* image_rep, NSSize size, bool is_opaque) { 213 NSImageRep* image_rep, NSSize size, bool is_opaque) {
211 return NSImageOrNSImageRepToSkBitmap(nil, image_rep, size, is_opaque); 214 return NSImageOrNSImageRepToSkBitmap(nil, image_rep, size, is_opaque);
212 } 215 }
213 216
214 NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& skiaBitmap) { 217 NSBitmapImageRep* SkBitmapToNSBitmapImageRep(const SkBitmap& skiaBitmap) {
215 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( 218 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
216 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); 219 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
217 return SkBitmapToNSBitmapImageRepWithColorSpace(skiaBitmap, color_space); 220 return SkBitmapToNSBitmapImageRepWithColorSpace(skiaBitmap, color_space);
218 } 221 }
219 222
220 NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace( 223 NSBitmapImageRep* SkBitmapToNSBitmapImageRepWithColorSpace(
221 const SkBitmap& skiaBitmap, 224 const SkBitmap& skiaBitmap,
222 CGColorSpaceRef colorSpace) { 225 CGColorSpaceRef colorSpace) {
223 // First convert SkBitmap to CGImageRef. 226 // First convert SkBitmap to CGImageRef.
224 base::mac::ScopedCFTypeRef<CGImageRef> cgimage( 227 base::ScopedCFTypeRef<CGImageRef> cgimage(
225 SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace)); 228 SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace));
226 229
227 // Now convert to NSBitmapImageRep. 230 // Now convert to NSBitmapImageRep.
228 scoped_nsobject<NSBitmapImageRep> bitmap( 231 scoped_nsobject<NSBitmapImageRep> bitmap(
229 [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); 232 [[NSBitmapImageRep alloc] initWithCGImage:cgimage]);
230 return [bitmap.release() autorelease]; 233 return [bitmap.release() autorelease];
231 } 234 }
232 235
233 NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap, 236 NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap,
234 CGColorSpaceRef colorSpace) { 237 CGColorSpaceRef colorSpace) {
235 if (skiaBitmap.isNull()) 238 if (skiaBitmap.isNull())
236 return nil; 239 return nil;
237 240
238 scoped_nsobject<NSImage> image([[NSImage alloc] init]); 241 scoped_nsobject<NSImage> image([[NSImage alloc] init]);
239 [image addRepresentation: 242 [image addRepresentation:
240 SkBitmapToNSBitmapImageRepWithColorSpace(skiaBitmap, colorSpace)]; 243 SkBitmapToNSBitmapImageRepWithColorSpace(skiaBitmap, colorSpace)];
241 [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())]; 244 [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())];
242 return [image.release() autorelease]; 245 return [image.release() autorelease];
243 } 246 }
244 247
245 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { 248 NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) {
246 base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( 249 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
247 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); 250 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
248 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); 251 return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get());
249 } 252 }
250 253
251 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas) 254 SkiaBitLocker::SkiaBitLocker(SkCanvas* canvas)
252 : canvas_(canvas), 255 : canvas_(canvas),
253 cgContext_(0) { 256 cgContext_(0) {
254 } 257 }
255 258
256 SkiaBitLocker::~SkiaBitLocker() { 259 SkiaBitLocker::~SkiaBitLocker() {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 useDeviceBits_ = deviceBits.getPixels(); 359 useDeviceBits_ = deviceBits.getPixels();
357 if (useDeviceBits_) { 360 if (useDeviceBits_) {
358 bitmap_ = deviceBits; 361 bitmap_ = deviceBits;
359 bitmap_.lockPixels(); 362 bitmap_.lockPixels();
360 } else { 363 } else {
361 bitmap_.setConfig( 364 bitmap_.setConfig(
362 SkBitmap::kARGB_8888_Config, deviceBits.width(), deviceBits.height()); 365 SkBitmap::kARGB_8888_Config, deviceBits.width(), deviceBits.height());
363 bitmap_.allocPixels(); 366 bitmap_.allocPixels();
364 bitmap_.eraseColor(0); 367 bitmap_.eraseColor(0);
365 } 368 }
366 base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( 369 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace(
367 CGColorSpaceCreateDeviceRGB()); 370 CGColorSpaceCreateDeviceRGB());
368 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(), 371 cgContext_ = CGBitmapContextCreate(bitmap_.getPixels(), bitmap_.width(),
369 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace, 372 bitmap_.height(), 8, bitmap_.rowBytes(), colorSpace,
370 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); 373 kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
371 374
372 // Apply device matrix. 375 // Apply device matrix.
373 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1); 376 CGAffineTransform contentsTransform = CGAffineTransformMakeScale(1, -1);
374 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0, 377 contentsTransform = CGAffineTransformTranslate(contentsTransform, 0,
375 -device->height()); 378 -device->height());
376 CGContextConcatCTM(cgContext_, contentsTransform); 379 CGContextConcatCTM(cgContext_, contentsTransform);
(...skipping 28 matching lines...) Expand all
405 // Apply content matrix. 408 // Apply content matrix.
406 SkMatrix skMatrix = canvas_->getTotalMatrix(); 409 SkMatrix skMatrix = canvas_->getTotalMatrix();
407 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY)); 410 skMatrix.postTranslate(-SkIntToScalar(pt.fX), -SkIntToScalar(pt.fY));
408 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix); 411 CGAffineTransform affine = SkMatrixToCGAffineTransform(skMatrix);
409 CGContextConcatCTM(cgContext_, affine); 412 CGContextConcatCTM(cgContext_, affine);
410 413
411 return cgContext_; 414 return cgContext_;
412 } 415 }
413 416
414 } // namespace gfx 417 } // namespace gfx
OLDNEW
« no previous file with comments | « skia/ext/skia_utils_ios.mm ('k') | sync/util/get_session_name_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698