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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/compiler_specific.h"
8 #include "base/debug/trace_event.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "cc/layers/delegated_frame_provider.h"
18 #include "cc/layers/delegated_frame_resource_collection.h"
19 #include "cc/layers/layer.h"
20 #include "cc/output/copy_output_request.h"
21 #include "cc/output/copy_output_result.h"
22 #include "cc/output/delegated_frame_data.h"
23 #include "cc/test/pixel_test_utils.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/compositor/compositor_observer.h"
26 #include "ui/compositor/layer.h"
27 #include "ui/compositor/layer_animation_sequence.h"
28 #include "ui/compositor/layer_animator.h"
29 #include "ui/compositor/test/context_factories_for_test.h"
30 #include "ui/compositor/test/draw_waiter_for_test.h"
31 #include "ui/compositor/test/test_compositor_host.h"
32 #include "ui/compositor/test/test_layers.h"
33 #include "ui/gfx/canvas.h"
34 #include "ui/gfx/codec/png_codec.h"
35 #include "ui/gfx/gfx_paths.h"
36 #include "ui/gfx/skia_util.h"
38 using cc::MatchesPNGFile;
44 // There are three test classes in here that configure the Compositor and
45 // Layer's slightly differently:
46 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This
47 // is typically the base class you want to use.
48 // - LayerWithDelegateTest uses LayerDelegate on the delegates.
49 // - LayerWithRealCompositorTest when a real compositor is required for testing.
50 // - Slow because they bring up a window and run the real compositor. This
51 // is typically not what you want.
53 class ColoredLayer : public Layer, public LayerDelegate {
55 explicit ColoredLayer(SkColor color)
56 : Layer(LAYER_TEXTURED),
61 virtual ~ColoredLayer() { }
63 // Overridden from LayerDelegate:
64 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
65 canvas->DrawColor(color_);
68 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
71 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
72 return base::Closure();
79 class LayerWithRealCompositorTest : public testing::Test {
81 LayerWithRealCompositorTest() {
82 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
83 test_data_directory_ = test_data_directory_.AppendASCII("compositor");
85 LOG(ERROR) << "Could not open test data directory.";
88 virtual ~LayerWithRealCompositorTest() {}
90 // Overridden from testing::Test:
91 virtual void SetUp() OVERRIDE {
92 bool enable_pixel_output = true;
93 InitializeContextFactoryForTests(enable_pixel_output);
94 Compositor::Initialize();
96 const gfx::Rect host_bounds(10, 10, 500, 500);
97 compositor_host_.reset(TestCompositorHost::Create(host_bounds));
98 compositor_host_->Show();
101 virtual void TearDown() OVERRIDE {
102 compositor_host_.reset();
103 TerminateContextFactoryForTests();
104 Compositor::Terminate();
107 Compositor* GetCompositor() { return compositor_host_->GetCompositor(); }
109 Layer* CreateLayer(LayerType type) {
110 return new Layer(type);
113 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) {
114 Layer* layer = new ColoredLayer(color);
115 layer->SetBounds(bounds);
119 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
120 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
121 layer->SetBounds(bounds);
125 void DrawTree(Layer* root) {
126 GetCompositor()->SetRootLayer(root);
127 GetCompositor()->ScheduleDraw();
131 bool ReadPixels(SkBitmap* bitmap) {
132 return ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
135 bool ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) {
136 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder);
137 scoped_ptr<cc::CopyOutputRequest> request =
138 cc::CopyOutputRequest::CreateBitmapRequest(
139 base::Bind(&ReadbackHolder::OutputRequestCallback, holder));
140 request->set_area(source_rect);
142 GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass());
144 // Wait for copy response. This needs to wait as the compositor could
145 // be in the middle of a draw right now, and the commit with the
146 // copy output request may not be done on the first draw.
147 for (int i = 0; i < 2; i++) {
148 GetCompositor()->ScheduleDraw();
152 if (holder->completed()) {
153 *bitmap = holder->result();
157 // Callback never called.
163 ui::DrawWaiterForTest::Wait(GetCompositor());
166 void WaitForCommit() {
167 ui::DrawWaiterForTest::WaitForCommit(GetCompositor());
170 // Invalidates the entire contents of the layer.
171 void SchedulePaintForLayer(Layer* layer) {
172 layer->SchedulePaint(
173 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
176 const base::FilePath& test_data_directory() const {
177 return test_data_directory_;
181 class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> {
183 ReadbackHolder() : completed_(false) {}
185 void OutputRequestCallback(scoped_ptr<cc::CopyOutputResult> result) {
187 result_ = result->TakeBitmap();
190 bool completed() const {
193 const SkBitmap& result() const { return *result_; }
196 friend class base::RefCountedThreadSafe<ReadbackHolder>;
198 virtual ~ReadbackHolder() {}
200 scoped_ptr<SkBitmap> result_;
204 scoped_ptr<TestCompositorHost> compositor_host_;
206 // The root directory for test files.
207 base::FilePath test_data_directory_;
209 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest);
212 // LayerDelegate that paints colors to the layer.
213 class TestLayerDelegate : public LayerDelegate {
215 explicit TestLayerDelegate() { reset(); }
216 virtual ~TestLayerDelegate() {}
218 void AddColor(SkColor color) {
219 colors_.push_back(color);
222 const gfx::Size& paint_size() const { return paint_size_; }
223 int color_index() const { return color_index_; }
225 std::string ToScaleString() const {
226 return base::StringPrintf("%.1f %.1f", scale_x_, scale_y_);
229 float device_scale_factor() const {
230 return device_scale_factor_;
233 // Overridden from LayerDelegate:
234 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
235 SkISize size = canvas->sk_canvas()->getBaseLayerSize();
236 paint_size_ = gfx::Size(size.width(), size.height());
237 canvas->FillRect(gfx::Rect(paint_size_), colors_[color_index_]);
238 color_index_ = (color_index_ + 1) % static_cast<int>(colors_.size());
239 const SkMatrix& matrix = canvas->sk_canvas()->getTotalMatrix();
240 scale_x_ = matrix.getScaleX();
241 scale_y_ = matrix.getScaleY();
244 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
245 device_scale_factor_ = device_scale_factor;
248 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
249 return base::Closure();
254 paint_size_.SetSize(0, 0);
255 scale_x_ = scale_y_ = 0.0f;
256 device_scale_factor_ = 0.0f;
260 std::vector<SkColor> colors_;
262 gfx::Size paint_size_;
265 float device_scale_factor_;
267 DISALLOW_COPY_AND_ASSIGN(TestLayerDelegate);
270 // LayerDelegate that verifies that a layer was asked to update its canvas.
271 class DrawTreeLayerDelegate : public LayerDelegate {
273 DrawTreeLayerDelegate() : painted_(false) {}
274 virtual ~DrawTreeLayerDelegate() {}
280 bool painted() const { return painted_; }
283 // Overridden from LayerDelegate:
284 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
287 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
289 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
290 return base::Closure();
295 DISALLOW_COPY_AND_ASSIGN(DrawTreeLayerDelegate);
298 // The simplest possible layer delegate. Does nothing.
299 class NullLayerDelegate : public LayerDelegate {
301 NullLayerDelegate() {}
302 virtual ~NullLayerDelegate() {}
305 // Overridden from LayerDelegate:
306 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
308 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
310 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
311 return base::Closure();
314 DISALLOW_COPY_AND_ASSIGN(NullLayerDelegate);
317 // Remembers if it has been notified.
318 class TestCompositorObserver : public CompositorObserver {
320 TestCompositorObserver()
321 : committed_(false), started_(false), ended_(false), aborted_(false) {}
323 bool committed() const { return committed_; }
324 bool notified() const { return started_ && ended_; }
325 bool aborted() const { return aborted_; }
335 virtual void OnCompositingDidCommit(Compositor* compositor) OVERRIDE {
339 virtual void OnCompositingStarted(Compositor* compositor,
340 base::TimeTicks start_time) OVERRIDE {
344 virtual void OnCompositingEnded(Compositor* compositor) OVERRIDE {
348 virtual void OnCompositingAborted(Compositor* compositor) OVERRIDE {
352 virtual void OnCompositingLockStateChanged(Compositor* compositor) OVERRIDE {
360 DISALLOW_COPY_AND_ASSIGN(TestCompositorObserver);
365 TEST_F(LayerWithRealCompositorTest, Draw) {
366 scoped_ptr<Layer> layer(CreateColorLayer(SK_ColorRED,
367 gfx::Rect(20, 20, 50, 50)));
368 DrawTree(layer.get());
371 // Create this hierarchy:
377 TEST_F(LayerWithRealCompositorTest, Hierarchy) {
378 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
379 gfx::Rect(20, 20, 400, 400)));
380 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
381 gfx::Rect(10, 10, 350, 350)));
382 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
383 gfx::Rect(5, 5, 25, 25)));
384 scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA,
385 gfx::Rect(300, 300, 100, 100)));
394 class LayerWithDelegateTest : public testing::Test {
396 LayerWithDelegateTest() {}
397 virtual ~LayerWithDelegateTest() {}
399 // Overridden from testing::Test:
400 virtual void SetUp() OVERRIDE {
401 bool enable_pixel_output = false;
402 InitializeContextFactoryForTests(enable_pixel_output);
403 Compositor::Initialize();
405 const gfx::Rect host_bounds(1000, 1000);
406 compositor_host_.reset(TestCompositorHost::Create(host_bounds));
407 compositor_host_->Show();
410 virtual void TearDown() OVERRIDE {
411 compositor_host_.reset();
412 TerminateContextFactoryForTests();
413 Compositor::Terminate();
416 Compositor* compositor() { return compositor_host_->GetCompositor(); }
418 virtual Layer* CreateLayer(LayerType type) {
419 return new Layer(type);
422 Layer* CreateColorLayer(SkColor color, const gfx::Rect& bounds) {
423 Layer* layer = new ColoredLayer(color);
424 layer->SetBounds(bounds);
428 virtual Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
429 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
430 layer->SetBounds(bounds);
434 void DrawTree(Layer* root) {
435 compositor()->SetRootLayer(root);
439 // Invalidates the entire contents of the layer.
440 void SchedulePaintForLayer(Layer* layer) {
441 layer->SchedulePaint(
442 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
445 // Invokes DrawTree on the compositor.
447 compositor()->ScheduleDraw();
452 DrawWaiterForTest::Wait(compositor());
455 void WaitForCommit() {
456 DrawWaiterForTest::WaitForCommit(compositor());
460 scoped_ptr<TestCompositorHost> compositor_host_;
462 DISALLOW_COPY_AND_ASSIGN(LayerWithDelegateTest);
467 TEST_F(LayerWithDelegateTest, ConvertPointToLayer_Simple) {
468 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
469 gfx::Rect(20, 20, 400, 400)));
470 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
471 gfx::Rect(10, 10, 350, 350)));
475 gfx::Point point1_in_l2_coords(5, 5);
476 Layer::ConvertPointToLayer(l2.get(), l1.get(), &point1_in_l2_coords);
477 gfx::Point point1_in_l1_coords(15, 15);
478 EXPECT_EQ(point1_in_l1_coords, point1_in_l2_coords);
480 gfx::Point point2_in_l1_coords(5, 5);
481 Layer::ConvertPointToLayer(l1.get(), l2.get(), &point2_in_l1_coords);
482 gfx::Point point2_in_l2_coords(-5, -5);
483 EXPECT_EQ(point2_in_l2_coords, point2_in_l1_coords);
489 TEST_F(LayerWithDelegateTest, ConvertPointToLayer_Medium) {
490 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
491 gfx::Rect(20, 20, 400, 400)));
492 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
493 gfx::Rect(10, 10, 350, 350)));
494 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
495 gfx::Rect(10, 10, 100, 100)));
500 gfx::Point point1_in_l3_coords(5, 5);
501 Layer::ConvertPointToLayer(l3.get(), l1.get(), &point1_in_l3_coords);
502 gfx::Point point1_in_l1_coords(25, 25);
503 EXPECT_EQ(point1_in_l1_coords, point1_in_l3_coords);
505 gfx::Point point2_in_l1_coords(5, 5);
506 Layer::ConvertPointToLayer(l1.get(), l3.get(), &point2_in_l1_coords);
507 gfx::Point point2_in_l3_coords(-15, -15);
508 EXPECT_EQ(point2_in_l3_coords, point2_in_l1_coords);
511 TEST_F(LayerWithRealCompositorTest, Delegate) {
512 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorBLACK,
513 gfx::Rect(20, 20, 400, 400)));
514 GetCompositor()->SetRootLayer(l1.get());
517 TestLayerDelegate delegate;
518 l1->set_delegate(&delegate);
519 delegate.AddColor(SK_ColorWHITE);
520 delegate.AddColor(SK_ColorYELLOW);
521 delegate.AddColor(SK_ColorGREEN);
523 l1->SchedulePaint(gfx::Rect(0, 0, 400, 400));
526 EXPECT_EQ(delegate.color_index(), 1);
527 EXPECT_EQ(delegate.paint_size(), l1->bounds().size());
529 l1->SchedulePaint(gfx::Rect(10, 10, 200, 200));
531 EXPECT_EQ(delegate.color_index(), 2);
532 EXPECT_EQ(delegate.paint_size(), gfx::Size(200, 200));
534 l1->SchedulePaint(gfx::Rect(5, 5, 50, 50));
536 EXPECT_EQ(delegate.color_index(), 0);
537 EXPECT_EQ(delegate.paint_size(), gfx::Size(50, 50));
540 TEST_F(LayerWithRealCompositorTest, DrawTree) {
541 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
542 gfx::Rect(20, 20, 400, 400)));
543 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
544 gfx::Rect(10, 10, 350, 350)));
545 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
546 gfx::Rect(10, 10, 100, 100)));
550 GetCompositor()->SetRootLayer(l1.get());
553 DrawTreeLayerDelegate d1;
554 l1->set_delegate(&d1);
555 DrawTreeLayerDelegate d2;
556 l2->set_delegate(&d2);
557 DrawTreeLayerDelegate d3;
558 l3->set_delegate(&d3);
560 l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
562 EXPECT_FALSE(d1.painted());
563 EXPECT_TRUE(d2.painted());
564 EXPECT_FALSE(d3.painted());
567 // Tests no-texture Layers.
568 // Create this hierarchy:
570 // +-- L2 - NO TEXTURE
574 TEST_F(LayerWithRealCompositorTest, HierarchyNoTexture) {
575 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
576 gfx::Rect(20, 20, 400, 400)));
577 scoped_ptr<Layer> l2(CreateNoTextureLayer(gfx::Rect(10, 10, 350, 350)));
578 scoped_ptr<Layer> l3(CreateColorLayer(SK_ColorYELLOW,
579 gfx::Rect(5, 5, 25, 25)));
580 scoped_ptr<Layer> l4(CreateColorLayer(SK_ColorMAGENTA,
581 gfx::Rect(300, 300, 100, 100)));
587 GetCompositor()->SetRootLayer(l1.get());
590 DrawTreeLayerDelegate d2;
591 l2->set_delegate(&d2);
592 DrawTreeLayerDelegate d3;
593 l3->set_delegate(&d3);
595 l2->SchedulePaint(gfx::Rect(5, 5, 5, 5));
596 l3->SchedulePaint(gfx::Rect(5, 5, 5, 5));
599 // |d2| should not have received a paint notification since it has no texture.
600 EXPECT_FALSE(d2.painted());
601 // |d3| should have received a paint notification.
602 EXPECT_TRUE(d3.painted());
605 class LayerWithNullDelegateTest : public LayerWithDelegateTest {
607 LayerWithNullDelegateTest() {}
608 virtual ~LayerWithNullDelegateTest() {}
610 virtual void SetUp() OVERRIDE {
611 LayerWithDelegateTest::SetUp();
612 default_layer_delegate_.reset(new NullLayerDelegate());
615 virtual Layer* CreateLayer(LayerType type) OVERRIDE {
616 Layer* layer = new Layer(type);
617 layer->set_delegate(default_layer_delegate_.get());
621 Layer* CreateTextureRootLayer(const gfx::Rect& bounds) {
622 Layer* layer = CreateTextureLayer(bounds);
623 compositor()->SetRootLayer(layer);
627 Layer* CreateTextureLayer(const gfx::Rect& bounds) {
628 Layer* layer = CreateLayer(LAYER_TEXTURED);
629 layer->SetBounds(bounds);
633 virtual Layer* CreateNoTextureLayer(const gfx::Rect& bounds) OVERRIDE {
634 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
635 layer->SetBounds(bounds);
640 scoped_ptr<NullLayerDelegate> default_layer_delegate_;
642 DISALLOW_COPY_AND_ASSIGN(LayerWithNullDelegateTest);
645 TEST_F(LayerWithNullDelegateTest, EscapedDebugNames) {
646 scoped_ptr<Layer> layer(CreateLayer(LAYER_NOT_DRAWN));
647 std::string name = "\"\'\\/\b\f\n\r\t\n";
648 layer->set_name(name);
649 scoped_refptr<base::debug::ConvertableToTraceFormat> debug_info =
650 layer->TakeDebugInfo();
651 EXPECT_TRUE(!!debug_info);
653 debug_info->AppendAsTraceFormat(&json);
654 base::JSONReader json_reader;
655 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(json));
656 EXPECT_TRUE(!!debug_info_value);
657 EXPECT_TRUE(debug_info_value->IsType(base::Value::TYPE_DICTIONARY));
658 base::DictionaryValue* dictionary = 0;
659 EXPECT_TRUE(debug_info_value->GetAsDictionary(&dictionary));
660 std::string roundtrip;
661 EXPECT_TRUE(dictionary->GetString("layer_name", &roundtrip));
662 EXPECT_EQ(name, roundtrip);
665 void ReturnMailbox(bool* run, uint32 sync_point, bool is_lost) {
669 TEST_F(LayerWithNullDelegateTest, SwitchLayerPreservesCCLayerState) {
670 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
671 gfx::Rect(20, 20, 400, 400)));
672 l1->SetFillsBoundsOpaquely(true);
673 l1->SetForceRenderSurface(true);
674 l1->SetVisible(false);
676 EXPECT_EQ(gfx::PointF().ToString(),
677 l1->cc_layer()->anchor_point().ToString());
678 EXPECT_TRUE(l1->cc_layer()->DrawsContent());
679 EXPECT_TRUE(l1->cc_layer()->contents_opaque());
680 EXPECT_TRUE(l1->cc_layer()->force_render_surface());
681 EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
683 cc::Layer* before_layer = l1->cc_layer();
685 bool callback1_run = false;
686 cc::TextureMailbox mailbox(gpu::Mailbox::Generate(), 0, 0);
687 l1->SetTextureMailbox(mailbox,
688 cc::SingleReleaseCallback::Create(
689 base::Bind(ReturnMailbox, &callback1_run)),
692 EXPECT_NE(before_layer, l1->cc_layer());
694 EXPECT_EQ(gfx::PointF().ToString(),
695 l1->cc_layer()->anchor_point().ToString());
696 EXPECT_TRUE(l1->cc_layer()->DrawsContent());
697 EXPECT_TRUE(l1->cc_layer()->contents_opaque());
698 EXPECT_TRUE(l1->cc_layer()->force_render_surface());
699 EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
700 EXPECT_FALSE(callback1_run);
702 bool callback2_run = false;
703 mailbox = cc::TextureMailbox(gpu::Mailbox::Generate(), 0, 0);
704 l1->SetTextureMailbox(mailbox,
705 cc::SingleReleaseCallback::Create(
706 base::Bind(ReturnMailbox, &callback2_run)),
708 EXPECT_TRUE(callback1_run);
709 EXPECT_FALSE(callback2_run);
711 l1->SetShowPaintedContent();
712 EXPECT_EQ(gfx::PointF().ToString(),
713 l1->cc_layer()->anchor_point().ToString());
714 EXPECT_TRUE(l1->cc_layer()->DrawsContent());
715 EXPECT_TRUE(l1->cc_layer()->contents_opaque());
716 EXPECT_TRUE(l1->cc_layer()->force_render_surface());
717 EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
718 EXPECT_TRUE(callback2_run);
721 // Various visibile/drawn assertions.
722 TEST_F(LayerWithNullDelegateTest, Visibility) {
723 scoped_ptr<Layer> l1(new Layer(LAYER_TEXTURED));
724 scoped_ptr<Layer> l2(new Layer(LAYER_TEXTURED));
725 scoped_ptr<Layer> l3(new Layer(LAYER_TEXTURED));
729 NullLayerDelegate delegate;
730 l1->set_delegate(&delegate);
731 l2->set_delegate(&delegate);
732 l3->set_delegate(&delegate);
734 // Layers should initially be drawn.
735 EXPECT_TRUE(l1->IsDrawn());
736 EXPECT_TRUE(l2->IsDrawn());
737 EXPECT_TRUE(l3->IsDrawn());
738 EXPECT_FALSE(l1->cc_layer()->hide_layer_and_subtree());
739 EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
740 EXPECT_FALSE(l3->cc_layer()->hide_layer_and_subtree());
742 compositor()->SetRootLayer(l1.get());
746 l1->SetVisible(false);
747 EXPECT_FALSE(l1->IsDrawn());
748 EXPECT_FALSE(l2->IsDrawn());
749 EXPECT_FALSE(l3->IsDrawn());
750 EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
751 EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
752 EXPECT_FALSE(l3->cc_layer()->hide_layer_and_subtree());
754 l3->SetVisible(false);
755 EXPECT_FALSE(l1->IsDrawn());
756 EXPECT_FALSE(l2->IsDrawn());
757 EXPECT_FALSE(l3->IsDrawn());
758 EXPECT_TRUE(l1->cc_layer()->hide_layer_and_subtree());
759 EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
760 EXPECT_TRUE(l3->cc_layer()->hide_layer_and_subtree());
762 l1->SetVisible(true);
763 EXPECT_TRUE(l1->IsDrawn());
764 EXPECT_TRUE(l2->IsDrawn());
765 EXPECT_FALSE(l3->IsDrawn());
766 EXPECT_FALSE(l1->cc_layer()->hide_layer_and_subtree());
767 EXPECT_FALSE(l2->cc_layer()->hide_layer_and_subtree());
768 EXPECT_TRUE(l3->cc_layer()->hide_layer_and_subtree());
771 // Checks that stacking-related methods behave as advertised.
772 TEST_F(LayerWithNullDelegateTest, Stacking) {
773 scoped_ptr<Layer> root(new Layer(LAYER_NOT_DRAWN));
774 scoped_ptr<Layer> l1(new Layer(LAYER_TEXTURED));
775 scoped_ptr<Layer> l2(new Layer(LAYER_TEXTURED));
776 scoped_ptr<Layer> l3(new Layer(LAYER_TEXTURED));
784 // Layers' children are stored in bottom-to-top order.
785 EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
787 root->StackAtTop(l3.get());
788 EXPECT_EQ("2 1 3", test::ChildLayerNamesAsString(*root.get()));
790 root->StackAtTop(l1.get());
791 EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
793 root->StackAtTop(l1.get());
794 EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
796 root->StackAbove(l2.get(), l3.get());
797 EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
799 root->StackAbove(l1.get(), l3.get());
800 EXPECT_EQ("3 1 2", test::ChildLayerNamesAsString(*root.get()));
802 root->StackAbove(l2.get(), l1.get());
803 EXPECT_EQ("3 1 2", test::ChildLayerNamesAsString(*root.get()));
805 root->StackAtBottom(l2.get());
806 EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
808 root->StackAtBottom(l3.get());
809 EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
811 root->StackAtBottom(l3.get());
812 EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
814 root->StackBelow(l2.get(), l3.get());
815 EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
817 root->StackBelow(l1.get(), l3.get());
818 EXPECT_EQ("2 1 3", test::ChildLayerNamesAsString(*root.get()));
820 root->StackBelow(l3.get(), l2.get());
821 EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
823 root->StackBelow(l3.get(), l2.get());
824 EXPECT_EQ("3 2 1", test::ChildLayerNamesAsString(*root.get()));
826 root->StackBelow(l3.get(), l1.get());
827 EXPECT_EQ("2 3 1", test::ChildLayerNamesAsString(*root.get()));
830 // Verifies SetBounds triggers the appropriate painting/drawing.
831 TEST_F(LayerWithNullDelegateTest, SetBoundsSchedulesPaint) {
832 scoped_ptr<Layer> l1(CreateTextureLayer(gfx::Rect(0, 0, 200, 200)));
833 compositor()->SetRootLayer(l1.get());
837 l1->SetBounds(gfx::Rect(5, 5, 200, 200));
839 // The CompositorDelegate (us) should have been told to draw for a move.
842 l1->SetBounds(gfx::Rect(5, 5, 100, 100));
844 // The CompositorDelegate (us) should have been told to draw for a resize.
848 // Checks that pixels are actually drawn to the screen with a read back.
849 TEST_F(LayerWithRealCompositorTest, DrawPixels) {
850 gfx::Size viewport_size = GetCompositor()->size();
852 // The window should be some non-trivial size but may not be exactly
853 // 500x500 on all platforms/bots.
854 EXPECT_GE(viewport_size.width(), 200);
855 EXPECT_GE(viewport_size.height(), 200);
857 int blue_height = 10;
859 scoped_ptr<Layer> layer(
860 CreateColorLayer(SK_ColorRED, gfx::Rect(viewport_size)));
861 scoped_ptr<Layer> layer2(
862 CreateColorLayer(SK_ColorBLUE,
863 gfx::Rect(0, 0, viewport_size.width(), blue_height)));
865 layer->Add(layer2.get());
867 DrawTree(layer.get());
870 ASSERT_TRUE(ReadPixels(&bitmap, gfx::Rect(viewport_size)));
871 ASSERT_FALSE(bitmap.empty());
873 SkAutoLockPixels lock(bitmap);
874 for (int x = 0; x < viewport_size.width(); x++) {
875 for (int y = 0; y < viewport_size.height(); y++) {
876 SkColor actual_color = bitmap.getColor(x, y);
877 SkColor expected_color = y < blue_height ? SK_ColorBLUE : SK_ColorRED;
878 EXPECT_EQ(expected_color, actual_color)
879 << "Pixel error at x=" << x << " y=" << y << "; "
881 << SkColorGetR(actual_color) << ","
882 << SkColorGetG(actual_color) << ","
883 << SkColorGetB(actual_color) << ","
884 << SkColorGetA(actual_color) << "); "
886 << SkColorGetR(expected_color) << ","
887 << SkColorGetG(expected_color) << ","
888 << SkColorGetB(expected_color) << ","
889 << SkColorGetA(expected_color) << ")";
894 // Checks the logic around Compositor::SetRootLayer and Layer::SetCompositor.
895 TEST_F(LayerWithRealCompositorTest, SetRootLayer) {
896 Compositor* compositor = GetCompositor();
897 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
898 gfx::Rect(20, 20, 400, 400)));
899 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
900 gfx::Rect(10, 10, 350, 350)));
902 EXPECT_EQ(NULL, l1->GetCompositor());
903 EXPECT_EQ(NULL, l2->GetCompositor());
905 compositor->SetRootLayer(l1.get());
906 EXPECT_EQ(compositor, l1->GetCompositor());
909 EXPECT_EQ(compositor, l2->GetCompositor());
911 l1->Remove(l2.get());
912 EXPECT_EQ(NULL, l2->GetCompositor());
915 EXPECT_EQ(compositor, l2->GetCompositor());
917 compositor->SetRootLayer(NULL);
918 EXPECT_EQ(NULL, l1->GetCompositor());
919 EXPECT_EQ(NULL, l2->GetCompositor());
922 // Checks that compositor observers are notified when:
923 // - DrawTree is called,
924 // - After ScheduleDraw is called, or
925 // - Whenever SetBounds, SetOpacity or SetTransform are called.
926 // TODO(vollick): could be reorganized into compositor_unittest.cc
927 TEST_F(LayerWithRealCompositorTest, CompositorObservers) {
928 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorRED,
929 gfx::Rect(20, 20, 400, 400)));
930 scoped_ptr<Layer> l2(CreateColorLayer(SK_ColorBLUE,
931 gfx::Rect(10, 10, 350, 350)));
933 TestCompositorObserver observer;
934 GetCompositor()->AddObserver(&observer);
936 // Explicitly called DrawTree should cause the observers to be notified.
937 // NOTE: this call to DrawTree sets l1 to be the compositor's root layer.
939 EXPECT_TRUE(observer.notified());
941 // ScheduleDraw without any visible change should cause a commit.
945 EXPECT_TRUE(observer.committed());
947 // Moving, but not resizing, a layer should alert the observers.
949 l2->SetBounds(gfx::Rect(0, 0, 350, 350));
951 EXPECT_TRUE(observer.notified());
953 // So should resizing a layer.
955 l2->SetBounds(gfx::Rect(0, 0, 400, 400));
957 EXPECT_TRUE(observer.notified());
959 // Opacity changes should alert the observers.
961 l2->SetOpacity(0.5f);
963 EXPECT_TRUE(observer.notified());
965 // So should setting the opacity back.
967 l2->SetOpacity(1.0f);
969 EXPECT_TRUE(observer.notified());
971 // Setting the transform of a layer should alert the observers.
973 gfx::Transform transform;
974 transform.Translate(200.0, 200.0);
975 transform.Rotate(90.0);
976 transform.Translate(-200.0, -200.0);
977 l2->SetTransform(transform);
979 EXPECT_TRUE(observer.notified());
981 // A change resulting in an aborted swap buffer should alert the observer
982 // and also signal an abort.
984 l2->SetOpacity(0.1f);
985 GetCompositor()->DidAbortSwapBuffers();
987 EXPECT_TRUE(observer.notified());
988 EXPECT_TRUE(observer.aborted());
990 GetCompositor()->RemoveObserver(&observer);
992 // Opacity changes should no longer alert the removed observer.
994 l2->SetOpacity(0.5f);
997 EXPECT_FALSE(observer.notified());
1000 // Checks that modifying the hierarchy correctly affects final composite.
1001 TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) {
1002 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1008 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1009 gfx::Rect(0, 0, 50, 50)));
1010 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1011 gfx::Rect(0, 0, 25, 25)));
1012 scoped_ptr<Layer> l21(CreateColorLayer(SK_ColorMAGENTA,
1013 gfx::Rect(0, 0, 15, 15)));
1014 scoped_ptr<Layer> l12(CreateColorLayer(SK_ColorBLUE,
1015 gfx::Rect(10, 10, 25, 25)));
1017 base::FilePath ref_img1 =
1018 test_data_directory().AppendASCII("ModifyHierarchy1.png");
1019 base::FilePath ref_img2 =
1020 test_data_directory().AppendASCII("ModifyHierarchy2.png");
1024 l11->Add(l21.get());
1027 ASSERT_TRUE(ReadPixels(&bitmap));
1028 ASSERT_FALSE(bitmap.empty());
1029 // WritePNGFile(bitmap, ref_img1);
1030 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1032 l0->StackAtTop(l11.get());
1034 ASSERT_TRUE(ReadPixels(&bitmap));
1035 ASSERT_FALSE(bitmap.empty());
1036 // WritePNGFile(bitmap, ref_img2);
1037 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1039 // should restore to original configuration
1040 l0->StackAbove(l12.get(), l11.get());
1042 ASSERT_TRUE(ReadPixels(&bitmap));
1043 ASSERT_FALSE(bitmap.empty());
1044 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1046 // l11 back to front
1047 l0->StackAtTop(l11.get());
1049 ASSERT_TRUE(ReadPixels(&bitmap));
1050 ASSERT_FALSE(bitmap.empty());
1051 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1053 // should restore to original configuration
1054 l0->StackAbove(l12.get(), l11.get());
1056 ASSERT_TRUE(ReadPixels(&bitmap));
1057 ASSERT_FALSE(bitmap.empty());
1058 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1060 // l11 back to front
1061 l0->StackAbove(l11.get(), l12.get());
1063 ASSERT_TRUE(ReadPixels(&bitmap));
1064 ASSERT_FALSE(bitmap.empty());
1065 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1068 // Opacity is rendered correctly.
1069 // Checks that modifying the hierarchy correctly affects final composite.
1070 TEST_F(LayerWithRealCompositorTest, Opacity) {
1071 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1075 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1076 gfx::Rect(0, 0, 50, 50)));
1077 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1078 gfx::Rect(0, 0, 25, 25)));
1080 base::FilePath ref_img = test_data_directory().AppendASCII("Opacity.png");
1082 l11->SetOpacity(0.75);
1086 ASSERT_TRUE(ReadPixels(&bitmap));
1087 ASSERT_FALSE(bitmap.empty());
1088 // WritePNGFile(bitmap, ref_img);
1089 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true)));
1094 class SchedulePaintLayerDelegate : public LayerDelegate {
1096 SchedulePaintLayerDelegate() : paint_count_(0), layer_(NULL) {}
1098 virtual ~SchedulePaintLayerDelegate() {}
1100 void set_layer(Layer* layer) {
1102 layer_->set_delegate(this);
1105 void SetSchedulePaintRect(const gfx::Rect& rect) {
1106 schedule_paint_rect_ = rect;
1109 int GetPaintCountAndClear() {
1110 int value = paint_count_;
1115 const gfx::RectF& last_clip_rect() const { return last_clip_rect_; }
1118 // Overridden from LayerDelegate:
1119 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
1121 if (!schedule_paint_rect_.IsEmpty()) {
1122 layer_->SchedulePaint(schedule_paint_rect_);
1123 schedule_paint_rect_ = gfx::Rect();
1125 SkRect sk_clip_rect;
1126 if (canvas->sk_canvas()->getClipBounds(&sk_clip_rect))
1127 last_clip_rect_ = gfx::SkRectToRectF(sk_clip_rect);
1130 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {
1133 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE {
1134 return base::Closure();
1139 gfx::Rect schedule_paint_rect_;
1140 gfx::RectF last_clip_rect_;
1142 DISALLOW_COPY_AND_ASSIGN(SchedulePaintLayerDelegate);
1147 // Verifies that if SchedulePaint is invoked during painting the layer is still
1149 TEST_F(LayerWithDelegateTest, SchedulePaintFromOnPaintLayer) {
1150 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorRED,
1151 gfx::Rect(0, 0, 500, 500)));
1152 SchedulePaintLayerDelegate child_delegate;
1153 scoped_ptr<Layer> child(CreateColorLayer(SK_ColorBLUE,
1154 gfx::Rect(0, 0, 200, 200)));
1155 child_delegate.set_layer(child.get());
1157 root->Add(child.get());
1159 SchedulePaintForLayer(root.get());
1160 DrawTree(root.get());
1161 child->SchedulePaint(gfx::Rect(0, 0, 20, 20));
1162 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear());
1164 // Set a rect so that when OnPaintLayer() is invoked SchedulePaint is invoked
1166 child_delegate.SetSchedulePaintRect(gfx::Rect(10, 10, 30, 30));
1168 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear());
1170 // Because SchedulePaint() was invoked from OnPaintLayer() |child| should
1171 // still need to be painted.
1173 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear());
1174 EXPECT_TRUE(child_delegate.last_clip_rect().Contains(
1175 gfx::Rect(10, 10, 30, 30)));
1178 TEST_F(LayerWithRealCompositorTest, ScaleUpDown) {
1179 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE,
1180 gfx::Rect(10, 20, 200, 220)));
1181 TestLayerDelegate root_delegate;
1182 root_delegate.AddColor(SK_ColorWHITE);
1183 root->set_delegate(&root_delegate);
1185 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE,
1186 gfx::Rect(10, 20, 140, 180)));
1187 TestLayerDelegate l1_delegate;
1188 l1_delegate.AddColor(SK_ColorWHITE);
1189 l1->set_delegate(&l1_delegate);
1191 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500));
1192 GetCompositor()->SetRootLayer(root.get());
1193 root->Add(l1.get());
1196 EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1197 EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1198 gfx::Size size_in_pixel = root->cc_layer()->bounds();
1199 EXPECT_EQ("200x220", size_in_pixel.ToString());
1200 size_in_pixel = l1->cc_layer()->bounds();
1201 EXPECT_EQ("140x180", size_in_pixel.ToString());
1202 // No scale change, so no scale notification.
1203 EXPECT_EQ(0.0f, root_delegate.device_scale_factor());
1204 EXPECT_EQ(0.0f, l1_delegate.device_scale_factor());
1206 EXPECT_EQ("200x220", root_delegate.paint_size().ToString());
1207 EXPECT_EQ("140x180", l1_delegate.paint_size().ToString());
1209 // Scale up to 2.0. Changing scale doesn't change the bounds in DIP.
1210 GetCompositor()->SetScaleAndSize(2.0f, gfx::Size(500, 500));
1211 EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1212 EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1213 // Pixel size must have been scaled up.
1214 size_in_pixel = root->cc_layer()->bounds();
1215 EXPECT_EQ("400x440", size_in_pixel.ToString());
1216 size_in_pixel = l1->cc_layer()->bounds();
1217 EXPECT_EQ("280x360", size_in_pixel.ToString());
1218 // New scale factor must have been notified.
1219 EXPECT_EQ(2.0f, root_delegate.device_scale_factor());
1220 EXPECT_EQ(2.0f, l1_delegate.device_scale_factor());
1222 // Canvas size must have been scaled down up.
1224 EXPECT_EQ("400x440", root_delegate.paint_size().ToString());
1225 EXPECT_EQ("2.0 2.0", root_delegate.ToScaleString());
1226 EXPECT_EQ("280x360", l1_delegate.paint_size().ToString());
1227 EXPECT_EQ("2.0 2.0", l1_delegate.ToScaleString());
1229 // Scale down back to 1.0f.
1230 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500));
1231 EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1232 EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1233 // Pixel size must have been scaled down.
1234 size_in_pixel = root->cc_layer()->bounds();
1235 EXPECT_EQ("200x220", size_in_pixel.ToString());
1236 size_in_pixel = l1->cc_layer()->bounds();
1237 EXPECT_EQ("140x180", size_in_pixel.ToString());
1238 // New scale factor must have been notified.
1239 EXPECT_EQ(1.0f, root_delegate.device_scale_factor());
1240 EXPECT_EQ(1.0f, l1_delegate.device_scale_factor());
1242 // Canvas size must have been scaled down too.
1244 EXPECT_EQ("200x220", root_delegate.paint_size().ToString());
1245 EXPECT_EQ("1.0 1.0", root_delegate.ToScaleString());
1246 EXPECT_EQ("140x180", l1_delegate.paint_size().ToString());
1247 EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString());
1249 root_delegate.reset();
1250 l1_delegate.reset();
1251 // Just changing the size shouldn't notify the scale change nor
1253 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(1000, 1000));
1254 // No scale change, so no scale notification.
1255 EXPECT_EQ(0.0f, root_delegate.device_scale_factor());
1256 EXPECT_EQ(0.0f, l1_delegate.device_scale_factor());
1258 EXPECT_EQ("0x0", root_delegate.paint_size().ToString());
1259 EXPECT_EQ("0.0 0.0", root_delegate.ToScaleString());
1260 EXPECT_EQ("0x0", l1_delegate.paint_size().ToString());
1261 EXPECT_EQ("0.0 0.0", l1_delegate.ToScaleString());
1264 TEST_F(LayerWithRealCompositorTest, ScaleReparent) {
1265 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE,
1266 gfx::Rect(10, 20, 200, 220)));
1267 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE,
1268 gfx::Rect(10, 20, 140, 180)));
1269 TestLayerDelegate l1_delegate;
1270 l1_delegate.AddColor(SK_ColorWHITE);
1271 l1->set_delegate(&l1_delegate);
1273 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(500, 500));
1274 GetCompositor()->SetRootLayer(root.get());
1277 root->Add(l1.get());
1278 EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1279 gfx::Size size_in_pixel = l1->cc_layer()->bounds();
1280 EXPECT_EQ("140x180", size_in_pixel.ToString());
1281 EXPECT_EQ(0.0f, l1_delegate.device_scale_factor());
1284 EXPECT_EQ("140x180", l1_delegate.paint_size().ToString());
1285 EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString());
1287 // Remove l1 from root and change the scale.
1288 root->Remove(l1.get());
1289 EXPECT_EQ(NULL, l1->parent());
1290 EXPECT_EQ(NULL, l1->GetCompositor());
1291 GetCompositor()->SetScaleAndSize(2.0f, gfx::Size(500, 500));
1292 // Sanity check on root and l1.
1293 EXPECT_EQ("10,20 200x220", root->bounds().ToString());
1294 size_in_pixel = l1->cc_layer()->bounds();
1295 EXPECT_EQ("140x180", size_in_pixel.ToString());
1298 root->Add(l1.get());
1299 EXPECT_EQ("10,20 140x180", l1->bounds().ToString());
1300 size_in_pixel = l1->cc_layer()->bounds();
1301 EXPECT_EQ("280x360", size_in_pixel.ToString());
1302 EXPECT_EQ(2.0f, l1_delegate.device_scale_factor());
1304 EXPECT_EQ("280x360", l1_delegate.paint_size().ToString());
1305 EXPECT_EQ("2.0 2.0", l1_delegate.ToScaleString());
1308 // Tests layer::set_scale_content(false).
1309 TEST_F(LayerWithRealCompositorTest, NoScaleCanvas) {
1310 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE,
1311 gfx::Rect(10, 20, 200, 220)));
1312 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE,
1313 gfx::Rect(10, 20, 140, 180)));
1314 l1->set_scale_content(false);
1315 root->Add(l1.get());
1316 TestLayerDelegate l1_delegate;
1317 l1_delegate.AddColor(SK_ColorWHITE);
1318 l1->set_delegate(&l1_delegate);
1320 GetCompositor()->SetScaleAndSize(2.0f, gfx::Size(500, 500));
1321 GetCompositor()->SetRootLayer(root.get());
1322 // Scale factor change is notified regardless of scale_content flag.
1323 EXPECT_EQ(2.0f, l1_delegate.device_scale_factor());
1326 EXPECT_EQ("280x360", l1_delegate.paint_size().ToString());
1327 EXPECT_EQ("1.0 1.0", l1_delegate.ToScaleString());
1330 // Verifies that when changing bounds on a layer that is invisible, and then
1331 // made visible, the right thing happens:
1332 // - if just a move, then no painting should happen.
1333 // - if a resize, the layer should be repainted.
1334 TEST_F(LayerWithDelegateTest, SetBoundsWhenInvisible) {
1335 scoped_ptr<Layer> root(CreateNoTextureLayer(gfx::Rect(0, 0, 1000, 1000)));
1337 scoped_ptr<Layer> child(CreateLayer(LAYER_TEXTURED));
1338 child->SetBounds(gfx::Rect(0, 0, 500, 500));
1339 DrawTreeLayerDelegate delegate;
1340 child->set_delegate(&delegate);
1341 root->Add(child.get());
1343 // Paint once for initial damage.
1344 child->SetVisible(true);
1345 DrawTree(root.get());
1347 // Reset into invisible state.
1348 child->SetVisible(false);
1349 DrawTree(root.get());
1353 child->SetBounds(gfx::Rect(200, 200, 500, 500));
1354 child->SetVisible(true);
1355 DrawTree(root.get());
1356 EXPECT_FALSE(delegate.painted());
1358 // Reset into invisible state.
1359 child->SetVisible(false);
1360 DrawTree(root.get());
1364 child->SetBounds(gfx::Rect(200, 200, 400, 400));
1365 child->SetVisible(true);
1366 DrawTree(root.get());
1367 EXPECT_TRUE(delegate.painted());
1370 static scoped_ptr<cc::DelegatedFrameData> MakeFrameData(gfx::Size size) {
1371 scoped_ptr<cc::DelegatedFrameData> frame_data(new cc::DelegatedFrameData);
1372 scoped_ptr<cc::RenderPass> render_pass(cc::RenderPass::Create());
1373 render_pass->SetNew(
1374 cc::RenderPass::Id(1, 1), gfx::Rect(size), gfx::Rect(), gfx::Transform());
1375 frame_data->render_pass_list.push_back(render_pass.Pass());
1376 return frame_data.Pass();
1379 TEST_F(LayerWithDelegateTest, DelegatedLayer) {
1380 scoped_ptr<Layer> root(CreateNoTextureLayer(gfx::Rect(0, 0, 1000, 1000)));
1382 scoped_ptr<Layer> child(CreateLayer(LAYER_TEXTURED));
1384 child->SetBounds(gfx::Rect(0, 0, 10, 10));
1385 child->SetVisible(true);
1386 root->Add(child.get());
1387 DrawTree(root.get());
1389 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection =
1390 new cc::DelegatedFrameResourceCollection;
1391 scoped_refptr<cc::DelegatedFrameProvider> frame_provider;
1393 // Content matches layer size.
1394 frame_provider = new cc::DelegatedFrameProvider(
1395 resource_collection.get(), MakeFrameData(gfx::Size(10, 10)));
1396 child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1397 EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1398 gfx::Size(10, 10).ToString());
1400 // Content larger than layer.
1401 child->SetBounds(gfx::Rect(0, 0, 5, 5));
1402 EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1403 gfx::Size(5, 5).ToString());
1405 // Content smaller than layer.
1406 child->SetBounds(gfx::Rect(0, 0, 10, 10));
1407 frame_provider = new cc::DelegatedFrameProvider(
1408 resource_collection.get(), MakeFrameData(gfx::Size(5, 5)));
1409 child->SetShowDelegatedContent(frame_provider, gfx::Size(5, 5));
1410 EXPECT_EQ(child->cc_layer()->bounds().ToString(), gfx::Size(5, 5).ToString());
1412 // Hi-DPI content on low-DPI layer.
1413 frame_provider = new cc::DelegatedFrameProvider(
1414 resource_collection.get(), MakeFrameData(gfx::Size(20, 20)));
1415 child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1416 EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1417 gfx::Size(10, 10).ToString());
1419 // Hi-DPI content on hi-DPI layer.
1420 compositor()->SetScaleAndSize(2.f, gfx::Size(1000, 1000));
1421 EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1422 gfx::Size(20, 20).ToString());
1424 // Low-DPI content on hi-DPI layer.
1425 frame_provider = new cc::DelegatedFrameProvider(
1426 resource_collection.get(), MakeFrameData(gfx::Size(10, 10)));
1427 child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1428 EXPECT_EQ(child->cc_layer()->bounds().ToString(),
1429 gfx::Size(20, 20).ToString());
1432 TEST_F(LayerWithDelegateTest, ExternalContent) {
1433 scoped_ptr<Layer> root(CreateNoTextureLayer(gfx::Rect(0, 0, 1000, 1000)));
1434 scoped_ptr<Layer> child(CreateLayer(LAYER_TEXTURED));
1436 child->SetBounds(gfx::Rect(0, 0, 10, 10));
1437 child->SetVisible(true);
1438 root->Add(child.get());
1440 // The layer is already showing painted content, so the cc layer won't change.
1441 scoped_refptr<cc::Layer> before = child->cc_layer();
1442 child->SetShowPaintedContent();
1443 EXPECT_TRUE(child->cc_layer());
1444 EXPECT_EQ(before, child->cc_layer());
1446 scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection =
1447 new cc::DelegatedFrameResourceCollection;
1448 scoped_refptr<cc::DelegatedFrameProvider> frame_provider =
1449 new cc::DelegatedFrameProvider(resource_collection.get(),
1450 MakeFrameData(gfx::Size(10, 10)));
1452 // Showing delegated content changes the underlying cc layer.
1453 before = child->cc_layer();
1454 child->SetShowDelegatedContent(frame_provider, gfx::Size(10, 10));
1455 EXPECT_TRUE(child->cc_layer());
1456 EXPECT_NE(before, child->cc_layer());
1458 // Changing to painted content should change the underlying cc layer.
1459 before = child->cc_layer();
1460 child->SetShowPaintedContent();
1461 EXPECT_TRUE(child->cc_layer());
1462 EXPECT_NE(before, child->cc_layer());
1465 // Tests Layer::AddThreadedAnimation and Layer::RemoveThreadedAnimation.
1466 TEST_F(LayerWithRealCompositorTest, AddRemoveThreadedAnimations) {
1467 scoped_ptr<Layer> root(CreateLayer(LAYER_TEXTURED));
1468 scoped_ptr<Layer> l1(CreateLayer(LAYER_TEXTURED));
1469 scoped_ptr<Layer> l2(CreateLayer(LAYER_TEXTURED));
1471 l1->SetAnimator(LayerAnimator::CreateImplicitAnimator());
1472 l2->SetAnimator(LayerAnimator::CreateImplicitAnimator());
1474 EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1476 // Trigger a threaded animation.
1477 l1->SetOpacity(0.5f);
1479 EXPECT_TRUE(l1->HasPendingThreadedAnimations());
1481 // Ensure we can remove a pending threaded animation.
1482 l1->GetAnimator()->StopAnimating();
1484 EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1486 // Trigger another threaded animation.
1487 l1->SetOpacity(0.2f);
1489 EXPECT_TRUE(l1->HasPendingThreadedAnimations());
1491 root->Add(l1.get());
1492 GetCompositor()->SetRootLayer(root.get());
1494 // Now that l1 is part of a tree, it should have dispatched the pending
1496 EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1498 // Ensure that l1 no longer holds on to animations.
1499 l1->SetOpacity(0.1f);
1500 EXPECT_FALSE(l1->HasPendingThreadedAnimations());
1502 // Ensure that adding a layer to an existing tree causes its pending
1503 // animations to get dispatched.
1504 l2->SetOpacity(0.5f);
1505 EXPECT_TRUE(l2->HasPendingThreadedAnimations());
1508 EXPECT_FALSE(l2->HasPendingThreadedAnimations());
1511 // Tests that in-progress threaded animations complete when a Layer's
1512 // cc::Layer changes.
1513 TEST_F(LayerWithRealCompositorTest, SwitchCCLayerAnimations) {
1514 scoped_ptr<Layer> root(CreateLayer(LAYER_TEXTURED));
1515 scoped_ptr<Layer> l1(CreateLayer(LAYER_TEXTURED));
1516 GetCompositor()->SetRootLayer(root.get());
1517 root->Add(l1.get());
1519 l1->SetAnimator(LayerAnimator::CreateImplicitAnimator());
1521 EXPECT_FLOAT_EQ(l1->opacity(), 1.0f);
1523 // Trigger a threaded animation.
1524 l1->SetOpacity(0.5f);
1526 // Change l1's cc::Layer.
1527 l1->SwitchCCLayerForTest();
1529 // Ensure that the opacity animation completed.
1530 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f);