OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/test/fake_picture_pile_impl.h" | 5 #include "cc/test/fake_picture_pile_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "cc/resources/picture_pile.h" | 12 #include "cc/resources/picture_pile.h" |
13 #include "cc/test/fake_picture_pile.h" | |
14 #include "cc/test/impl_side_painting_settings.h" | 13 #include "cc/test/impl_side_painting_settings.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 | 15 |
17 namespace cc { | 16 namespace cc { |
18 | 17 |
19 FakePicturePileImpl::FakePicturePileImpl() : playback_allowed_event_(nullptr) { | 18 FakePicturePileImpl::FakePicturePileImpl() : playback_allowed_event_(nullptr) { |
20 } | 19 } |
21 | 20 |
22 FakePicturePileImpl::FakePicturePileImpl( | 21 FakePicturePileImpl::FakePicturePileImpl( |
23 const PicturePile* other, | 22 const PicturePile* other, |
24 base::WaitableEvent* playback_allowed_event) | 23 base::WaitableEvent* playback_allowed_event) |
25 : PicturePileImpl(other), | 24 : PicturePileImpl(other), |
26 playback_allowed_event_(playback_allowed_event), | 25 playback_allowed_event_(playback_allowed_event), |
27 tile_grid_size_(other->GetTileGridSizeForTesting()) { | 26 tile_grid_size_(other->GetTileGridSizeForTesting()) { |
28 } | 27 } |
29 | 28 |
30 FakePicturePileImpl::~FakePicturePileImpl() {} | 29 FakePicturePileImpl::~FakePicturePileImpl() {} |
31 | 30 |
32 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreateFilledPile( | 31 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreatePile( |
33 const gfx::Size& tile_size, | 32 const gfx::Size& tile_size, |
34 const gfx::Size& layer_bounds) { | 33 const gfx::Size& layer_bounds, |
| 34 bool is_filled) { |
35 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, | 35 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, |
36 ImplSidePaintingSettings().default_tile_grid_size); | 36 ImplSidePaintingSettings().default_tile_grid_size); |
37 pile.tiling().SetBorderTexels(0); | 37 pile.tiling().SetBorderTexels(0); |
38 pile.tiling().SetTilingSize(layer_bounds); | 38 pile.tiling().SetTilingSize(layer_bounds); |
39 pile.tiling().SetMaxTextureSize(tile_size); | 39 pile.tiling().SetMaxTextureSize(tile_size); |
40 pile.SetRecordedViewport(gfx::Rect(layer_bounds)); | 40 pile.SetRecordedViewport(is_filled ? gfx::Rect(layer_bounds) : gfx::Rect()); |
41 pile.SetHasAnyRecordings(true); | 41 pile.SetHasAnyRecordings(is_filled); |
42 | 42 if (is_filled) { |
| 43 for (int x = 0; x < pile.tiling().num_tiles_x(); ++x) { |
| 44 for (int y = 0; y < pile.tiling().num_tiles_y(); ++y) |
| 45 pile.AddRecordingAt(x, y); |
| 46 } |
| 47 } |
43 scoped_refptr<FakePicturePileImpl> pile_impl( | 48 scoped_refptr<FakePicturePileImpl> pile_impl( |
44 new FakePicturePileImpl(&pile, nullptr)); | 49 new FakePicturePileImpl(&pile, nullptr)); |
45 for (int x = 0; x < pile_impl->tiling().num_tiles_x(); ++x) { | |
46 for (int y = 0; y < pile_impl->tiling().num_tiles_y(); ++y) | |
47 pile_impl->AddRecordingAt(x, y); | |
48 } | |
49 return pile_impl; | 50 return pile_impl; |
50 } | 51 } |
51 | 52 |
| 53 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreateFilledPile( |
| 54 const gfx::Size& tile_size, |
| 55 const gfx::Size& layer_bounds) { |
| 56 bool is_filled = true; |
| 57 return CreatePile(tile_size, layer_bounds, is_filled); |
| 58 } |
| 59 |
52 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreateEmptyPile( | 60 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreateEmptyPile( |
53 const gfx::Size& tile_size, | 61 const gfx::Size& tile_size, |
54 const gfx::Size& layer_bounds) { | 62 const gfx::Size& layer_bounds) { |
55 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, | 63 bool is_filled = false; |
56 ImplSidePaintingSettings().default_tile_grid_size); | 64 return CreatePile(tile_size, layer_bounds, is_filled); |
57 pile.tiling().SetBorderTexels(0); | |
58 pile.tiling().SetTilingSize(layer_bounds); | |
59 pile.tiling().SetMaxTextureSize(tile_size); | |
60 pile.SetRecordedViewport(gfx::Rect()); | |
61 pile.SetHasAnyRecordings(false); | |
62 return make_scoped_refptr(new FakePicturePileImpl(&pile, nullptr)); | |
63 } | 65 } |
64 | 66 |
65 scoped_refptr<FakePicturePileImpl> | 67 scoped_refptr<FakePicturePileImpl> |
66 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings( | 68 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings( |
67 const gfx::Size& tile_size, | 69 const gfx::Size& tile_size, |
68 const gfx::Size& layer_bounds) { | 70 const gfx::Size& layer_bounds, |
| 71 bool is_solid_color) { |
69 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, | 72 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, |
70 ImplSidePaintingSettings().default_tile_grid_size); | 73 ImplSidePaintingSettings().default_tile_grid_size); |
71 pile.tiling().SetBorderTexels(0); | 74 pile.tiling().SetBorderTexels(0); |
72 pile.tiling().SetTilingSize(layer_bounds); | 75 pile.tiling().SetTilingSize(layer_bounds); |
73 pile.tiling().SetMaxTextureSize(tile_size); | 76 pile.tiling().SetMaxTextureSize(tile_size); |
74 // This simulates a false positive for this flag. | 77 // This simulates a false positive for this flag. |
75 pile.SetRecordedViewport(gfx::Rect()); | 78 pile.SetRecordedViewport(gfx::Rect()); |
76 pile.SetHasAnyRecordings(true); | 79 pile.SetHasAnyRecordings(true); |
| 80 pile.SetIsSolidColor(is_solid_color); |
77 return make_scoped_refptr(new FakePicturePileImpl(&pile, nullptr)); | 81 return make_scoped_refptr(new FakePicturePileImpl(&pile, nullptr)); |
78 } | 82 } |
79 | 83 |
80 scoped_refptr<FakePicturePileImpl> | 84 scoped_refptr<FakePicturePileImpl> |
81 FakePicturePileImpl::CreateInfiniteFilledPile() { | 85 FakePicturePileImpl::CreateInfiniteFilledPile() { |
82 gfx::Size size(std::numeric_limits<int>::max(), | 86 gfx::Size size(std::numeric_limits<int>::max(), |
83 std::numeric_limits<int>::max()); | 87 std::numeric_limits<int>::max()); |
84 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, size); | 88 FakePicturePile pile(ImplSidePaintingSettings().minimum_contents_scale, size); |
85 pile.tiling().SetBorderTexels(0); | 89 pile.tiling().SetBorderTexels(0); |
86 pile.tiling().SetTilingSize(size); | 90 pile.tiling().SetTilingSize(size); |
87 pile.tiling().SetMaxTextureSize(size); | 91 pile.tiling().SetMaxTextureSize(size); |
88 pile.SetRecordedViewport(gfx::Rect(size)); | 92 pile.SetRecordedViewport(gfx::Rect(size)); |
89 pile.SetHasAnyRecordings(true); | 93 pile.SetHasAnyRecordings(true); |
90 | 94 pile.AddRecordingAt(0, 0); |
91 scoped_refptr<FakePicturePileImpl> pile_impl( | 95 scoped_refptr<FakePicturePileImpl> pile_impl( |
92 new FakePicturePileImpl(&pile, nullptr)); | 96 new FakePicturePileImpl(&pile, nullptr)); |
93 pile_impl->AddRecordingAt(0, 0); | |
94 return pile_impl; | 97 return pile_impl; |
95 } | 98 } |
96 | 99 |
97 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreateFromPile( | 100 scoped_refptr<FakePicturePileImpl> FakePicturePileImpl::CreateFromPile( |
98 const PicturePile* other, | 101 const PicturePile* other, |
99 base::WaitableEvent* playback_allowed_event) { | 102 base::WaitableEvent* playback_allowed_event) { |
100 return make_scoped_refptr( | 103 return make_scoped_refptr( |
101 new FakePicturePileImpl(other, playback_allowed_event)); | 104 new FakePicturePileImpl(other, playback_allowed_event)); |
102 } | 105 } |
103 | 106 |
104 void FakePicturePileImpl::PlaybackToCanvas(SkCanvas* canvas, | 107 void FakePicturePileImpl::PlaybackToCanvas(SkCanvas* canvas, |
105 const gfx::Rect& canvas_rect, | 108 const gfx::Rect& canvas_rect, |
106 float contents_scale) const { | 109 float contents_scale) const { |
107 if (playback_allowed_event_) | 110 if (playback_allowed_event_) |
108 playback_allowed_event_->Wait(); | 111 playback_allowed_event_->Wait(); |
109 PicturePileImpl::PlaybackToCanvas(canvas, canvas_rect, contents_scale); | 112 PicturePileImpl::PlaybackToCanvas(canvas, canvas_rect, contents_scale); |
110 } | 113 } |
111 | 114 |
112 void FakePicturePileImpl::AddRecordingAt(int x, int y) { | |
113 EXPECT_GE(x, 0); | |
114 EXPECT_GE(y, 0); | |
115 EXPECT_LT(x, tiling_.num_tiles_x()); | |
116 EXPECT_LT(y, tiling_.num_tiles_y()); | |
117 | |
118 if (HasRecordingAt(x, y)) | |
119 return; | |
120 gfx::Rect bounds(tiling().TileBounds(x, y)); | |
121 bounds.Inset(-buffer_pixels(), -buffer_pixels()); | |
122 | |
123 scoped_refptr<Picture> picture( | |
124 Picture::Create(bounds, &client_, tile_grid_size_, true, | |
125 RecordingSource::RECORD_NORMALLY)); | |
126 picture_map_[std::pair<int, int>(x, y)].SetPicture(picture); | |
127 EXPECT_TRUE(HasRecordingAt(x, y)); | |
128 | |
129 has_any_recordings_ = true; | |
130 } | |
131 | |
132 void FakePicturePileImpl::RemoveRecordingAt(int x, int y) { | |
133 EXPECT_GE(x, 0); | |
134 EXPECT_GE(y, 0); | |
135 EXPECT_LT(x, tiling_.num_tiles_x()); | |
136 EXPECT_LT(y, tiling_.num_tiles_y()); | |
137 | |
138 if (!HasRecordingAt(x, y)) | |
139 return; | |
140 picture_map_.erase(std::pair<int, int>(x, y)); | |
141 EXPECT_FALSE(HasRecordingAt(x, y)); | |
142 } | |
143 | |
144 bool FakePicturePileImpl::HasRecordingAt(int x, int y) const { | 115 bool FakePicturePileImpl::HasRecordingAt(int x, int y) const { |
145 PictureMap::const_iterator found = picture_map_.find(PictureMapKey(x, y)); | 116 PictureMap::const_iterator found = picture_map_.find(PictureMapKey(x, y)); |
146 if (found == picture_map_.end()) | 117 if (found == picture_map_.end()) |
147 return false; | 118 return false; |
148 return !!found->second.GetPicture(); | 119 return !!found->second.GetPicture(); |
149 } | 120 } |
150 | 121 |
151 void FakePicturePileImpl::RerecordPile() { | |
152 for (int y = 0; y < num_tiles_y(); ++y) { | |
153 for (int x = 0; x < num_tiles_x(); ++x) { | |
154 RemoveRecordingAt(x, y); | |
155 AddRecordingAt(x, y); | |
156 } | |
157 } | |
158 } | |
159 | |
160 void FakePicturePileImpl::SetMinContentsScale(float min_contents_scale) { | |
161 if (min_contents_scale_ == min_contents_scale) | |
162 return; | |
163 | |
164 // Picture contents are played back scaled. When the final contents scale is | |
165 // less than 1 (i.e. low res), then multiple recorded pixels will be used | |
166 // to raster one final pixel. To avoid splitting a final pixel across | |
167 // pictures (which would result in incorrect rasterization due to blending), a | |
168 // buffer margin is added so that any picture can be snapped to integral | |
169 // final pixels. | |
170 // | |
171 // For example, if a 1/4 contents scale is used, then that would be 3 buffer | |
172 // pixels, since that's the minimum number of pixels to add so that resulting | |
173 // content can be snapped to a four pixel aligned grid. | |
174 int buffer_pixels = static_cast<int>(ceil(1 / min_contents_scale) - 1); | |
175 buffer_pixels = std::max(0, buffer_pixels); | |
176 SetBufferPixels(buffer_pixels); | |
177 min_contents_scale_ = min_contents_scale; | |
178 } | |
179 | |
180 void FakePicturePileImpl::SetBufferPixels(int new_buffer_pixels) { | |
181 if (new_buffer_pixels == buffer_pixels()) | |
182 return; | |
183 | |
184 Clear(); | |
185 tiling_.SetBorderTexels(new_buffer_pixels); | |
186 } | |
187 | |
188 void FakePicturePileImpl::Clear() { | |
189 picture_map_.clear(); | |
190 recorded_viewport_ = gfx::Rect(); | |
191 } | |
192 | |
193 } // namespace cc | 122 } // namespace cc |
OLD | NEW |