OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/compositor/layer.h" | 5 #include "ui/compositor/layer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 DrawFadedStringLayerDelegate* delegate) { | 181 DrawFadedStringLayerDelegate* delegate) { |
182 std::unique_ptr<Layer> layer(new Layer(LAYER_TEXTURED)); | 182 std::unique_ptr<Layer> layer(new Layer(LAYER_TEXTURED)); |
183 layer->SetBounds(bounds); | 183 layer->SetBounds(bounds); |
184 layer->set_delegate(delegate); | 184 layer->set_delegate(delegate); |
185 return layer; | 185 return layer; |
186 } | 186 } |
187 | 187 |
188 void DrawTree(Layer* root) { | 188 void DrawTree(Layer* root) { |
189 GetCompositor()->SetRootLayer(root); | 189 GetCompositor()->SetRootLayer(root); |
190 GetCompositor()->ScheduleDraw(); | 190 GetCompositor()->ScheduleDraw(); |
191 WaitForDraw(); | 191 WaitForSwap(); |
192 } | 192 } |
193 | 193 |
194 void ReadPixels(SkBitmap* bitmap) { | 194 void ReadPixels(SkBitmap* bitmap) { |
195 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); | 195 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); |
196 } | 196 } |
197 | 197 |
198 void ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { | 198 void ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { |
199 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); | 199 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); |
200 std::unique_ptr<cc::CopyOutputRequest> request = | 200 std::unique_ptr<cc::CopyOutputRequest> request = |
201 cc::CopyOutputRequest::CreateBitmapRequest( | 201 cc::CopyOutputRequest::CreateBitmapRequest( |
(...skipping 13 matching lines...) Expand all Loading... |
215 // Waits for the callback to finish run and return result. | 215 // Waits for the callback to finish run and return result. |
216 holder->WaitForReadback(); | 216 holder->WaitForReadback(); |
217 | 217 |
218 *bitmap = holder->result(); | 218 *bitmap = holder->result(); |
219 } | 219 } |
220 | 220 |
221 void WaitForDraw() { | 221 void WaitForDraw() { |
222 ui::DrawWaiterForTest::WaitForCompositingStarted(GetCompositor()); | 222 ui::DrawWaiterForTest::WaitForCompositingStarted(GetCompositor()); |
223 } | 223 } |
224 | 224 |
| 225 void WaitForSwap() { |
| 226 ui::DrawWaiterForTest::WaitForCompositingEnded(GetCompositor()); |
| 227 } |
| 228 |
225 void WaitForCommit() { | 229 void WaitForCommit() { |
226 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); | 230 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); |
227 } | 231 } |
228 | 232 |
229 // Invalidates the entire contents of the layer. | 233 // Invalidates the entire contents of the layer. |
230 void SchedulePaintForLayer(Layer* layer) { | 234 void SchedulePaintForLayer(Layer* layer) { |
231 layer->SchedulePaint( | 235 layer->SchedulePaint( |
232 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 236 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
233 } | 237 } |
234 | 238 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 | 361 |
358 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); | 362 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); |
359 }; | 363 }; |
360 | 364 |
361 // Remembers if it has been notified. | 365 // Remembers if it has been notified. |
362 class TestCompositorObserver : public CompositorObserver { | 366 class TestCompositorObserver : public CompositorObserver { |
363 public: | 367 public: |
364 TestCompositorObserver() = default; | 368 TestCompositorObserver() = default; |
365 | 369 |
366 bool committed() const { return committed_; } | 370 bool committed() const { return committed_; } |
367 bool started() const { return started_; } | 371 bool notified() const { return started_ && ended_; } |
368 | 372 |
369 void Reset() { | 373 void Reset() { |
370 committed_ = false; | 374 committed_ = false; |
371 started_ = false; | 375 started_ = false; |
| 376 ended_ = false; |
372 } | 377 } |
373 | 378 |
374 private: | 379 private: |
375 void OnCompositingDidCommit(Compositor* compositor) override { | 380 void OnCompositingDidCommit(Compositor* compositor) override { |
376 committed_ = true; | 381 committed_ = true; |
377 } | 382 } |
378 | 383 |
379 void OnCompositingStarted(Compositor* compositor, | 384 void OnCompositingStarted(Compositor* compositor, |
380 base::TimeTicks start_time) override { | 385 base::TimeTicks start_time) override { |
381 started_ = true; | 386 started_ = true; |
382 } | 387 } |
383 | 388 |
| 389 void OnCompositingEnded(Compositor* compositor) override { ended_ = true; } |
| 390 |
384 void OnCompositingLockStateChanged(Compositor* compositor) override {} | 391 void OnCompositingLockStateChanged(Compositor* compositor) override {} |
385 | 392 |
386 void OnCompositingShuttingDown(Compositor* compositor) override {} | 393 void OnCompositingShuttingDown(Compositor* compositor) override {} |
387 | 394 |
388 bool committed_ = false; | 395 bool committed_ = false; |
389 bool started_ = false; | 396 bool started_ = false; |
| 397 bool ended_ = false; |
390 | 398 |
391 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); | 399 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); |
392 }; | 400 }; |
393 | 401 |
394 class TestCompositorAnimationObserver : public CompositorAnimationObserver { | 402 class TestCompositorAnimationObserver : public CompositorAnimationObserver { |
395 public: | 403 public: |
396 explicit TestCompositorAnimationObserver(ui::Compositor* compositor) | 404 explicit TestCompositorAnimationObserver(ui::Compositor* compositor) |
397 : compositor_(compositor), | 405 : compositor_(compositor), |
398 animation_step_count_(0), | 406 animation_step_count_(0), |
399 shutdown_(false) { | 407 shutdown_(false) { |
(...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1302 CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400))); | 1310 CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400))); |
1303 std::unique_ptr<Layer> l2( | 1311 std::unique_ptr<Layer> l2( |
1304 CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350))); | 1312 CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350))); |
1305 l1->Add(l2.get()); | 1313 l1->Add(l2.get()); |
1306 TestCompositorObserver observer; | 1314 TestCompositorObserver observer; |
1307 GetCompositor()->AddObserver(&observer); | 1315 GetCompositor()->AddObserver(&observer); |
1308 | 1316 |
1309 // Explicitly called DrawTree should cause the observers to be notified. | 1317 // Explicitly called DrawTree should cause the observers to be notified. |
1310 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer. | 1318 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer. |
1311 DrawTree(l1.get()); | 1319 DrawTree(l1.get()); |
1312 EXPECT_TRUE(observer.started()); | 1320 EXPECT_TRUE(observer.notified()); |
1313 | 1321 |
1314 // ScheduleDraw without any visible change should cause a commit. | 1322 // ScheduleDraw without any visible change should cause a commit. |
1315 observer.Reset(); | 1323 observer.Reset(); |
1316 l1->ScheduleDraw(); | 1324 l1->ScheduleDraw(); |
1317 WaitForCommit(); | 1325 WaitForCommit(); |
1318 EXPECT_TRUE(observer.committed()); | 1326 EXPECT_TRUE(observer.committed()); |
1319 | 1327 |
1320 // Moving, but not resizing, a layer should alert the observers. | 1328 // Moving, but not resizing, a layer should alert the observers. |
1321 observer.Reset(); | 1329 observer.Reset(); |
1322 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); | 1330 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); |
1323 WaitForDraw(); | 1331 WaitForSwap(); |
1324 EXPECT_TRUE(observer.started()); | 1332 EXPECT_TRUE(observer.notified()); |
1325 | 1333 |
1326 // So should resizing a layer. | 1334 // So should resizing a layer. |
1327 observer.Reset(); | 1335 observer.Reset(); |
1328 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); | 1336 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); |
1329 WaitForDraw(); | 1337 WaitForSwap(); |
1330 EXPECT_TRUE(observer.started()); | 1338 EXPECT_TRUE(observer.notified()); |
1331 | 1339 |
1332 // Opacity changes should alert the observers. | 1340 // Opacity changes should alert the observers. |
1333 observer.Reset(); | 1341 observer.Reset(); |
1334 l2->SetOpacity(0.5f); | 1342 l2->SetOpacity(0.5f); |
1335 WaitForDraw(); | 1343 WaitForSwap(); |
1336 EXPECT_TRUE(observer.started()); | 1344 EXPECT_TRUE(observer.notified()); |
1337 | 1345 |
1338 // So should setting the opacity back. | 1346 // So should setting the opacity back. |
1339 observer.Reset(); | 1347 observer.Reset(); |
1340 l2->SetOpacity(1.0f); | 1348 l2->SetOpacity(1.0f); |
1341 WaitForDraw(); | 1349 WaitForSwap(); |
1342 EXPECT_TRUE(observer.started()); | 1350 EXPECT_TRUE(observer.notified()); |
1343 | 1351 |
1344 // Setting the transform of a layer should alert the observers. | 1352 // Setting the transform of a layer should alert the observers. |
1345 observer.Reset(); | 1353 observer.Reset(); |
1346 gfx::Transform transform; | 1354 gfx::Transform transform; |
1347 transform.Translate(200.0, 200.0); | 1355 transform.Translate(200.0, 200.0); |
1348 transform.Rotate(90.0); | 1356 transform.Rotate(90.0); |
1349 transform.Translate(-200.0, -200.0); | 1357 transform.Translate(-200.0, -200.0); |
1350 l2->SetTransform(transform); | 1358 l2->SetTransform(transform); |
1351 WaitForDraw(); | 1359 WaitForSwap(); |
1352 EXPECT_TRUE(observer.started()); | 1360 EXPECT_TRUE(observer.notified()); |
1353 | 1361 |
1354 GetCompositor()->RemoveObserver(&observer); | 1362 GetCompositor()->RemoveObserver(&observer); |
1355 | 1363 |
1356 // Opacity changes should no longer alert the removed observer. | 1364 // Opacity changes should no longer alert the removed observer. |
1357 observer.Reset(); | 1365 observer.Reset(); |
1358 l2->SetOpacity(0.5f); | 1366 l2->SetOpacity(0.5f); |
1359 WaitForDraw(); | 1367 WaitForSwap(); |
1360 | 1368 |
1361 EXPECT_FALSE(observer.started()); | 1369 EXPECT_FALSE(observer.notified()); |
1362 } | 1370 } |
1363 | 1371 |
1364 // Checks that modifying the hierarchy correctly affects final composite. | 1372 // Checks that modifying the hierarchy correctly affects final composite. |
1365 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { | 1373 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { |
1366 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); | 1374 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); |
1367 | 1375 |
1368 // l0 | 1376 // l0 |
1369 // +-l11 | 1377 // +-l11 |
1370 // | +-l21 | 1378 // | +-l21 |
1371 // +-l12 | 1379 // +-l12 |
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2230 layer.set_name("foo"); | 2238 layer.set_name("foo"); |
2231 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = | 2239 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = |
2232 layer.TakeDebugInfo(nullptr); | 2240 layer.TakeDebugInfo(nullptr); |
2233 std::string trace_format("bar,"); | 2241 std::string trace_format("bar,"); |
2234 debug_info->AppendAsTraceFormat(&trace_format); | 2242 debug_info->AppendAsTraceFormat(&trace_format); |
2235 std::string expected("bar,{\"layer_name\":\"foo\"}"); | 2243 std::string expected("bar,{\"layer_name\":\"foo\"}"); |
2236 EXPECT_EQ(expected, trace_format); | 2244 EXPECT_EQ(expected, trace_format); |
2237 } | 2245 } |
2238 | 2246 |
2239 } // namespace ui | 2247 } // namespace ui |
OLD | NEW |