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" |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) { | 328 void Canvas::DrawImageInt(const ImageSkia& image, int x, int y, uint8 a) { |
329 SkPaint paint; | 329 SkPaint paint; |
330 paint.setAlpha(a); | 330 paint.setAlpha(a); |
331 DrawImageInt(image, x, y, paint); | 331 DrawImageInt(image, x, y, paint); |
332 } | 332 } |
333 | 333 |
334 void Canvas::DrawImageInt(const ImageSkia& image, | 334 void Canvas::DrawImageInt(const ImageSkia& image, |
335 int x, | 335 int x, |
336 int y, | 336 int y, |
337 const SkPaint& paint) { | 337 const SkPaint& paint) { |
338 const ImageSkiaRep& image_rep = GetImageRepToPaint(image); | 338 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
339 if (image_rep.is_null()) | 339 if (image_rep.is_null()) |
340 return; | 340 return; |
341 const SkBitmap& bitmap = image_rep.sk_bitmap(); | 341 const SkBitmap& bitmap = image_rep.sk_bitmap(); |
342 float bitmap_scale = image_rep.scale(); | 342 float bitmap_scale = image_rep.scale(); |
343 | 343 |
344 canvas_->save(); | 344 canvas_->save(); |
345 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), | 345 canvas_->scale(SkFloatToScalar(1.0f / bitmap_scale), |
346 SkFloatToScalar(1.0f / bitmap_scale)); | 346 SkFloatToScalar(1.0f / bitmap_scale)); |
347 canvas_->drawBitmap(bitmap, | 347 canvas_->drawBitmap(bitmap, |
348 SkFloatToScalar(x * bitmap_scale), | 348 SkFloatToScalar(x * bitmap_scale), |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 | 434 |
435 // Restore the state of the canvas. | 435 // Restore the state of the canvas. |
436 Restore(); | 436 Restore(); |
437 } | 437 } |
438 | 438 |
439 void Canvas::DrawImageInPath(const ImageSkia& image, | 439 void Canvas::DrawImageInPath(const ImageSkia& image, |
440 int x, | 440 int x, |
441 int y, | 441 int y, |
442 const SkPath& path, | 442 const SkPath& path, |
443 const SkPaint& paint) { | 443 const SkPaint& paint) { |
444 const ImageSkiaRep& image_rep = GetImageRepToPaint(image); | 444 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
445 if (image_rep.is_null()) | 445 if (image_rep.is_null()) |
446 return; | 446 return; |
447 | 447 |
448 SkMatrix matrix; | 448 SkMatrix matrix; |
449 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); | 449 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
450 skia::RefPtr<SkShader> shader = CreateImageRepShader( | 450 skia::RefPtr<SkShader> shader = CreateImageRepShader( |
451 image_rep, | 451 image_rep, |
452 SkShader::kRepeat_TileMode, | 452 SkShader::kRepeat_TileMode, |
453 matrix); | 453 matrix); |
454 | 454 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 int src_y, | 497 int src_y, |
498 float tile_scale_x, | 498 float tile_scale_x, |
499 float tile_scale_y, | 499 float tile_scale_y, |
500 int dest_x, | 500 int dest_x, |
501 int dest_y, | 501 int dest_y, |
502 int w, | 502 int w, |
503 int h) { | 503 int h) { |
504 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) | 504 if (!IntersectsClipRectInt(dest_x, dest_y, w, h)) |
505 return; | 505 return; |
506 | 506 |
507 const ImageSkiaRep& image_rep = GetImageRepToPaint( | 507 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale_); |
508 image, image_scale_, tile_scale_x, tile_scale_y); | |
509 if (image_rep.is_null()) | 508 if (image_rep.is_null()) |
510 return; | 509 return; |
511 | 510 |
512 SkMatrix shader_scale; | 511 SkMatrix shader_scale; |
513 shader_scale.setScale(SkFloatToScalar(tile_scale_x), | 512 shader_scale.setScale(SkFloatToScalar(tile_scale_x), |
514 SkFloatToScalar(tile_scale_y)); | 513 SkFloatToScalar(tile_scale_y)); |
515 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); | 514 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
516 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); | 515 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
517 | 516 |
518 skia::RefPtr<SkShader> shader = CreateImageRepShader( | 517 skia::RefPtr<SkShader> shader = CreateImageRepShader( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 return canvas_->getClipBounds(&clip) && | 554 return canvas_->getClipBounds(&clip) && |
556 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), | 555 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
557 SkIntToScalar(y + h)); | 556 SkIntToScalar(y + h)); |
558 } | 557 } |
559 | 558 |
560 bool Canvas::IntersectsClipRect(const Rect& rect) { | 559 bool Canvas::IntersectsClipRect(const Rect& rect) { |
561 return IntersectsClipRectInt(rect.x(), rect.y(), | 560 return IntersectsClipRectInt(rect.x(), rect.y(), |
562 rect.width(), rect.height()); | 561 rect.width(), rect.height()); |
563 } | 562 } |
564 | 563 |
565 const ImageSkiaRep& Canvas::GetImageRepToPaint(const ImageSkia& image) const { | |
566 return GetImageRepToPaint(image, image_scale_, 1.0f, 1.0f); | |
567 } | |
568 | |
569 const ImageSkiaRep& Canvas::GetImageRepToPaint( | |
570 const ImageSkia& image, | |
571 float image_scale, | |
572 float user_additional_scale_x, | |
573 float user_additional_scale_y) const { | |
574 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale); | |
575 | |
576 if (!image_rep.is_null()) { | |
577 SkMatrix m = canvas_->getTotalMatrix(); | |
578 float scale_x = SkScalarToFloat(SkScalarAbs(m.getScaleX())) * | |
579 user_additional_scale_x; | |
580 float scale_y = SkScalarToFloat(SkScalarAbs(m.getScaleY())) * | |
581 user_additional_scale_y; | |
582 | |
583 float bitmap_scale = image_rep.scale(); | |
584 if (scale_x < bitmap_scale || scale_y < bitmap_scale) | |
585 const_cast<SkBitmap&>(image_rep.sk_bitmap()).buildMipMap(); | |
586 } | |
587 | |
588 return image_rep; | |
589 } | |
590 | |
591 void Canvas::DrawImageIntHelper(const ImageSkia& image, | 564 void Canvas::DrawImageIntHelper(const ImageSkia& image, |
592 int src_x, | 565 int src_x, |
593 int src_y, | 566 int src_y, |
594 int src_w, | 567 int src_w, |
595 int src_h, | 568 int src_h, |
596 int dest_x, | 569 int dest_x, |
597 int dest_y, | 570 int dest_y, |
598 int dest_w, | 571 int dest_w, |
599 int dest_h, | 572 int dest_h, |
600 bool filter, | 573 bool filter, |
601 const SkPaint& paint, | 574 const SkPaint& paint, |
602 float image_scale, | 575 float image_scale, |
603 bool pixel) { | 576 bool pixel) { |
604 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && | 577 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
605 src_y + src_h < std::numeric_limits<int16_t>::max()); | 578 src_y + src_h < std::numeric_limits<int16_t>::max()); |
606 if (src_w <= 0 || src_h <= 0) { | 579 if (src_w <= 0 || src_h <= 0) { |
607 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; | 580 NOTREACHED() << "Attempting to draw bitmap from an empty rect!"; |
608 return; | 581 return; |
609 } | 582 } |
610 | 583 |
611 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) | 584 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h)) |
612 return; | 585 return; |
613 | 586 |
614 float user_scale_x = static_cast<float>(dest_w) / src_w; | 587 float user_scale_x = static_cast<float>(dest_w) / src_w; |
615 float user_scale_y = static_cast<float>(dest_h) / src_h; | 588 float user_scale_y = static_cast<float>(dest_h) / src_h; |
616 | 589 |
617 const ImageSkiaRep& image_rep = GetImageRepToPaint(image, | 590 const ImageSkiaRep& image_rep = image.GetRepresentation(image_scale); |
618 image_scale, user_scale_x, user_scale_y); | |
619 if (image_rep.is_null()) | 591 if (image_rep.is_null()) |
620 return; | 592 return; |
621 | 593 |
622 SkRect dest_rect = { SkIntToScalar(dest_x), | 594 SkRect dest_rect = { SkIntToScalar(dest_x), |
623 SkIntToScalar(dest_y), | 595 SkIntToScalar(dest_y), |
624 SkIntToScalar(dest_x + dest_w), | 596 SkIntToScalar(dest_x + dest_w), |
625 SkIntToScalar(dest_y + dest_h) }; | 597 SkIntToScalar(dest_y + dest_h) }; |
626 | 598 |
627 if (src_w == dest_w && src_h == dest_h && | 599 if (src_w == dest_w && src_h == dest_h && |
628 user_scale_x == 1.0f && user_scale_y == 1.0f && | 600 user_scale_x == 1.0f && user_scale_y == 1.0f && |
(...skipping 18 matching lines...) Expand all Loading... |
647 | 619 |
648 skia::RefPtr<SkShader> shader = CreateImageRepShaderForScale( | 620 skia::RefPtr<SkShader> shader = CreateImageRepShaderForScale( |
649 image_rep, | 621 image_rep, |
650 SkShader::kRepeat_TileMode, | 622 SkShader::kRepeat_TileMode, |
651 shader_scale, | 623 shader_scale, |
652 pixel ? 1.0f : image_scale); | 624 pixel ? 1.0f : image_scale); |
653 | 625 |
654 // Set up our paint to use the shader & release our reference (now just owned | 626 // Set up our paint to use the shader & release our reference (now just owned |
655 // by the paint). | 627 // by the paint). |
656 SkPaint p(paint); | 628 SkPaint p(paint); |
657 p.setFilterBitmap(filter); | 629 p.setFilterLevel(filter ? SkPaint::kLow_FilterLevel |
| 630 : SkPaint::kNone_FilterLevel); |
658 p.setShader(shader.get()); | 631 p.setShader(shader.get()); |
659 | 632 |
660 // The rect will be filled by the bitmap. | 633 // The rect will be filled by the bitmap. |
661 canvas_->drawRect(dest_rect, p); | 634 canvas_->drawRect(dest_rect, p); |
662 } | 635 } |
663 | 636 |
664 } // namespace gfx | 637 } // namespace gfx |
OLD | NEW |