1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228

cc / layers / ui_resource_layer_unittest.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/layers/ui_resource_layer.h"

#include "cc/animation/animation_host.h"
#include "cc/resources/scoped_ui_resource.h"
#include "cc/resources/ui_resource_manager.h"
#include "cc/test/fake_layer_tree_host.h"
#include "cc/test/layer_tree_impl_test_base.h"
#include "cc/test/stub_layer_tree_host_single_thread_client.h"
#include "cc/trees/single_thread_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"

using ::testing::Mock;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::AnyNumber;

namespace cc {
namespace {

class TestUIResourceLayer : public UIResourceLayer {
 public:
  static scoped_refptr<TestUIResourceLayer> Create() {
    return base::WrapRefCounted(new TestUIResourceLayer());
  }

  using UIResourceLayer::resource_id;
  using UIResourceLayer::HasDrawableContent;

 protected:
  TestUIResourceLayer() : UIResourceLayer() { SetIsDrawable(true); }
  ~TestUIResourceLayer() override = default;
};

class UIResourceLayerTest : public testing::Test {
 protected:
  void TearDown() override {
    Mock::VerifyAndClearExpectations(layer_tree_host());
  }

  FakeLayerTreeHost* layer_tree_host() { return layer_impl_test_.host(); }

 private:
  LayerTreeImplTestBase layer_impl_test_;
};

TEST_F(UIResourceLayerTest, SetBitmap) {
  scoped_refptr<UIResourceLayer> test_layer = TestUIResourceLayer::Create();
  ASSERT_TRUE(test_layer.get());
  test_layer->SetBounds(gfx::Size(100, 100));

  layer_tree_host()->SetRootLayer(test_layer);
  Mock::VerifyAndClearExpectations(layer_tree_host());
  EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host());

  test_layer->Update();

  EXPECT_FALSE(test_layer->draws_content());

  SkBitmap bitmap;
  bitmap.allocN32Pixels(10, 10);
  bitmap.setImmutable();

  test_layer->SetBitmap(bitmap);
  test_layer->Update();

