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