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

Side by Side Diff: gfx/canvas_skia.cc

Issue 6246027: Move src/gfx/ to src/ui/gfx... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 months 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 | « gfx/canvas_skia.h ('k') | gfx/canvas_skia_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "gfx/canvas_skia.h"
6
7 #include <limits>
8
9 #include "base/i18n/rtl.h"
10 #include "base/logging.h"
11 #include "gfx/brush.h"
12 #include "gfx/font.h"
13 #include "gfx/rect.h"
14 #include "third_party/skia/include/effects/SkGradientShader.h"
15
16 #if defined(OS_WIN)
17 #include "gfx/canvas_skia_paint.h"
18 #endif
19
20 namespace {
21
22 // A platform wrapper for a Skia shader that makes sure the underlying
23 // SkShader object is unref'ed when this object is destroyed.
24 class SkiaShader : public gfx::Brush {
25 public:
26 explicit SkiaShader(SkShader* shader) : shader_(shader) {
27 }
28 virtual ~SkiaShader() {
29 shader_->unref();
30 }
31
32 // Accessor for shader, so it can be associated with a SkPaint.
33 SkShader* shader() const { return shader_; }
34
35 private:
36 SkShader* shader_;
37
38 DISALLOW_COPY_AND_ASSIGN(SkiaShader);
39 };
40
41
42 } // namespace
43
44 namespace gfx {
45
46 ////////////////////////////////////////////////////////////////////////////////
47 // CanvasSkia, public:
48
49 // static
50 int CanvasSkia::DefaultCanvasTextAlignment() {
51 if (!base::i18n::IsRTL())
52 return gfx::Canvas::TEXT_ALIGN_LEFT;
53 return gfx::Canvas::TEXT_ALIGN_RIGHT;
54 }
55
56 SkBitmap CanvasSkia::ExtractBitmap() const {
57 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false);
58
59 // Make a bitmap to return, and a canvas to draw into it. We don't just want
60 // to call extractSubset or the copy constructor, since we want an actual copy
61 // of the bitmap.
62 SkBitmap result;
63 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
64 return result;
65 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68 // CanvasSkia, Canvas implementation:
69
70 void CanvasSkia::Save() {
71 save();
72 }
73
74 void CanvasSkia::SaveLayerAlpha(uint8 alpha) {
75 saveLayerAlpha(NULL, alpha);
76 }
77
78
79 void CanvasSkia::SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) {
80 SkRect bounds;
81 bounds.set(SkIntToScalar(layer_bounds.x()),
82 SkIntToScalar(layer_bounds.y()),
83 SkIntToScalar(layer_bounds.right()),
84 SkIntToScalar(layer_bounds.bottom()));
85 saveLayerAlpha(&bounds, alpha);
86 }
87
88 void CanvasSkia::Restore() {
89 restore();
90 }
91
92 bool CanvasSkia::ClipRectInt(int x, int y, int w, int h) {
93 SkRect new_clip;
94 new_clip.set(SkIntToScalar(x), SkIntToScalar(y),
95 SkIntToScalar(x + w), SkIntToScalar(y + h));
96 return clipRect(new_clip);
97 }
98
99 void CanvasSkia::TranslateInt(int x, int y) {
100 translate(SkIntToScalar(x), SkIntToScalar(y));
101 }
102
103 void CanvasSkia::ScaleInt(int x, int y) {
104 scale(SkIntToScalar(x), SkIntToScalar(y));
105 }
106
107 void CanvasSkia::FillRectInt(const SkColor& color, int x, int y, int w, int h) {
108 FillRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode);
109 }
110
111 void CanvasSkia::FillRectInt(const SkColor& color,
112 int x, int y, int w, int h,
113 SkXfermode::Mode mode) {
114 SkPaint paint;
115 paint.setColor(color);
116 paint.setStyle(SkPaint::kFill_Style);
117 paint.setXfermodeMode(mode);
118 DrawRectInt(x, y, w, h, paint);
119 }
120
121 void CanvasSkia::FillRectInt(const gfx::Brush* brush,
122 int x, int y, int w, int h) {
123 const SkiaShader* shader = static_cast<const SkiaShader*>(brush);
124 SkPaint paint;
125 paint.setShader(shader->shader());
126 // TODO(beng): set shader transform to match canvas transform.
127 DrawRectInt(x, y, w, h, paint);
128 }
129
130 void CanvasSkia::DrawRectInt(const SkColor& color, int x, int y, int w, int h) {
131 DrawRectInt(color, x, y, w, h, SkXfermode::kSrcOver_Mode);
132 }
133
134 void CanvasSkia::DrawRectInt(const SkColor& color,
135 int x, int y, int w, int h,
136 SkXfermode::Mode mode) {
137 SkPaint paint;
138 paint.setColor(color);
139 paint.setStyle(SkPaint::kStroke_Style);
140 // Set a stroke width of 0, which will put us down the stroke rect path. If
141 // we set a stroke width of 1, for example, this will internally create a
142 // path and fill it, which causes problems near the edge of the canvas.
143 paint.setStrokeWidth(SkIntToScalar(0));
144 paint.setXfermodeMode(mode);
145
146 DrawRectInt(x, y, w, h, paint);
147 }
148
149 void CanvasSkia::DrawRectInt(int x, int y, int w, int h, const SkPaint& paint) {
150 SkIRect rc = { x, y, x + w, y + h };
151 drawIRect(rc, paint);
152 }
153
154 void CanvasSkia::DrawLineInt(const SkColor& color,
155 int x1, int y1,
156 int x2, int y2) {
157 SkPaint paint;
158 paint.setColor(color);
159 paint.setStrokeWidth(SkIntToScalar(1));
160 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2),
161 SkIntToScalar(y2), paint);
162 }
163
164 void CanvasSkia::DrawFocusRect(int x, int y, int width, int height) {
165 // Create a 2D bitmap containing alternating on/off pixels - we do this
166 // so that you never get two pixels of the same color around the edges
167 // of the focus rect (this may mean that opposing edges of the rect may
168 // have a dot pattern out of phase to each other).
169 static SkBitmap* dots = NULL;
170 if (!dots) {
171 int col_pixels = 32;
172 int row_pixels = 32;
173
174 dots = new SkBitmap;
175 dots->setConfig(SkBitmap::kARGB_8888_Config, col_pixels, row_pixels);
176 dots->allocPixels();
177 dots->eraseARGB(0, 0, 0, 0);
178
179 uint32_t* dot = dots->getAddr32(0, 0);
180 for (int i = 0; i < row_pixels; i++) {
181 for (int u = 0; u < col_pixels; u++) {
182 if ((u % 2 + i % 2) % 2 != 0) {
183 dot[i * row_pixels + u] = SK_ColorGRAY;
184 }
185 }
186 }
187 }
188
189 // First the horizontal lines.
190
191 // Make a shader for the bitmap with an origin of the box we'll draw. This
192 // shader is refcounted and will have an initial refcount of 1.
193 SkShader* shader = SkShader::CreateBitmapShader(
194 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
195 // Assign the shader to the paint & release our reference. The paint will
196 // now own the shader and the shader will be destroyed when the paint goes
197 // out of scope.
198 SkPaint paint;
199 paint.setShader(shader);
200 shader->unref();
201
202 DrawRectInt(x, y, width, 1, paint);
203 DrawRectInt(x, y + height - 1, width, 1, paint);
204 DrawRectInt(x, y, 1, height, paint);
205 DrawRectInt(x + width - 1, y, 1, height, paint);
206 }
207
208 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) {
209 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y));
210 }
211
212 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap,
213 int x, int y,
214 const SkPaint& paint) {
215 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
216 }
217
218 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap,
219 int src_x, int src_y, int src_w, int src_h,
220 int dest_x, int dest_y, int dest_w, int dest_h,
221 bool filter) {
222 SkPaint p;
223 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y,
224 dest_w, dest_h, filter, p);
225 }
226
227 void CanvasSkia::DrawBitmapInt(const SkBitmap& bitmap,
228 int src_x, int src_y, int src_w, int src_h,
229 int dest_x, int dest_y, int dest_w, int dest_h,
230 bool filter,
231 const SkPaint& paint) {
232 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
233 src_y + src_h < std::numeric_limits<int16_t>::max());
234 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) {
235 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!";
236 return;
237 }
238
239 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
240 return;
241
242 SkRect dest_rect = { SkIntToScalar(dest_x),
243 SkIntToScalar(dest_y),
244 SkIntToScalar(dest_x + dest_w),
245 SkIntToScalar(dest_y + dest_h) };
246
247 if (src_w == dest_w && src_h == dest_h) {
248 // Workaround for apparent bug in Skia that causes image to occasionally
249 // shift.
250 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
251 drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
252 return;
253 }
254
255 // Make a bitmap shader that contains the bitmap we want to draw. This is
256 // basically what SkCanvas.drawBitmap does internally, but it gives us
257 // more control over quality and will use the mipmap in the source image if
258 // it has one, whereas drawBitmap won't.
259 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
260 SkShader::kRepeat_TileMode,
261 SkShader::kRepeat_TileMode);
262 SkMatrix shader_scale;
263 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w),
264 SkFloatToScalar(static_cast<float>(dest_h) / src_h));
265 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
266 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
267 shader->setLocalMatrix(shader_scale);
268
269 // Set up our paint to use the shader & release our reference (now just owned
270 // by the paint).
271 SkPaint p(paint);
272 p.setFilterBitmap(filter);
273 p.setShader(shader);
274 shader->unref();
275
276 // The rect will be filled by the bitmap.
277 drawRect(dest_rect, p);
278 }
279
280 void CanvasSkia::DrawStringInt(const string16& text,
281 const gfx::Font& font,
282 const SkColor& color,
283 int x, int y, int w, int h) {
284 DrawStringInt(text, font, color, x, y, w, h,
285 gfx::CanvasSkia::DefaultCanvasTextAlignment());
286 }
287
288 void CanvasSkia::DrawStringInt(const string16& text,
289 const gfx::Font& font,
290 const SkColor& color,
291 const gfx::Rect& display_rect) {
292 DrawStringInt(text, font, color, display_rect.x(), display_rect.y(),
293 display_rect.width(), display_rect.height());
294 }
295
296 void CanvasSkia::TileImageInt(const SkBitmap& bitmap,
297 int x, int y, int w, int h) {
298 TileImageInt(bitmap, 0, 0, x, y, w, h);
299 }
300
301 void CanvasSkia::TileImageInt(const SkBitmap& bitmap,
302 int src_x, int src_y,
303 int dest_x, int dest_y, int w, int h) {
304 if (!IntersectsClipRectInt(dest_x, dest_y, w, h))
305 return;
306
307 SkPaint paint;
308
309 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
310 SkShader::kRepeat_TileMode,
311 SkShader::kRepeat_TileMode);
312 paint.setShader(shader);
313 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
314
315 // CreateBitmapShader returns a Shader with a reference count of one, we
316 // need to unref after paint takes ownership of the shader.
317 shader->unref();
318 save();
319 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y));
320 ClipRectInt(src_x, src_y, w, h);
321 drawPaint(paint);
322 restore();
323 }
324
325 gfx::NativeDrawingContext CanvasSkia::BeginPlatformPaint() {
326 return beginPlatformPaint();
327 }
328
329 void CanvasSkia::EndPlatformPaint() {
330 endPlatformPaint();
331 }
332
333 CanvasSkia* CanvasSkia::AsCanvasSkia() {
334 return this;
335 }
336
337 const CanvasSkia* CanvasSkia::AsCanvasSkia() const {
338 return this;
339 }
340
341 ////////////////////////////////////////////////////////////////////////////////
342 // CanvasSkia, private:
343
344 bool CanvasSkia::IntersectsClipRectInt(int x, int y, int w, int h) {
345 SkRect clip;
346 return getClipBounds(&clip) &&
347 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
348 SkIntToScalar(y + h));
349 }
350
351 ////////////////////////////////////////////////////////////////////////////////
352 // Canvas, public:
353
354 Canvas* Canvas::CreateCanvas() {
355 return new CanvasSkia;
356 }
357
358 Canvas* Canvas::CreateCanvas(int width, int height, bool is_opaque) {
359 return new CanvasSkia(width, height, is_opaque);
360 }
361
362 #if defined(OS_WIN)
363 // TODO(beng): move to canvas_win.cc, etc.
364 class CanvasPaintWin : public CanvasSkiaPaint, public CanvasPaint {
365 public:
366 CanvasPaintWin(gfx::NativeView view) : CanvasSkiaPaint(view) {}
367
368 // Overridden from CanvasPaint2:
369 virtual bool IsValid() const {
370 return isEmpty();
371 }
372
373 virtual gfx::Rect GetInvalidRect() const {
374 return gfx::Rect(paintStruct().rcPaint);
375 }
376
377 virtual Canvas* AsCanvas() {
378 return this;
379 }
380 };
381 #endif
382
383 CanvasPaint* CanvasPaint::CreateCanvasPaint(gfx::NativeView view) {
384 #if defined(OS_WIN)
385 return new CanvasPaintWin(view);
386 #else
387 return NULL;
388 #endif
389 }
390
391 } // namespace gfx
OLDNEW
« no previous file with comments | « gfx/canvas_skia.h ('k') | gfx/canvas_skia_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698