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

Side by Side Diff: cc/paint/display_item_list_unittest.cc

Issue 2768143002: Back PaintRecord with PaintOpBuffer instead of SkPicture (Closed)
Patch Set: Remove unneeded expectation Created 3 years, 8 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 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/test_skcanvas.h"
26 #include "cc/paint/transform_display_item.h" 27 #include "cc/paint/transform_display_item.h"
27 #include "cc/test/geometry_test_utils.h" 28 #include "cc/test/geometry_test_utils.h"
28 #include "cc/test/pixel_test_utils.h" 29 #include "cc/test/pixel_test_utils.h"
29 #include "cc/test/skia_common.h" 30 #include "cc/test/skia_common.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"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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.restore_count_);
738 EXPECT_EQ(gfx::RectToSkRect(kVisualRect), canvas.draw_rect_);
739
740 float expected_alpha = 80 * 40 / 255.f;
741 EXPECT_LE(std::abs(expected_alpha - canvas.paint_.getAlpha()), 1.f);
742 }
743
744 // Verify that compositing item / end compositing item is a noop.
745 TEST(DisplayItemListTest, SaveRestoreNoops) {
746 auto list = make_scoped_refptr(new DisplayItemList);
747
748 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>(
749 80, SkBlendMode::kSrcOver, nullptr, nullptr, false);
750 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>();
751 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>(
752 255, SkBlendMode::kSrcOver, nullptr, nullptr, false);
753 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>();
754 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>(
755 255, SkBlendMode::kSrc, nullptr, nullptr, false);
756 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>();
757 list->Finalize();
758
759 SaveCountingCanvas canvas;
760 list->Raster(&canvas, nullptr);
761
762 EXPECT_EQ(0, canvas.restore_count_);
vmpstr 2017/04/12 21:47:12 This is a bit interesting for me. Specifically, fo
mtklein_C 2017/04/12 22:32:00 For what it's worth, SkCanvas does internally dela
enne (OOO) 2017/04/12 22:41:30 OOPS. The unit test appears to pass without this,
enne (OOO) 2017/04/12 22:41:30 Skia does appear to optimize out noop saveLayer re
763 }
764
765 // The same as SaveDrawRestore, but with save flags that prevent the
766 // optimization.
767 TEST(DisplayItemListTest, SaveDrawRestoreFail_BadSaveFlags) {
768 auto list = make_scoped_refptr(new DisplayItemList);
769
770 // Use a blend mode that's not compatible with the SaveDrawRestore
771 // optimization.
772 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>(
773 80, SkBlendMode::kSrc, nullptr, nullptr, false);
774 list->CreateAndAppendDrawingItem<DrawingDisplayItem>(
775 kVisualRect, CreateRectPictureWithAlpha(kVisualRect, 40));
776 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>();
777 list->Finalize();
778
779 SaveCountingCanvas canvas;
780 list->Raster(&canvas, nullptr);
781
782 EXPECT_EQ(1, canvas.restore_count_);
783 EXPECT_EQ(gfx::RectToSkRect(kVisualRect), canvas.draw_rect_);
784 EXPECT_LE(40, canvas.paint_.getAlpha());
785 }
786
787 // The same as SaveDrawRestore, but with too many ops in the PaintRecord.
788 TEST(DisplayItemListTest, SaveDrawRestoreFail_TooManyOps) {
789 sk_sp<const PaintRecord> record;
790 {
791 PaintRecorder recorder;
792 PaintCanvas* canvas =
793 recorder.beginRecording(kVisualRect.width(), kVisualRect.height());
794 PaintFlags flags;
795 flags.setAlpha(40);
796 canvas->drawRect(gfx::RectToSkRect(kVisualRect), flags);
797 // Add an extra op here.
798 canvas->drawRect(gfx::RectToSkRect(kVisualRect), flags);
799 record = recorder.finishRecordingAsPicture();
800 }
801 EXPECT_GT(record->approximateOpCount(), 1);
802
803 auto list = make_scoped_refptr(new DisplayItemList);
804
805 list->CreateAndAppendPairedBeginItem<CompositingDisplayItem>(
806 80, SkBlendMode::kSrcOver, nullptr, nullptr, false);
807 list->CreateAndAppendDrawingItem<DrawingDisplayItem>(kVisualRect,
808 std::move(record));
809 list->CreateAndAppendPairedEndItem<EndCompositingDisplayItem>();
810 list->Finalize();
811
812 SaveCountingCanvas canvas;
813 list->Raster(&canvas, nullptr);
814
815 EXPECT_EQ(1, canvas.restore_count_);
816 EXPECT_EQ(gfx::RectToSkRect(kVisualRect), canvas.draw_rect_);
817 EXPECT_LE(40, canvas.paint_.getAlpha());
818 }
819
707 } // namespace cc 820 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698