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 | |
53 // Because processing happens in these CreateAndAppend functions, all the set | |
54 // up for the item should be done via the args, which is why the return type | |
55 // needs to be const, to prevent set-after-processing mistakes. | |
56 | |
57 // Most paired begin item types default to an empty visual rect, which will | |
58 // subsequently be grown as needed to encompass any contained items that draw | |
59 // content, such as drawing or filter items. | |
60 template <typename DisplayItemType, typename... Args> | |
61 const DisplayItemType& CreateAndAppendPairedBeginItem(Args&&... args) { | |
62 return CreateAndAppendPairedBeginItemWithVisualRect<DisplayItemType>( | |
63 gfx::Rect(), std::forward<Args>(args)...); | |
64 } | |
65 | |
66 // This method variant is exposed to allow filters to specify their visual | |
67 // rect since they may draw content despite containing no drawing items. | |
68 template <typename DisplayItemType, typename... Args> | |
69 const DisplayItemType& CreateAndAppendPairedBeginItemWithVisualRect( | |
70 const gfx::Rect& visual_rect, | |
71 Args&&... args) { | |
72 size_t item_index = inputs_.visual_rects.size(); | |
73 inputs_.visual_rects.push_back(visual_rect); | |
74 inputs_.begin_item_indices.push_back(item_index); | |
75 | |
76 return AllocateAndConstruct<DisplayItemType>(std::forward<Args>(args)...); | |
77 } | |
78 | |
79 template <typename DisplayItemType, typename... Args> | |
80 const DisplayItemType& CreateAndAppendPairedEndItem(Args&&... args) { | |
81 DCHECK(!inputs_.begin_item_indices.empty()); | |
82 size_t last_begin_index = inputs_.begin_item_indices.back(); | |
83 inputs_.begin_item_indices.pop_back(); | |
84 | |
85 // Note that we are doing two separate things below: | |
86 // | |
87 // 1. Appending a new rect to the |visual_rects| list associated with | |
88 // the newly-being-added paired end item, with that visual rect | |
89 // having same bounds as its paired begin item, referenced via | |
90 // |last_begin_index|. The paired begin item may or may not be the | |
91 // current last visual rect in |visual_rects|, and its bounds has | |
92 // potentially been grown via calls to CreateAndAppendDrawingItem(). | |
93 // | |
94 // 2. If there is still a containing paired begin item after closing the | |
95 // pair ended in this method call, growing that item's visual rect to | |
96 // incorporate the bounds of the now-finished pair. | |
97 // | |
98 // Thus we're carefully pushing and growing by the visual rect of the | |
99 // paired begin item we're closing in this method call, which is not | |
100 // necessarily the same as |visual_rects.back()|, and given that the | |
101 // |visual_rects| list is mutated in step 1 before step 2, we also can't | |
102 // shorten the reference via a |const auto| reference. We could make a | |
103 // copy of the rect before list mutation, but that would incur copy | |
104 // overhead. | |
105 | |
106 // Ending bounds match the starting bounds. | |
107 inputs_.visual_rects.push_back(inputs_.visual_rects[last_begin_index]); | |
108 | |
109 // The block that ended needs to be included in the bounds of the enclosing | |
110 // block. | |
111 GrowCurrentBeginItemVisualRect(inputs_.visual_rects[last_begin_index]); | |
112 | |
113 return AllocateAndConstruct<DisplayItemType>(std::forward<Args>(args)...); | |
114 } | |
115 | |
116 template <typename DisplayItemType, typename... Args> | |
117 const DisplayItemType& CreateAndAppendDrawingItem( | |
118 const gfx::Rect& visual_rect, | |
119 Args&&... args) { | |
120 inputs_.visual_rects.push_back(visual_rect); | |
121 GrowCurrentBeginItemVisualRect(visual_rect); | |
122 | |
123 return AllocateAndConstruct<DisplayItemType>(std::forward<Args>(args)...); | |
124 } | |
125 | |
126 // Called after all items are appended, to process the items and, if | |
127 // applicable, create an internally cached SkPicture. | |
128 void Finalize(); | |
129 | |
130 void SetIsSuitableForGpuRasterization(bool is_suitable) { | |
131 inputs_.all_items_are_suitable_for_gpu_rasterization = is_suitable; | |
132 } | |
133 bool IsSuitableForGpuRasterization() const; | |
134 | |
135 int ApproximateOpCount() const; | |
136 size_t ApproximateMemoryUsage() const; | |
137 bool ShouldBeAnalyzedForSolidColor() const; | |
138 | |
139 void EmitTraceSnapshot() const; | |
140 | |
141 void GenerateDiscardableImagesMetadata(); | |
142 void GetDiscardableImagesInRect(const gfx::Rect& rect, | |
143 float contents_scale, | |
144 std::vector<DrawImage>* images); | |
145 gfx::Rect GetRectForImage(ImageId image_id) const; | |
146 | |
147 void SetRetainVisualRectsForTesting(bool retain) { | |
148 retain_visual_rects_ = retain; | |
149 } | |
150 | |
151 size_t size() const { return inputs_.items.size(); } | |
152 | |
153 gfx::Rect VisualRectForTesting(int index) { | |
154 return inputs_.visual_rects[index]; | |
155 } | |
156 | |
157 ContiguousContainer<DisplayItem>::const_iterator begin() const { | |
158 return inputs_.items.begin(); | |
159 } | |
160 | |
161 ContiguousContainer<DisplayItem>::const_iterator end() const { | |
162 return inputs_.items.end(); | |
163 } | |
164 | |
165 private: | |
166 FRIEND_TEST_ALL_PREFIXES(DisplayItemListTest, AsValueWithNoItems); | |
167 FRIEND_TEST_ALL_PREFIXES(DisplayItemListTest, AsValueWithItems); | |
168 | |
169 ~DisplayItemList(); | |
170 | |
171 std::unique_ptr<base::trace_event::TracedValue> CreateTracedValue( | |
172 bool include_items) const; | |
173 | |
174 RTree rtree_; | |
175 // For testing purposes only. Whether to keep visual rects across calls to | |
176 // Finalize(). | |
177 bool retain_visual_rects_ = false; | |
178 | |
179 // If we're currently within a paired display item block, unions the | |
180 // given visual rect with the begin display item's visual rect. | |
181 void GrowCurrentBeginItemVisualRect(const gfx::Rect& visual_rect); | |
182 | |
183 template <typename DisplayItemType, typename... Args> | |
184 const DisplayItemType& AllocateAndConstruct(Args&&... args) { | |
185 auto* item = &inputs_.items.AllocateAndConstruct<DisplayItemType>( | |
186 std::forward<Args>(args)...); | |
187 approximate_op_count_ += item->ApproximateOpCount(); | |
188 return *item; | |
189 } | |
190 | |
191 int approximate_op_count_ = 0; | |
192 | |
193 DiscardableImageMap image_map_; | |
194 | |
195 struct Inputs { | |
196 Inputs(); | |
197 ~Inputs(); | |
198 | |
199 ContiguousContainer<DisplayItem> items; | |
200 // The visual rects associated with each of the display items in the | |
201 // display item list. There is one rect per display item, and the | |
202 // position in |visual_rects| matches the position of the item in | |
203 // |items| . These rects are intentionally kept separate | |
204 // because they are not needed while walking the |items| for raster. | |
205 std::vector<gfx::Rect> visual_rects; | |
206 std::vector<size_t> begin_item_indices; | |
207 bool all_items_are_suitable_for_gpu_rasterization = true; | |
208 }; | |
209 | |
210 Inputs inputs_; | |
211 | |
212 friend class base::RefCountedThreadSafe<DisplayItemList>; | |
213 FRIEND_TEST_ALL_PREFIXES(DisplayItemListTest, ApproximateMemoryUsage); | |
214 DISALLOW_COPY_AND_ASSIGN(DisplayItemList); | |
215 }; | |
216 | |
217 } // namespace cc | |
218 | |
219 #endif // CC_PLAYBACK_DISPLAY_ITEM_LIST_H_ | |
OLD | NEW |