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