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

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

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

Powered by Google App Engine
This is Rietveld 408576698