| 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/image/image_skia_operations.h" | 5 #include "ui/gfx/image/image_skia_operations.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 return ImageSkiaRep(shadow_bitmap, image_rep.scale()); | 389 return ImageSkiaRep(shadow_bitmap, image_rep.scale()); |
| 390 } | 390 } |
| 391 | 391 |
| 392 private: | 392 private: |
| 393 const ImageSkia source_; | 393 const ImageSkia source_; |
| 394 const ShadowValues shadows_in_dip_; | 394 const ShadowValues shadows_in_dip_; |
| 395 | 395 |
| 396 DISALLOW_COPY_AND_ASSIGN(DropShadowSource); | 396 DISALLOW_COPY_AND_ASSIGN(DropShadowSource); |
| 397 }; | 397 }; |
| 398 | 398 |
| 399 class ShadowNineboxSource : public CanvasImageSource { | |
| 400 public: | |
| 401 ShadowNineboxSource(const std::vector<ShadowValue>& shadows, | |
| 402 float corner_radius) | |
| 403 : CanvasImageSource(CalculateSize(shadows, corner_radius), false), | |
| 404 shadows_(shadows), | |
| 405 corner_radius_(corner_radius) { | |
| 406 DCHECK(!shadows.empty()); | |
| 407 } | |
| 408 ~ShadowNineboxSource() override {} | |
| 409 | |
| 410 // CanvasImageSource overrides: | |
| 411 void Draw(Canvas* canvas) override { | |
| 412 SkPaint paint; | |
| 413 paint.setLooper(CreateShadowDrawLooperCorrectBlur(shadows_)); | |
| 414 Insets insets = -ShadowValue::GetMargin(shadows_); | |
| 415 gfx::Rect bounds(size()); | |
| 416 bounds.Inset(insets); | |
| 417 SkRRect r_rect = SkRRect::MakeRectXY(gfx::RectToSkRect(bounds), | |
| 418 corner_radius_, corner_radius_); | |
| 419 | |
| 420 // Clip out the center so it's not painted with the shadow. | |
| 421 canvas->sk_canvas()->clipRRect(r_rect, SkClipOp::kDifference, true); | |
| 422 // Clipping alone is not enough --- due to anti aliasing there will still be | |
| 423 // some of the fill color in the rounded corners. We must make the fill | |
| 424 // color transparent. | |
| 425 paint.setColor(SK_ColorTRANSPARENT); | |
| 426 canvas->sk_canvas()->drawRRect(r_rect, paint); | |
| 427 } | |
| 428 | |
| 429 private: | |
| 430 static Size CalculateSize(const std::vector<ShadowValue>& shadows, | |
| 431 float corner_radius) { | |
| 432 // The "content" area (the middle tile in the 3x3 grid) is a single pixel. | |
| 433 gfx::Rect bounds(0, 0, 1, 1); | |
| 434 // We need enough space to render the full range of blur. | |
| 435 bounds.Inset(-ShadowValue::GetBlurRegion(shadows)); | |
| 436 // We also need space for the full roundrect corner rounding. | |
| 437 bounds.Inset(-gfx::Insets(corner_radius)); | |
| 438 return bounds.size(); | |
| 439 } | |
| 440 | |
| 441 const std::vector<ShadowValue> shadows_; | |
| 442 | |
| 443 const float corner_radius_; | |
| 444 | |
| 445 DISALLOW_COPY_AND_ASSIGN(ShadowNineboxSource); | |
| 446 }; | |
| 447 | |
| 448 // An image source that is 1px wide, suitable for tiling horizontally. | 399 // An image source that is 1px wide, suitable for tiling horizontally. |
| 449 class HorizontalShadowSource : public CanvasImageSource { | 400 class HorizontalShadowSource : public CanvasImageSource { |
| 450 public: | 401 public: |
| 451 HorizontalShadowSource(const std::vector<ShadowValue>& shadows, | 402 HorizontalShadowSource(const std::vector<ShadowValue>& shadows, |
| 452 bool fades_down) | 403 bool fades_down) |
| 453 : CanvasImageSource(Size(1, GetHeightForShadows(shadows)), false), | 404 : CanvasImageSource(Size(1, GetHeightForShadows(shadows)), false), |
| 454 shadows_(shadows), | 405 shadows_(shadows), |
| 455 fades_down_(fades_down) {} | 406 fades_down_(fades_down) {} |
| 456 ~HorizontalShadowSource() override {} | 407 ~HorizontalShadowSource() override {} |
| 457 | 408 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 return ImageSkia(); | 583 return ImageSkia(); |
| 633 | 584 |
| 634 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); | 585 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); |
| 635 gfx::Size shadow_image_size = source.size(); | 586 gfx::Size shadow_image_size = source.size(); |
| 636 shadow_image_size.Enlarge(shadow_padding.width(), | 587 shadow_image_size.Enlarge(shadow_padding.width(), |
| 637 shadow_padding.height()); | 588 shadow_padding.height()); |
| 638 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); | 589 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); |
| 639 } | 590 } |
| 640 | 591 |
| 641 // static | 592 // static |
| 642 ImageSkia ImageSkiaOperations::CreateShadowNinebox(const ShadowValues& shadows, | |
| 643 float corner_radius) { | |
| 644 auto source = new ShadowNineboxSource(shadows, corner_radius); | |
| 645 return ImageSkia(source, source->size()); | |
| 646 } | |
| 647 | |
| 648 // static | |
| 649 ImageSkia ImageSkiaOperations::CreateHorizontalShadow( | 593 ImageSkia ImageSkiaOperations::CreateHorizontalShadow( |
| 650 const std::vector<ShadowValue>& shadows, | 594 const std::vector<ShadowValue>& shadows, |
| 651 bool fades_down) { | 595 bool fades_down) { |
| 652 auto source = new HorizontalShadowSource(shadows, fades_down); | 596 auto source = new HorizontalShadowSource(shadows, fades_down); |
| 653 return ImageSkia(source, source->size()); | 597 return ImageSkia(source, source->size()); |
| 654 } | 598 } |
| 655 | 599 |
| 656 // static | 600 // static |
| 657 ImageSkia ImageSkiaOperations::CreateRotatedImage( | 601 ImageSkia ImageSkiaOperations::CreateRotatedImage( |
| 658 const ImageSkia& source, | 602 const ImageSkia& source, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 673 if (icon.isNull()) | 617 if (icon.isNull()) |
| 674 return ImageSkia(); | 618 return ImageSkia(); |
| 675 | 619 |
| 676 if (badge.isNull()) | 620 if (badge.isNull()) |
| 677 return icon; | 621 return icon; |
| 678 | 622 |
| 679 return ImageSkia(new IconWithBadgeSource(icon, badge), icon.size()); | 623 return ImageSkia(new IconWithBadgeSource(icon, badge), icon.size()); |
| 680 } | 624 } |
| 681 | 625 |
| 682 } // namespace gfx | 626 } // namespace gfx |
| OLD | NEW |