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

Side by Side Diff: ui/gfx/skia_util.cc

Issue 11418217: Add skia::RefPtr class to wrap ref counted classes from Skia. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Drop TNoRef Created 8 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/skia_util.h" 5 #include "ui/gfx/skia_util.h"
6 6
7 #include "third_party/skia/include/core/SkBitmap.h" 7 #include "third_party/skia/include/core/SkBitmap.h"
8 #include "third_party/skia/include/core/SkColorFilter.h" 8 #include "third_party/skia/include/core/SkColorFilter.h"
9 #include "third_party/skia/include/core/SkColorPriv.h" 9 #include "third_party/skia/include/core/SkColorPriv.h"
10 #include "third_party/skia/include/core/SkUnPreMultiply.h" 10 #include "third_party/skia/include/core/SkUnPreMultiply.h"
(...skipping 29 matching lines...) Expand all
40 } 40 }
41 41
42 RectF SkRectToRectF(const SkRect& rect) { 42 RectF SkRectToRectF(const SkRect& rect) {
43 return RectF(SkScalarToFloat(rect.x()), 43 return RectF(SkScalarToFloat(rect.x()),
44 SkScalarToFloat(rect.y()), 44 SkScalarToFloat(rect.y()),
45 SkScalarToFloat(rect.width()), 45 SkScalarToFloat(rect.width()),
46 SkScalarToFloat(rect.height())); 46 SkScalarToFloat(rect.height()));
47 } 47 }
48 48
49 49
50 SkShader* CreateImageRepShader(const gfx::ImageSkiaRep& image_rep, 50 skia::RefPtr<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep,
51 SkShader::TileMode tile_mode, 51 SkShader::TileMode tile_mode,
52 const SkMatrix& local_matrix) { 52 const SkMatrix& local_matrix) {
53 SkShader* shader = SkShader::CreateBitmapShader(image_rep.sk_bitmap(), 53 skia::RefPtr<SkShader> shader = SkShader::CreateBitmapShader(
54 tile_mode, tile_mode); 54 image_rep.sk_bitmap(), tile_mode, tile_mode);
55 SkScalar scale_x = local_matrix.getScaleX(); 55 SkScalar scale_x = local_matrix.getScaleX();
56 SkScalar scale_y = local_matrix.getScaleY(); 56 SkScalar scale_y = local_matrix.getScaleY();
57 SkScalar bitmap_scale = SkFloatToScalar(image_rep.GetScale()); 57 SkScalar bitmap_scale = SkFloatToScalar(image_rep.GetScale());
58 58
59 // Unscale matrix by |bitmap_scale| such that the bitmap is drawn at the 59 // Unscale matrix by |bitmap_scale| such that the bitmap is drawn at the
60 // correct density. 60 // correct density.
61 // Convert skew and translation to pixel coordinates. 61 // Convert skew and translation to pixel coordinates.
62 // Thus, for |bitmap_scale| = 2: 62 // Thus, for |bitmap_scale| = 2:
63 // x scale = 2, x translation = 1 DIP, 63 // x scale = 2, x translation = 1 DIP,
64 // should be converted to 64 // should be converted to
65 // x scale = 1, x translation = 2 pixels. 65 // x scale = 1, x translation = 2 pixels.
66 SkMatrix shader_scale = local_matrix; 66 SkMatrix shader_scale = local_matrix;
67 shader_scale.preScale(bitmap_scale, bitmap_scale); 67 shader_scale.preScale(bitmap_scale, bitmap_scale);
68 shader_scale.setScaleX(SkScalarDiv(scale_x, bitmap_scale)); 68 shader_scale.setScaleX(SkScalarDiv(scale_x, bitmap_scale));
69 shader_scale.setScaleY(SkScalarDiv(scale_y, bitmap_scale)); 69 shader_scale.setScaleY(SkScalarDiv(scale_y, bitmap_scale));
70 70
71 shader->setLocalMatrix(shader_scale); 71 shader->setLocalMatrix(shader_scale);
72 return shader; 72 return shader;
73 } 73 }
74 74
75 SkShader* CreateGradientShader(int start_point, 75 skia::RefPtr<SkShader> CreateGradientShader(int start_point,
76 int end_point, 76 int end_point,
77 SkColor start_color, 77 SkColor start_color,
78 SkColor end_color) { 78 SkColor end_color) {
79 SkColor grad_colors[2] = { start_color, end_color}; 79 SkColor grad_colors[2] = { start_color, end_color};
80 SkPoint grad_points[2]; 80 SkPoint grad_points[2];
81 grad_points[0].iset(0, start_point); 81 grad_points[0].iset(0, start_point);
82 grad_points[1].iset(0, end_point); 82 grad_points[1].iset(0, end_point);
83 83
84 return SkGradientShader::CreateLinear( 84 return skia::RefPtr<SkShader>(SkGradientShader::CreateLinear(
85 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode); 85 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode));
86 } 86 }
87 87
88 SkDrawLooper* CreateShadowDrawLooper(const std::vector<ShadowValue>& shadows) { 88 skia::RefPtr<SkDrawLooper> CreateShadowDrawLooper(
89 const std::vector<ShadowValue>& shadows) {
89 if (shadows.empty()) 90 if (shadows.empty())
90 return NULL; 91 return NULL;
91 92
92 SkLayerDrawLooper* looper = new SkLayerDrawLooper; 93 skia::RefPtr<SkLayerDrawLooper> looper = new SkLayerDrawLooper;
93 94
94 looper->addLayer(); // top layer of the original. 95 looper->addLayer(); // top layer of the original.
95 96
96 SkLayerDrawLooper::LayerInfo layer_info; 97 SkLayerDrawLooper::LayerInfo layer_info;
97 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; 98 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
98 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit; 99 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
99 layer_info.fColorMode = SkXfermode::kSrc_Mode; 100 layer_info.fColorMode = SkXfermode::kSrc_Mode;
100 101
101 for (size_t i = 0; i < shadows.size(); ++i) { 102 for (size_t i = 0; i < shadows.size(); ++i) {
102 const ShadowValue& shadow = shadows[i]; 103 const ShadowValue& shadow = shadows[i];
103 104
104 layer_info.fOffset.set(SkIntToScalar(shadow.x()), 105 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
105 SkIntToScalar(shadow.y())); 106 SkIntToScalar(shadow.y()));
106 107
107 // SkBlurMaskFilter's blur radius defines the range to extend the blur from 108 // SkBlurMaskFilter's blur radius defines the range to extend the blur from
108 // original mask, which is half of blur amount as defined in ShadowValue. 109 // original mask, which is half of blur amount as defined in ShadowValue.
109 SkMaskFilter* blur_mask = SkBlurMaskFilter::Create( 110 skia::RefPtr<SkMaskFilter> blur_mask = SkBlurMaskFilter::Create(
110 SkDoubleToScalar(shadow.blur() / 2), 111 SkDoubleToScalar(shadow.blur() / 2),
111 SkBlurMaskFilter::kNormal_BlurStyle, 112 SkBlurMaskFilter::kNormal_BlurStyle,
112 SkBlurMaskFilter::kHighQuality_BlurFlag); 113 SkBlurMaskFilter::kHighQuality_BlurFlag);
113 SkColorFilter* color_filter = SkColorFilter::CreateModeFilter( 114 skia::RefPtr<SkColorFilter> color_filter = SkColorFilter::CreateModeFilter(
114 shadow.color(), 115 shadow.color(),
115 SkXfermode::kSrcIn_Mode); 116 SkXfermode::kSrcIn_Mode);
116 117
117 SkPaint* paint = looper->addLayer(layer_info); 118 SkPaint* paint = looper->addLayer(layer_info);
118 SkSafeUnref(paint->setMaskFilter(blur_mask)); 119 paint->setMaskFilter(blur_mask.get());
119 SkSafeUnref(paint->setColorFilter(color_filter)); 120 paint->setColorFilter(color_filter.get());
120 } 121 }
121 122
122 return looper; 123 return looper.PassAs<SkDrawLooper>();
123 } 124 }
124 125
125 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { 126 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
126 void* addr1 = NULL; 127 void* addr1 = NULL;
127 void* addr2 = NULL; 128 void* addr2 = NULL;
128 size_t size1 = 0; 129 size_t size1 = 0;
129 size_t size2 = 0; 130 size_t size2 = 0;
130 131
131 bitmap1.lockPixels(); 132 bitmap1.lockPixels();
132 addr1 = bitmap1.getAddr32(0, 0); 133 addr1 = bitmap1.getAddr32(0, 0);
(...skipping 26 matching lines...) Expand all
159 } else { 160 } else {
160 rgba[i + 0] = SkGetPackedR32(pixel_in); 161 rgba[i + 0] = SkGetPackedR32(pixel_in);
161 rgba[i + 1] = SkGetPackedG32(pixel_in); 162 rgba[i + 1] = SkGetPackedG32(pixel_in);
162 rgba[i + 2] = SkGetPackedB32(pixel_in); 163 rgba[i + 2] = SkGetPackedB32(pixel_in);
163 rgba[i + 3] = alpha; 164 rgba[i + 3] = alpha;
164 } 165 }
165 } 166 }
166 } 167 }
167 168
168 } // namespace gfx 169 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698