  EXPECT_TRUE(test_layer->draws_content());
}

TEST_F(UIResourceLayerTest, SetUIResourceId) {
  scoped_refptr<TestUIResourceLayer> test_layer = TestUIResourceLayer::Create();
  ASSERT_TRUE(test_layer.get());
  test_layer->SetBounds(gfx::Size(100, 100));

  layer_tree_host()->SetRootLayer(test_layer);
  Mock::VerifyAndClearExpectations(layer_tree_host());
  EXPECT_EQ(test_layer->layer_tree_host(), layer_tree_host());

  test_layer->Update();

  EXPECT_FALSE(test_layer->draws_content());

  bool is_opaque = false;
  std::unique_ptr<ScopedUIResource> resource =
      ScopedUIResource::Create(layer_tree_host()->GetUIResourceManager(),
                               UIResourceBitmap(gfx::Size(10, 10), is_opaque));
  test_layer->SetUIResourceId(resource->id());
  test_layer->Update();

  EXPECT_TRUE(test_layer->draws_content());

  // ID is preserved even when you set ID first and attach it to the tree.
  layer_tree_host()->SetRootLayer(nullptr);
  std::unique_ptr<ScopedUIResource> shared_resource =
      ScopedUIResource::Create(layer_tree_host()->GetUIResourceManager(),
                               UIResourceBitmap(gfx::Size(5, 5), is_opaque));
  test_layer->SetUIResourceId(shared_resource->id());
  layer_tree_host()->SetRootLayer(test_layer);
  EXPECT_EQ(shared_resource->id(), test_layer->resource_id());
  EXPECT_TRUE(test_layer->draws_content());
}

TEST_F(UIResourceLayerTest, BitmapClearedOnSetUIResourceId) {
  scoped_refptr<TestUIResourceLayer> test_layer = TestUIResourceLayer::Create();
  ASSERT_TRUE(test_layer.get());
  test_layer->SetBounds(gfx::Size(100, 100));
  EXPECT_FALSE(test_layer->HasDrawableContent());

  SkBitmap bitmap;
  bitmap.allocN32Pixels(10, 10);
  bitmap.setImmutable();
  ASSERT_FALSE(bitmap.isNull());
  ASSERT_TRUE(bitmap.pixelRef()->unique());

  // Without a layer tree, the only additional reference is in UIResourceLayer.
  test_layer->SetBitmap(bitmap);
  ASSERT_FALSE(bitmap.pixelRef()->unique());
  // Also, there's no drawable content due to the lack of a LTH.
  EXPECT_FALSE(test_layer->HasDrawableContent());

  test_layer->SetUIResourceId(0);
  EXPECT_TRUE(bitmap.pixelRef()->unique());
  EXPECT_FALSE(test_layer->HasDrawableContent());

  // Add to a layer tree; now the UIResourceManager holds onto a ref
  // indefinitely.
  {
    LayerTreeImplTestBase impl;
    impl.host()->SetRootLayer(test_layer);

    test_layer->SetBitmap(bitmap);
    EXPECT_FALSE(bitmap.pixelRef()->unique());
    EXPECT_TRUE(test_layer->HasDrawableContent());
    test_layer->SetUIResourceId(0);
    EXPECT_FALSE(bitmap.pixelRef()->unique());
    EXPECT_FALSE(test_layer->HasDrawableContent());
  }

  // After the layer tree/resource manager are destroyed, refs are back to 1.
  test_layer->SetUIResourceId(0);
  EXPECT_TRUE(bitmap.pixelRef()->unique());
  EXPECT_FALSE(test_layer->HasDrawableContent());
}

TEST_F(UIResourceLayerTest, SharedBitmap) {
  SkBitmap bitmap;
  bitmap.allocN32Pixels(10, 10);
  bitmap.setImmutable();
  // The SkPixelRef is what's important, not the SkBitmap itself.
  SkBitmap bitmap_copy = bitmap;

  scoped_refptr<TestUIResourceLayer> layer1 = TestUIResourceLayer::Create();
  layer_tree_host()->SetRootLayer(layer1);
  layer1->SetBitmap(bitmap);
  bitmap.reset();
  layer1->Update();
  EXPECT_TRUE(layer1->draws_content());
  const auto resource_id = layer1->resource_id();

  // Second layer, same LTH. Resource is shared (has same ID).
  scoped_refptr<TestUIResourceLayer> layer2 = TestUIResourceLayer::Create();
  layer_tree_host()->SetRootLayer(layer2);
  layer2->SetBitmap(bitmap_copy);
  layer2->Update();
  EXPECT_TRUE(layer2->draws_content());
  EXPECT_EQ(resource_id, layer2->resource_id());

  // Change bitmap, different resource id.
  SkBitmap other_bitmap;
  other_bitmap.allocN32Pixels(12, 12);
  other_bitmap.setImmutable();
  layer2->SetBitmap(other_bitmap);
  EXPECT_NE(resource_id, layer2->resource_id());

  // Switch layer to different LTH. ID is in a new namespace (LTH), so it may
  // still be the same. We can make sure it's using the same shared bitmap by
  // verifying that whatever ID it has, it changes away from and back to when we
  // change the shared bitmap to something else then back to the original.
  LayerTreeImplTestBase impl;
  impl.host()->SetRootLayer(layer1);
  layer1->Update();
  EXPECT_TRUE(layer1->draws_content());
  const auto other_lth_resource_id = layer1->resource_id();
  layer1->SetBitmap(other_bitmap);
  EXPECT_NE(other_lth_resource_id, layer1->resource_id());
  layer1->SetBitmap(bitmap_copy);
  EXPECT_EQ(other_lth_resource_id, layer1->resource_id());
}

TEST_F(UIResourceLayerTest, SharedBitmapCacheSizeLimit) {
  scoped_refptr<TestUIResourceLayer> layer = TestUIResourceLayer::Create();
  layer_tree_host()->SetRootLayer(layer);

  // Number of bitmaps that are created then get their references dropped.
  constexpr size_t kDroppedResources = 100u;

  // Populate the shared bitmap cache.
  auto* manager = layer_tree_host()->GetUIResourceManager();
  while (manager->owned_shared_resources_size_for_test() < kDroppedResources) {
    SkBitmap bitmap;
    bitmap.allocN32Pixels(10, 10);
    bitmap.setImmutable();
    layer->SetBitmap(bitmap);
  }

  // No eviction because bitmaps are references by UIResourcesRequests.
  EXPECT_EQ(manager->owned_shared_resources_size_for_test(), kDroppedResources);

  // Pretend UIResourcesRequests are processed to drop bitmap references.
  manager->TakeUIResourcesRequests();

  // Create one more shared bitmap resource and the eviction happens.
  SkBitmap bitmap;
  bitmap.allocN32Pixels(10, 10);
  bitmap.setImmutable();
  layer->SetBitmap(bitmap);

  // The cache should trimmed down.
  EXPECT_EQ(manager->owned_shared_resources_size_for_test(), 1u);
}

}  // namespace
}  // namespace cc