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

Side by Side Diff: ui/gfx/skia_paint_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_paint_util.h ('k') | ui/gfx/skia_util.h » ('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) 2017 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 "ui/gfx/skia_paint_util.h"
6
7 #include "third_party/skia/include/core/SkColorFilter.h"
8 #include "third_party/skia/include/effects/SkBlurMaskFilter.h"
9 #include "third_party/skia/include/effects/SkGradientShader.h"
10 #include "third_party/skia/include/effects/SkLayerDrawLooper.h"
11 #include "ui/gfx/image/image_skia_rep.h"
12
13 namespace gfx {
14
15 sk_sp<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep,
16 SkShader::TileMode tile_mode,
17 const SkMatrix& local_matrix) {
18 return CreateImageRepShaderForScale(image_rep, tile_mode, local_matrix,
19 image_rep.scale());
20 }
21
22 sk_sp<SkShader> CreateImageRepShaderForScale(const gfx::ImageSkiaRep& image_rep,
23 SkShader::TileMode tile_mode,
24 const SkMatrix& local_matrix,
25 SkScalar scale) {
26 // Unscale matrix by |scale| such that the bitmap is drawn at the
27 // correct density.
28 // Convert skew and translation to pixel coordinates.
29 // Thus, for |bitmap_scale| = 2:
30 // x scale = 2, x translation = 1 DIP,
31 // should be converted to
32 // x scale = 1, x translation = 2 pixels.
33 SkMatrix shader_scale = local_matrix;
34 shader_scale.preScale(scale, scale);
35 shader_scale.setScaleX(local_matrix.getScaleX() / scale);
36 shader_scale.setScaleY(local_matrix.getScaleY() / scale);
37
38 return SkShader::MakeBitmapShader(image_rep.sk_bitmap(), tile_mode, tile_mode,
39 &shader_scale);
40 }
41
42 sk_sp<SkShader> CreateGradientShader(int start_point,
43 int end_point,
44 SkColor start_color,
45 SkColor end_color) {
46 SkColor grad_colors[2] = {start_color, end_color};
47 SkPoint grad_points[2];
48 grad_points[0].iset(0, start_point);
49 grad_points[1].iset(0, end_point);
50
51 return SkGradientShader::MakeLinear(grad_points, grad_colors, NULL, 2,
52 SkShader::kClamp_TileMode);
53 }
54
55 // TODO(estade): remove. Only exists to support legacy CreateShadowDrawLooper.
56 static SkScalar DeprecatedRadiusToSigma(double radius) {
57 // This captures historically what skia did under the hood. Now skia accepts
58 // sigma, not radius, so we perform the conversion.
59 return radius > 0 ? SkDoubleToScalar(0.57735f * radius + 0.5) : 0;
60 }
61
62 // This is copied from
63 // third_party/WebKit/Source/platform/graphics/skia/SkiaUtils.h
64 static SkScalar RadiusToSigma(double radius) {
65 return radius > 0 ? SkDoubleToScalar(0.288675f * radius + 0.5f) : 0;
66 }
67
68 sk_sp<SkDrawLooper> CreateShadowDrawLooper(
69 const std::vector<ShadowValue>& shadows) {
70 if (shadows.empty())
71 return nullptr;
72
73 SkLayerDrawLooper::Builder looper_builder;
74
75 looper_builder.addLayer(); // top layer of the original.
76
77 SkLayerDrawLooper::LayerInfo layer_info;
78 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
79 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
80 layer_info.fColorMode = SkBlendMode::kSrc;
81
82 for (size_t i = 0; i < shadows.size(); ++i) {
83 const ShadowValue& shadow = shadows[i];
84
85 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
86 SkIntToScalar(shadow.y()));
87
88 SkPaint* paint = looper_builder.addLayer(layer_info);
89 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
90 // original mask, which is half of blur amount as defined in ShadowValue.
91 // Note that because this function uses DeprecatedRadiusToSigma, it actually
92 // creates a draw looper with roughly twice the desired blur.
93 paint->setMaskFilter(SkBlurMaskFilter::Make(
94 kNormal_SkBlurStyle, DeprecatedRadiusToSigma(shadow.blur() / 2),
95 SkBlurMaskFilter::kHighQuality_BlurFlag));
96 paint->setColorFilter(
97 SkColorFilter::MakeModeFilter(shadow.color(), SkBlendMode::kSrcIn));
98 }
99
100 return looper_builder.detach();
101 }
102
103 sk_sp<SkDrawLooper> CreateShadowDrawLooperCorrectBlur(
104 const std::vector<ShadowValue>& shadows) {
105 if (shadows.empty())
106 return nullptr;
107
108 SkLayerDrawLooper::Builder looper_builder;
109
110 looper_builder.addLayer(); // top layer of the original.
111
112 SkLayerDrawLooper::LayerInfo layer_info;
113 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
114 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
115 layer_info.fColorMode = SkBlendMode::kSrc;
116
117 for (size_t i = 0; i < shadows.size(); ++i) {
118 const ShadowValue& shadow = shadows[i];
119
120 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
121 SkIntToScalar(shadow.y()));
122
123 SkPaint* paint = looper_builder.addLayer(layer_info);
124 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
125 // original mask, which is half of blur amount as defined in ShadowValue.
126 paint->setMaskFilter(SkBlurMaskFilter::Make(
127 kNormal_SkBlurStyle, RadiusToSigma(shadow.blur() / 2),
128 SkBlurMaskFilter::kHighQuality_BlurFlag));
129 paint->setColorFilter(
130 SkColorFilter::MakeModeFilter(shadow.color(), SkBlendMode::kSrcIn));
131 }
132
133 return looper_builder.detach();
134 }
135
136 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/skia_paint_util.h ('k') | ui/gfx/skia_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698