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

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

Issue 11299262: ui: Use skia::RefPtr<T> for implicit safe reference counting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: winbuild 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
« no previous file with comments | « ui/gfx/skia_util.h ('k') | ui/native_theme/native_theme_base.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 = skia::AdoptRef(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::AdoptRef(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 skia::RefPtr<SkDrawLooper>();
91 92
92 SkLayerDrawLooper* looper = new SkLayerDrawLooper; 93 skia::RefPtr<SkLayerDrawLooper> looper =
94 skia::AdoptRef(new SkLayerDrawLooper);
93 95
94 looper->addLayer(); // top layer of the original. 96 looper->addLayer(); // top layer of the original.
95 97
96 SkLayerDrawLooper::LayerInfo layer_info; 98 SkLayerDrawLooper::LayerInfo layer_info;
97 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; 99 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
98 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit; 100 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
99 layer_info.fColorMode = SkXfermode::kSrc_Mode; 101 layer_info.fColorMode = SkXfermode::kSrc_Mode;
100 102
101 for (size_t i = 0; i < shadows.size(); ++i) { 103 for (size_t i = 0; i < shadows.size(); ++i) {
102 const ShadowValue& shadow = shadows[i]; 104 const ShadowValue& shadow = shadows[i];
103 105
104 layer_info.fOffset.set(SkIntToScalar(shadow.x()), 106 layer_info.fOffset.set(SkIntToScalar(shadow.x()),
105 SkIntToScalar(shadow.y())); 107 SkIntToScalar(shadow.y()));
106 108
107 // SkBlurMaskFilter's blur radius defines the range to extend the blur from 109 // 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. 110 // original mask, which is half of blur amount as defined in ShadowValue.
109 SkMaskFilter* blur_mask = SkBlurMaskFilter::Create( 111 skia::RefPtr<SkMaskFilter> blur_mask = skia::AdoptRef(
110 SkDoubleToScalar(shadow.blur() / 2), 112 SkBlurMaskFilter::Create(SkDoubleToScalar(shadow.blur() / 2),
111 SkBlurMaskFilter::kNormal_BlurStyle, 113 SkBlurMaskFilter::kNormal_BlurStyle,
112 SkBlurMaskFilter::kHighQuality_BlurFlag); 114 SkBlurMaskFilter::kHighQuality_BlurFlag));
113 SkColorFilter* color_filter = SkColorFilter::CreateModeFilter( 115 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef(
114 shadow.color(), 116 SkColorFilter::CreateModeFilter(shadow.color(),
115 SkXfermode::kSrcIn_Mode); 117 SkXfermode::kSrcIn_Mode));
116 118
117 SkPaint* paint = looper->addLayer(layer_info); 119 SkPaint* paint = looper->addLayer(layer_info);
118 SkSafeUnref(paint->setMaskFilter(blur_mask)); 120 paint->setMaskFilter(blur_mask.get());
119 SkSafeUnref(paint->setColorFilter(color_filter)); 121 paint->setColorFilter(color_filter.get());
120 } 122 }
121 123
122 return looper; 124 return looper;
123 } 125 }
124 126
125 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { 127 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
126 void* addr1 = NULL; 128 void* addr1 = NULL;
127 void* addr2 = NULL; 129 void* addr2 = NULL;
128 size_t size1 = 0; 130 size_t size1 = 0;
129 size_t size2 = 0; 131 size_t size2 = 0;
(...skipping 29 matching lines...) Expand all
159 } else { 161 } else {
160 rgba[i + 0] = SkGetPackedR32(pixel_in); 162 rgba[i + 0] = SkGetPackedR32(pixel_in);
161 rgba[i + 1] = SkGetPackedG32(pixel_in); 163 rgba[i + 1] = SkGetPackedG32(pixel_in);
162 rgba[i + 2] = SkGetPackedB32(pixel_in); 164 rgba[i + 2] = SkGetPackedB32(pixel_in);
163 rgba[i + 3] = alpha; 165 rgba[i + 3] = alpha;
164 } 166 }
165 } 167 }
166 } 168 }
167 169
168 } // namespace gfx 170 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/skia_util.h ('k') | ui/native_theme/native_theme_base.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698