OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "cc/paint/display_item_list.h" | 5 #include "cc/paint/display_item_list.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
13 #include "cc/base/filter_operation.h" | 13 #include "cc/base/filter_operation.h" |
14 #include "cc/base/filter_operations.h" | 14 #include "cc/base/filter_operations.h" |
15 #include "cc/paint/clip_display_item.h" | 15 #include "cc/paint/clip_display_item.h" |
16 #include "cc/paint/clip_path_display_item.h" | 16 #include "cc/paint/clip_path_display_item.h" |
17 #include "cc/paint/compositing_display_item.h" | 17 #include "cc/paint/compositing_display_item.h" |
18 #include "cc/paint/drawing_display_item.h" | 18 #include "cc/paint/drawing_display_item.h" |
19 #include "cc/paint/filter_display_item.h" | 19 #include "cc/paint/filter_display_item.h" |
20 | |
21 #include "cc/paint/float_clip_display_item.h" | 20 #include "cc/paint/float_clip_display_item.h" |
22 #include "cc/paint/paint_canvas.h" | 21 #include "cc/paint/paint_canvas.h" |
23 #include "cc/paint/paint_flags.h" | 22 #include "cc/paint/paint_flags.h" |
24 #include "cc/paint/paint_record.h" | 23 #include "cc/paint/paint_record.h" |
25 #include "cc/paint/paint_recorder.h" | 24 #include "cc/paint/paint_recorder.h" |
| 25 #include "cc/paint/skia_paint_canvas.h" |
26 #include "cc/paint/transform_display_item.h" | 26 #include "cc/paint/transform_display_item.h" |
27 #include "cc/test/geometry_test_utils.h" | 27 #include "cc/test/geometry_test_utils.h" |
28 #include "cc/test/pixel_test_utils.h" | 28 #include "cc/test/pixel_test_utils.h" |
29 #include "cc/test/skia_common.h" | 29 #include "cc/test/skia_common.h" |
| 30 #include "cc/test/test_skcanvas.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "third_party/skia/include/core/SkBitmap.h" | 33 #include "third_party/skia/include/core/SkBitmap.h" |
33 #include "third_party/skia/include/core/SkColor.h" | 34 #include "third_party/skia/include/core/SkColor.h" |
34 #include "third_party/skia/include/core/SkSurface.h" | 35 #include "third_party/skia/include/core/SkSurface.h" |
35 #include "third_party/skia/include/effects/SkColorMatrixFilter.h" | 36 #include "third_party/skia/include/effects/SkColorMatrixFilter.h" |
36 #include "third_party/skia/include/effects/SkImageSource.h" | 37 #include "third_party/skia/include/effects/SkImageSource.h" |
37 #include "ui/gfx/geometry/rect.h" | 38 #include "ui/gfx/geometry/rect.h" |
38 #include "ui/gfx/geometry/rect_conversions.h" | 39 #include "ui/gfx/geometry/rect_conversions.h" |
39 #include "ui/gfx/skia_util.h" | 40 #include "ui/gfx/skia_util.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 sk_sp<const PaintRecord> CreateRectPicture(const gfx::Rect& bounds) { | 74 sk_sp<const PaintRecord> CreateRectPicture(const gfx::Rect& bounds) { |
74 PaintRecorder recorder; | 75 PaintRecorder recorder; |
75 PaintCanvas* canvas = | 76 PaintCanvas* canvas = |
76 recorder.beginRecording(bounds.width(), bounds.height()); | 77 recorder.beginRecording(bounds.width(), bounds.height()); |
77 canvas->drawRect( | 78 canvas->drawRect( |
78 SkRect::MakeXYWH(bounds.x(), bounds.y(), bounds.width(), bounds.height()), | 79 SkRect::MakeXYWH(bounds.x(), bounds.y(), bounds.width(), bounds.height()), |
79 PaintFlags()); | 80 PaintFlags()); |
80 return recorder.finishRecordingAsPicture(); | 81 return recorder.finishRecordingAsPicture(); |
81 } | 82 } |
82 | 83 |
| 84 sk_sp<const PaintRecord> CreateRectPictureWithAlpha(const gfx::Rect& bounds, |
| 85 uint8_t alpha) { |
| 86 PaintRecorder recorder; |
| 87 PaintCanvas* canvas = |
| 88 recorder.beginRecording(bounds.width(), bounds.height()); |
| 89 PaintFlags flags; |
| 90 flags.setAlpha(alpha); |
| 91 canvas->drawRect( |
| 92 SkRect::MakeXYWH(bounds.x(), bounds.y(), bounds.width(), bounds.height()), |
| 93 flags); |
| 94 return recorder.finishRecordingAsPicture(); |
| 95 } |
| 96 |
83 void AppendFirstSerializationTestPicture(scoped_refptr<DisplayItemList> list, | 97 void AppendFirstSerializationTestPicture(scoped_refptr<DisplayItemList> list, |
84 const gfx::Size& layer_size) { | 98 const gfx::Size& layer_size) { |
85 gfx::PointF offset(2.f, 3.f); | 99 gfx::PointF offset(2.f, 3.f); |
86 PaintRecorder recorder; | 100 PaintRecorder recorder; |
87 | 101 |
88 PaintFlags red_paint; | 102 PaintFlags red_paint; |
89 red_paint.setColor(SK_ColorRED); | 103 red_paint.setColor(SK_ColorRED); |
90 | 104 |
91 PaintCanvas* canvas = recorder.beginRecording(SkRect::MakeXYWH( | 105 PaintCanvas* canvas = recorder.beginRecording(SkRect::MakeXYWH( |
92 offset.x(), offset.y(), layer_size.width(), layer_size.height())); | 106 offset.x(), offset.y(), layer_size.width(), layer_size.height())); |
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 list->CreateAndAppendPairedEndItem<EndFilterDisplayItem>(); | 711 list->CreateAndAppendPairedEndItem<EndFilterDisplayItem>(); |
698 list->CreateAndAppendPairedEndItem<EndClipDisplayItem>(); | 712 list->CreateAndAppendPairedEndItem<EndClipDisplayItem>(); |
699 | 713 |
700 EXPECT_EQ(4u, list->size()); | 714 EXPECT_EQ(4u, list->size()); |
701 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(0)); | 715 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(0)); |
702 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(1)); | 716 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(1)); |
703 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(2)); | 717 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(2)); |
704 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(3)); | 718 EXPECT_RECT_EQ(filter_bounds, list->VisualRectForTesting(3)); |
705 } | 719 } |
706 | 720 |
| 721 // Verify that raster time optimizations for compositing item / draw single op / |
| 722 // end compositing item can be collapsed together into a single draw op |
| 723 // with the opacity from the compositing item folded in. |
| 724 TEST(DisplayItemListTest, SaveDrawRestore) { |
| 725 auto list = make_scoped_refptr(new DisplayItemList); |
| 726 |
| 727 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>( |
| 728 80, SkBlendMode::kSrcOver, nullptr, nullptr, false); |
| 729 list->CreateAndAppendDrawingItem<DrawingDisplayItem>( |
| 730 kVisualRect, CreateRectPictureWithAlpha(kVisualRect, 40)); |
| 731 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>(); |
| 732 list->Finalize(); |
| 733 |
| 734 SaveCountingCanvas canvas; |
| 735 list->Raster(&canvas, nullptr); |
| 736 |
| 737 EXPECT_EQ(0, canvas.save_count_); |
| 738 EXPECT_EQ(0, canvas.restore_count_); |
| 739 EXPECT_EQ(gfx::RectToSkRect(kVisualRect), canvas.draw_rect_); |
| 740 |
| 741 float expected_alpha = 80 * 40 / 255.f; |
| 742 EXPECT_LE(std::abs(expected_alpha - canvas.paint_.getAlpha()), 1.f); |
| 743 } |
| 744 |
| 745 // Verify that compositing item / end compositing item is a noop. |
| 746 // Here we're testing that Skia does an optimization that skips |
| 747 // save/restore with nothing in between. If skia stops doing this |
| 748 // then we should reimplement this optimization in display list raster. |
| 749 TEST(DisplayItemListTest, SaveRestoreNoops) { |
| 750 auto list = make_scoped_refptr(new DisplayItemList); |
| 751 |
| 752 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>( |
| 753 80, SkBlendMode::kSrcOver, nullptr, nullptr, false); |
| 754 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>(); |
| 755 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>( |
| 756 255, SkBlendMode::kSrcOver, nullptr, nullptr, false); |
| 757 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>(); |
| 758 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>( |
| 759 255, SkBlendMode::kSrc, nullptr, nullptr, false); |
| 760 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>(); |
| 761 list->Finalize(); |
| 762 |
| 763 SaveCountingCanvas canvas; |
| 764 list->Raster(&canvas, nullptr); |
| 765 |
| 766 EXPECT_EQ(0, canvas.save_count_); |
| 767 EXPECT_EQ(0, canvas.restore_count_); |
| 768 } |
| 769 |
| 770 // The same as SaveDrawRestore, but with save flags that prevent the |
| 771 // optimization. |
| 772 TEST(DisplayItemListTest, SaveDrawRestoreFail_BadSaveFlags) { |
| 773 auto list = make_scoped_refptr(new DisplayItemList); |
| 774 |
| 775 // Use a blend mode that's not compatible with the SaveDrawRestore |
| 776 // optimization. |
| 777 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>( |
| 778 80, SkBlendMode::kSrc, nullptr, nullptr, false); |
| 779 list->CreateAndAppendDrawingItem<DrawingDisplayItem>( |
| 780 kVisualRect, CreateRectPictureWithAlpha(kVisualRect, 40)); |
| 781 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>(); |
| 782 list->Finalize(); |
| 783 |
| 784 SaveCountingCanvas canvas; |
| 785 list->Raster(&canvas, nullptr); |
| 786 |
| 787 EXPECT_EQ(1, canvas.save_count_); |
| 788 EXPECT_EQ(1, canvas.restore_count_); |
| 789 EXPECT_EQ(gfx::RectToSkRect(kVisualRect), canvas.draw_rect_); |
| 790 EXPECT_LE(40, canvas.paint_.getAlpha()); |
| 791 } |
| 792 |
| 793 // The same as SaveDrawRestore, but with too many ops in the PaintRecord. |
| 794 TEST(DisplayItemListTest, SaveDrawRestoreFail_TooManyOps) { |
| 795 sk_sp<const PaintRecord> record; |
| 796 { |
| 797 PaintRecorder recorder; |
| 798 PaintCanvas* canvas = |
| 799 recorder.beginRecording(kVisualRect.width(), kVisualRect.height()); |
| 800 PaintFlags flags; |
| 801 flags.setAlpha(40); |
| 802 canvas->drawRect(gfx::RectToSkRect(kVisualRect), flags); |
| 803 // Add an extra op here. |
| 804 canvas->drawRect(gfx::RectToSkRect(kVisualRect), flags); |
| 805 record = recorder.finishRecordingAsPicture(); |
| 806 } |
| 807 EXPECT_GT(record->approximateOpCount(), 1); |
| 808 |
| 809 auto list = make_scoped_refptr(new DisplayItemList); |
| 810 |
| 811 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>( |
| 812 80, SkBlendMode::kSrcOver, nullptr, nullptr, false); |
| 813 list->CreateAndAppendDrawingItem<DrawingDisplayItem>(kVisualRect, |
| 814 std::move(record)); |
| 815 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>(); |
| 816 list->Finalize(); |
| 817 |
| 818 SaveCountingCanvas canvas; |
| 819 list->Raster(&canvas, nullptr); |
| 820 |
| 821 EXPECT_EQ(1, canvas.save_count_); |
| 822 EXPECT_EQ(1, canvas.restore_count_); |
| 823 EXPECT_EQ(gfx::RectToSkRect(kVisualRect), canvas.draw_rect_); |
| 824 EXPECT_LE(40, canvas.paint_.getAlpha()); |
| 825 } |
| 826 |
707 } // namespace cc | 827 } // namespace cc |
OLD | NEW |