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

Side by Side Diff: ui/gfx/skia_util.cc

Issue 2661353002: ui/gfx: split out code that depends on paint (Closed)
Patch Set: Fix mac, chromeos build Created 3 years, 10 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 | « ui/gfx/skia_util.h ('k') | ui/native_theme/native_theme_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 "ui/gfx/skia_util.h" 5 #include "ui/gfx/skia_util.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/numerics/safe_conversions.h" 10 #include "base/numerics/safe_conversions.h"
11 #include "base/numerics/safe_math.h" 11 #include "base/numerics/safe_math.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkColorFilter.h"
14 #include "third_party/skia/include/core/SkColorPriv.h" 13 #include "third_party/skia/include/core/SkColorPriv.h"
15 #include "third_party/skia/include/core/SkUnPreMultiply.h" 14 #include "third_party/skia/include/core/SkUnPreMultiply.h"
16 #include "third_party/skia/include/effects/SkBlurMaskFilter.h" 15 #include "third_party/skia/include/effects/SkBlurMaskFilter.h"
17 #include "third_party/skia/include/effects/SkGradientShader.h" 16 #include "third_party/skia/include/effects/SkGradientShader.h"
18 #include "third_party/skia/include/effects/SkLayerDrawLooper.h" 17 #include "third_party/skia/include/effects/SkLayerDrawLooper.h"
19 #include "ui/gfx/geometry/quad_f.h" 18 #include "ui/gfx/geometry/quad_f.h"
20 #include "ui/gfx/geometry/rect.h" 19 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gfx/geometry/rect_f.h" 20 #include "ui/gfx/geometry/rect_f.h"
22 #include "ui/gfx/image/image_skia_rep.h" 21 #include "ui/gfx/image/image_skia_rep.h"
23 #include "ui/gfx/shadow_value.h" 22 #include "ui/gfx/shadow_value.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 flattened->set(1, SkMScalarToScalar(transform.matrix().get(0, 1))); 96 flattened->set(1, SkMScalarToScalar(transform.matrix().get(0, 1)));
98 flattened->set(2, SkMScalarToScalar(transform.matrix().get(0, 3))); 97 flattened->set(2, SkMScalarToScalar(transform.matrix().get(0, 3)));
99 flattened->set(3, SkMScalarToScalar(transform.matrix().get(1, 0))); 98 flattened->set(3, SkMScalarToScalar(transform.matrix().get(1, 0)));
100 flattened->set(4, SkMScalarToScalar(transform.matrix().get(1, 1))); 99 flattened->set(4, SkMScalarToScalar(transform.matrix().get(1, 1)));
101 flattened->set(5, SkMScalarToScalar(transform.matrix().get(1, 3))); 100 flattened->set(5, SkMScalarToScalar(transform.matrix().get(1, 3)));
102 flattened->set(6, SkMScalarToScalar(transform.matrix().get(3, 0))); 101 flattened->set(6, SkMScalarToScalar(transform.matrix().get(3, 0)));
103 flattened->set(7, SkMScalarToScalar(transform.matrix().get(3, 1))); 102 flattened->set(7, SkMScalarToScalar(transform.matrix().get(3, 1)));
104 flattened->set(8, SkMScalarToScalar(transform.matrix().get(3, 3))); 103 flattened->set(8, SkMScalarToScalar(transform.matrix().get(3, 3)));
105 } 104 }
106 105
107 sk_sp<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep,
108 SkShader::TileMode tile_mode,
109 const SkMatrix& local_matrix) {
110 return CreateImageRepShaderForScale(image_rep, tile_mode, local_matrix,
111 image_rep.scale());
112 }
113
114 sk_sp<SkShader> CreateImageRepShaderForScale(
115 const gfx::ImageSkiaRep& image_rep,
116 SkShader::TileMode tile_mode,
117 const SkMatrix& local_matrix,
118 SkScalar scale) {
119 // Unscale matrix by |scale| such that the bitmap is drawn at the
120 // correct density.
121 // Convert skew and translation to pixel coordinates.
122 // Thus, for |bitmap_scale| = 2:
123 // x scale = 2, x translation = 1 DIP,
124 // should be converted to
125 // x scale = 1, x translation = 2 pixels.
126 SkMatrix shader_scale = local_matrix;
127 shader_scale.preScale(scale, scale);
128 shader_scale.setScaleX(local_matrix.getScaleX() / scale);
129 shader_scale.setScaleY(local_matrix.getScaleY() / scale);
130
131 return SkShader::MakeBitmapShader(
132 image_rep.sk_bitmap(), tile_mode, tile_mode, &shader_scale);
133 }
134
135 sk_sp<SkShader> CreateGradientShader(int start_point,
136 int end_point,
137 SkColor start_color,
138 SkColor end_color) {
139 SkColor grad_colors[2] = { start_color, end_color};
140 SkPoint grad_points[2];
141 grad_points[0].iset(0, start_point);
142 grad_points[1].iset(0, end_point);
143
144 return SkGradientShader::MakeLinear(
145 grad_points, grad_colors, NULL, 2, SkShader::kClamp_TileMode);
146 }
147
148 // TODO(estade): remove. Only exists to support legacy CreateShadowDrawLooper.
149 static SkScalar DeprecatedRadiusToSigma(double radius) {
150 // This captures historically what skia did under the hood. Now skia accepts
151 // sigma, not radius, so we perform the conversion.
152 return radius > 0 ? SkDoubleToScalar(0.57735f * radius + 0.5) : 0;
153 }
154
155 // This is copied from
156 // third_party/WebKit/Source/platform/graphics/skia/SkiaUtils.h
157 static SkScalar RadiusToSigma(double radius) {
158 return radius > 0 ? SkDoubleToScalar(0.288675f * radius + 0.5f) : 0;
159 }
160
161 sk_sp<SkDrawLooper> CreateShadowDrawLooper(
162 const std::vector<ShadowValue>& shadows) {
163 if (shadows.empty())
164 return nullptr;
165
166 SkLayerDrawLooper::Builder looper_builder;
167
168 looper_builder.addLayer(); // top layer of the original.
169
170 SkLayerDrawLooper::LayerInfo layer_info;
171 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
172 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
173 layer_info.fColorMode = SkBlendMode::kSrc;
174
175 for (size_t i = 0; i < shadows.size(); ++i) {
176 const ShadowValue& shadow = shadows[i];
177
178 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
179 SkIntToScalar(shadow.y()));
180
181 SkPaint* paint = looper_builder.addLayer(layer_info);
182 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
183 // original mask, which is half of blur amount as defined in ShadowValue.
184 // Note that because this function uses DeprecatedRadiusToSigma, it actually
185 // creates a draw looper with roughly twice the desired blur.
186 paint->setMaskFilter(SkBlurMaskFilter::Make(
187 kNormal_SkBlurStyle, DeprecatedRadiusToSigma(shadow.blur() / 2),
188 SkBlurMaskFilter::kHighQuality_BlurFlag));
189 paint->setColorFilter(
190 SkColorFilter::MakeModeFilter(shadow.color(), SkBlendMode::kSrcIn));
191 }
192
193 return looper_builder.detach();
194 }
195
196 sk_sp<SkDrawLooper> CreateShadowDrawLooperCorrectBlur(
197 const std::vector<ShadowValue>& shadows) {
198 if (shadows.empty())
199 return nullptr;
200
201 SkLayerDrawLooper::Builder looper_builder;
202
203 looper_builder.addLayer(); // top layer of the original.
204
205 SkLayerDrawLooper::LayerInfo layer_info;
206 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
207 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
208 layer_info.fColorMode = SkBlendMode::kSrc;
209
210 for (size_t i = 0; i < shadows.size(); ++i) {
211 const ShadowValue& shadow = shadows[i];
212
213 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
214 SkIntToScalar(shadow.y()));
215
216 SkPaint* paint = looper_builder.addLayer(layer_info);
217 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
218 // original mask, which is half of blur amount as defined in ShadowValue.
219 paint->setMaskFilter(SkBlurMaskFilter::Make(
220 kNormal_SkBlurStyle, RadiusToSigma(shadow.blur() / 2),
221 SkBlurMaskFilter::kHighQuality_BlurFlag));
222 paint->setColorFilter(
223 SkColorFilter::MakeModeFilter(shadow.color(), SkBlendMode::kSrcIn));
224 }
225
226 return looper_builder.detach();
227 }
228
229 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { 106 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
230 void* addr1 = NULL; 107 void* addr1 = NULL;
231 void* addr2 = NULL; 108 void* addr2 = NULL;
232 size_t size1 = 0; 109 size_t size1 = 0;
233 size_t size2 = 0; 110 size_t size2 = 0;
234 111
235 bitmap1.lockPixels(); 112 bitmap1.lockPixels();
236 addr1 = bitmap1.getAddr32(0, 0); 113 addr1 = bitmap1.getAddr32(0, 0);
237 size1 = bitmap1.getSize(); 114 size1 = bitmap1.getSize();
238 bitmap1.unlockPixels(); 115 bitmap1.unlockPixels();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 static const SkScalar kSkToHbRatio = SK_Scalar1 / kHbUnit1; 164 static const SkScalar kSkToHbRatio = SK_Scalar1 / kHbUnit1;
288 return kSkToHbRatio * value; 165 return kSkToHbRatio * value;
289 } 166 }
290 167
291 float HarfBuzzUnitsToFloat(int value) { 168 float HarfBuzzUnitsToFloat(int value) {
292 static const float kFloatToHbRatio = 1.0f / kHbUnit1; 169 static const float kFloatToHbRatio = 1.0f / kHbUnit1;
293 return kFloatToHbRatio * value; 170 return kFloatToHbRatio * value;
294 } 171 }
295 172
296 } // namespace gfx 173 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/skia_util.h ('k') | ui/native_theme/native_theme_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698