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

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

Issue 118488: Add file icons to chrome://downloads/ on the Mac. Add Skia helper CGImageToSkBitmap(). (Closed)
Patch Set: Switch to using |-drawInRect:| rather than |-drawAtPoint:| Created 11 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
« no previous file with comments | « skia/ext/skia_utils_mac.cc ('k') | skia/skia.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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 #include "skia/ext/skia_utils_mac.h"
6
7 #import <AppKit/AppKit.h>
8
9 #include "base/logging.h"
10 #include "base/scoped_cftyperef.h"
11 #include "base/scoped_ptr.h"
12 #include "skia/ext/bitmap_platform_device_mac.h"
13
14 namespace gfx {
15
16 CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) {
17 // CGAffineTransforms don't support perspective transforms, so make sure
18 // we don't get those.
19 DCHECK(matrix[SkMatrix::kMPersp0] == 0.0f);
20 DCHECK(matrix[SkMatrix::kMPersp1] == 0.0f);
21 DCHECK(matrix[SkMatrix::kMPersp2] == 1.0f);
22
23 return CGAffineTransformMake(matrix[SkMatrix::kMScaleX],
24 matrix[SkMatrix::kMSkewY],
25 matrix[SkMatrix::kMSkewX],
26 matrix[SkMatrix::kMScaleY],
27 matrix[SkMatrix::kMTransX],
28 matrix[SkMatrix::kMTransY]);
29 }
30
31 SkIRect CGRectToSkIRect(const CGRect& rect) {
32 SkIRect sk_rect = {
33 SkScalarRound(rect.origin.x),
34 SkScalarRound(rect.origin.y),
35 SkScalarRound(rect.origin.x + rect.size.width),
36 SkScalarRound(rect.origin.y + rect.size.height)
37 };
38 return sk_rect;
39 }
40
41 SkRect CGRectToSkRect(const CGRect& rect) {
42 SkRect sk_rect = {
43 rect.origin.x,
44 rect.origin.y,
45 rect.origin.x + rect.size.width,
46 rect.origin.y + rect.size.height,
47 };
48 return sk_rect;
49 }
50
51 CGRect SkIRectToCGRect(const SkIRect& rect) {
52 CGRect cg_rect = {
53 { rect.fLeft, rect.fTop },
54 { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop }
55 };
56 return cg_rect;
57 }
58
59 CGRect SkRectToCGRect(const SkRect& rect) {
60 CGRect cg_rect = {
61 { rect.fLeft, rect.fTop },
62 { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop }
63 };
64 return cg_rect;
65 }
66
67 // Converts CGColorRef to the ARGB layout Skia expects.
68 SkColor CGColorRefToSkColor(CGColorRef color) {
69 DCHECK(CGColorGetNumberOfComponents(color) == 4);
70 const CGFloat *components = CGColorGetComponents(color);
71 return SkColorSetARGB(SkScalarRound(255.0 * components[3]), // alpha
72 SkScalarRound(255.0 * components[0]), // red
73 SkScalarRound(255.0 * components[1]), // green
74 SkScalarRound(255.0 * components[2])); // blue
75 }
76
77 // Converts ARGB to CGColorRef.
78 CGColorRef SkColorToCGColorRef(SkColor color) {
79 return CGColorCreateGenericRGB(SkColorGetR(color) / 255.0,
80 SkColorGetG(color) / 255.0,
81 SkColorGetB(color) / 255.0,
82 SkColorGetA(color) / 255.0);
83 }
84
85 SkBitmap CGImageToSkBitmap(CGImageRef image) {
86 DCHECK(image != NULL);
87
88 int width = CGImageGetWidth(image);
89 int height = CGImageGetHeight(image);
90
91 scoped_ptr<skia::BitmapPlatformDevice> device(
92 skia::BitmapPlatformDevice::Create(NULL, width, height, false));
93
94 CGContextRef context = device->GetBitmapContext();
95
96 // We need to invert the y-axis of the canvas so that Core Graphics drawing
97 // happens right-side up. Skia has an upper-left origin and CG has a lower-
98 // left one.
99 CGContextScaleCTM(context, 1.0, -1.0);
100 CGContextTranslateCTM(context, 1, -height);
101
102 // We want to copy transparent pixels from |image|, instead of blending it
103 // onto uninitialized pixels.
104 CGContextSetBlendMode(context, kCGBlendModeCopy);
105
106 CGRect rect = CGRectMake(0, 0, width, height);
107 CGContextDrawImage(context, rect, image);
108
109 // Because |device| will be cleaned up and will take its pixels with it, we
110 // copy it to the stack and return it.
111 SkBitmap bitmap = device->accessBitmap(false);
112
113 return bitmap;
114 }
115
116 SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) {
117 SkBitmap bitmap;
118 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height);
119 if (bitmap.allocPixels() != true)
120 return bitmap; // Return |bitmap| which should respond true to isNull().
121
122 bitmap.setIsOpaque(is_opaque);
123
124 scoped_cftyperef<CGColorSpaceRef> color_space(
125 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
126 void* data = bitmap.getPixels();
127
128 // Allocate a bitmap context with 4 components per pixel (BGRA). Apple
129 // recommends these flags for improved CG performance.
130 scoped_cftyperef<CGContextRef> context(
131 CGBitmapContextCreate(data, size.width, size.height, 8, size.width*4,
132 color_space,
133 kCGImageAlphaPremultipliedFirst |
134 kCGBitmapByteOrder32Host));
135
136 // Something went really wrong. Best guess is that the bitmap data is invalid.
137 DCHECK(context != NULL);
138
139 // Save the current graphics context so that we can restore it later.
140 NSGraphicsContext* current_context = [NSGraphicsContext currentContext];
141
142 // Dummy context that we will draw into.
143 NSGraphicsContext* context_cocoa =
144 [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
145 [NSGraphicsContext setCurrentContext:context_cocoa];
146
147 // This will stretch any images to |size| if it does not fit or is non-square.
148 [image drawInRect:NSMakeRect(0, 0, size.width, size.height)
149 fromRect:NSZeroRect
150 operation:NSCompositeCopy
151 fraction:1.0];
152
153 // Done drawing, restore context.
154 [NSGraphicsContext setCurrentContext:current_context];
155
156 return bitmap;
157 }
158
159 } // namespace gfx
160
OLDNEW
« no previous file with comments | « skia/ext/skia_utils_mac.cc ('k') | skia/skia.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698