OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/graphics/paint/DrawingDisplayItem.h" | 5 #include "platform/graphics/paint/DrawingDisplayItem.h" |
6 | 6 |
7 #include "platform/graphics/GraphicsContext.h" | 7 #include "platform/graphics/GraphicsContext.h" |
8 #include "platform/graphics/paint/PaintCanvas.h" | 8 #include "platform/graphics/paint/PaintCanvas.h" |
9 #include "public/platform/WebDisplayItemList.h" | 9 #include "public/platform/WebDisplayItemList.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
11 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
12 #include "third_party/skia/include/core/SkData.h" | 12 #include "third_party/skia/include/core/SkData.h" |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 | 15 |
16 void DrawingDisplayItem::Replay(GraphicsContext& context) const { | 16 void DrawingDisplayItem::Replay(GraphicsContext& context) const { |
17 if (record_) | 17 if (record_) |
18 context.DrawRecord(record_); | 18 context.DrawRecord(record_); |
19 } | 19 } |
20 | 20 |
21 void DrawingDisplayItem::AppendToWebDisplayItemList( | 21 void DrawingDisplayItem::AppendToWebDisplayItemList( |
22 const IntRect& visual_rect, | 22 const IntRect& visual_rect, |
23 WebDisplayItemList* list) const { | 23 WebDisplayItemList* list) const { |
24 if (record_) { | 24 if (record_) |
25 list->AppendDrawingItem(visual_rect, record_, | 25 list->AppendDrawingItem(visual_rect, record_); |
26 EnclosingIntRect(record_bounds_)); | |
27 } | |
28 } | 26 } |
29 | 27 |
30 bool DrawingDisplayItem::DrawsContent() const { | 28 bool DrawingDisplayItem::DrawsContent() const { |
31 return record_.get(); | 29 return record_.get(); |
32 } | 30 } |
33 | 31 |
34 int DrawingDisplayItem::NumberOfSlowPaths() const { | 32 int DrawingDisplayItem::NumberOfSlowPaths() const { |
35 return record_ ? record_->numSlowPaths() : 0; | 33 return record_ ? record_->numSlowPaths() : 0; |
36 } | 34 } |
37 | 35 |
38 #ifndef NDEBUG | 36 #ifndef NDEBUG |
39 void DrawingDisplayItem::DumpPropertiesAsDebugString( | 37 void DrawingDisplayItem::DumpPropertiesAsDebugString( |
40 StringBuilder& string_builder) const { | 38 StringBuilder& string_builder) const { |
41 DisplayItem::DumpPropertiesAsDebugString(string_builder); | 39 DisplayItem::DumpPropertiesAsDebugString(string_builder); |
42 if (record_) { | 40 if (record_) { |
43 string_builder.Append(String::Format( | 41 string_builder.Append( |
44 ", rect: [%f,%f %fx%f]", record_bounds_.X(), record_bounds_.Y(), | 42 String::Format(", rect: [%f,%f %fx%f]", record_->cullRect().x(), |
45 record_bounds_.Width(), record_bounds_.Height())); | 43 record_->cullRect().y(), record_->cullRect().width(), |
| 44 record_->cullRect().height())); |
46 } | 45 } |
47 } | 46 } |
48 #endif | 47 #endif |
49 | 48 |
50 static bool RecordsEqual(sk_sp<const PaintRecord> record1, | 49 static bool RecordsEqual(sk_sp<const PaintRecord> record1, |
51 sk_sp<const PaintRecord> record2, | 50 sk_sp<const PaintRecord> record2) { |
52 const FloatRect& bounds) { | |
53 if (record1->size() != record2->size()) | 51 if (record1->size() != record2->size()) |
54 return false; | 52 return false; |
55 | 53 |
56 // TODO(enne): PaintRecord should have an operator== | 54 // TODO(enne): PaintRecord should have an operator== |
57 sk_sp<SkData> data1 = ToSkPicture(record1, bounds)->serialize(); | 55 sk_sp<SkData> data1 = ToSkPicture(record1)->serialize(); |
58 sk_sp<SkData> data2 = ToSkPicture(record2, bounds)->serialize(); | 56 sk_sp<SkData> data2 = ToSkPicture(record2)->serialize(); |
59 return data1->equals(data2.get()); | 57 return data1->equals(data2.get()); |
60 } | 58 } |
61 | 59 |
62 static SkBitmap RecordToBitmap(sk_sp<const PaintRecord> record, | 60 static SkBitmap RecordToBitmap(sk_sp<const PaintRecord> record) { |
63 const FloatRect& bounds) { | |
64 SkBitmap bitmap; | 61 SkBitmap bitmap; |
65 bitmap.allocPixels( | 62 SkRect rect = record->cullRect(); |
66 SkImageInfo::MakeN32Premul(bounds.Width(), bounds.Height())); | 63 bitmap.allocPixels(SkImageInfo::MakeN32Premul(rect.width(), rect.height())); |
67 SkiaPaintCanvas canvas(bitmap); | 64 SkiaPaintCanvas canvas(bitmap); |
68 canvas.clear(SK_ColorTRANSPARENT); | 65 canvas.clear(SK_ColorTRANSPARENT); |
69 canvas.translate(-bounds.X(), -bounds.Y()); | 66 canvas.translate(-rect.x(), -rect.y()); |
70 canvas.drawPicture(std::move(record)); | 67 canvas.drawPicture(std::move(record)); |
71 return bitmap; | 68 return bitmap; |
72 } | 69 } |
73 | 70 |
74 static bool BitmapsEqual(sk_sp<const PaintRecord> record1, | 71 static bool BitmapsEqual(sk_sp<const PaintRecord> record1, |
75 sk_sp<const PaintRecord> record2, | 72 sk_sp<const PaintRecord> record2) { |
76 const FloatRect& bounds) { | 73 SkRect rect = record1->cullRect(); |
77 SkBitmap bitmap1 = RecordToBitmap(record1, bounds); | 74 if (rect != record2->cullRect()) |
78 SkBitmap bitmap2 = RecordToBitmap(record2, bounds); | 75 return false; |
| 76 |
| 77 SkBitmap bitmap1 = RecordToBitmap(record1); |
| 78 SkBitmap bitmap2 = RecordToBitmap(record2); |
79 int mismatch_count = 0; | 79 int mismatch_count = 0; |
80 const int kMaxMismatches = 10; | 80 const int kMaxMismatches = 10; |
81 for (int y = 0; y < bounds.Height() && mismatch_count < kMaxMismatches; ++y) { | 81 for (int y = 0; y < rect.height() && mismatch_count < kMaxMismatches; ++y) { |
82 for (int x = 0; x < bounds.Width() && mismatch_count < kMaxMismatches; | 82 for (int x = 0; x < rect.width() && mismatch_count < kMaxMismatches; ++x) { |
83 ++x) { | |
84 SkColor pixel1 = bitmap1.getColor(x, y); | 83 SkColor pixel1 = bitmap1.getColor(x, y); |
85 SkColor pixel2 = bitmap2.getColor(x, y); | 84 SkColor pixel2 = bitmap2.getColor(x, y); |
86 if (pixel1 != pixel2) { | 85 if (pixel1 != pixel2) { |
87 LOG(ERROR) << "x=" << x << " y=" << y << " " << std::hex << pixel1 | 86 LOG(ERROR) << "x=" << x << " y=" << y << " " << std::hex << pixel1 |
88 << " vs " << std::hex << pixel2; | 87 << " vs " << std::hex << pixel2; |
89 ++mismatch_count; | 88 ++mismatch_count; |
90 } | 89 } |
91 } | 90 } |
92 } | 91 } |
93 return !mismatch_count; | 92 return !mismatch_count; |
94 } | 93 } |
95 | 94 |
96 bool DrawingDisplayItem::Equals(const DisplayItem& other) const { | 95 bool DrawingDisplayItem::Equals(const DisplayItem& other) const { |
97 if (!DisplayItem::Equals(other)) | 96 if (!DisplayItem::Equals(other)) |
98 return false; | 97 return false; |
99 | 98 |
100 const sk_sp<const PaintRecord>& record = this->GetPaintRecord(); | 99 const sk_sp<const PaintRecord>& record = this->GetPaintRecord(); |
101 const FloatRect& bounds = this->GetPaintRecordBounds(); | |
102 const sk_sp<const PaintRecord>& other_record = | 100 const sk_sp<const PaintRecord>& other_record = |
103 static_cast<const DrawingDisplayItem&>(other).GetPaintRecord(); | 101 static_cast<const DrawingDisplayItem&>(other).GetPaintRecord(); |
104 const FloatRect& other_bounds = | |
105 static_cast<const DrawingDisplayItem&>(other).GetPaintRecordBounds(); | |
106 | 102 |
107 if (!record && !other_record) | 103 if (!record && !other_record) |
108 return true; | 104 return true; |
109 if (!record || !other_record) | 105 if (!record || !other_record) |
110 return false; | 106 return false; |
111 if (bounds != other_bounds) | |
112 return false; | |
113 | 107 |
114 if (RecordsEqual(record, other_record, bounds)) | 108 if (RecordsEqual(record, other_record)) |
115 return true; | 109 return true; |
116 | 110 |
117 // Sometimes the client may produce different records for the same visual | 111 // Sometimes the client may produce different records for the same visual |
118 // result, which should be treated as equal. | 112 // result, which should be treated as equal. |
119 return BitmapsEqual(std::move(record), std::move(other_record), bounds); | 113 return BitmapsEqual(std::move(record), std::move(other_record)); |
120 } | 114 } |
121 | 115 |
122 } // namespace blink | 116 } // namespace blink |
OLD | NEW |