Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / skia_util.cc
blobef07241ba5778efd278d269f626852544574890f
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
3 // found in the LICENSE file.
5 #include "ui/gfx/skia_util.h"
7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "third_party/skia/include/core/SkColorFilter.h"
9 #include "third_party/skia/include/core/SkColorPriv.h"
10 #include "third_party/skia/include/core/SkUnPreMultiply.h"
11 #include "third_party/skia/include/effects/SkBlurMaskFilter.h"
12 #include "third_party/skia/include/effects/SkGradientShader.h"
13 #include "third_party/skia/include/effects/SkLayerDrawLooper.h"
14 #include "ui/gfx/geometry/quad_f.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/rect_f.h"
17 #include "ui/gfx/image/image_skia_rep.h"
18 #include "ui/gfx/shadow_value.h"
19 #include "ui/gfx/transform.h"
21 namespace gfx {
23 SkRect RectToSkRect(const Rect& rect) {
24 SkRect r;
25 r.iset(rect.x(), rect.y(), rect.right(), rect.bottom());
26 return r;
29 SkIRect RectToSkIRect(const Rect& rect) {
30 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height());
33 Rect SkIRectToRect(const SkIRect& rect) {
34 return Rect(rect.x(), rect.y(), rect.width(), rect.height());
37 SkRect RectFToSkRect(const RectF& rect) {
38 return SkRect::MakeXYWH(SkFloatToScalar(rect.x()),
39 SkFloatToScalar(rect.y()),
40 SkFloatToScalar(rect.width()),
41 SkFloatToScalar(rect.height()));
44 RectF SkRectToRectF(const SkRect& rect) {
45 return RectF(SkScalarToFloat(rect.x()),
46 SkScalarToFloat(rect.y()),
47 SkScalarToFloat(rect.width()),
48 SkScalarToFloat(rect.height()));
51 SkSize SizeFToSkSize(const SizeF& size) {
52 return SkSize::Make(SkFloatToScalar(size.width()),
53 SkFloatToScalar(size.height()));
56 SizeF SkSizeToSizeF(const SkSize& size) {
57 return SizeF(SkScalarToFloat(size.width()), SkScalarToFloat(size.height()));
60 void TransformToFlattenedSkMatrix(const gfx::Transform& transform,
61 SkMatrix* flattened) {
62 // Convert from 4x4 to 3x3 by dropping the third row and column.
63 flattened->set(0, SkMScalarToScalar(transform.matrix().get(0, 0)));
64 flattened->set(1, SkMScalarToScalar(transform.matrix().get(0, 1)));
65 flattened->set(2, SkMScalarToScalar(transform.matrix().get(0, 3)));
66 flattened->set(3, SkMScalarToScalar(transform.matrix().get(1, 0)));
67 flattened->set(4, SkMScalarToScalar(transform.matrix().get(1, 1)));
68 flattened->set(5, SkMScalarToScalar(transform.matrix().get(1, 3)));
69 flattened->set(6, SkMScalarToScalar(transform.matrix().get(3, 0)));
70 flattened->set(7, SkMScalarToScalar(transform.matrix().get(3, 1)));
71 flattened->set(8, SkMScalarToScalar(transform.matrix().get(3, 3)));
74 skia::RefPtr<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep,
75 SkShader::TileMode tile_mode,
76 const SkMatrix& local_matrix) {
77 return CreateImageRepShaderForScale(image_rep, tile_mode, local_matrix,
78 image_rep.scale());
81 skia::RefPtr<SkShader> CreateImageRepShaderForScale(
82 const gfx::ImageSkiaRep& image_rep,
83 SkShader::TileMode tile_mode,
84 const SkMatrix& local_matrix,
85 SkScalar scale) {
86 // Unscale matrix by |scale| such that the bitmap is drawn at the
87 // correct density.
88 // Convert skew and translation to pixel coordinates.
89 // Thus, for |bitmap_scale| = 2:
90 // x scale = 2, x translation = 1 DIP,
91 // should be converted to
92 // x scale = 1, x translation = 2 pixels.
93 SkMatrix shader_scale = local_matrix;
94 shader_scale.preScale(scale, scale);
95 shader_scale.setScaleX(local_matrix.getScaleX() / scale);
96 shader_scale.setScaleY(local_matrix.getScaleY() / scale);
98 skia::RefPtr<SkShader> shader = skia::AdoptRef(SkShader::CreateBitmapShader(
99 image_rep.sk_bitmap(), tile_mode, tile_mode, &shader_scale));
100 return shader;
103 skia::RefPtr<SkShader> CreateGradientShader(int start_point,
104 int end_point,
105 SkColor start_color,
106 SkColor end_color) {
107 SkColor grad_colors[2] = { start_color, end_color};
108 SkPoint grad_points[2];
109 grad_points[0].iset(0, start_point);
110 grad_points[1].iset(0, end_point);
112 return skia::AdoptRef(SkGradientShader::CreateLinear(
113 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode));
116 static SkScalar RadiusToSigma(double radius) {
117 // This captures historically what skia did under the hood. Now skia accepts
118 // sigma, not radius, so we perform the conversion.
119 return radius > 0 ? SkDoubleToScalar(0.57735f * radius + 0.5) : 0;
122 skia::RefPtr<SkDrawLooper> CreateShadowDrawLooper(
123 const std::vector<ShadowValue>& shadows) {
124 if (shadows.empty())
125 return skia::RefPtr<SkDrawLooper>();
127 SkLayerDrawLooper::Builder looper_builder;
129 looper_builder.addLayer(); // top layer of the original.
131 SkLayerDrawLooper::LayerInfo layer_info;
132 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
133 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
134 layer_info.fColorMode = SkXfermode::kSrc_Mode;
136 for (size_t i = 0; i < shadows.size(); ++i) {
137 const ShadowValue& shadow = shadows[i];
139 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
140 SkIntToScalar(shadow.y()));
142 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
143 // original mask, which is half of blur amount as defined in ShadowValue.
144 skia::RefPtr<SkMaskFilter> blur_mask = skia::AdoptRef(
145 SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
146 RadiusToSigma(shadow.blur() / 2),
147 SkBlurMaskFilter::kHighQuality_BlurFlag));
148 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef(
149 SkColorFilter::CreateModeFilter(shadow.color(),
150 SkXfermode::kSrcIn_Mode));
152 SkPaint* paint = looper_builder.addLayer(layer_info);
153 paint->setMaskFilter(blur_mask.get());
154 paint->setColorFilter(color_filter.get());
157 return skia::AdoptRef<SkDrawLooper>(looper_builder.detachLooper());
160 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
161 void* addr1 = NULL;
162 void* addr2 = NULL;
163 size_t size1 = 0;
164 size_t size2 = 0;
166 bitmap1.lockPixels();
167 addr1 = bitmap1.getAddr32(0, 0);
168 size1 = bitmap1.getSize();
169 bitmap1.unlockPixels();
171 bitmap2.lockPixels();
172 addr2 = bitmap2.getAddr32(0, 0);
173 size2 = bitmap2.getSize();
174 bitmap2.unlockPixels();
176 return (size1 == size2) && (0 == memcmp(addr1, addr2, bitmap1.getSize()));
179 void ConvertSkiaToRGBA(const unsigned char* skia,
180 int pixel_width,
181 unsigned char* rgba) {
182 int total_length = pixel_width * 4;
183 for (int i = 0; i < total_length; i += 4) {
184 const uint32_t pixel_in = *reinterpret_cast<const uint32_t*>(&skia[i]);
186 // Pack the components here.
187 SkAlpha alpha = SkGetPackedA32(pixel_in);
188 if (alpha != 0 && alpha != 255) {
189 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel_in);
190 rgba[i + 0] = SkColorGetR(unmultiplied);
191 rgba[i + 1] = SkColorGetG(unmultiplied);
192 rgba[i + 2] = SkColorGetB(unmultiplied);
193 rgba[i + 3] = alpha;
194 } else {
195 rgba[i + 0] = SkGetPackedR32(pixel_in);
196 rgba[i + 1] = SkGetPackedG32(pixel_in);
197 rgba[i + 2] = SkGetPackedB32(pixel_in);
198 rgba[i + 3] = alpha;
203 void QuadFToSkPoints(const gfx::QuadF& quad, SkPoint points[4]) {
204 points[0] = SkPoint::Make(quad.p1().x(), quad.p1().y());
205 points[1] = SkPoint::Make(quad.p2().x(), quad.p2().y());
206 points[2] = SkPoint::Make(quad.p3().x(), quad.p3().y());
207 points[3] = SkPoint::Make(quad.p4().x(), quad.p4().y());
210 } // namespace gfx