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

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

Issue 2768143002: Back PaintRecord with PaintOpBuffer instead of SkPicture (Closed)
Patch Set: More unit tests 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
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cc/paint/paint_op_buffer.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace cc {
9
10 TEST(PaintOpBufferTest, Empty) {
11 PaintOpBuffer buffer;
12 EXPECT_EQ(buffer.approximateOpCount(), 0);
13 EXPECT_EQ(buffer.approximateBytesUsed(), 0u);
14 EXPECT_EQ(PaintOpBuffer::Iterator(&buffer), false);
15
16 buffer.Reset();
17 EXPECT_EQ(buffer.approximateOpCount(), 0);
18 EXPECT_EQ(buffer.approximateBytesUsed(), 0u);
19 EXPECT_EQ(PaintOpBuffer::Iterator(&buffer), false);
20 }
21
22 TEST(PaintOpBufferTest, SimpleAppend) {
23 SkRect rect = SkRect::MakeXYWH(2, 3, 4, 5);
24 PaintFlags flags;
25 flags.setColor(SK_ColorMAGENTA);
26 flags.setAlpha(100);
27 SkColor draw_color = SK_ColorRED;
28 SkBlendMode blend = SkBlendMode::kSrc;
29
30 PaintOpBuffer buffer;
31 buffer.push<SaveLayerOp>(&rect, &flags);
32 buffer.push<SaveOp>();
33 buffer.push<DrawColorOp>(draw_color, blend);
34 buffer.push<RestoreOp>();
35
36 EXPECT_EQ(buffer.approximateOpCount(), 4);
37
38 PaintOpBuffer::Iterator iter(&buffer);
39 ASSERT_EQ(iter->GetType(), PaintOpType::SaveLayer);
40 SaveLayerOp* save_op = static_cast<SaveLayerOp*>(*iter);
41 EXPECT_EQ(save_op->bounds, rect);
42 EXPECT_TRUE(save_op->flags == flags);
43 ++iter;
44
45 ASSERT_EQ(iter->GetType(), PaintOpType::Save);
46 ++iter;
47
48 ASSERT_EQ(iter->GetType(), PaintOpType::DrawColor);
49 DrawColorOp* op = static_cast<DrawColorOp*>(*iter);
50 EXPECT_EQ(op->color, draw_color);
51 EXPECT_EQ(op->mode, blend);
52 ++iter;
53
54 ASSERT_EQ(iter->GetType(), PaintOpType::Restore);
55 ++iter;
56
57 EXPECT_FALSE(iter);
58 }
59
60 // PaintOpBuffer has a special case for first ops stored locally, so
61 // make sure that appending different kind of ops as a first op works
62 // properly, as well as resetting and reusing the first local op.
63 TEST(PaintOpBufferTest, FirstOpWithAndWithoutData) {
64 PaintOpBuffer buffer;
65 char text[] = "asdf";
66
67 // Use a color filter and its ref count to verify that the destructor
68 // is called on ops after reset.
69 PaintFlags flags;
70 sk_sp<SkColorFilter> filter =
71 SkColorFilter::MakeModeFilter(SK_ColorMAGENTA, SkBlendMode::kSrcOver);
72 flags.setColorFilter(filter);
73 EXPECT_EQ(filter->getRefCnt(), 2);
74
75 buffer.push_with_data<DrawTextOp>(text, arraysize(text), 0.f, 0.f, flags);
76 EXPECT_EQ(filter->getRefCnt(), 3);
77
78 // Verify that when the first op has data, which may not fit in the
79 // PaintRecord internal buffer, that it adds a noop as the first op
80 // and then appends the "op with data" into the heap buffer.
81 ASSERT_EQ(buffer.approximateOpCount(), 2);
82 EXPECT_EQ(buffer.GetFirstOp()->GetType(), PaintOpType::Noop);
83
84 // Verify iteration behavior and brief smoke test of op state.
85 {
86 PaintOpBuffer::Iterator iter(&buffer);
87 PaintOp* noop = *iter;
88 EXPECT_EQ(buffer.GetFirstOp(), noop);
89 ++iter;
90
91 PaintOp* op = *iter;
92 ASSERT_EQ(op->GetType(), PaintOpType::DrawText);
93 DrawTextOp* draw_text_op = static_cast<DrawTextOp*>(op);
94 EXPECT_EQ(draw_text_op->bytes, arraysize(text));
95
96 void* data = paint_op_data(draw_text_op);
97 EXPECT_EQ(memcmp(data, text, arraysize(text)), 0);
98
99 ++iter;
100 EXPECT_FALSE(iter);
101 }
102
103 // Reset, verify state, and append an op that will fit in the first slot.
104 buffer.Reset();
105 EXPECT_EQ(filter->getRefCnt(), 2);
106
107 ASSERT_EQ(buffer.approximateOpCount(), 0);
108 EXPECT_EQ(PaintOpBuffer::Iterator(&buffer), false);
109
110 SkRect rect = SkRect::MakeXYWH(1, 2, 3, 4);
111 buffer.push<DrawRectOp>(rect, flags);
112 EXPECT_EQ(filter->getRefCnt(), 3);
113
114 ASSERT_EQ(buffer.approximateOpCount(), 1);
115 EXPECT_EQ(buffer.GetFirstOp()->GetType(), PaintOpType::DrawRect);
116
117 PaintOpBuffer::Iterator iter(&buffer);
118 ASSERT_EQ(iter->GetType(), PaintOpType::DrawRect);
119 DrawRectOp* draw_rect_op = static_cast<DrawRectOp*>(*iter);
120 EXPECT_EQ(draw_rect_op->rect, rect);
121
122 ++iter;
123 EXPECT_FALSE(iter);
124
125 buffer.Reset();
126 ASSERT_EQ(buffer.approximateOpCount(), 0);
127 EXPECT_EQ(filter->getRefCnt(), 2);
128 }
129
130 // TODO(enne): test Save/restore optimization
131 // TODO(enne): Blink webkit_unit_tests cover slow path counting, but should be
132 // tested separately here.
133
134 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698