OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 "base/gfx/platform_device_mac.h" | 5 #include "base/gfx/platform_device_mac.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/gfx/skia_utils_mac.h" | 8 #include "base/gfx/skia_utils_mac.h" |
9 #include "SkMatrix.h" | 9 #include "SkMatrix.h" |
10 #include "SkPath.h" | 10 #include "SkPath.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 break; | 90 break; |
91 } | 91 } |
92 } | 92 } |
93 } | 93 } |
94 CGContextClosePath(context); | 94 CGContextClosePath(context); |
95 } | 95 } |
96 | 96 |
97 // static | 97 // static |
98 void PlatformDeviceMac::LoadTransformToCGContext(CGContextRef context, | 98 void PlatformDeviceMac::LoadTransformToCGContext(CGContextRef context, |
99 const SkMatrix& matrix) { | 99 const SkMatrix& matrix) { |
100 // TODO: CoreGraphics can concatenate transforms, but not reset the current | 100 // CoreGraphics can concatenate transforms, but not reset the current one. |
101 // one. Either find a workaround or remove this function if it turns out | 101 // So in order to get the required behavior here, we need to first make |
102 // to be unneeded on the Mac. | 102 // the current transformation matrix identity and only then load the new one. |
103 // For now, just load the translation. | |
104 | 103 |
105 // First reset the Transforms. | 104 // Reset matrix to identity. |
106 // TODO(playmobil): no need to call CGContextTranslateCTM() twice | 105 CGAffineTransform orig_cg_matrix = CGContextGetCTM(context); |
107 // just add up the numbers and call through. | 106 CGAffineTransform orig_cg_matrix_inv = CGAffineTransformInvert(orig_cg_matrix)
; |
108 CGAffineTransform orig_transform = CGContextGetCTM(context); | 107 CGContextConcatCTM(context, orig_cg_matrix_inv); |
109 CGContextTranslateCTM(context, | |
110 -orig_transform.tx, | |
111 orig_transform.ty); // y axis is flipped. | |
112 | |
113 // TODO(playmobil): remove debug code. | |
114 // CGAffineTransform temp_transform = CGContextGetCTM(context); | |
115 | 108 |
116 // Now set the new transform. | 109 // assert that we have indeed returned to the identity Matrix. |
117 int tx = matrix.getTranslateX(); | 110 DCHECK(CGAffineTransformIsIdentity(CGContextGetCTM(context))); |
118 int ty = -matrix.getTranslateY(); | 111 |
119 int height = CGBitmapContextGetHeight(context); | 112 // Convert xform to CG-land. |
120 CGContextTranslateCTM(context, | 113 // Our coordinate system is flipped to match WebKit's so we need to modify |
121 tx, | 114 // the xform to match that. |
122 -(ty+height)); | 115 SkMatrix transformed_matrix = matrix; |
123 CGAffineTransform new_transform = CGContextGetCTM(context); | 116 SkScalar sy = matrix.getScaleY() * (SkScalar)-1; |
124 // TODO(playmobil): remove debug code. | 117 transformed_matrix.setScaleY(sy); |
125 // printf("tx_matrix (%lf,%lf)->(%lf,%lf)->(%lf,%lf) (%d, %d) height=%d\n", ori
g_transform.tx, | 118 size_t height = CGBitmapContextGetHeight(context); |
126 // orig_transform.ty, | 119 SkScalar ty = -matrix.getTranslateY(); // y axis is flipped. |
127 // foo_transform.tx, | 120 transformed_matrix.setTranslateY(ty + (SkScalar)height); |
128 // foo_transform.ty, | 121 |
129 // new_transform.tx, | 122 CGAffineTransform cg_matrix = SkMatrixToCGAffineTransform(transformed_matrix); |
130 // new_transform.ty, tx, ty, height); | 123 |
| 124 // Load final transform into context. |
| 125 CGContextConcatCTM(context, cg_matrix); |
131 } | 126 } |
132 | 127 |
133 // static | 128 // static |
134 void PlatformDeviceMac::LoadClippingRegionToCGContext( | 129 void PlatformDeviceMac::LoadClippingRegionToCGContext( |
135 CGContextRef context, | 130 CGContextRef context, |
136 const SkRegion& region, | 131 const SkRegion& region, |
137 const SkMatrix& transformation) { | 132 const SkMatrix& transformation) { |
138 if (region.isEmpty()) { | 133 if (region.isEmpty()) { |
139 // region can be empty, in which case everything will be clipped. | 134 // region can be empty, in which case everything will be clipped. |
140 SkRect rect; | 135 SkRect rect; |
141 rect.setEmpty(); | 136 rect.setEmpty(); |
142 CGContextClipToRect(context, SkRectToCGRect(rect)); | 137 CGContextClipToRect(context, SkRectToCGRect(rect)); |
143 } else if (region.isRect()) { | 138 } else if (region.isRect()) { |
144 // Do the transformation. | 139 // Do the transformation. |
145 SkRect rect; | 140 SkRect rect; |
146 rect.set(region.getBounds()); | 141 rect.set(region.getBounds()); |
147 transformation.mapRect(&rect); | 142 transformation.mapRect(&rect); |
148 SkIRect irect; | 143 SkIRect irect; |
149 rect.round(&irect); | 144 rect.round(&irect); |
150 // TODO(playmobil): remove debug code. | |
151 // printf("Clipping to (%d,%d) (%d,%d)\n", irect.fLeft, irect.fTop, | |
152 // irect.fRight, irect.fBottom); | |
153 CGContextClipToRect(context, SkIRectToCGRect(irect)); | 145 CGContextClipToRect(context, SkIRectToCGRect(irect)); |
154 } else { | 146 } else { |
155 // It is complex. | 147 // It is complex. |
156 SkPath path; | 148 SkPath path; |
157 region.getBoundaryPath(&path); | 149 region.getBoundaryPath(&path); |
158 // Clip. Note that windows clipping regions are not affected by the | 150 // Clip. Note that windows clipping regions are not affected by the |
159 // transform so apply it manually. | 151 // transform so apply it manually. |
160 path.transform(transformation); | 152 path.transform(transformation); |
161 // TODO(playmobil): Implement. | 153 // TODO(playmobil): Implement. |
162 NOTREACHED(); | 154 NOTREACHED(); |
163 // LoadPathToDC(context, path); | 155 // LoadPathToDC(context, path); |
164 // hrgn = PathToRegion(context); | 156 // hrgn = PathToRegion(context); |
165 } | 157 } |
166 } | 158 } |
167 | 159 |
168 } // namespace gfx | 160 } // namespace gfx |
169 | 161 |
OLD | NEW |