OLD | NEW |
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/canvas.h" | 5 #include "ui/gfx/canvas.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "cc/paint/paint_flags.h" |
| 13 #include "cc/paint/paint_shader.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "third_party/skia/include/core/SkPath.h" | 15 #include "third_party/skia/include/core/SkPath.h" |
14 #include "third_party/skia/include/core/SkRefCnt.h" | 16 #include "third_party/skia/include/core/SkRefCnt.h" |
15 #include "third_party/skia/include/core/SkSurface.h" | |
16 #include "third_party/skia/include/effects/SkGradientShader.h" | 17 #include "third_party/skia/include/effects/SkGradientShader.h" |
17 #include "ui/gfx/font_list.h" | 18 #include "ui/gfx/font_list.h" |
18 #include "ui/gfx/geometry/insets_f.h" | 19 #include "ui/gfx/geometry/insets_f.h" |
19 #include "ui/gfx/geometry/rect.h" | 20 #include "ui/gfx/geometry/rect.h" |
20 #include "ui/gfx/geometry/rect_conversions.h" | 21 #include "ui/gfx/geometry/rect_conversions.h" |
21 #include "ui/gfx/geometry/rect_f.h" | 22 #include "ui/gfx/geometry/rect_f.h" |
22 #include "ui/gfx/geometry/safe_integer_conversions.h" | 23 #include "ui/gfx/geometry/safe_integer_conversions.h" |
23 #include "ui/gfx/geometry/size_conversions.h" | 24 #include "ui/gfx/geometry/size_conversions.h" |
24 #include "ui/gfx/scoped_canvas.h" | 25 #include "ui/gfx/scoped_canvas.h" |
25 #include "ui/gfx/skia_paint_util.h" | 26 #include "ui/gfx/skia_paint_util.h" |
26 #include "ui/gfx/skia_util.h" | 27 #include "ui/gfx/skia_util.h" |
27 #include "ui/gfx/transform.h" | 28 #include "ui/gfx/transform.h" |
28 | 29 |
29 namespace gfx { | 30 namespace gfx { |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 sk_sp<SkSurface> CreateSurface(const Size& size, bool is_opaque) { | 34 sk_sp<cc::PaintSurface> CreateSurface(const Size& size, bool is_opaque) { |
34 // SkSurface cannot be zero-sized, but clients of Canvas sometimes request | 35 // SkSurface cannot be zero-sized, but clients of Canvas sometimes request |
35 // that (and then later resize). | 36 // that (and then later resize). |
36 int width = std::max(size.width(), 1); | 37 int width = std::max(size.width(), 1); |
37 int height = std::max(size.height(), 1); | 38 int height = std::max(size.height(), 1); |
38 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | 39 SkAlphaType alpha = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
39 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); | 40 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha); |
40 return SkSurface::MakeRaster(info); | 41 return cc::PaintSurface::MakeRaster(info); |
41 } | 42 } |
42 | 43 |
43 } // namespace | 44 } // namespace |
44 | 45 |
45 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) | 46 Canvas::Canvas(const Size& size, float image_scale, bool is_opaque) |
46 : image_scale_(image_scale) { | 47 : image_scale_(image_scale) { |
47 Size pixel_size = ScaleToCeiledSize(size, image_scale); | 48 Size pixel_size = ScaleToCeiledSize(size, image_scale); |
48 surface_ = CreateSurface(pixel_size, is_opaque); | 49 surface_ = CreateSurface(pixel_size, is_opaque); |
49 canvas_ = surface_->getCanvas(); | 50 canvas_ = surface_->getCanvas(); |
50 | 51 |
51 #if !defined(USE_CAIRO) | 52 #if !defined(USE_CAIRO) |
52 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but | 53 // skia::PlatformCanvas instances are initialized to 0 by Cairo, but |
53 // uninitialized on other platforms. | 54 // uninitialized on other platforms. |
54 if (!is_opaque) | 55 if (!is_opaque) |
55 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); | 56 canvas_->clear(SkColorSetARGB(0, 0, 0, 0)); |
56 #endif | 57 #endif |
57 | 58 |
58 SkScalar scale_scalar = SkFloatToScalar(image_scale); | 59 SkScalar scale_scalar = SkFloatToScalar(image_scale); |
59 canvas_->scale(scale_scalar, scale_scalar); | 60 canvas_->scale(scale_scalar, scale_scalar); |
60 } | 61 } |
61 | 62 |
62 Canvas::Canvas() | 63 Canvas::Canvas() |
63 : image_scale_(1.f), | 64 : image_scale_(1.f), |
64 surface_(CreateSurface({0, 0}, false)), | 65 surface_(CreateSurface({0, 0}, false)), |
65 canvas_(surface_->getCanvas()) {} | 66 canvas_(surface_->getCanvas()) {} |
66 | 67 |
67 Canvas::Canvas(SkCanvas* canvas, float image_scale) | 68 Canvas::Canvas(cc::PaintCanvas* canvas, float image_scale) |
68 : image_scale_(image_scale), canvas_(canvas) { | 69 : image_scale_(image_scale), canvas_(canvas) { |
69 DCHECK(canvas_); | 70 DCHECK(canvas_); |
70 } | 71 } |
71 | 72 |
72 Canvas::~Canvas() { | 73 Canvas::~Canvas() { |
73 } | 74 } |
74 | 75 |
75 void Canvas::RecreateBackingCanvas(const Size& size, | 76 void Canvas::RecreateBackingCanvas(const Size& size, |
76 float image_scale, | 77 float image_scale, |
77 bool is_opaque) { | 78 bool is_opaque) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 for (int i = 0; i < row_pixels; i++) { | 160 for (int i = 0; i < row_pixels; i++) { |
160 for (int u = 0; u < col_pixels; u++) { | 161 for (int u = 0; u < col_pixels; u++) { |
161 if ((u % 2 + i % 2) % 2 != 0) { | 162 if ((u % 2 + i % 2) % 2 != 0) { |
162 dot[i * row_pixels + u] = color; | 163 dot[i * row_pixels + u] = color; |
163 } | 164 } |
164 } | 165 } |
165 } | 166 } |
166 } | 167 } |
167 | 168 |
168 // Make a shader for the bitmap with an origin of the box we'll draw. | 169 // Make a shader for the bitmap with an origin of the box we'll draw. |
169 SkPaint paint; | 170 cc::PaintFlags paint; |
170 paint.setShader(SkShader::MakeBitmapShader(*dots, SkShader::kRepeat_TileMode, | 171 paint.setShader(cc::WrapSkShader(SkShader::MakeBitmapShader( |
171 SkShader::kRepeat_TileMode)); | 172 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode))); |
172 | 173 |
173 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), paint); | 174 DrawRect(RectF(rect.x(), rect.y(), rect.width(), 1), paint); |
174 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), | 175 DrawRect(RectF(rect.x(), rect.y() + rect.height() - 1, rect.width(), 1), |
175 paint); | 176 paint); |
176 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), paint); | 177 DrawRect(RectF(rect.x(), rect.y(), 1, rect.height()), paint); |
177 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), | 178 DrawRect(RectF(rect.x() + rect.width() - 1, rect.y(), 1, rect.height()), |
178 paint); | 179 paint); |
179 } | 180 } |
180 | 181 |
181 float Canvas::UndoDeviceScaleFactor() { | 182 float Canvas::UndoDeviceScaleFactor() { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 242 |
242 void Canvas::DrawColor(SkColor color, SkBlendMode mode) { | 243 void Canvas::DrawColor(SkColor color, SkBlendMode mode) { |
243 canvas_->drawColor(color, mode); | 244 canvas_->drawColor(color, mode); |
244 } | 245 } |
245 | 246 |
246 void Canvas::FillRect(const Rect& rect, SkColor color) { | 247 void Canvas::FillRect(const Rect& rect, SkColor color) { |
247 FillRect(rect, color, SkBlendMode::kSrcOver); | 248 FillRect(rect, color, SkBlendMode::kSrcOver); |
248 } | 249 } |
249 | 250 |
250 void Canvas::FillRect(const Rect& rect, SkColor color, SkBlendMode mode) { | 251 void Canvas::FillRect(const Rect& rect, SkColor color, SkBlendMode mode) { |
251 SkPaint paint; | 252 cc::PaintFlags paint; |
252 paint.setColor(color); | 253 paint.setColor(color); |
253 paint.setStyle(SkPaint::kFill_Style); | 254 paint.setStyle(cc::PaintFlags::kFill_Style); |
254 paint.setBlendMode(mode); | 255 paint.setBlendMode(mode); |
255 DrawRect(rect, paint); | 256 DrawRect(rect, paint); |
256 } | 257 } |
257 | 258 |
258 void Canvas::DrawRect(const Rect& rect, SkColor color) { | 259 void Canvas::DrawRect(const Rect& rect, SkColor color) { |
259 DrawRect(RectF(rect), color); | 260 DrawRect(RectF(rect), color); |
260 } | 261 } |
261 | 262 |
262 void Canvas::DrawRect(const RectF& rect, SkColor color) { | 263 void Canvas::DrawRect(const RectF& rect, SkColor color) { |
263 DrawRect(rect, color, SkBlendMode::kSrcOver); | 264 DrawRect(rect, color, SkBlendMode::kSrcOver); |
264 } | 265 } |
265 | 266 |
266 void Canvas::DrawRect(const Rect& rect, SkColor color, SkBlendMode mode) { | 267 void Canvas::DrawRect(const Rect& rect, SkColor color, SkBlendMode mode) { |
267 DrawRect(RectF(rect), color, mode); | 268 DrawRect(RectF(rect), color, mode); |
268 } | 269 } |
269 | 270 |
270 void Canvas::DrawRect(const RectF& rect, SkColor color, SkBlendMode mode) { | 271 void Canvas::DrawRect(const RectF& rect, SkColor color, SkBlendMode mode) { |
271 SkPaint paint; | 272 cc::PaintFlags paint; |
272 paint.setColor(color); | 273 paint.setColor(color); |
273 paint.setStyle(SkPaint::kStroke_Style); | 274 paint.setStyle(cc::PaintFlags::kStroke_Style); |
274 // Set a stroke width of 0, which will put us down the stroke rect path. If | 275 // Set a stroke width of 0, which will put us down the stroke rect path. If |
275 // we set a stroke width of 1, for example, this will internally create a | 276 // we set a stroke width of 1, for example, this will internally create a |
276 // path and fill it, which causes problems near the edge of the canvas. | 277 // path and fill it, which causes problems near the edge of the canvas. |
277 paint.setStrokeWidth(SkIntToScalar(0)); | 278 paint.setStrokeWidth(SkIntToScalar(0)); |
278 paint.setBlendMode(mode); | 279 paint.setBlendMode(mode); |
279 | 280 |
280 DrawRect(rect, paint); | 281 DrawRect(rect, paint); |
281 } | 282 } |
282 | 283 |
283 void Canvas::DrawRect(const Rect& rect, const SkPaint& paint) { | 284 void Canvas::DrawRect(const Rect& rect, const cc::PaintFlags& paint) { |
284 DrawRect(RectF(rect), paint); | 285 DrawRect(RectF(rect), paint); |
285 } | 286 } |
286 | 287 |
287 void Canvas::DrawRect(const RectF& rect, const SkPaint& paint) { | 288 void Canvas::DrawRect(const RectF& rect, const cc::PaintFlags& paint) { |
288 canvas_->drawRect(RectFToSkRect(rect), paint); | 289 canvas_->drawRect(RectFToSkRect(rect), paint); |
289 } | 290 } |
290 | 291 |
291 void Canvas::DrawPoint(const Point& p1, const SkPaint& paint) { | 292 void Canvas::DrawPoint(const Point& p1, const cc::PaintFlags& paint) { |
292 DrawPoint(PointF(p1), paint); | 293 DrawPoint(PointF(p1), paint); |
293 } | 294 } |
294 | 295 |
295 void Canvas::DrawPoint(const PointF& p1, const SkPaint& paint) { | 296 void Canvas::DrawPoint(const PointF& p1, const cc::PaintFlags& paint) { |
296 canvas_->drawPoint(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), paint); | 297 canvas_->drawPoint(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), paint); |
297 } | 298 } |
298 | 299 |
299 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) { | 300 void Canvas::DrawLine(const Point& p1, const Point& p2, SkColor color) { |
300 DrawLine(PointF(p1), PointF(p2), color); | 301 DrawLine(PointF(p1), PointF(p2), color); |
301 } | 302 } |
302 | 303 |
303 void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) { | 304 void Canvas::DrawLine(const PointF& p1, const PointF& p2, SkColor color) { |
304 SkPaint paint; | 305 cc::PaintFlags paint; |
305 paint.setColor(color); | 306 paint.setColor(color); |
306 paint.setStrokeWidth(SkIntToScalar(1)); | 307 paint.setStrokeWidth(SkIntToScalar(1)); |
307 DrawLine(p1, p2, paint); | 308 DrawLine(p1, p2, paint); |
308 } | 309 } |
309 | 310 |
310 void Canvas::DrawLine(const Point& p1, const Point& p2, const SkPaint& paint) { | 311 void Canvas::DrawLine(const Point& p1, |
| 312 const Point& p2, |
| 313 const cc::PaintFlags& paint) { |
311 DrawLine(PointF(p1), PointF(p2), paint); | 314 DrawLine(PointF(p1), PointF(p2), paint); |
312 } | 315 } |
313 | 316 |
314 void Canvas::DrawLine(const PointF& p1, | 317 void Canvas::DrawLine(const PointF& p1, |
315 const PointF& p2, | 318 const PointF& p2, |
316 const SkPaint& paint) { | 319 const cc::PaintFlags& paint) { |
317 canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), | 320 canvas_->drawLine(SkFloatToScalar(p1.x()), SkFloatToScalar(p1.y()), |
318 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), paint); | 321 SkFloatToScalar(p2.x()), SkFloatToScalar(p2.y()), paint); |
319 } | 322 } |
320 | 323 |
321 void Canvas::DrawCircle(const Point& center_point, | 324 void Canvas::DrawCircle(const Point& center_point, |
322 int radius, | 325 int radius, |
323 const SkPaint& paint) { | 326 const cc::PaintFlags& paint) { |
324 DrawCircle(PointF(center_point), radius, paint); | 327 DrawCircle(PointF(center_point), radius, paint); |
325 } | 328 } |
326 | 329 |
327 void Canvas::DrawCircle(const PointF& center_point, | 330 void Canvas::DrawCircle(const PointF& center_point, |
328 float radius, | 331 float radius, |
329 const SkPaint& paint) { | 332 const cc::PaintFlags& paint) { |
330 canvas_->drawCircle(SkFloatToScalar(center_point.x()), | 333 canvas_->drawCircle(SkFloatToScalar(center_point.x()), |
331 SkFloatToScalar(center_point.y()), | 334 SkFloatToScalar(center_point.y()), |
332 SkFloatToScalar(radius), paint); | 335 SkFloatToScalar(radius), paint); |
333 } | 336 } |
334 | 337 |
335 void Canvas::DrawRoundRect(const Rect& rect, | 338 void Canvas::DrawRoundRect(const Rect& rect, |
336 int radius, | 339 int radius, |
337 const SkPaint& paint) { | 340 const cc::PaintFlags& paint) { |
338 DrawRoundRect(RectF(rect), radius, paint); | 341 DrawRoundRect(RectF(rect), radius, paint); |
339 } | 342 } |
340 | 343 |
341 void Canvas::DrawRoundRect(const RectF& rect, | 344 void Canvas::DrawRoundRect(const RectF& rect, |
342 float radius, | 345 float radius, |
343 const SkPaint& paint) { | 346 const cc::PaintFlags& paint) { |
344 canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius), | 347 canvas_->drawRoundRect(RectFToSkRect(rect), SkFloatToScalar(radius), |
345 SkFloatToScalar(radius), paint); | 348 SkFloatToScalar(radius), paint); |
346 } | 349 } |
347 | 350 |
348 void Canvas::DrawPath(const SkPath& path, const SkPaint& paint) { | 351 void Canvas::DrawPath(const SkPath& path, const cc::PaintFlags& paint) { |
349 canvas_->drawPath(path, paint); | 352 canvas_->drawPath(path, paint); |
350 } | 353 } |
351 | 354 |
352 void Canvas::DrawFocusRect(const Rect& rect) { | 355 void Canvas::DrawFocusRect(const Rect& rect) { |
353 DrawFocusRect(RectF(rect)); | 356 DrawFocusRect(RectF(rect)); |
354 } | 357 } |
355 | 358 |
356 void Canvas::DrawFocusRect(const RectF& rect) { | 359 void Canvas::DrawFocusRect(const RectF& rect) { |
357 DrawDashedRect(rect, SK_ColorGRAY); | 360 DrawDashedRect(rect, SK_ColorGRAY); |
358 } | 361 } |
359 | 362 |
360 void Canvas::DrawSolidFocusRect(const RectF& rect, | 363 void Canvas::DrawSolidFocusRect(const RectF& rect, |
361 SkColor color, | 364 SkColor color, |
362 float thickness) { | 365 float thickness) { |
363 SkPaint paint; | 366 cc::PaintFlags paint; |
364 paint.setColor(color); | 367 paint.setColor(color); |
365 paint.setStrokeWidth(SkFloatToScalar(thickness)); | 368 paint.setStrokeWidth(SkFloatToScalar(thickness)); |
366 paint.setStyle(SkPaint::kStroke_Style); | 369 paint.setStyle(cc::PaintFlags::kStroke_Style); |
367 gfx::RectF draw_rect = rect; | 370 gfx::RectF draw_rect = rect; |
368 draw_rect.Inset(gfx::InsetsF(thickness / 2)); | 371 draw_rect.Inset(gfx::InsetsF(thickness / 2)); |
369 DrawRect(draw_rect, paint); | 372 DrawRect(draw_rect, paint); |
370 } | 373 } |
371 | 374 |
372 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) { | 375 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y) { |
373 SkPaint paint; | 376 cc::PaintFlags paint; |
374 DrawImageInt(image, x, y, paint); | 377 DrawImageInt(image, x, y, paint); |
375 } | 378 } |
376 | 379 |
377 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) { | 380 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8_t a) { |
378 SkPaint paint; | 381 cc::PaintFlags paint; |
379 paint.setAlpha(a); | 382 paint.setAlpha(a); |
380 DrawImageInt(image, x, y, paint); | 383 DrawImageInt(image, x, y, paint); |
381 } | 384 } |
382 | 385 |
383 void Canvas::DrawImageInt(const ImageSkia& image, | 386 void Canvas::DrawImageInt(const ImageSkia& image, |
384 int x, | 387 int x, |
385 int y, | 388 int y, |
386 const SkPaint& paint) { | 389 const cc::PaintFlags& paint) { |
387 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 390 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
388 if (image_rep.is_null()) | 391 if (image_rep.is_null()) |
389 return; | 392 return; |
390 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 393 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
391 float bitmap_scale = image_rep.scale(); | 394 float bitmap_scale = image_rep.scale(); |
392 | 395 |
393 ScopedCanvas scoper(this); | 396 ScopedCanvas scoper(this); |
394 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 397 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
395 SkFloatToScalar(1.0f / bitmap_scale)); | 398 SkFloatToScalar(1.0f / bitmap_scale)); |
396 canvas_->drawBitmap(bitmap, | 399 canvas_->drawBitmap(bitmap, |
397 SkFloatToScalar(x * bitmap_scale), | 400 SkFloatToScalar(x * bitmap_scale), |
398 SkFloatToScalar(y * bitmap_scale), | 401 SkFloatToScalar(y * bitmap_scale), |
399 &paint); | 402 &paint); |
400 } | 403 } |
401 | 404 |
402 void Canvas::DrawImageInt(const ImageSkia& image, | 405 void Canvas::DrawImageInt(const ImageSkia& image, |
403 int src_x, | 406 int src_x, |
404 int src_y, | 407 int src_y, |
405 int src_w, | 408 int src_w, |
406 int src_h, | 409 int src_h, |
407 int dest_x, | 410 int dest_x, |
408 int dest_y, | 411 int dest_y, |
409 int dest_w, | 412 int dest_w, |
410 int dest_h, | 413 int dest_h, |
411 bool filter) { | 414 bool filter) { |
412 SkPaint p; | 415 cc::PaintFlags p; |
413 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, | 416 DrawImageInt(image, src_x, src_y, src_w, src_h, dest_x, dest_y, |
414 dest_w, dest_h, filter, p); | 417 dest_w, dest_h, filter, p); |
415 } | 418 } |
416 | 419 |
417 void Canvas::DrawImageInt(const ImageSkia& image, | 420 void Canvas::DrawImageInt(const ImageSkia& image, |
418 int src_x, | 421 int src_x, |
419 int src_y, | 422 int src_y, |
420 int src_w, | 423 int src_w, |
421 int src_h, | 424 int src_h, |
422 int dest_x, | 425 int dest_x, |
423 int dest_y, | 426 int dest_y, |
424 int dest_w, | 427 int dest_w, |
425 int dest_h, | 428 int dest_h, |
426 bool filter, | 429 bool filter, |
427 const SkPaint& paint) { | 430 const cc::PaintFlags& paint) { |
428 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 431 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
429 if (image_rep.is_null()) | 432 if (image_rep.is_null()) |
430 return; | 433 return; |
431 bool remove_image_scale = true; | 434 bool remove_image_scale = true; |
432 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, | 435 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, |
433 dest_w, dest_h, filter, paint, remove_image_scale); | 436 dest_w, dest_h, filter, paint, remove_image_scale); |
434 } | 437 } |
435 | 438 |
436 void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep, | 439 void Canvas::DrawImageIntInPixel(const ImageSkiaRep& image_rep, |
437 int dest_x, | 440 int dest_x, |
438 int dest_y, | 441 int dest_y, |
439 int dest_w, | 442 int dest_w, |
440 int dest_h, | 443 int dest_h, |
441 bool filter, | 444 bool filter, |
442 const SkPaint& paint) { | 445 const cc::PaintFlags& paint) { |
443 int src_x = 0; | 446 int src_x = 0; |
444 int src_y = 0; | 447 int src_y = 0; |
445 int src_w = image_rep.pixel_width(); | 448 int src_w = image_rep.pixel_width(); |
446 int src_h = image_rep.pixel_height(); | 449 int src_h = image_rep.pixel_height(); |
447 // Don't remove image scale here, this function is used to draw the | 450 // Don't remove image scale here, this function is used to draw the |
448 // (already scaled) |image_rep| at a 1:1 scale with the canvas. | 451 // (already scaled) |image_rep| at a 1:1 scale with the canvas. |
449 bool remove_image_scale = false; | 452 bool remove_image_scale = false; |
450 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, | 453 DrawImageIntHelper(image_rep, src_x, src_y, src_w, src_h, dest_x, dest_y, |
451 dest_w, dest_h, filter, paint, remove_image_scale); | 454 dest_w, dest_h, filter, paint, remove_image_scale); |
452 } | 455 } |
453 | 456 |
454 void Canvas::DrawImageInPath(const ImageSkia& image, | 457 void Canvas::DrawImageInPath(const ImageSkia& image, |
455 int x, | 458 int x, |
456 int y, | 459 int y, |
457 const SkPath& path, | 460 const SkPath& path, |
458 const SkPaint& paint) { | 461 const cc::PaintFlags& paint) { |
459 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 462 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
460 if (image_rep.is_null()) | 463 if (image_rep.is_null()) |
461 return; | 464 return; |
462 | 465 |
463 SkMatrix matrix; | 466 SkMatrix matrix; |
464 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); | 467 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
465 SkPaint p(paint); | 468 cc::PaintFlags p(paint); |
466 p.setShader(CreateImageRepShader(image_rep, | 469 p.setShader( |
467 SkShader::kRepeat_TileMode, | 470 CreateImageRepShader(image_rep, SkShader::kRepeat_TileMode, matrix)); |
468 matrix)); | |
469 canvas_->drawPath(path, p); | 471 canvas_->drawPath(path, p); |
470 } | 472 } |
471 | 473 |
472 void Canvas::DrawStringRect(const base::string16& text, | 474 void Canvas::DrawStringRect(const base::string16& text, |
473 const FontList& font_list, | 475 const FontList& font_list, |
474 SkColor color, | 476 SkColor color, |
475 const Rect& display_rect) { | 477 const Rect& display_rect) { |
476 DrawStringRectWithFlags(text, font_list, color, display_rect, | 478 DrawStringRectWithFlags(text, font_list, color, display_rect, |
477 DefaultCanvasTextAlignment()); | 479 DefaultCanvasTextAlignment()); |
478 } | 480 } |
(...skipping 25 matching lines...) Expand all Loading... |
504 int dest_y, | 506 int dest_y, |
505 int w, | 507 int w, |
506 int h) { | 508 int h) { |
507 SkRect dest_rect = { SkIntToScalar(dest_x), | 509 SkRect dest_rect = { SkIntToScalar(dest_x), |
508 SkIntToScalar(dest_y), | 510 SkIntToScalar(dest_y), |
509 SkIntToScalar(dest_x + w), | 511 SkIntToScalar(dest_x + w), |
510 SkIntToScalar(dest_y + h) }; | 512 SkIntToScalar(dest_y + h) }; |
511 if (!IntersectsClipRect(dest_rect)) | 513 if (!IntersectsClipRect(dest_rect)) |
512 return; | 514 return; |
513 | 515 |
514 SkPaint paint; | 516 cc::PaintFlags paint; |
515 if (InitSkPaintForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y, | 517 if (InitPaintFlagsForTiling(image, src_x, src_y, tile_scale_x, tile_scale_y, |
516 dest_x, dest_y, &paint)) | 518 dest_x, dest_y, &paint)) |
517 canvas_->drawRect(dest_rect, paint); | 519 canvas_->drawRect(dest_rect, paint); |
518 } | 520 } |
519 | 521 |
520 bool Canvas::InitSkPaintForTiling(const ImageSkia& image, | 522 bool Canvas::InitPaintFlagsForTiling(const ImageSkia& image, |
521 int src_x, | 523 int src_x, |
522 int src_y, | 524 int src_y, |
523 float tile_scale_x, | 525 float tile_scale_x, |
524 float tile_scale_y, | 526 float tile_scale_y, |
525 int dest_x, | 527 int dest_x, |
526 int dest_y, | 528 int dest_y, |
527 SkPaint* paint) { | 529 cc::PaintFlags* paint) { |
528 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); | 530 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
529 if (image_rep.is_null()) | 531 if (image_rep.is_null()) |
530 return false; | 532 return false; |
531 | 533 |
532 SkMatrix shader_scale; | 534 SkMatrix shader_scale; |
533 shader_scale.setScale(SkFloatToScalar(tile_scale_x), | 535 shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
534 SkFloatToScalar(tile_scale_y)); | 536 SkFloatToScalar(tile_scale_y)); |
535 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 537 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
536 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 538 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
537 | 539 |
(...skipping 15 matching lines...) Expand all Loading... |
553 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, | 555 void Canvas::DrawImageIntHelper(const ImageSkiaRep& image_rep, |
554 int src_x, | 556 int src_x, |
555 int src_y, | 557 int src_y, |
556 int src_w, | 558 int src_w, |
557 int src_h, | 559 int src_h, |
558 int dest_x, | 560 int dest_x, |
559 int dest_y, | 561 int dest_y, |
560 int dest_w, | 562 int dest_w, |
561 int dest_h, | 563 int dest_h, |
562 bool filter, | 564 bool filter, |
563 const SkPaint& paint, | 565 const cc::PaintFlags& paint, |
564 bool remove_image_scale) { | 566 bool remove_image_scale) { |
565 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 567 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
566 src_y + src_h < std::numeric_limits<int16_t>::max()); | 568 src_y + src_h < std::numeric_limits<int16_t>::max()); |
567 if (src_w <= 0 || src_h <= 0) { | 569 if (src_w <= 0 || src_h <= 0) { |
568 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 570 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
569 return; | 571 return; |
570 } | 572 } |
571 | 573 |
572 SkRect dest_rect = { SkIntToScalar(dest_x), | 574 SkRect dest_rect = { SkIntToScalar(dest_x), |
573 SkIntToScalar(dest_y), | 575 SkIntToScalar(dest_y), |
574 SkIntToScalar(dest_x + dest_w), | 576 SkIntToScalar(dest_x + dest_w), |
575 SkIntToScalar(dest_y + dest_h) }; | 577 SkIntToScalar(dest_y + dest_h) }; |
576 if (!IntersectsClipRect(dest_rect)) | 578 if (!IntersectsClipRect(dest_rect)) |
577 return; | 579 return; |
578 | 580 |
579 float user_scale_x = static_cast<float>(dest_w) / src_w; | 581 float user_scale_x = static_cast<float>(dest_w) / src_w; |
580 float user_scale_y = static_cast<float>(dest_h) / src_h; | 582 float user_scale_y = static_cast<float>(dest_h) / src_h; |
581 | 583 |
582 // Make a bitmap shader that contains the bitmap we want to draw. This is | 584 // Make a bitmap shader that contains the bitmap we want to draw. This is |
583 // basically what SkCanvas.drawBitmap does internally, but it gives us | 585 // basically what SkCanvas.drawBitmap does internally, but it gives us |
584 // more control over quality and will use the mipmap in the source image if | 586 // more control over quality and will use the mipmap in the source image if |
585 // it has one, whereas drawBitmap won't. | 587 // it has one, whereas drawBitmap won't. |
586 SkMatrix shader_scale; | 588 SkMatrix shader_scale; |
587 shader_scale.setScale(SkFloatToScalar(user_scale_x), | 589 shader_scale.setScale(SkFloatToScalar(user_scale_x), |
588 SkFloatToScalar(user_scale_y)); | 590 SkFloatToScalar(user_scale_y)); |
589 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 591 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
590 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 592 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
591 | 593 |
592 SkPaint p(paint); | 594 cc::PaintFlags p(paint); |
593 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); | 595 p.setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
594 p.setShader(CreateImageRepShaderForScale( | 596 p.setShader(CreateImageRepShaderForScale( |
595 image_rep, SkShader::kRepeat_TileMode, shader_scale, | 597 image_rep, SkShader::kRepeat_TileMode, shader_scale, |
596 remove_image_scale ? image_rep.scale() : 1.f)); | 598 remove_image_scale ? image_rep.scale() : 1.f)); |
597 | 599 |
598 // The rect will be filled by the bitmap. | 600 // The rect will be filled by the bitmap. |
599 canvas_->drawRect(dest_rect, p); | 601 canvas_->drawRect(dest_rect, p); |
600 } | 602 } |
601 | 603 |
602 } // namespace gfx | 604 } // namespace gfx |
OLD | NEW |