OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/testing/PictureMatchers.h" | 5 #include "platform/testing/PictureMatchers.h" |
6 | 6 |
7 #include "platform/geometry/FloatQuad.h" | 7 #include "platform/geometry/FloatQuad.h" |
8 #include "platform/geometry/FloatRect.h" | 8 #include "platform/geometry/FloatRect.h" |
9 #include "third_party/skia/include/core/SkCanvas.h" | 9 #include "platform/graphics/paint/PaintCanvas.h" |
10 #include "third_party/skia/include/core/SkPicture.h" | 10 #include "platform/graphics/paint/PaintFlags.h" |
| 11 #include "platform/graphics/paint/PaintRecord.h" |
11 #include "wtf/Vector.h" | 12 #include "wtf/Vector.h" |
12 #include "wtf/text/WTFString.h" | 13 #include "wtf/text/WTFString.h" |
13 #include <utility> | 14 #include <utility> |
14 | 15 |
15 namespace blink { | 16 namespace blink { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 struct QuadWithColor { | 20 struct QuadWithColor { |
20 FloatQuad quad; | 21 FloatQuad quad; |
21 Color color; | 22 Color color; |
22 }; | 23 }; |
23 | 24 |
24 class DrawsRectangleCanvas : public SkCanvas { | 25 class DrawsRectangleCanvas : public PaintCanvas { |
25 public: | 26 public: |
26 DrawsRectangleCanvas() | 27 DrawsRectangleCanvas() |
27 : SkCanvas(800, 600), | 28 : PaintCanvas(800, 600), |
28 m_saveCount(0), | 29 m_saveCount(0), |
29 m_alpha(255), | 30 m_alpha(255), |
30 m_alphaSaveLayerCount(-1) {} | 31 m_alphaSaveLayerCount(-1) {} |
31 const Vector<QuadWithColor>& quadsWithColor() const { return m_quads; } | 32 const Vector<QuadWithColor>& quadsWithColor() const { return m_quads; } |
32 | 33 |
33 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { | 34 void onDrawRect(const SkRect& rect, const PaintFlags& paint) override { |
34 SkRect clippedRect(rect); | 35 SkRect clippedRect(rect); |
35 for (Vector<ClipAndIndex>::const_reverse_iterator clip = m_clips.rbegin(); | 36 for (Vector<ClipAndIndex>::const_reverse_iterator clip = m_clips.rbegin(); |
36 clip != m_clips.rend(); clip++) { | 37 clip != m_clips.rend(); clip++) { |
37 if (SkRect::Intersects(rect, clip->rect)) | 38 if (SkRect::Intersects(rect, clip->rect)) |
38 CHECK(clippedRect.intersect(clip->rect)); | 39 CHECK(clippedRect.intersect(clip->rect)); |
39 } | 40 } |
40 SkPoint quad[4]; | 41 SkPoint quad[4]; |
41 getTotalMatrix().mapRectToQuad(quad, clippedRect); | 42 getTotalMatrix().mapRectToQuad(quad, clippedRect); |
42 QuadWithColor quadWithColor; | 43 QuadWithColor quadWithColor; |
43 quadWithColor.quad = FloatQuad(quad); | 44 quadWithColor.quad = FloatQuad(quad); |
44 | 45 |
45 unsigned paintAlpha = static_cast<unsigned>(paint.getAlpha()); | 46 unsigned paintAlpha = static_cast<unsigned>(paint.getAlpha()); |
46 SkPaint paintWithAlpha(paint); | 47 PaintFlags paintWithAlpha(paint); |
47 paintWithAlpha.setAlpha(static_cast<U8CPU>(m_alpha * paintAlpha / 255)); | 48 paintWithAlpha.setAlpha(static_cast<U8CPU>(m_alpha * paintAlpha / 255)); |
48 quadWithColor.color = Color(paintWithAlpha.getColor()); | 49 quadWithColor.color = Color(paintWithAlpha.getColor()); |
49 m_quads.push_back(quadWithColor); | 50 m_quads.push_back(quadWithColor); |
50 SkCanvas::onDrawRect(clippedRect, paint); | 51 PaintCanvas::onDrawRect(clippedRect, paint); |
51 } | 52 } |
52 | 53 |
53 SkCanvas::SaveLayerStrategy getSaveLayerStrategy( | 54 PaintCanvas::SaveLayerStrategy getSaveLayerStrategy( |
54 const SaveLayerRec& rec) override { | 55 const SaveLayerRec& rec) override { |
55 m_saveCount++; | 56 m_saveCount++; |
56 unsigned layerAlpha = static_cast<unsigned>(rec.fPaint->getAlpha()); | 57 unsigned layerAlpha = static_cast<unsigned>(rec.fPaint->getAlpha()); |
57 if (layerAlpha < 255) { | 58 if (layerAlpha < 255) { |
58 DCHECK_EQ(m_alphaSaveLayerCount, -1); | 59 DCHECK_EQ(m_alphaSaveLayerCount, -1); |
59 m_alphaSaveLayerCount = m_saveCount; | 60 m_alphaSaveLayerCount = m_saveCount; |
60 m_alpha = layerAlpha; | 61 m_alpha = layerAlpha; |
61 } | 62 } |
62 return SkCanvas::getSaveLayerStrategy(rec); | 63 return PaintCanvas::getSaveLayerStrategy(rec); |
63 } | 64 } |
64 | 65 |
65 void willSave() override { | 66 void willSave() override { |
66 m_saveCount++; | 67 m_saveCount++; |
67 SkCanvas::willSave(); | 68 PaintCanvas::willSave(); |
68 } | 69 } |
69 | 70 |
70 void willRestore() override { | 71 void willRestore() override { |
71 DCHECK_GT(m_saveCount, 0); | 72 DCHECK_GT(m_saveCount, 0); |
72 if (m_clips.size() && m_saveCount == m_clips.back().saveCount) | 73 if (m_clips.size() && m_saveCount == m_clips.back().saveCount) |
73 m_clips.pop_back(); | 74 m_clips.pop_back(); |
74 if (m_alphaSaveLayerCount == m_saveCount) { | 75 if (m_alphaSaveLayerCount == m_saveCount) { |
75 m_alpha = 255; | 76 m_alpha = 255; |
76 m_alphaSaveLayerCount = -1; | 77 m_alphaSaveLayerCount = -1; |
77 } | 78 } |
78 m_saveCount--; | 79 m_saveCount--; |
79 SkCanvas::willRestore(); | 80 PaintCanvas::willRestore(); |
80 } | 81 } |
81 | 82 |
82 void onClipRect(const SkRect& rect, | 83 void onClipRect(const SkRect& rect, |
83 SkClipOp op, | 84 SkClipOp op, |
84 ClipEdgeStyle style) override { | 85 ClipEdgeStyle style) override { |
85 ClipAndIndex clipStruct; | 86 ClipAndIndex clipStruct; |
86 clipStruct.rect = rect; | 87 clipStruct.rect = rect; |
87 clipStruct.saveCount = m_saveCount; | 88 clipStruct.saveCount = m_saveCount; |
88 m_clips.push_back(clipStruct); | 89 m_clips.push_back(clipStruct); |
89 SkCanvas::onClipRect(rect, op, style); | 90 PaintCanvas::onClipRect(rect, op, style); |
90 } | 91 } |
91 | 92 |
92 struct ClipAndIndex { | 93 struct ClipAndIndex { |
93 SkRect rect; | 94 SkRect rect; |
94 int saveCount; | 95 int saveCount; |
95 }; | 96 }; |
96 | 97 |
97 private: | 98 private: |
98 Vector<QuadWithColor> m_quads; | 99 Vector<QuadWithColor> m_quads; |
99 Vector<ClipAndIndex> m_clips; | 100 Vector<ClipAndIndex> m_clips; |
100 int m_saveCount; | 101 int m_saveCount; |
101 unsigned m_alpha; | 102 unsigned m_alpha; |
102 int m_alphaSaveLayerCount; | 103 int m_alphaSaveLayerCount; |
103 }; | 104 }; |
104 | 105 |
105 class DrawsRectanglesMatcher | 106 class DrawsRectanglesMatcher |
106 : public ::testing::MatcherInterface<const SkPicture&> { | 107 : public ::testing::MatcherInterface<const PaintRecord&> { |
107 public: | 108 public: |
108 DrawsRectanglesMatcher(const Vector<RectWithColor>& rectsWithColor) | 109 DrawsRectanglesMatcher(const Vector<RectWithColor>& rectsWithColor) |
109 : m_rectsWithColor(rectsWithColor) {} | 110 : m_rectsWithColor(rectsWithColor) {} |
110 | 111 |
111 bool MatchAndExplain( | 112 bool MatchAndExplain( |
112 const SkPicture& picture, | 113 const PaintRecord& picture, |
113 ::testing::MatchResultListener* listener) const override { | 114 ::testing::MatchResultListener* listener) const override { |
114 DrawsRectangleCanvas canvas; | 115 DrawsRectangleCanvas canvas; |
115 picture.playback(&canvas); | 116 picture.playback(&canvas); |
116 const auto& quads = canvas.quadsWithColor(); | 117 const auto& quads = canvas.quadsWithColor(); |
117 if (quads.size() != m_rectsWithColor.size()) { | 118 if (quads.size() != m_rectsWithColor.size()) { |
118 *listener << "which draws " << quads.size() << " quads"; | 119 *listener << "which draws " << quads.size() << " quads"; |
119 return false; | 120 return false; |
120 } | 121 } |
121 | 122 |
122 for (unsigned index = 0; index < quads.size(); index++) { | 123 for (unsigned index = 0; index < quads.size(); index++) { |
(...skipping 26 matching lines...) Expand all Loading... |
149 << "\n"; | 150 << "\n"; |
150 } | 151 } |
151 } | 152 } |
152 | 153 |
153 private: | 154 private: |
154 const Vector<RectWithColor> m_rectsWithColor; | 155 const Vector<RectWithColor> m_rectsWithColor; |
155 }; | 156 }; |
156 | 157 |
157 } // namespace | 158 } // namespace |
158 | 159 |
159 ::testing::Matcher<const SkPicture&> drawsRectangle(const FloatRect& rect, | 160 ::testing::Matcher<const PaintRecord&> drawsRectangle(const FloatRect& rect, |
160 Color color) { | 161 Color color) { |
161 Vector<RectWithColor> rectsWithColor; | 162 Vector<RectWithColor> rectsWithColor; |
162 rectsWithColor.push_back(RectWithColor(rect, color)); | 163 rectsWithColor.push_back(RectWithColor(rect, color)); |
163 return ::testing::MakeMatcher(new DrawsRectanglesMatcher(rectsWithColor)); | 164 return ::testing::MakeMatcher(new DrawsRectanglesMatcher(rectsWithColor)); |
164 } | 165 } |
165 | 166 |
166 ::testing::Matcher<const SkPicture&> drawsRectangles( | 167 ::testing::Matcher<const PaintRecord&> drawsRectangles( |
167 const Vector<RectWithColor>& rectsWithColor) { | 168 const Vector<RectWithColor>& rectsWithColor) { |
168 return ::testing::MakeMatcher(new DrawsRectanglesMatcher(rectsWithColor)); | 169 return ::testing::MakeMatcher(new DrawsRectanglesMatcher(rectsWithColor)); |
169 } | 170 } |
170 | 171 |
171 } // namespace blink | 172 } // namespace blink |
OLD | NEW |