OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CC_PLAYBACK_DISPLAY_ITEM_LIST_H_ | |
6 #define CC_PLAYBACK_DISPLAY_ITEM_LIST_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <memory> | |
11 #include <utility> | |
12 | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/trace_event/trace_event.h" | |
17 #include "cc/base/cc_export.h" | |
18 #include "cc/base/contiguous_container.h" | |
19 #include "cc/base/rtree.h" | |
20 #include "cc/playback/discardable_image_map.h" | |
21 #include "cc/playback/display_item.h" | |
22 #include "cc/playback/image_id.h" | |
23 #include "third_party/skia/include/core/SkPicture.h" | |
24 #include "ui/gfx/color_space.h" | |
25 #include "ui/gfx/geometry/rect.h" | |
26 #include "ui/gfx/geometry/rect_conversions.h" | |
27 | |
28 class SkCanvas; | |
29 | |
30 namespace base { | |
31 namespace trace_event { | |
32 class TracedValue; | |
33 } | |
34 } | |
35 | |
36 namespace cc { | |
37 class DisplayItem; | |
38 | |
39 class CC_EXPORT DisplayItemList | |
40 : public base::RefCountedThreadSafe<DisplayItemList> { | |
41 public: | |
42 DisplayItemList(); | |
43 | |
44 // TODO(trchen): Deprecated. Apply clip and scale on the canvas instead. | |
45 void Raster(SkCanvas* canvas, | |
46 SkPicture::AbortCallback* callback, | |
47 const gfx::Rect& canvas_target_playback_rect, | |
48 float contents_scale) const; | |
49 | |
50 void Raster(SkCanvas* canvas, SkPicture::AbortCallback* callback) const; | |
51 | |
52 // Because processing happens in these CreateAndAppend functions, all the set | |
53 // up for the item should be done via the args, which is why the return type | |
54 // needs to be const, to prevent set-after-processing mistakes. | |
55 | |
56 // Most paired begin item types default to an empty visual rect, which will | |
57 // subsequently be grown as needed to encompass any contained items that draw | |
58 // content, such as drawing or filter items. | |
59 template <typename DisplayItemType, typename... Args> | |
60 const DisplayItemType& CreateAndAppendPairedBeginItem(Args&&... args) { | |
61 return CreateAndAppendPairedBeginItemWithVisualRect<DisplayItemType>( | |
62 gfx::Rect(), std::forward<Args>(args)...); | |
63 } | |
64 | |
65 // This method variant is exposed to allow filters to specify their visual | |
66 // rect since they may draw content despite containing no drawing items. | |
67 template <typename DisplayItemType, typename... Args> | |
68 const DisplayItemType& CreateAndAppendPairedBeginItemWithVisualRect( | |
69 const gfx::Rect& visual_rect, | |
70 Args&&... args) { | |
71 size_t item_index = visual_rects_.size(); | |
72 visual_rects_.push_back(visual_rect); | |
73 begin_item_indices_.push_back(item_index); | |
74 | |
75 return AllocateAndConstruct<DisplayItemType>(std::forward<Args>(args)...); | |
76 } | |
77 | |
78 template <typename DisplayItemType, typename... Args> | |
79 const DisplayItemType& CreateAndAppendPairedEndItem(Args&&... args) { | |
80 DCHECK(!begin_item_indices_.empty()); | |
81 size_t last_begin_index = begin_item_indices_.back(); | |
82 begin_item_indices_.pop_back(); | |
83 | |
84 // Note that we are doing two separate things below: | |
85 // | |
86 // 1. Appending a new rect to the |visual_rects| list associated with | |
87 // the newly-being-added paired end item, with that visual rect | |
88 // having same bounds as its paired begin item, referenced via | |
89 // |last_begin_index|. The paired begin item may or may not be the | |
90 // current last visual rect in |visual_rects|, and its bounds has | |
91 // potentially been grown via calls to CreateAndAppendDrawingItem(). | |
92 // | |
93 // 2. If there is still a containing paired begin item after closing the | |
94 // pair ended in this method call, growing that item's visual rect to | |
95 // incorporate the bounds of the now-finished pair. | |
96 // | |
97 // Thus we're carefully pushing and growing by the visual rect of the | |
98 // paired begin item we're closing in this method call, which is not | |
99 // necessarily the same as |visual_rects.back()|, and given that the | |
100 // |visual_rects| list is mutated in step 1 before step 2, we also can't | |
101 // shorten the reference via a |const auto| reference. We could make a | |
102 // copy of the rect before list mutation, but that would incur copy | |
103 // overhead. | |
104 | |
105 // Ending bounds match the starting bounds. | |
106 visual_rects_.push_back(visual_rects_[last_begin_index]); | |
107 | |
108 // The block that ended needs to be included in the bounds of the enclosing | |
109 // block. | |
110 GrowCurrentBeginItemVisualRect(visual_rects_[last_begin_index]); | |
111 | |
112 return AllocateAndConstruct<DisplayItemType>(std::forward<Args>(args)...); | |
113 } | |
114 | |
115 template <typename DisplayItemType, typename... Args> | |
116 const DisplayItemType& CreateAndAppendDrawingItem( | |
117 const gfx::Rect& visual_rect, | |
118 Args&&... args) { | |
119 visual_rects_.push_back(visual_rect); | |
120 GrowCurrentBeginItemVisualRect(visual_rect); | |
121 | |
122 return AllocateAndConstruct<DisplayItemType>(std::forward<Args>(args)...); | |
123 } | |
124 | |
125 // Called after all items are appended, to process the items and, if | |
126 // applicable, create an internally cached SkPicture. | |
127 void Finalize(); | |
128 | |
129 void SetIsSuitableForGpuRasterization(bool is_suitable) { | |
130 all_items_are_suitable_for_gpu_rasterization_ = is_suitable; | |
131 } | |
132 bool IsSuitableForGpuRasterization() const; | |
133 | |
134 int ApproximateOpCount() const; | |
135 size_t ApproximateMemoryUsage() const; | |
136 bool ShouldBeAnalyzedForSolidColor() const; | |
137 | |
138 void EmitTraceSnapshot() const; | |
139 | |
140 void GenerateDiscardableImagesMetadata(); | |
141 void GetDiscardableImagesInRect(const gfx::Rect& rect, | |
142 float contents_scale, | |
143 std::vector<DrawImage>* images); | |
144 gfx::Rect GetRectForImage(ImageId image_id) const; | |
145 | |
146 void SetRetainVisualRectsForTesting(bool retain) { | |
147 retain_visual_rects_ = retain; | |
148 } | |
149 | |
150 size_t size() const { return items_.size(); } | |
151 | |
152 gfx::Rect VisualRectForTesting(int index) { return visual_rects_[index]; } | |
153 | |
154 ContiguousContainer<DisplayItem>::const_iterator begin() const { | |
155 return items_.begin(); | |
156 } | |
157 | |
158 ContiguousContainer<DisplayItem>::const_iterator end() const { | |
159 return items_.end(); | |
160 } | |
161 | |
162 private: | |
163 FRIEND_TEST_ALL_PREFIXES(DisplayItemListTest, AsValueWithNoItems); | |
164 FRIEND_TEST_ALL_PREFIXES(DisplayItemListTest, AsValueWithItems); | |
165 | |
166 ~DisplayItemList(); | |
167 | |
168 std::unique_ptr<base::trace_event::TracedValue> CreateTracedValue( | |
169 bool include_items) const; | |
170 | |
171 // If we're currently within a paired display item block, unions the | |
172 // given visual rect with the begin display item's visual rect. | |
173 void GrowCurrentBeginItemVisualRect(const gfx::Rect& visual_rect); | |
174 | |
175 template <typename DisplayItemType, typename... Args> | |
176 const DisplayItemType& AllocateAndConstruct(Args&&... args) { | |
177 auto* item = &items_.AllocateAndConstruct<DisplayItemType>( | |
178 std::forward<Args>(args)...); | |
179 approximate_op_count_ += item->ApproximateOpCount(); | |
180 return *item; | |
181 } | |
182 | |
183 RTree rtree_; | |
184 DiscardableImageMap image_map_; | |
185 ContiguousContainer<DisplayItem> items_; | |
186 | |
187 // The visual rects associated with each of the display items in the | |
188 // display item list. There is one rect per display item, and the | |
189 // position in |visual_rects| matches the position of the item in | |
190 // |items| . These rects are intentionally kept separate | |
191 // because they are not needed while walking the |items| for raster. | |
192 std::vector<gfx::Rect> visual_rects_; | |
193 std::vector<size_t> begin_item_indices_; | |
194 | |
195 int approximate_op_count_ = 0; | |
196 bool all_items_are_suitable_for_gpu_rasterization_ = true; | |
197 // For testing purposes only. Whether to keep visual rects across calls to | |
198 // Finalize(). | |
199 bool retain_visual_rects_ = false; | |
200 | |
201 friend class base::RefCountedThreadSafe<DisplayItemList>; | |
202 FRIEND_TEST_ALL_PREFIXES(DisplayItemListTest, ApproximateMemoryUsage); | |
203 DISALLOW_COPY_AND_ASSIGN(DisplayItemList); | |
204 }; | |
205 | |
206 } // namespace cc | |
207 | |
208 #endif // CC_PLAYBACK_DISPLAY_ITEM_LIST_H_ | |
OLD | NEW |