OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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. | |
4 | |
5 #include "app/gfx/canvas.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "app/gfx/font.h" | |
10 #include "base/i18n/rtl.h" | |
11 #include "base/logging.h" | |
12 #include "gfx/rect.h" | |
13 #include "third_party/skia/include/core/SkShader.h" | |
14 | |
15 namespace gfx { | |
16 | |
17 bool Canvas::GetClipRect(gfx::Rect* r) { | |
18 SkRect clip; | |
19 if (!getClipBounds(&clip)) { | |
20 if (r) | |
21 r->SetRect(0, 0, 0, 0); | |
22 return false; | |
23 } | |
24 r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop), | |
25 SkScalarRound(clip.fRight - clip.fLeft), | |
26 SkScalarRound(clip.fBottom - clip.fTop)); | |
27 return true; | |
28 } | |
29 | |
30 bool Canvas::ClipRectInt(int x, int y, int w, int h) { | |
31 SkRect new_clip; | |
32 new_clip.set(SkIntToScalar(x), SkIntToScalar(y), | |
33 SkIntToScalar(x + w), SkIntToScalar(y + h)); | |
34 return clipRect(new_clip); | |
35 } | |
36 | |
37 bool Canvas::IntersectsClipRectInt(int x, int y, int w, int h) { | |
38 SkRect clip; | |
39 return getClipBounds(&clip) && | |
40 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | |
41 SkIntToScalar(y + h)); | |
42 } | |
43 | |
44 void Canvas::TranslateInt(int x, int y) { | |
45 translate(SkIntToScalar(x), SkIntToScalar(y)); | |
46 } | |
47 | |
48 void Canvas::ScaleInt(int x, int y) { | |
49 scale(SkIntToScalar(x), SkIntToScalar(y)); | |
50 } | |
51 | |
52 void Canvas::FillRectInt(const SkColor& color, int x, int y, int w, int h) { | |
53 SkPaint paint; | |
54 paint.setColor(color); | |
55 paint.setStyle(SkPaint::kFill_Style); | |
56 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
57 FillRectInt(x, y, w, h, paint); | |
58 } | |
59 | |
60 void Canvas::FillRectInt(int x, int y, int w, int h, const SkPaint& paint) { | |
61 SkIRect rc = {x, y, x + w, y + h}; | |
62 drawIRect(rc, paint); | |
63 } | |
64 | |
65 void Canvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h) { | |
66 DrawRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode); | |
67 } | |
68 | |
69 void Canvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h, | |
70 SkXfermode::Mode mode) { | |
71 SkPaint paint; | |
72 paint.setColor(color); | |
73 paint.setStyle(SkPaint::kStroke_Style); | |
74 // Set a stroke width of 0, which will put us down the stroke rect path. If | |
75 // we set a stroke width of 1, for example, this will internally create a | |
76 // path and fill it, which causes problems near the edge of the canvas. | |
77 paint.setStrokeWidth(SkIntToScalar(0)); | |
78 paint.setXfermodeMode(mode); | |
79 | |
80 SkIRect rc = {x, y, x + w, y + h}; | |
81 drawIRect(rc, paint); | |
82 } | |
83 | |
84 void Canvas::DrawLineInt(const SkColor& color, int x1, int y1, int x2, int y2) { | |
85 SkPaint paint; | |
86 paint.setColor(color); | |
87 paint.setStrokeWidth(SkIntToScalar(1)); | |
88 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2), | |
89 SkIntToScalar(y2), paint); | |
90 } | |
91 | |
92 void Canvas::DrawFocusRect(int x, int y, int width, int height) { | |
93 // Create a 2D bitmap containing alternating on/off pixels - we do this | |
94 // so that you never get two pixels of the same color around the edges | |
95 // of the focus rect (this may mean that opposing edges of the rect may | |
96 // have a dot pattern out of phase to each other). | |
97 static SkBitmap* dots = NULL; | |
98 if (!dots) { | |
99 int col_pixels = 32; | |
100 int row_pixels = 32; | |
101 | |
102 dots = new SkBitmap; | |
103 dots->setConfig(SkBitmap::kARGB_8888_Config, col_pixels, row_pixels); | |
104 dots->allocPixels(); | |
105 dots->eraseARGB(0, 0, 0, 0); | |
106 | |
107 uint32_t* dot = dots->getAddr32(0, 0); | |
108 for (int i = 0; i < row_pixels; i++) { | |
109 for (int u = 0; u < col_pixels; u++) { | |
110 if ((u % 2 + i % 2) % 2 != 0) { | |
111 dot[i * row_pixels + u] = SK_ColorGRAY; | |
112 } | |
113 } | |
114 } | |
115 } | |
116 | |
117 // First the horizontal lines. | |
118 | |
119 // Make a shader for the bitmap with an origin of the box we'll draw. This | |
120 // shader is refcounted and will have an initial refcount of 1. | |
121 SkShader* shader = SkShader::CreateBitmapShader( | |
122 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); | |
123 // Assign the shader to the paint & release our reference. The paint will | |
124 // now own the shader and the shader will be destroyed when the paint goes | |
125 // out of scope. | |
126 SkPaint paint; | |
127 paint.setShader(shader); | |
128 shader->unref(); | |
129 | |
130 SkRect rect; | |
131 rect.set(SkIntToScalar(x), SkIntToScalar(y), | |
132 SkIntToScalar(x + width), SkIntToScalar(y + 1)); | |
133 drawRect(rect, paint); | |
134 rect.set(SkIntToScalar(x), SkIntToScalar(y + height - 1), | |
135 SkIntToScalar(x + width), SkIntToScalar(y + height)); | |
136 drawRect(rect, paint); | |
137 | |
138 rect.set(SkIntToScalar(x), SkIntToScalar(y), | |
139 SkIntToScalar(x + 1), SkIntToScalar(y + height)); | |
140 drawRect(rect, paint); | |
141 rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y), | |
142 SkIntToScalar(x + width), SkIntToScalar(y + height)); | |
143 drawRect(rect, paint); | |
144 } | |
145 | |
146 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) { | |
147 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y)); | |
148 } | |
149 | |
150 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y, | |
151 const SkPaint& paint) { | |
152 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); | |
153 } | |
154 | |
155 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, | |
156 int src_w, int src_h, int dest_x, int dest_y, | |
157 int dest_w, int dest_h, | |
158 bool filter) { | |
159 SkPaint p; | |
160 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y, | |
161 dest_w, dest_h, filter, p); | |
162 } | |
163 | |
164 void Canvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, | |
165 int src_w, int src_h, int dest_x, int dest_y, | |
166 int dest_w, int dest_h, | |
167 bool filter, const SkPaint& paint) { | |
168 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | |
169 src_y + src_h < std::numeric_limits<int16_t>::max()); | |
170 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { | |
171 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; | |
172 return; | |
173 } | |
174 | |
175 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | |
176 return; | |
177 | |
178 SkRect dest_rect = { SkIntToScalar(dest_x), | |
179 SkIntToScalar(dest_y), | |
180 SkIntToScalar(dest_x + dest_w), | |
181 SkIntToScalar(dest_y + dest_h) }; | |
182 | |
183 if (src_w == dest_w && src_h == dest_h) { | |
184 // Workaround for apparent bug in Skia that causes image to occasionally | |
185 // shift. | |
186 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; | |
187 drawBitmapRect(bitmap, &src_rect, dest_rect, &paint); | |
188 return; | |
189 } | |
190 | |
191 // Make a bitmap shader that contains the bitmap we want to draw. This is | |
192 // basically what SkCanvas.drawBitmap does internally, but it gives us | |
193 // more control over quality and will use the mipmap in the source image if | |
194 // it has one, whereas drawBitmap won't. | |
195 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | |
196 SkShader::kRepeat_TileMode, | |
197 SkShader::kRepeat_TileMode); | |
198 SkMatrix shader_scale; | |
199 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), | |
200 SkFloatToScalar(static_cast<float>(dest_h) / src_h)); | |
201 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | |
202 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | |
203 shader->setLocalMatrix(shader_scale); | |
204 | |
205 // Set up our paint to use the shader & release our reference (now just owned | |
206 // by the paint). | |
207 SkPaint p(paint); | |
208 p.setFilterBitmap(filter); | |
209 p.setShader(shader); | |
210 shader->unref(); | |
211 | |
212 // The rect will be filled by the bitmap. | |
213 drawRect(dest_rect, p); | |
214 } | |
215 | |
216 void Canvas::DrawStringInt(const std::wstring& text, | |
217 const gfx::Font& font, | |
218 const SkColor& color, | |
219 int x, int y, int w, int h) { | |
220 DrawStringInt(text, font, color, x, y, w, h, | |
221 gfx::Canvas::DefaultCanvasTextAlignment()); | |
222 } | |
223 | |
224 void Canvas::DrawStringInt(const std::wstring& text, | |
225 const gfx::Font& font, | |
226 const SkColor& color, | |
227 const gfx::Rect& display_rect) { | |
228 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(), | |
229 display_rect.width(), display_rect.height()); | |
230 } | |
231 | |
232 void Canvas::TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h) { | |
233 TileImageInt(bitmap, 0, 0, x, y, w, h); | |
234 } | |
235 | |
236 void Canvas::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, | |
237 int dest_x, int dest_y, int w, int h) { | |
238 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | |
239 return; | |
240 | |
241 SkPaint paint; | |
242 | |
243 SkShader* shader = SkShader::CreateBitmapShader(bitmap, | |
244 SkShader::kRepeat_TileMode, | |
245 SkShader::kRepeat_TileMode); | |
246 paint.setShader(shader); | |
247 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
248 | |
249 // CreateBitmapShader returns a Shader with a reference count of one, we | |
250 // need to unref after paint takes ownership of the shader. | |
251 shader->unref(); | |
252 save(); | |
253 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y)); | |
254 ClipRectInt(src_x, src_y, w, h); | |
255 drawPaint(paint); | |
256 restore(); | |
257 } | |
258 | |
259 SkBitmap Canvas::ExtractBitmap() const { | |
260 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false); | |
261 | |
262 // Make a bitmap to return, and a canvas to draw into it. We don't just want | |
263 // to call extractSubset or the copy constuctor, since we want an actual copy | |
264 // of the bitmap. | |
265 SkBitmap result; | |
266 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); | |
267 return result; | |
268 } | |
269 | |
270 // static | |
271 int Canvas::DefaultCanvasTextAlignment() { | |
272 if (!base::i18n::IsRTL()) | |
273 return gfx::Canvas::TEXT_ALIGN_LEFT; | |
274 return gfx::Canvas::TEXT_ALIGN_RIGHT; | |
275 } | |
276 | |
277 } // namespace gfx | |
OLD | NEW |