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

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

Powered by Google App Engine
This is Rietveld 408576698