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

Side by Side Diff: third_party/WebKit/Source/platform/testing/PictureMatchers.cpp

Issue 2581843002: Implement merging non-composited paint property nodes in the PACompositor. (Closed)
Patch Set: none Created 4 years 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 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 "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkPicture.h" 10 #include "third_party/skia/include/core/SkPicture.h"
11 #include "wtf/Vector.h" 11 #include "wtf/Vector.h"
12 #include "wtf/text/WTFString.h" 12 #include "wtf/text/WTFString.h"
13 #include <utility> 13 #include <utility>
14 14
15 namespace blink { 15 namespace blink {
16 16
17 namespace { 17 namespace {
18 18
19 struct QuadWithColor {
20 FloatQuad quad;
21 Color color;
22 };
23
19 class DrawsRectangleCanvas : public SkCanvas { 24 class DrawsRectangleCanvas : public SkCanvas {
20 public: 25 public:
21 DrawsRectangleCanvas() : SkCanvas(800, 600) {} 26 DrawsRectangleCanvas() : SkCanvas(800, 600), m_saveCount(0) {}
22 const Vector<std::pair<FloatQuad, Color>>& quads() const { return m_quads; } 27 const Vector<QuadWithColor>& quadsWithColor() const { return m_quads; }
28
23 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { 29 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
30 SkRect clippedRect(rect);
31 for (Vector<ClipAndIndex>::const_reverse_iterator clip = m_clips.rbegin();
32 clip != m_clips.rend(); clip++) {
33 if (SkRect::Intersects(rect, clip->rect))
34 CHECK(clippedRect.intersect(clip->rect));
35 }
36
24 SkPoint quad[4]; 37 SkPoint quad[4];
25 getTotalMatrix().mapRectToQuad(quad, rect); 38 getTotalMatrix().mapRectToQuad(quad, clippedRect);
26 FloatQuad floatQuad(quad); 39 QuadWithColor quadWithColor;
27 m_quads.append(std::make_pair(floatQuad, Color(paint.getColor()))); 40 quadWithColor.quad = FloatQuad(quad);
28 SkCanvas::onDrawRect(rect, paint); 41 quadWithColor.color = Color(paint.getColor());
42 m_quads.append(quadWithColor);
43 SkCanvas::onDrawRect(clippedRect, paint);
29 } 44 }
30 45
46 SkCanvas::SaveLayerStrategy getSaveLayerStrategy(
47 const SaveLayerRec& rec) override {
48 m_saveCount++;
49 return SkCanvas::getSaveLayerStrategy(rec);
50 }
51
52 void willSave() override {
53 m_saveCount++;
54 SkCanvas::willSave();
55 }
56
57 void willRestore() override {
58 DCHECK(m_saveCount > 0);
59 if (m_clips.size() && m_saveCount == m_clips.back().saveCount)
60 m_clips.pop_back();
61
62 m_saveCount--;
63 SkCanvas::willRestore();
64 }
65
66 void onClipRect(const SkRect& rect,
67 SkClipOp op,
68 ClipEdgeStyle style) override {
69 ClipAndIndex clipStruct;
70 clipStruct.rect = rect;
71 clipStruct.saveCount = m_saveCount;
72 m_clips.push_back(clipStruct);
73 SkCanvas::onClipRect(rect, op, style);
74 }
75
76 struct ClipAndIndex {
77 SkRect rect;
78 unsigned saveCount;
79 };
80
31 private: 81 private:
32 Vector<std::pair<FloatQuad, Color>> m_quads; 82 Vector<QuadWithColor> m_quads;
83 Vector<ClipAndIndex> m_clips;
84 unsigned m_saveCount;
33 }; 85 };
34 86
35 class DrawsRectangleMatcher 87 class DrawsRectanglesMatcher
36 : public ::testing::MatcherInterface<const SkPicture&> { 88 : public ::testing::MatcherInterface<const SkPicture&> {
37 public: 89 public:
38 DrawsRectangleMatcher(const FloatRect& rect, Color color) 90 DrawsRectanglesMatcher(const Vector<RectWithColor>& rectsWithColor)
39 : m_rect(rect), m_color(color) {} 91 : m_rectsWithColor(rectsWithColor) {}
40 92
41 bool MatchAndExplain( 93 bool MatchAndExplain(
42 const SkPicture& picture, 94 const SkPicture& picture,
43 ::testing::MatchResultListener* listener) const override { 95 ::testing::MatchResultListener* listener) const override {
44 DrawsRectangleCanvas canvas; 96 DrawsRectangleCanvas canvas;
45 picture.playback(&canvas); 97 picture.playback(&canvas);
46 const auto& quads = canvas.quads(); 98 const auto& quads = canvas.quadsWithColor();
47 99
48 if (quads.size() != 1) { 100 if (quads.size() != m_rectsWithColor.size()) {
49 *listener << "which draws " << quads.size() << " quads"; 101 *listener << "which draws " << quads.size() << " quads";
50 return false; 102 return false;
51 } 103 }
52 104
53 const FloatQuad& quad = quads[0].first; 105 for (unsigned index = 0; index < quads.size(); index++) {
54 if (!quad.isRectilinear()) { 106 const auto& quadWithColor = quads[index];
55 if (listener->IsInterested()) { 107 const auto& rectWithColor = m_rectsWithColor[index];
56 *listener << "which draws "; 108 if (!quadWithColor.quad.isRectilinear()) {
57 PrintTo(quad, listener->stream()); 109 if (listener->IsInterested()) {
58 *listener << " with color " 110 *listener << "at index " << index << " which draws ";
59 << quads[0].second.serialized().ascii().data(); 111 PrintTo(quadWithColor.quad, listener->stream());
112 *listener << " with color "
113 << quadWithColor.color.serialized().ascii().data() << "\n";
114 }
115 return false;
60 } 116 }
61 return false;
62 }
63 117
64 const FloatRect& rect = quad.boundingBox(); 118 const FloatRect& rect = quadWithColor.quad.boundingBox();
65 if (rect != m_rect || quads[0].second != m_color) { 119 if (rect != rectWithColor.rect ||
66 if (listener->IsInterested()) { 120 quadWithColor.color != rectWithColor.color) {
67 *listener << "which draws "; 121 if (listener->IsInterested()) {
68 PrintTo(rect, listener->stream()); 122 *listener << "at index " << index << " which draws ";
69 *listener << " with color " 123 PrintTo(rect, listener->stream());
70 << quads[0].second.serialized().ascii().data(); 124 *listener << " with color "
125 << quadWithColor.color.serialized().ascii().data() << "\n";
126 }
127 return false;
71 } 128 }
72 return false;
73 } 129 }
74 130
75 return true; 131 return true;
76 } 132 }
77 133
78 void DescribeTo(::std::ostream* os) const override { 134 void DescribeTo(::std::ostream* os) const override {
79 *os << "draws "; 135 for (unsigned index = 0; index < m_rectsWithColor.size(); index++) {
80 PrintTo(m_rect, os); 136 const auto& rectWithColor = m_rectsWithColor[index];
81 *os << " with color " << m_color.serialized().ascii().data(); 137 *os << "at index " << index << " rect draws ";
138 PrintTo(rectWithColor.rect, os);
139 *os << " with color " << rectWithColor.color.serialized().ascii().data()
140 << "\n";
141 }
82 } 142 }
83 143
84 private: 144 private:
85 const FloatRect m_rect; 145 const Vector<RectWithColor> m_rectsWithColor;
86 const Color m_color;
87 }; 146 };
88 147
89 } // namespace 148 } // namespace
90 149
91 ::testing::Matcher<const SkPicture&> drawsRectangle(const FloatRect& rect, 150 ::testing::Matcher<const SkPicture&> drawsRectangle(const FloatRect& rect,
92 Color color) { 151 Color color) {
93 return ::testing::MakeMatcher(new DrawsRectangleMatcher(rect, color)); 152 Vector<RectWithColor> rectsWithColor;
153 rectsWithColor.push_back(RectWithColor(rect, color));
154 return ::testing::MakeMatcher(new DrawsRectanglesMatcher(rectsWithColor));
155 }
156
157 ::testing::Matcher<const SkPicture&> drawsRectangles(
158 const Vector<RectWithColor>& rectsWithColor) {
159 return ::testing::MakeMatcher(new DrawsRectanglesMatcher(rectsWithColor));
94 } 160 }
95 161
96 } // namespace blink 162 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698