ui_resource_layer_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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
  3. // found in the LICENSE file.
  4. #include "cc/layers/ui_resource_layer.h"
  5. #include "base/threading/thread_task_runner_handle.h"
  6. #include "cc/animation/animation_host.h"
  7. #include "cc/resources/scoped_ui_resource.h"
  8. #include "cc/test/fake_layer_tree_host.h"
  9. #include "cc/test/layer_tree_impl_test_base.h"
  10. #include "cc/test/stub_layer_tree_host_single_thread_client.h"
  11. #include "cc/trees/single_thread_proxy.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "third_party/skia/include/core/SkBitmap.h"
  15. using ::testing::Mock;
  16. using ::testing::_;
  17. using ::testing::AtLeast;
  18. using ::testing::AnyNumber;
  19. namespace cc {
  20. namespace {
  21. class TestUIResourceLayer : public UIResourceLayer {
  22. public:
  23. static scoped_refptr<TestUIResourceLayer> Create() {
  24. return base::WrapRefCounted(new TestUIResourceLayer());
  25. }
  26. using UIResourceLayer::resource_id;
  27. using UIResourceLayer::HasDrawableContent;
  28. protected:
  29. TestUIResourceLayer() : UIResourceLayer() { SetIsDrawable(true); }
  30. ~TestUIResourceLayer() override = default;
  31. };
  32. class UIResourceLayerTest : public testing::Test {
  33. protected:
  34. void TearDown() override {
  35. Mock::VerifyAndClearExpectations(layer_tree_host());
  36. }
  37. FakeLayerTreeHost* layer_tree_host() { return layer_impl_test_.host(); }
  38. private:
  39. LayerTreeImplTestBase layer_impl_test_;
  40. };
  41. TEST_F(UIResourceLayerTest, SetBitmap) {
  42. scoped_refptr<UIResourceLayer> test_layer = TestUIResourceLayer::Create();
  43. ASSERT_TRUE(test_layer.get());
  44. test_layer->SetBounds(gfx::Size(100, 100));
  45. layer_tree_host()->SetRootLayer(test_layer);
  46. Mock::VerifyAndClearExpectations(layer_tree_host());
  47. EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host());
  48. test_layer->Update();
  49. EXPECT_FALSE(test_layer->draws_content());
  50. SkBitmap bitmap;
  51. bitmap.allocN32Pixels(10, 10);
  52. bitmap.setImmutable();
  53. test_layer->SetBitmap(bitmap);
  54. test_layer->Update();
  55. EXPECT_TRUE(test_layer->draws_content());
  56. }
  57. TEST_F(UIResourceLayerTest, SetUIResourceId) {
  58. scoped_refptr<TestUIResourceLayer> test_layer = TestUIResourceLayer::Create();
  59. ASSERT_TRUE(test_layer.get());
  60. test_layer->SetBounds(gfx::Size(100, 100));
  61. layer_tree_host()->SetRootLayer(test_layer);
  62. Mock::VerifyAndClearExpectations(layer_tree_host());
  63. EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host());
  64. test_layer->Update();
  65. EXPECT_FALSE(test_layer->draws_content());
  66. bool is_opaque = false;
  67. std::unique_ptr<ScopedUIResource> resource =
  68. ScopedUIResource::Create(layer_tree_host()->GetUIResourceManager(),
  69. UIResourceBitmap(gfx::Size(10, 10), is_opaque));
  70. test_layer->SetUIResourceId(resource->id());
  71. test_layer->Update();
  72. EXPECT_TRUE(test_layer->draws_content());
  73. // ID is preserved even when you set ID first and attach it to the tree.
  74. layer_tree_host()->SetRootLayer(nullptr);
  75. std::unique_ptr<ScopedUIResource> shared_resource =
  76. ScopedUIResource::Create(layer_tree_host()->GetUIResourceManager(),
  77. UIResourceBitmap(gfx::Size(5, 5), is_opaque));
  78. test_layer->SetUIResourceId(shared_resource->id());
  79. layer_tree_host()->SetRootLayer(test_layer);
  80. EXPECT_EQ(shared_resource->id(), test_layer->resource_id());
  81. EXPECT_TRUE(test_layer->draws_content());
  82. }
  83. TEST_F(UIResourceLayerTest, BitmapClearedOnSetUIResourceId) {
  84. scoped_refptr<TestUIResourceLayer> test_layer = TestUIResourceLayer::Create();
  85. ASSERT_TRUE(test_layer.get());
  86. test_layer->SetBounds(gfx::Size(100, 100));
  87. EXPECT_FALSE(test_layer->HasDrawableContent());
  88. SkBitmap bitmap;
  89. bitmap.allocN32Pixels(10, 10);
  90. bitmap.setImmutable();
  91. ASSERT_FALSE(bitmap.isNull());
  92. ASSERT_TRUE(bitmap.pixelRef()->unique());
  93. // Without a layer tree, the only additional reference is in UIResourceLayer.
  94. test_layer->SetBitmap(bitmap);
  95. ASSERT_FALSE(bitmap.pixelRef()->unique());
  96. // Also, there's no drawable content due to the lack of a LTH.
  97. EXPECT_FALSE(test_layer->HasDrawableContent());
  98. test_layer->SetUIResourceId(0);
  99. EXPECT_TRUE(bitmap.pixelRef()->unique());
  100. EXPECT_FALSE(test_layer->HasDrawableContent());
  101. // Add to a layer tree; now the UIResourceManager holds onto a ref
  102. // indefinitely.
  103. {
  104. LayerTreeImplTestBase impl;
  105. impl.host()->SetRootLayer(test_layer);
  106. test_layer->SetBitmap(bitmap);
  107. EXPECT_FALSE(bitmap.pixelRef()->unique());
  108. EXPECT_TRUE(test_layer->HasDrawableContent());
  109. test_layer->SetUIResourceId(0);
  110. EXPECT_FALSE(bitmap.pixelRef()->unique());
  111. EXPECT_FALSE(test_layer->HasDrawableContent());
  112. }
  113. // After the layer tree/resource manager are destroyed, refs are back to 1.
  114. test_layer->SetUIResourceId(0);
  115. EXPECT_TRUE(bitmap.pixelRef()->unique());
  116. EXPECT_FALSE(test_layer->HasDrawableContent());
  117. }
  118. TEST_F(UIResourceLayerTest, SharedBitmap) {
  119. SkBitmap bitmap;
  120. bitmap.allocN32Pixels(10, 10);
  121. bitmap.setImmutable();
  122. // The SkPixelRef is what's important, not the SkBitmap itself.
  123. SkBitmap bitmap_copy = bitmap;
  124. scoped_refptr<TestUIResourceLayer> layer1 = TestUIResourceLayer::Create();
  125. layer_tree_host()->SetRootLayer(layer1);
  126. layer1->SetBitmap(bitmap);
  127. bitmap.reset();
  128. layer1->Update();
  129. EXPECT_TRUE(layer1->draws_content());
  130. const auto resource_id = layer1->resource_id();
  131. // Second layer, same LTH. Resource is shared (has same ID).
  132. scoped_refptr<TestUIResourceLayer> layer2 = TestUIResourceLayer::Create();
  133. layer_tree_host()->SetRootLayer(layer2);
  134. layer2->SetBitmap(bitmap_copy);
  135. layer2->Update();
  136. EXPECT_TRUE(layer2->draws_content());
  137. EXPECT_EQ(resource_id, layer2->resource_id());
  138. // Change bitmap, different resource id.
  139. SkBitmap other_bitmap;
  140. other_bitmap.allocN32Pixels(12, 12);
  141. other_bitmap.setImmutable();
  142. layer2->SetBitmap(other_bitmap);
  143. EXPECT_NE(resource_id, layer2->resource_id());
  144. // Switch layer to different LTH. ID is in a new namespace (LTH), so it may
  145. // still be the same. We can make sure it's using the same shared bitmap by
  146. // verifying that whatever ID it has, it changes away from and back to when we
  147. // change the shared bitmap to something else then back to the original.
  148. LayerTreeImplTestBase impl;
  149. impl.host()->SetRootLayer(layer1);
  150. layer1->Update();
  151. EXPECT_TRUE(layer1->draws_content());
  152. const auto other_lth_resource_id = layer1->resource_id();
  153. layer1->SetBitmap(other_bitmap);
  154. EXPECT_NE(other_lth_resource_id, layer1->resource_id());
  155. layer1->SetBitmap(bitmap_copy);
  156. EXPECT_EQ(other_lth_resource_id, layer1->resource_id());
  157. }
  158. } // namespace
  159. } // namespace cc