Removed unused icon resources from app_list.
[chromium-blink-merge.git] / ui / gfx / skia_util.cc
blob7a9e4ecfe95166d243af90b0ac03d7514ea58904
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/image/image_skia_rep.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/rect_f.h"
17 #include "ui/gfx/shadow_value.h"
18 #include "ui/gfx/transform.h"
20 namespace gfx {
22 SkRect RectToSkRect(const Rect& rect) {
23 SkRect r;
24 r.iset(rect.x(), rect.y(), rect.right(), rect.bottom());
25 return r;
28 SkIRect RectToSkIRect(const Rect& rect) {
29 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height());
32 Rect SkIRectToRect(const SkIRect& rect) {
33 return Rect(rect.x(), rect.y(), rect.width(), rect.height());
36 SkRect RectFToSkRect(const RectF& rect) {
37 return SkRect::MakeXYWH(SkFloatToScalar(rect.x()),
38 SkFloatToScalar(rect.y()),
39 SkFloatToScalar(rect.width()),
40 SkFloatToScalar(rect.height()));
43 RectF SkRectToRectF(const SkRect& rect) {
44 return RectF(SkScalarToFloat(rect.x()),
45 SkScalarToFloat(rect.y()),
46 SkScalarToFloat(rect.width()),
47 SkScalarToFloat(rect.height()));
50 void TransformToFlattenedSkMatrix(const gfx::Transform& transform,
51 SkMatrix* flattened) {
52 // Convert from 4x4 to 3x3 by dropping the third row and column.
53 flattened->set(0, SkMScalarToScalar(transform.matrix().get(0, 0)));
54 flattened->set(1, SkMScalarToScalar(transform.matrix().get(0, 1)));
55 flattened->set(2, SkMScalarToScalar(transform.matrix().get(0, 3)));
56 flattened->set(3, SkMScalarToScalar(transform.matrix().get(1, 0)));
57 flattened->set(4, SkMScalarToScalar(transform.matrix().get(1, 1)));
58 flattened->set(5, SkMScalarToScalar(transform.matrix().get(1, 3)));
59 flattened->set(6, SkMScalarToScalar(transform.matrix().get(3, 0)));
60 flattened->set(7, SkMScalarToScalar(transform.matrix().get(3, 1)));
61 flattened->set(8, SkMScalarToScalar(transform.matrix().get(3, 3)));
64 skia::RefPtr<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep,
65 SkShader::TileMode tile_mode,
66 const SkMatrix& local_matrix) {
67 return CreateImageRepShaderForScale(image_rep, tile_mode, local_matrix,
68 image_rep.scale());
71 skia::RefPtr<SkShader> CreateImageRepShaderForScale(
72 const gfx::ImageSkiaRep& image_rep,
73 SkShader::TileMode tile_mode,
74 const SkMatrix& local_matrix,
75 SkScalar scale) {
76 // Unscale matrix by |scale| such that the bitmap is drawn at the
77 // correct density.
78 // Convert skew and translation to pixel coordinates.
79 // Thus, for |bitmap_scale| = 2:
80 // x scale = 2, x translation = 1 DIP,
81 // should be converted to
82 // x scale = 1, x translation = 2 pixels.
83 SkMatrix shader_scale = local_matrix;
84 shader_scale.preScale(scale, scale);
85 shader_scale.setScaleX(local_matrix.getScaleX() / scale);
86 shader_scale.setScaleY(local_matrix.getScaleY() / scale);
88 skia::RefPtr<SkShader> shader = skia::AdoptRef(SkShader::CreateBitmapShader(
89 image_rep.sk_bitmap(), tile_mode, tile_mode, &shader_scale));
90 return shader;
93 skia::RefPtr<SkShader> CreateGradientShader(int start_point,
94 int end_point,
95 SkColor start_color,
96 SkColor end_color) {
97 SkColor grad_colors[2] = { start_color, end_color};
98 SkPoint grad_points[2];
99 grad_points[0].iset(0, start_point);
100 grad_points[1].iset(0, end_point);
102 return skia::AdoptRef(SkGradientShader::CreateLinear(
103 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode));
106 static SkScalar RadiusToSigma(double radius) {
107 // This captures historically what skia did under the hood. Now skia accepts
108 // sigma, not radius, so we perform the conversion.
109 return radius > 0 ? SkDoubleToScalar(0.57735f * radius + 0.5) : 0;
112 skia::RefPtr<SkDrawLooper> CreateShadowDrawLooper(
113 const std::vector<ShadowValue>& shadows) {
114 if (shadows.empty())
115 return skia::RefPtr<SkDrawLooper>();
117 SkLayerDrawLooper::Builder looper_builder;
119 looper_builder.addLayer(); // top layer of the original.
121 SkLayerDrawLooper::LayerInfo layer_info;
122 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
123 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
124 layer_info.fColorMode = SkXfermode::kSrc_Mode;
126 for (size_t i = 0; i < shadows.size(); ++i) {
127 const ShadowValue& shadow = shadows[i];
129 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
130 SkIntToScalar(shadow.y()));
132 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
133 // original mask, which is half of blur amount as defined in ShadowValue.
134 skia::RefPtr<SkMaskFilter> blur_mask = skia::AdoptRef(
135 SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
136 RadiusToSigma(shadow.blur() / 2),
137 SkBlurMaskFilter::kHighQuality_BlurFlag));
138 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef(
139 SkColorFilter::CreateModeFilter(shadow.color(),
140 SkXfermode::kSrcIn_Mode));
142 SkPaint* paint = looper_builder.addLayer(layer_info);
143 paint->setMaskFilter(blur_mask.get());
144 paint->setColorFilter(color_filter.get());
147 return skia::AdoptRef<SkDrawLooper>(looper_builder.detachLooper());
150 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
151 void* addr1 = NULL;
152 void* addr2 = NULL;
153 size_t size1 = 0;
154 size_t size2 = 0;
156 bitmap1.lockPixels();
157 addr1 = bitmap1.getAddr32(0, 0);
158 size1 = bitmap1.getSize();
159 bitmap1.unlockPixels();
161 bitmap2.lockPixels();
162 addr2 = bitmap2.getAddr32(0, 0);
163 size2 = bitmap2.getSize();
164 bitmap2.unlockPixels();
166 return (size1 == size2) && (0 == memcmp(addr1, addr2, bitmap1.getSize()));
169 void ConvertSkiaToRGBA(const unsigned char* skia,
170 int pixel_width,
171 unsigned char* rgba) {
172 int total_length = pixel_width * 4;
173 for (int i = 0; i < total_length; i += 4) {
174 const uint32_t pixel_in = *reinterpret_cast<const uint32_t*>(&skia[i]);
176 // Pack the components here.
177 SkAlpha alpha = SkGetPackedA32(pixel_in);
178 if (alpha != 0 && alpha != 255) {
179 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel_in);
180 rgba[i + 0] = SkColorGetR(unmultiplied);
181 rgba[i + 1] = SkColorGetG(unmultiplied);
182 rgba[i + 2] = SkColorGetB(unmultiplied);
183 rgba[i + 3] = alpha;
184 } else {
185 rgba[i + 0] = SkGetPackedR32(pixel_in);
186 rgba[i + 1] = SkGetPackedG32(pixel_in);
187 rgba[i + 2] = SkGetPackedB32(pixel_in);
188 rgba[i + 3] = alpha;
193 } // namespace gfx