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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 const gfx::Rect& bounds, DrawStringLayerDelegate* delegate) { | 203 const gfx::Rect& bounds, DrawStringLayerDelegate* delegate) { |
204 std::unique_ptr<Layer> layer(new Layer(LAYER_TEXTURED)); | 204 std::unique_ptr<Layer> layer(new Layer(LAYER_TEXTURED)); |
205 layer->SetBounds(bounds); | 205 layer->SetBounds(bounds); |
206 layer->set_delegate(delegate); | 206 layer->set_delegate(delegate); |
207 return layer; | 207 return layer; |
208 } | 208 } |
209 | 209 |
210 void DrawTree(Layer* root) { | 210 void DrawTree(Layer* root) { |
211 GetCompositor()->SetRootLayer(root); | 211 GetCompositor()->SetRootLayer(root); |
212 GetCompositor()->ScheduleDraw(); | 212 GetCompositor()->ScheduleDraw(); |
213 WaitForSwap(); | 213 WaitForDraw(); |
214 } | 214 } |
215 | 215 |
216 void ReadPixels(SkBitmap* bitmap) { | 216 void ReadPixels(SkBitmap* bitmap) { |
217 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); | 217 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); |
218 } | 218 } |
219 | 219 |
220 void ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { | 220 void ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { |
221 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); | 221 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); |
222 std::unique_ptr<cc::CopyOutputRequest> request = | 222 std::unique_ptr<cc::CopyOutputRequest> request = |
223 cc::CopyOutputRequest::CreateBitmapRequest( | 223 cc::CopyOutputRequest::CreateBitmapRequest( |
(...skipping 13 matching lines...) Expand all Loading... |
237 // Waits for the callback to finish run and return result. | 237 // Waits for the callback to finish run and return result. |
238 holder->WaitForReadback(); | 238 holder->WaitForReadback(); |
239 | 239 |
240 *bitmap = holder->result(); | 240 *bitmap = holder->result(); |
241 } | 241 } |
242 | 242 |
243 void WaitForDraw() { | 243 void WaitForDraw() { |
244 ui::DrawWaiterForTest::WaitForCompositingStarted(GetCompositor()); | 244 ui::DrawWaiterForTest::WaitForCompositingStarted(GetCompositor()); |
245 } | 245 } |
246 | 246 |
247 void WaitForSwap() { | |
248 DrawWaiterForTest::WaitForCompositingEnded(GetCompositor()); | |
249 } | |
250 | |
251 void WaitForCommit() { | 247 void WaitForCommit() { |
252 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); | 248 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); |
253 } | 249 } |
254 | 250 |
255 // Invalidates the entire contents of the layer. | 251 // Invalidates the entire contents of the layer. |
256 void SchedulePaintForLayer(Layer* layer) { | 252 void SchedulePaintForLayer(Layer* layer) { |
257 layer->SchedulePaint( | 253 layer->SchedulePaint( |
258 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 254 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
259 } | 255 } |
260 | 256 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 | 379 |
384 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); | 380 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate); |
385 }; | 381 }; |
386 | 382 |
387 // Remembers if it has been notified. | 383 // Remembers if it has been notified. |
388 class TestCompositorObserver : public CompositorObserver { | 384 class TestCompositorObserver : public CompositorObserver { |
389 public: | 385 public: |
390 TestCompositorObserver() = default; | 386 TestCompositorObserver() = default; |
391 | 387 |
392 bool committed() const { return committed_; } | 388 bool committed() const { return committed_; } |
393 bool notified() const { return started_ && ended_; } | 389 bool started() const { return started_; } |
394 | 390 |
395 void Reset() { | 391 void Reset() { |
396 committed_ = false; | 392 committed_ = false; |
397 started_ = false; | 393 started_ = false; |
398 ended_ = false; | |
399 } | 394 } |
400 | 395 |
401 private: | 396 private: |
402 void OnCompositingDidCommit(Compositor* compositor) override { | 397 void OnCompositingDidCommit(Compositor* compositor) override { |
403 committed_ = true; | 398 committed_ = true; |
404 } | 399 } |
405 | 400 |
406 void OnCompositingStarted(Compositor* compositor, | 401 void OnCompositingStarted(Compositor* compositor, |
407 base::TimeTicks start_time) override { | 402 base::TimeTicks start_time) override { |
408 started_ = true; | 403 started_ = true; |
409 } | 404 } |
410 | 405 |
411 void OnCompositingEnded(Compositor* compositor) override { ended_ = true; } | |
412 | |
413 void OnCompositingLockStateChanged(Compositor* compositor) override {} | 406 void OnCompositingLockStateChanged(Compositor* compositor) override {} |
414 | 407 |
415 void OnCompositingShuttingDown(Compositor* compositor) override {} | 408 void OnCompositingShuttingDown(Compositor* compositor) override {} |
416 | 409 |
417 bool committed_ = false; | 410 bool committed_ = false; |
418 bool started_ = false; | 411 bool started_ = false; |
419 bool ended_ = false; | |
420 | 412 |
421 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); | 413 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver); |
422 }; | 414 }; |
423 | 415 |
424 class TestCompositorAnimationObserver : public CompositorAnimationObserver { | 416 class TestCompositorAnimationObserver : public CompositorAnimationObserver { |
425 public: | 417 public: |
426 explicit TestCompositorAnimationObserver(ui::Compositor* compositor) | 418 explicit TestCompositorAnimationObserver(ui::Compositor* compositor) |
427 : compositor_(compositor), | 419 : compositor_(compositor), |
428 animation_step_count_(0), | 420 animation_step_count_(0), |
429 shutdown_(false) { | 421 shutdown_(false) { |
(...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1332 CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400))); | 1324 CreateColorLayer(SK_ColorRED, gfx::Rect(20, 20, 400, 400))); |
1333 std::unique_ptr<Layer> l2( | 1325 std::unique_ptr<Layer> l2( |
1334 CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350))); | 1326 CreateColorLayer(SK_ColorBLUE, gfx::Rect(10, 10, 350, 350))); |
1335 l1->Add(l2.get()); | 1327 l1->Add(l2.get()); |
1336 TestCompositorObserver observer; | 1328 TestCompositorObserver observer; |
1337 GetCompositor()->AddObserver(&observer); | 1329 GetCompositor()->AddObserver(&observer); |
1338 | 1330 |
1339 // Explicitly called DrawTree should cause the observers to be notified. | 1331 // Explicitly called DrawTree should cause the observers to be notified. |
1340 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer. | 1332 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer. |
1341 DrawTree(l1.get()); | 1333 DrawTree(l1.get()); |
1342 EXPECT_TRUE(observer.notified()); | 1334 EXPECT_TRUE(observer.started()); |
1343 | 1335 |
1344 // ScheduleDraw without any visible change should cause a commit. | 1336 // ScheduleDraw without any visible change should cause a commit. |
1345 observer.Reset(); | 1337 observer.Reset(); |
1346 l1->ScheduleDraw(); | 1338 l1->ScheduleDraw(); |
1347 WaitForCommit(); | 1339 WaitForCommit(); |
1348 EXPECT_TRUE(observer.committed()); | 1340 EXPECT_TRUE(observer.committed()); |
1349 | 1341 |
1350 // Moving, but not resizing, a layer should alert the observers. | 1342 // Moving, but not resizing, a layer should alert the observers. |
1351 observer.Reset(); | 1343 observer.Reset(); |
1352 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); | 1344 l2->SetBounds(gfx::Rect(0, 0, 350, 350)); |
1353 WaitForSwap(); | 1345 WaitForDraw(); |
1354 EXPECT_TRUE(observer.notified()); | 1346 EXPECT_TRUE(observer.started()); |
1355 | 1347 |
1356 // So should resizing a layer. | 1348 // So should resizing a layer. |
1357 observer.Reset(); | 1349 observer.Reset(); |
1358 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); | 1350 l2->SetBounds(gfx::Rect(0, 0, 400, 400)); |
1359 WaitForSwap(); | 1351 WaitForDraw(); |
1360 EXPECT_TRUE(observer.notified()); | 1352 EXPECT_TRUE(observer.started()); |
1361 | 1353 |
1362 // Opacity changes should alert the observers. | 1354 // Opacity changes should alert the observers. |
1363 observer.Reset(); | 1355 observer.Reset(); |
1364 l2->SetOpacity(0.5f); | 1356 l2->SetOpacity(0.5f); |
1365 WaitForSwap(); | 1357 WaitForDraw(); |
1366 EXPECT_TRUE(observer.notified()); | 1358 EXPECT_TRUE(observer.started()); |
1367 | 1359 |
1368 // So should setting the opacity back. | 1360 // So should setting the opacity back. |
1369 observer.Reset(); | 1361 observer.Reset(); |
1370 l2->SetOpacity(1.0f); | 1362 l2->SetOpacity(1.0f); |
1371 WaitForSwap(); | 1363 WaitForDraw(); |
1372 EXPECT_TRUE(observer.notified()); | 1364 EXPECT_TRUE(observer.started()); |
1373 | 1365 |
1374 // Setting the transform of a layer should alert the observers. | 1366 // Setting the transform of a layer should alert the observers. |
1375 observer.Reset(); | 1367 observer.Reset(); |
1376 gfx::Transform transform; | 1368 gfx::Transform transform; |
1377 transform.Translate(200.0, 200.0); | 1369 transform.Translate(200.0, 200.0); |
1378 transform.Rotate(90.0); | 1370 transform.Rotate(90.0); |
1379 transform.Translate(-200.0, -200.0); | 1371 transform.Translate(-200.0, -200.0); |
1380 l2->SetTransform(transform); | 1372 l2->SetTransform(transform); |
1381 WaitForSwap(); | 1373 WaitForDraw(); |
1382 EXPECT_TRUE(observer.notified()); | 1374 EXPECT_TRUE(observer.started()); |
1383 | 1375 |
1384 GetCompositor()->RemoveObserver(&observer); | 1376 GetCompositor()->RemoveObserver(&observer); |
1385 | 1377 |
1386 // Opacity changes should no longer alert the removed observer. | 1378 // Opacity changes should no longer alert the removed observer. |
1387 observer.Reset(); | 1379 observer.Reset(); |
1388 l2->SetOpacity(0.5f); | 1380 l2->SetOpacity(0.5f); |
1389 WaitForSwap(); | 1381 WaitForDraw(); |
1390 | 1382 |
1391 EXPECT_FALSE(observer.notified()); | 1383 EXPECT_FALSE(observer.started()); |
1392 } | 1384 } |
1393 | 1385 |
1394 // Checks that modifying the hierarchy correctly affects final composite. | 1386 // Checks that modifying the hierarchy correctly affects final composite. |
1395 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { | 1387 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { |
1396 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); | 1388 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); |
1397 | 1389 |
1398 // l0 | 1390 // l0 |
1399 // +-l11 | 1391 // +-l11 |
1400 // | +-l21 | 1392 // | +-l21 |
1401 // +-l12 | 1393 // +-l12 |
(...skipping 828 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2230 | 2222 |
2231 TEST_F(LayerWithRealCompositorTest, CompositorAnimationObserverTest) { | 2223 TEST_F(LayerWithRealCompositorTest, CompositorAnimationObserverTest) { |
2232 std::unique_ptr<Layer> root(CreateLayer(LAYER_TEXTURED)); | 2224 std::unique_ptr<Layer> root(CreateLayer(LAYER_TEXTURED)); |
2233 | 2225 |
2234 root->SetAnimator(LayerAnimator::CreateImplicitAnimator()); | 2226 root->SetAnimator(LayerAnimator::CreateImplicitAnimator()); |
2235 | 2227 |
2236 TestCompositorAnimationObserver animation_observer(GetCompositor()); | 2228 TestCompositorAnimationObserver animation_observer(GetCompositor()); |
2237 EXPECT_EQ(0u, animation_observer.animation_step_count()); | 2229 EXPECT_EQ(0u, animation_observer.animation_step_count()); |
2238 | 2230 |
2239 root->SetOpacity(0.5f); | 2231 root->SetOpacity(0.5f); |
2240 WaitForSwap(); | 2232 WaitForDraw(); |
2241 EXPECT_EQ(1u, animation_observer.animation_step_count()); | 2233 EXPECT_EQ(1u, animation_observer.animation_step_count()); |
2242 | 2234 |
2243 EXPECT_FALSE(animation_observer.shutdown()); | 2235 EXPECT_FALSE(animation_observer.shutdown()); |
2244 ResetCompositor(); | 2236 ResetCompositor(); |
2245 EXPECT_TRUE(animation_observer.shutdown()); | 2237 EXPECT_TRUE(animation_observer.shutdown()); |
2246 } | 2238 } |
2247 | 2239 |
2248 // A simple AnimationMetricsReporter class that remembers smoothness metric | 2240 // A simple AnimationMetricsReporter class that remembers smoothness metric |
2249 // when animation completes. | 2241 // when animation completes. |
2250 class TestMetricsReporter : public ui::AnimationMetricsReporter { | 2242 class TestMetricsReporter : public ui::AnimationMetricsReporter { |
(...skipping 25 matching lines...) Expand all Loading... |
2276 LayerAnimator* animator = root->GetAnimator(); | 2268 LayerAnimator* animator = root->GetAnimator(); |
2277 std::unique_ptr<ui::LayerAnimationElement> animation_element = | 2269 std::unique_ptr<ui::LayerAnimationElement> animation_element = |
2278 ui::LayerAnimationElement::CreateColorElement( | 2270 ui::LayerAnimationElement::CreateColorElement( |
2279 SK_ColorRED, base::TimeDelta::FromMilliseconds(100)); | 2271 SK_ColorRED, base::TimeDelta::FromMilliseconds(100)); |
2280 ui::LayerAnimationSequence* animation_sequence = | 2272 ui::LayerAnimationSequence* animation_sequence = |
2281 new ui::LayerAnimationSequence(std::move(animation_element)); | 2273 new ui::LayerAnimationSequence(std::move(animation_element)); |
2282 TestMetricsReporter reporter; | 2274 TestMetricsReporter reporter; |
2283 animation_sequence->SetAnimationMetricsReporter(&reporter); | 2275 animation_sequence->SetAnimationMetricsReporter(&reporter); |
2284 animator->StartAnimation(animation_sequence); | 2276 animator->StartAnimation(animation_sequence); |
2285 while (!reporter.report_called()) | 2277 while (!reporter.report_called()) |
2286 WaitForSwap(); | 2278 WaitForDraw(); |
2287 ResetCompositor(); | 2279 ResetCompositor(); |
2288 // Even though most of the time 100% smooth animations are expected, on the | 2280 // Even though most of the time 100% smooth animations are expected, on the |
2289 // test bots this cannot be guaranteed. Therefore simply check that some | 2281 // test bots this cannot be guaranteed. Therefore simply check that some |
2290 // value was reported. | 2282 // value was reported. |
2291 EXPECT_GT(reporter.value(), 0); | 2283 EXPECT_GT(reporter.value(), 0); |
2292 } | 2284 } |
2293 | 2285 |
2294 TEST(LayerDebugInfoTest, LayerNameDoesNotClobber) { | 2286 TEST(LayerDebugInfoTest, LayerNameDoesNotClobber) { |
2295 Layer layer(LAYER_NOT_DRAWN); | 2287 Layer layer(LAYER_NOT_DRAWN); |
2296 layer.set_name("foo"); | 2288 layer.set_name("foo"); |
2297 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = | 2289 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = |
2298 layer.TakeDebugInfo(nullptr); | 2290 layer.TakeDebugInfo(nullptr); |
2299 std::string trace_format("bar,"); | 2291 std::string trace_format("bar,"); |
2300 debug_info->AppendAsTraceFormat(&trace_format); | 2292 debug_info->AppendAsTraceFormat(&trace_format); |
2301 std::string expected("bar,{\"layer_name\":\"foo\"}"); | 2293 std::string expected("bar,{\"layer_name\":\"foo\"}"); |
2302 EXPECT_EQ(expected, trace_format); | 2294 EXPECT_EQ(expected, trace_format); |
2303 } | 2295 } |
2304 | 2296 |
2305 } // namespace ui | 2297 } // namespace ui |
OLD | NEW |