ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / gfx / skia_util.cc
blob63827e37b2c6a9f3b9428dd2225c82f3cd751882
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/rect.h"
15 #include "ui/gfx/geometry/rect_f.h"
16 #include "ui/gfx/image/image_skia_rep.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 SkSize SizeFToSkSize(const SizeF& size) {
51 return SkSize::Make(SkFloatToScalar(size.width()),
52 SkFloatToScalar(size.height()));
55 SizeF SkSizeToSizeF(const SkSize& size) {
56 return SizeF(SkScalarToFloat(size.width()), SkScalarToFloat(size.height()));
59 void TransformToFlattenedSkMatrix(const gfx::Transform& transform,
60 SkMatrix* flattened) {
61 // Convert from 4x4 to 3x3 by dropping the third row and column.
62 flattened->set(0, SkMScalarToScalar(transform.matrix().get(0, 0)));
63 flattened->set(1, SkMScalarToScalar(transform.matrix().get(0, 1)));
64 flattened->set(2, SkMScalarToScalar(transform.matrix().get(0, 3)));
65 flattened->set(3, SkMScalarToScalar(transform.matrix().get(1, 0)));
66 flattened->set(4, SkMScalarToScalar(transform.matrix().get(1, 1)));
67 flattened->set(5, SkMScalarToScalar(transform.matrix().get(1, 3)));
68 flattened->set(6, SkMScalarToScalar(transform.matrix().get(3, 0)));
69 flattened->set(7, SkMScalarToScalar(transform.matrix().get(3, 1)));
70 flattened->set(8, SkMScalarToScalar(transform.matrix().get(3, 3)));
73 skia::RefPtr<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep,
74 SkShader::TileMode tile_mode,
75 const SkMatrix& local_matrix) {
76 return CreateImageRepShaderForScale(image_rep, tile_mode, local_matrix,
77 image_rep.scale());
80 skia::RefPtr<SkShader> CreateImageRepShaderForScale(
81 const gfx::ImageSkiaRep& image_rep,
82 SkShader::TileMode tile_mode,
83 const SkMatrix& local_matrix,
84 SkScalar scale) {
85 // Unscale matrix by |scale| such that the bitmap is drawn at the
86 // correct density.
87 // Convert skew and translation to pixel coordinates.
88 // Thus, for |bitmap_scale| = 2:
89 // x scale = 2, x translation = 1 DIP,
90 // should be converted to
91 // x scale = 1, x translation = 2 pixels.
92 SkMatrix shader_scale = local_matrix;
93 shader_scale.preScale(scale, scale);
94 shader_scale.setScaleX(local_matrix.getScaleX() / scale);
95 shader_scale.setScaleY(local_matrix.getScaleY() / scale);
97 skia::RefPtr<SkShader> shader = skia::AdoptRef(SkShader::CreateBitmapShader(
98 image_rep.sk_bitmap(), tile_mode, tile_mode, &shader_scale));
99 return shader;
102 skia::RefPtr<SkShader> CreateGradientShader(int start_point,
103 int end_point,
104 SkColor start_color,
105 SkColor end_color) {
106 SkColor grad_colors[2] = { start_color, end_color};
107 SkPoint grad_points[2];
108 grad_points[0].iset(0, start_point);
109 grad_points[1].iset(0, end_point);
111 return skia::AdoptRef(SkGradientShader::CreateLinear(
112 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode));
115 static SkScalar RadiusToSigma(double radius) {
116 // This captures historically what skia did under the hood. Now skia accepts
117 // sigma, not radius, so we perform the conversion.
118 return radius > 0 ? SkDoubleToScalar(0.57735f * radius + 0.5) : 0;
121 skia::RefPtr<SkDrawLooper> CreateShadowDrawLooper(
122 const std::vector<ShadowValue>& shadows) {
123 if (shadows.empty())
124 return skia::RefPtr<SkDrawLooper>();
126 SkLayerDrawLooper::Builder looper_builder;
128 looper_builder.addLayer(); // top layer of the original.
130 SkLayerDrawLooper::LayerInfo layer_info;
131 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
132 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
133 layer_info.fColorMode = SkXfermode::kSrc_Mode;
135 for (size_t i = 0; i < shadows.size(); ++i) {
136 const ShadowValue& shadow = shadows[i];
138 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
139 SkIntToScalar(shadow.y()));
141 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
142 // original mask, which is half of blur amount as defined in ShadowValue.
143 skia::RefPtr<SkMaskFilter> blur_mask = skia::AdoptRef(
144 SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
145 RadiusToSigma(shadow.blur() / 2),
146 SkBlurMaskFilter::kHighQuality_BlurFlag));
147 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef(
148 SkColorFilter::CreateModeFilter(shadow.color(),
149 SkXfermode::kSrcIn_Mode));
151 SkPaint* paint = looper_builder.addLayer(layer_info);
152 paint->setMaskFilter(blur_mask.get());
153 paint->setColorFilter(color_filter.get());
156 return skia::AdoptRef<SkDrawLooper>(looper_builder.detachLooper());
159 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
160 void* addr1 = NULL;
161 void* addr2 = NULL;
162 size_t size1 = 0;
163 size_t size2 = 0;
165 bitmap1.lockPixels();
166 addr1 = bitmap1.getAddr32(0, 0);
167 size1 = bitmap1.getSize();
168 bitmap1.unlockPixels();
170 bitmap2.lockPixels();
171 addr2 = bitmap2.getAddr32(0, 0);
172 size2 = bitmap2.getSize();
173 bitmap2.unlockPixels();
175 return (size1 == size2) && (0 == memcmp(addr1, addr2, bitmap1.getSize()));
178 void ConvertSkiaToRGBA(const unsigned char* skia,
179 int pixel_width,
180 unsigned char* rgba) {
181 int total_length = pixel_width * 4;
182 for (int i = 0; i < total_length; i += 4) {
183 const uint32_t pixel_in = *reinterpret_cast<const uint32_t*>(&skia[i]);
185 // Pack the components here.
186 SkAlpha alpha = SkGetPackedA32(pixel_in);
187 if (alpha != 0 && alpha != 255) {
188 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel_in);
189 rgba[i + 0] = SkColorGetR(unmultiplied);
190 rgba[i + 1] = SkColorGetG(unmultiplied);
191 rgba[i + 2] = SkColorGetB(unmultiplied);
192 rgba[i + 3] = alpha;
193 } else {
194 rgba[i + 0] = SkGetPackedR32(pixel_in);
195 rgba[i + 1] = SkGetPackedG32(pixel_in);
196 rgba[i + 2] = SkGetPackedB32(pixel_in);
197 rgba[i + 3] = alpha;
202 } // namespace gfx