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"
22 SkRect
RectToSkRect(const Rect
& rect
) {
24 r
.iset(rect
.x(), rect
.y(), rect
.right(), rect
.bottom());
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 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(SkShader::CreateBitmapShader(
68 image_rep
.sk_bitmap(), tile_mode
, tile_mode
));
69 SkScalar scale_x
= local_matrix
.getScaleX();
70 SkScalar scale_y
= local_matrix
.getScaleY();
71 SkScalar bitmap_scale
= SkFloatToScalar(image_rep
.scale());
73 // Unscale matrix by |bitmap_scale| such that the bitmap is drawn at the
75 // Convert skew and translation to pixel coordinates.
76 // Thus, for |bitmap_scale| = 2:
77 // x scale = 2, x translation = 1 DIP,
78 // should be converted to
79 // x scale = 1, x translation = 2 pixels.
80 SkMatrix shader_scale
= local_matrix
;
81 shader_scale
.preScale(bitmap_scale
, bitmap_scale
);
82 shader_scale
.setScaleX(SkScalarDiv(scale_x
, bitmap_scale
));
83 shader_scale
.setScaleY(SkScalarDiv(scale_y
, bitmap_scale
));
85 shader
->setLocalMatrix(shader_scale
);
89 skia::RefPtr
<SkShader
> CreateGradientShader(int start_point
,
93 SkColor grad_colors
[2] = { start_color
, end_color
};
94 SkPoint grad_points
[2];
95 grad_points
[0].iset(0, start_point
);
96 grad_points
[1].iset(0, end_point
);
98 return skia::AdoptRef(SkGradientShader::CreateLinear(
99 grad_points
, grad_colors
, NULL
, 2, SkShader::kRepeat_TileMode
));
102 skia::RefPtr
<SkDrawLooper
> CreateShadowDrawLooper(
103 const std::vector
<ShadowValue
>& shadows
) {
105 return skia::RefPtr
<SkDrawLooper
>();
107 SkLayerDrawLooper::Builder looper_builder
;
109 looper_builder
.addLayer(); // top layer of the original.
111 SkLayerDrawLooper::LayerInfo layer_info
;
112 layer_info
.fPaintBits
|= SkLayerDrawLooper::kMaskFilter_Bit
;
113 layer_info
.fPaintBits
|= SkLayerDrawLooper::kColorFilter_Bit
;
114 layer_info
.fColorMode
= SkXfermode::kSrc_Mode
;
116 for (size_t i
= 0; i
< shadows
.size(); ++i
) {
117 const ShadowValue
& shadow
= shadows
[i
];
119 layer_info
.fOffset
.set(SkIntToScalar(shadow
.x()),
120 SkIntToScalar(shadow
.y()));
122 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
123 // original mask, which is half of blur amount as defined in ShadowValue.
124 skia::RefPtr
<SkMaskFilter
> blur_mask
= skia::AdoptRef(
125 SkBlurMaskFilter::Create(SkDoubleToScalar(shadow
.blur() / 2),
126 SkBlurMaskFilter::kNormal_BlurStyle
,
127 SkBlurMaskFilter::kHighQuality_BlurFlag
));
128 skia::RefPtr
<SkColorFilter
> color_filter
= skia::AdoptRef(
129 SkColorFilter::CreateModeFilter(shadow
.color(),
130 SkXfermode::kSrcIn_Mode
));
132 SkPaint
* paint
= looper_builder
.addLayer(layer_info
);
133 paint
->setMaskFilter(blur_mask
.get());
134 paint
->setColorFilter(color_filter
.get());
137 return skia::AdoptRef
<SkDrawLooper
>(looper_builder
.detachLooper());
140 bool BitmapsAreEqual(const SkBitmap
& bitmap1
, const SkBitmap
& bitmap2
) {
146 bitmap1
.lockPixels();
147 addr1
= bitmap1
.getAddr32(0, 0);
148 size1
= bitmap1
.getSize();
149 bitmap1
.unlockPixels();
151 bitmap2
.lockPixels();
152 addr2
= bitmap2
.getAddr32(0, 0);
153 size2
= bitmap2
.getSize();
154 bitmap2
.unlockPixels();
156 return (size1
== size2
) && (0 == memcmp(addr1
, addr2
, bitmap1
.getSize()));
159 void ConvertSkiaToRGBA(const unsigned char* skia
,
161 unsigned char* rgba
) {
162 int total_length
= pixel_width
* 4;
163 for (int i
= 0; i
< total_length
; i
+= 4) {
164 const uint32_t pixel_in
= *reinterpret_cast<const uint32_t*>(&skia
[i
]);
166 // Pack the components here.
167 int alpha
= SkGetPackedA32(pixel_in
);
168 if (alpha
!= 0 && alpha
!= 255) {
169 SkColor unmultiplied
= SkUnPreMultiply::PMColorToColor(pixel_in
);
170 rgba
[i
+ 0] = SkColorGetR(unmultiplied
);
171 rgba
[i
+ 1] = SkColorGetG(unmultiplied
);
172 rgba
[i
+ 2] = SkColorGetB(unmultiplied
);
175 rgba
[i
+ 0] = SkGetPackedR32(pixel_in
);
176 rgba
[i
+ 1] = SkGetPackedG32(pixel_in
);
177 rgba
[i
+ 2] = SkGetPackedB32(pixel_in
);