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

Side by Side Diff: cc/playback/display_item_list.cc

Issue 1835843002: WIP: Fix foreignObject cullrect Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « cc/playback/display_item_list.h ('k') | cc/playback/display_item_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "cc/playback/display_item_list.h" 5 #include "cc/playback/display_item_list.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/numerics/safe_conversions.h" 11 #include "base/numerics/safe_conversions.h"
12 #include "base/process/process.h"
12 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
13 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_argument.h" 15 #include "base/trace_event/trace_event_argument.h"
15 #include "cc/base/math_util.h" 16 #include "cc/base/math_util.h"
16 #include "cc/debug/picture_debug_util.h" 17 #include "cc/debug/picture_debug_util.h"
17 #include "cc/debug/traced_display_item_list.h" 18 #include "cc/debug/traced_display_item_list.h"
18 #include "cc/debug/traced_value.h" 19 #include "cc/debug/traced_value.h"
19 #include "cc/playback/display_item_list_settings.h" 20 #include "cc/playback/display_item_list_settings.h"
20 #include "cc/playback/display_item_proto_factory.h" 21 #include "cc/playback/display_item_proto_factory.h"
21 #include "cc/playback/drawing_display_item.h" 22 #include "cc/playback/drawing_display_item.h"
22 #include "cc/playback/largest_display_item.h" 23 #include "cc/playback/largest_display_item.h"
23 #include "cc/proto/display_item.pb.h" 24 #include "cc/proto/display_item.pb.h"
24 #include "cc/proto/gfx_conversions.h" 25 #include "cc/proto/gfx_conversions.h"
25 #include "third_party/skia/include/core/SkCanvas.h" 26 #include "third_party/skia/include/core/SkCanvas.h"
26 #include "third_party/skia/include/core/SkPictureRecorder.h" 27 #include "third_party/skia/include/core/SkPictureRecorder.h"
27 #include "third_party/skia/include/utils/SkPictureUtils.h" 28 #include "third_party/skia/include/utils/SkPictureUtils.h"
28 #include "ui/gfx/geometry/rect.h" 29 #include "ui/gfx/geometry/rect.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
29 #include "ui/gfx/skia_util.h" 31 #include "ui/gfx/skia_util.h"
30 32
31 namespace cc { 33 namespace cc {
32 class ImageSerializationProcessor; 34 class ImageSerializationProcessor;
33 35
34 namespace { 36 namespace {
35 37
36 // We don't perform per-layer solid color analysis when there are too many skia 38 // We don't perform per-layer solid color analysis when there are too many skia
37 // operations. 39 // operations.
38 const int kOpCountThatIsOkToAnalyze = 10; 40 const int kOpCountThatIsOkToAnalyze = 10;
39 41
40 bool DisplayItemsTracingEnabled() { 42 bool DisplayItemsTracingEnabled() {
41 bool tracing_enabled; 43 bool tracing_enabled;
42 TRACE_EVENT_CATEGORY_GROUP_ENABLED( 44 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
43 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled); 45 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled);
44 return tracing_enabled; 46 return tracing_enabled;
45 } 47 }
46 48
47 const int kDefaultNumDisplayItemsToReserve = 100; 49 const int kDefaultNumDisplayItemsToReserve = 100;
48 50
49 } // namespace 51 } // namespace
50 52
51 scoped_refptr<DisplayItemList> DisplayItemList::Create( 53 scoped_refptr<DisplayItemList> DisplayItemList::Create(
52 const gfx::Rect& layer_rect,
53 const DisplayItemListSettings& settings) { 54 const DisplayItemListSettings& settings) {
54 return make_scoped_refptr(new DisplayItemList( 55 return make_scoped_refptr(new DisplayItemList(settings));
55 layer_rect, settings,
56 !settings.use_cached_picture || DisplayItemsTracingEnabled()));
57 } 56 }
58 57
59 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( 58 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto(
60 const proto::DisplayItemList& proto, 59 const proto::DisplayItemList& proto,
61 ImageSerializationProcessor* image_serialization_processor) { 60 ImageSerializationProcessor* image_serialization_processor) {
62 gfx::Rect layer_rect = ProtoToRect(proto.layer_rect());
63 scoped_refptr<DisplayItemList> list = 61 scoped_refptr<DisplayItemList> list =
64 DisplayItemList::Create(ProtoToRect(proto.layer_rect()), 62 DisplayItemList::Create(DisplayItemListSettings(proto.settings()));
65 DisplayItemListSettings(proto.settings()));
66 63
67 for (int i = 0; i < proto.items_size(); i++) { 64 for (int i = 0; i < proto.items_size(); i++) {
68 const proto::DisplayItem& item_proto = proto.items(i); 65 const proto::DisplayItem& item_proto = proto.items(i);
66 const gfx::Rect visual_rect = ProtoToRect(proto.visual_rects(i));
69 DisplayItemProtoFactory::AllocateAndConstruct( 67 DisplayItemProtoFactory::AllocateAndConstruct(
70 layer_rect, list.get(), item_proto, image_serialization_processor); 68 visual_rect, list.get(), item_proto, image_serialization_processor);
71 } 69 }
72 70
73 list->Finalize(); 71 list->Finalize();
74 72
75 return list; 73 return list;
76 } 74 }
77 75
78 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, 76 DisplayItemList::DisplayItemList(const DisplayItemListSettings& settings)
79 const DisplayItemListSettings& settings,
80 bool retain_individual_display_items)
81 : items_(LargestDisplayItemSize(), 77 : items_(LargestDisplayItemSize(),
82 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve), 78 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve),
79 retain_visual_rects_(false),
83 settings_(settings), 80 settings_(settings),
84 retain_individual_display_items_(retain_individual_display_items), 81 all_items_are_suitable_for_gpu_rasterization_(true),
85 layer_rect_(layer_rect), 82 approximate_op_count_(0) {}
86 is_suitable_for_gpu_rasterization_(true),
87 approximate_op_count_(0),
88 picture_memory_usage_(0) {
89 if (settings_.use_cached_picture) {
90 SkRTreeFactory factory;
91 recorder_.reset(new SkPictureRecorder());
92 canvas_ = skia::SharePtr(recorder_->beginRecording(
93 layer_rect_.width(), layer_rect_.height(), &factory));
94 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
95 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
96 }
97 }
98 83
99 DisplayItemList::~DisplayItemList() { 84 DisplayItemList::~DisplayItemList() {
100 } 85 }
101 86
102 void DisplayItemList::ToProtobuf( 87 void DisplayItemList::ToProtobuf(
103 proto::DisplayItemList* proto, 88 proto::DisplayItemList* proto,
104 ImageSerializationProcessor* image_serialization_processor) { 89 ImageSerializationProcessor* image_serialization_processor) {
105 // The flattened SkPicture approach is going away, and the proto 90 // The flattened SkPicture approach is going away, and the proto
106 // doesn't currently support serializing that flattened picture. 91 // doesn't currently support serializing that flattened picture.
107 DCHECK(retain_individual_display_items_);
108
109 RectToProto(layer_rect_, proto->mutable_layer_rect());
110 settings_.ToProtobuf(proto->mutable_settings()); 92 settings_.ToProtobuf(proto->mutable_settings());
111 93
112 DCHECK_EQ(0, proto->items_size()); 94 DCHECK_EQ(0, proto->items_size());
113 for (const auto& item : items_) 95 DCHECK_EQ(0, proto->visual_rects_size());
96 DCHECK(items_.size() == visual_rects_.size())
97 << "items.size() " << items_.size() << " visual_rects.size() "
98 << visual_rects_.size();
99 int i = 0;
100 for (const auto& item : items_) {
114 item.ToProtobuf(proto->add_items(), image_serialization_processor); 101 item.ToProtobuf(proto->add_items(), image_serialization_processor);
102 RectToProto(visual_rects_[i++], proto->add_visual_rects());
103 }
115 } 104 }
116 105
117 void DisplayItemList::Raster(SkCanvas* canvas, 106 void DisplayItemList::Raster(SkCanvas* canvas,
118 SkPicture::AbortCallback* callback, 107 SkPicture::AbortCallback* callback,
119 const gfx::Rect& canvas_target_playback_rect, 108 const gfx::Rect& canvas_playback_rect,
120 float contents_scale) const { 109 float contents_scale) const {
121 if (!settings_.use_cached_picture) { 110 canvas->save();
122 canvas->save(); 111 canvas->scale(contents_scale, contents_scale);
123 canvas->scale(contents_scale, contents_scale);
124 for (const auto& item : items_)
125 item.Raster(canvas, canvas_target_playback_rect, callback);
126 canvas->restore();
127 } else {
128 DCHECK(picture_);
129 112
130 canvas->save(); 113 gfx::Rect query_rect = GetQueryRect(canvas_playback_rect, contents_scale);
131 canvas->scale(contents_scale, contents_scale); 114 std::vector<size_t> indices;
132 canvas->translate(layer_rect_.x(), layer_rect_.y()); 115 rtree_.Search(query_rect, &indices);
133 if (callback) { 116 for (size_t index : indices) {
134 // If we have a callback, we need to call |draw()|, |drawPicture()| 117 items_[index].Raster(canvas, callback);
135 // doesn't take a callback. This is used by |AnalysisCanvas| to early 118 // We use a callback during solid color analysis on the compositor thread to
136 // out. 119 // break out early. Since we're handling a sequence of pictures via rtree
137 picture_->playback(canvas, callback); 120 // query results ourselves, we have to respect the callback and early out.
138 } else { 121 if (callback && callback->abort())
139 // Prefer to call |drawPicture()| on the canvas since it could place the 122 break;
140 // entire picture on the canvas instead of parsing the skia operations.
141 canvas->drawPicture(picture_.get());
142 }
143 canvas->restore();
144 } 123 }
124 canvas->restore();
145 } 125 }
146 126
147 void DisplayItemList::ProcessAppendedItem(const DisplayItem* item) { 127 gfx::Rect DisplayItemList::GetQueryRect(const gfx::Rect& rect,
148 if (settings_.use_cached_picture) { 128 float scale) const {
149 DCHECK(canvas_); 129 // If the playback rect is empty, assume that we need to play back everything.
150 item->Raster(canvas_.get(), gfx::Rect(), nullptr); 130 return rect.IsEmpty() ? rtree_.GetBounds()
151 } 131 : ScaleToEnclosingRect(rect, 1.f / scale);
152 if (!retain_individual_display_items_) {
153 items_.Clear();
154 }
155 }
156
157 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
158 DCHECK(canvas_);
159 DCHECK(!retain_individual_display_items_);
160
161 item.Raster(canvas_.get(), gfx::Rect(), nullptr);
162 }
163
164 bool DisplayItemList::RetainsIndividualDisplayItems() const {
165 return retain_individual_display_items_;
166 } 132 }
167 133
168 void DisplayItemList::Finalize() { 134 void DisplayItemList::Finalize() {
169 // TODO(dtrainor): Need to deal with serializing visual_rects_. 135 // TODO(dtrainor): Need to deal with serializing visual_rects_.
170 // http://crbug.com/568757. 136 // http://crbug.com/568757.
171 DCHECK(!retain_individual_display_items_ || 137 DCHECK(items_.size() == visual_rects_.size())
172 items_.size() == visual_rects_.size())
173 << "items.size() " << items_.size() << " visual_rects.size() " 138 << "items.size() " << items_.size() << " visual_rects.size() "
174 << visual_rects_.size(); 139 << visual_rects_.size();
140 rtree_.Build(visual_rects_);
175 141
176 // TODO(vmpstr): Build and make use of an RTree from the visual 142 if (!retain_visual_rects_)
177 // rects. For now we just clear them out since we won't ever need 143 // This clears both the vector and the vector's capacity, since
178 // them to stick around post-Finalize. http://crbug.com/527245 144 // visual_rects_
179 // This clears both the vector and the vector's capacity, since visual_rects_ 145 // won't be used anymore.
180 // won't be used anymore. 146 std::vector<gfx::Rect>().swap(visual_rects_);
181 std::vector<gfx::Rect>().swap(visual_rects_);
182
183 if (settings_.use_cached_picture) {
184 // Convert to an SkPicture for faster rasterization.
185 DCHECK(settings_.use_cached_picture);
186 DCHECK(!picture_);
187 picture_ = recorder_->finishRecordingAsPicture();
188 DCHECK(picture_);
189 picture_memory_usage_ =
190 SkPictureUtils::ApproximateBytesUsed(picture_.get());
191 recorder_.reset();
192 canvas_.clear();
193 is_suitable_for_gpu_rasterization_ =
194 picture_->suitableForGpuRasterization(nullptr);
195 }
196 } 147 }
197 148
198 bool DisplayItemList::IsSuitableForGpuRasterization() const { 149 bool DisplayItemList::IsSuitableForGpuRasterization() const {
199 return is_suitable_for_gpu_rasterization_; 150 // This is more permissive than Picture's implementation, since none of the
151 // items might individually trigger a veto even though they collectively have
152 // enough "bad" operations that a corresponding Picture would get vetoed. See
153 // crbug.com/513016.
154 return all_items_are_suitable_for_gpu_rasterization_;
200 } 155 }
201 156
202 int DisplayItemList::ApproximateOpCount() const { 157 int DisplayItemList::ApproximateOpCount() const {
203 return approximate_op_count_; 158 return approximate_op_count_;
204 } 159 }
205 160
206 size_t DisplayItemList::ApproximateMemoryUsage() const { 161 size_t DisplayItemList::ApproximateMemoryUsage() const {
207 // We double-count in this case. Produce zero to avoid being misleading.
208 if (settings_.use_cached_picture && retain_individual_display_items_)
209 return 0;
210
211 DCHECK(!settings_.use_cached_picture || picture_);
212
213 size_t memory_usage = sizeof(*this); 162 size_t memory_usage = sizeof(*this);
214 163
215 size_t external_memory_usage = 0; 164 size_t external_memory_usage = 0;
216 if (retain_individual_display_items_) { 165 // Warning: this double-counts SkPicture data if use_cached_picture is
217 // Warning: this double-counts SkPicture data if use_cached_picture is 166 // also true.
218 // also true. 167 for (const auto& item : items_) {
219 for (const auto& item : items_) { 168 external_memory_usage += item.ExternalMemoryUsage();
220 external_memory_usage += item.ExternalMemoryUsage();
221 }
222 } 169 }
223 170
224 // Memory outside this class due to |items_|. 171 // Memory outside this class due to |items_|.
225 memory_usage += items_.GetCapacityInBytes() + external_memory_usage; 172 memory_usage += items_.GetCapacityInBytes() + external_memory_usage;
226 173
227 // Memory outside this class due to |picture|.
228 memory_usage += picture_memory_usage_;
229
230 // TODO(jbroman): Does anything else owned by this class substantially 174 // TODO(jbroman): Does anything else owned by this class substantially
231 // contribute to memory usage? 175 // contribute to memory usage?
176 // TODO(vmpstr): Probably DiscardableImageMap is worth counting here.
232 177
233 return memory_usage; 178 return memory_usage;
234 } 179 }
235 180
236 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const { 181 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
237 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze; 182 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
238 } 183 }
239 184
240 scoped_ptr<base::trace_event::ConvertableToTraceFormat> 185 scoped_ptr<base::trace_event::ConvertableToTraceFormat>
241 DisplayItemList::AsValue(bool include_items) const { 186 DisplayItemList::AsValue(bool include_items) const {
242 scoped_ptr<base::trace_event::TracedValue> state( 187 scoped_ptr<base::trace_event::TracedValue> state(
243 new base::trace_event::TracedValue()); 188 new base::trace_event::TracedValue());
244 189
245 state->BeginDictionary("params"); 190 state->BeginDictionary("params");
246 if (include_items) { 191 if (include_items) {
247 state->BeginArray("items"); 192 state->BeginArray("items");
248 size_t item_index = 0; 193 size_t item_index = 0;
249 for (const DisplayItem& item : items_) { 194 for (const DisplayItem& item : items_) {
250 item.AsValueInto(item_index < visual_rects_.size() 195 item.AsValueInto(item_index < visual_rects_.size()
251 ? visual_rects_[item_index] 196 ? visual_rects_[item_index]
252 : gfx::Rect(), 197 : gfx::Rect(),
253 state.get()); 198 state.get());
254 item_index++; 199 item_index++;
255 } 200 }
256 state->EndArray(); // "items". 201 state->EndArray(); // "items".
257 } 202 }
258 state->SetValue("layer_rect", MathUtil::AsValue(layer_rect_));
259 state->EndDictionary(); // "params". 203 state->EndDictionary(); // "params".
260 204
261 if (!layer_rect_.IsEmpty()) { 205 SkPictureRecorder recorder;
262 SkPictureRecorder recorder; 206 gfx::Rect bounds = rtree_.GetBounds();
263 SkCanvas* canvas = 207 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
264 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); 208 canvas->translate(-bounds.x(), -bounds.y());
265 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); 209 canvas->clipRect(gfx::RectToSkRect(bounds));
266 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); 210 Raster(canvas, nullptr, gfx::Rect(), 1.f);
267 Raster(canvas, NULL, gfx::Rect(), 1.f); 211 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
268 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
269 212
270 std::string b64_picture; 213 std::string b64_picture;
271 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); 214 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
272 state->SetString("skp64", b64_picture); 215 state->SetString("skp64", b64_picture);
273 }
274 216
275 return std::move(state); 217 return std::move(state);
276 } 218 }
277 219
278 void DisplayItemList::EmitTraceSnapshot() const { 220 void DisplayItemList::EmitTraceSnapshot() const {
279 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( 221 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
280 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") "," 222 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
281 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," 223 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
282 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), 224 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
283 "cc::DisplayItemList", this, 225 "cc::DisplayItemList", this,
284 TracedDisplayItemList::AsTraceableDisplayItemList(this, 226 TracedDisplayItemList::AsTraceableDisplayItemList(this,
285 DisplayItemsTracingEnabled())); 227 DisplayItemsTracingEnabled()));
286 } 228 }
287 229
288 void DisplayItemList::GenerateDiscardableImagesMetadata() { 230 void DisplayItemList::GenerateDiscardableImagesMetadata() {
289 // This should be only called once, and only after CreateAndCacheSkPicture. 231 // This should be only called once, and only after CreateAndCacheSkPicture.
290 DCHECK(image_map_.empty()); 232 DCHECK(image_map_.empty());
291 DCHECK(!settings_.use_cached_picture || picture_);
292 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps())
293 return;
294 233
295 // The cached picture is translated by -layer_rect_.origin during record, 234 gfx::Rect bounds = rtree_.GetBounds();
296 // so we need to offset that back in order to get right positioning for
297 // images.
298 DiscardableImageMap::ScopedMetadataGenerator generator( 235 DiscardableImageMap::ScopedMetadataGenerator generator(
299 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); 236 &image_map_, gfx::Size(bounds.right(), bounds.bottom()));
300 Raster(generator.canvas(), nullptr, 237 Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f);
301 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f);
302 } 238 }
303 239
304 void DisplayItemList::GetDiscardableImagesInRect( 240 void DisplayItemList::GetDiscardableImagesInRect(
305 const gfx::Rect& rect, 241 const gfx::Rect& rect,
306 float raster_scale, 242 float raster_scale,
307 std::vector<DrawImage>* images) { 243 std::vector<DrawImage>* images) {
308 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); 244 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images);
309 } 245 }
310 246
311 bool DisplayItemList::MayHaveDiscardableImages() const { 247 bool DisplayItemList::MayHaveDiscardableImages() const {
312 return !image_map_.empty(); 248 return !image_map_.empty();
313 } 249 }
314 250
315 } // namespace cc 251 } // namespace cc
OLDNEW
« no previous file with comments | « cc/playback/display_item_list.h ('k') | cc/playback/display_item_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698