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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/common/gpu/context_cache_controller.h"
#include <utility>
#include <vector>
#include "base/test/test_mock_time_task_runner.h"
#include "components/viz/test/test_context_provider.h"
#include "components/viz/test/test_context_support.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
using ::testing::Mock;
using ::testing::StrictMock;
namespace viz {
namespace {
class MockContextSupport : public TestContextSupport {
public:
MockContextSupport() = default;
MOCK_METHOD1(SetAggressivelyFreeResources,
void(bool aggressively_free_resources));
};
TEST(ContextCacheControllerTest, ScopedVisibilityBasic) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
ContextCacheController cache_controller(&context_support, task_runner);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
std::unique_ptr<ContextCacheController::ScopedVisibility> visibility =
cache_controller.ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.ClientBecameNotVisible(std::move(visibility));
}
TEST(ContextCacheControllerTest, ScopedVisibilityMulti) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
ContextCacheController cache_controller(&context_support, task_runner);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
auto visibility_1 = cache_controller.ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
auto visibility_2 = cache_controller.ClientBecameVisible();
cache_controller.ClientBecameNotVisible(std::move(visibility_1));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.ClientBecameNotVisible(std::move(visibility_2));
}
// Check that resources aren't freed during shutdown until the
// ContextCacheController is also deleted.
TEST(ContextCacheControllerTest, ScopedVisibilityDuringShutdown) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
std::unique_ptr<ContextCacheController> cache_controller =
std::make_unique<ContextCacheController>(&context_support, task_runner);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
std::unique_ptr<ContextCacheController::ScopedVisibility> visibility =
cache_controller->ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
cache_controller->ClientBecameNotVisibleDuringShutdown(std::move(visibility));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.reset();
}
// Check that multiple clients can shutdown successfully.
TEST(ContextCacheControllerTest, ScopedVisibilityDuringShutdownMulti) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
std::unique_ptr<ContextCacheController> cache_controller =
std::make_unique<ContextCacheController>(&context_support, task_runner);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
auto visibility_1 = cache_controller->ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
auto visibility_2 = cache_controller->ClientBecameVisible();
cache_controller->ClientBecameNotVisibleDuringShutdown(
std::move(visibility_1));
cache_controller->ClientBecameNotVisibleDuringShutdown(
std::move(visibility_2));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.reset();
}
TEST(ContextCacheControllerTest, ScopedBusyWhileVisible) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
ContextCacheController cache_controller(&context_support, task_runner);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
auto visibility = cache_controller.ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
// Now that we're visible, ensure that going idle triggers a delayed cleanup.
auto busy = cache_controller.ClientBecameBusy();
cache_controller.ClientBecameNotBusy(std::move(busy));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
task_runner->FastForwardBy(base::Seconds(5));
Mock::VerifyAndClearExpectations(&context_support);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.ClientBecameNotVisible(std::move(visibility));
}
TEST(ContextCacheControllerTest, ScopedBusyWhileNotVisible) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
ContextCacheController cache_controller(&context_support, task_runner);
auto busy = cache_controller.ClientBecameBusy();
// We are not visible, so becoming busy should not trigger an idle callback.
cache_controller.ClientBecameNotBusy(std::move(busy));
task_runner->FastForwardBy(base::Seconds(5));
}
TEST(ContextCacheControllerTest, ScopedBusyMulitpleWhileVisible) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
ContextCacheController cache_controller(&context_support, task_runner);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
auto visible = cache_controller.ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
auto busy_1 = cache_controller.ClientBecameBusy();
cache_controller.ClientBecameNotBusy(std::move(busy_1));
auto busy_2 = cache_controller.ClientBecameBusy();
cache_controller.ClientBecameNotBusy(std::move(busy_2));
// When we fast forward, only one cleanup should happen.
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
task_runner->FastForwardBy(base::Seconds(5));
Mock::VerifyAndClearExpectations(&context_support);
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.ClientBecameNotVisible(std::move(visible));
}
// Confirms that the Skia performDeferredCleanup API used by the cache
// controller behaves as expected.
TEST(ContextCacheControllerTest, CheckSkiaResourcePurgeAPI) {
StrictMock<MockContextSupport> context_support;
auto task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
// Must outlive `cache_controller`.
auto context_provider = TestContextProvider::Create();
context_provider->BindToCurrentSequence();
ContextCacheController cache_controller(&context_support, task_runner);
auto* gr_context = context_provider->GrContext();
cache_controller.SetGrContext(gr_context);
// Make us visible.
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
auto visibility = cache_controller.ClientBecameVisible();
Mock::VerifyAndClearExpectations(&context_support);
// Now that we're visible, become busy, create and release a skia resource.
auto busy = cache_controller.ClientBecameBusy();
{
auto image_info = SkImageInfo::MakeN32Premul(200, 200);
std::vector<uint8_t> image_data(image_info.computeMinByteSize());
SkPixmap pixmap(image_info, image_data.data(), image_info.minRowBytes());
auto image = SkImages::RasterFromPixmapCopy(pixmap);
auto image_gpu = SkImages::TextureFromImage(gr_context, std::move(image));
gr_context->flushAndSubmit();
}
// Ensure we see size taken up for the image (now released, but cached for
// re-use).
EXPECT_GT(gr_context->getResourceCachePurgeableBytes(), 0u);
// Make the client idle and wait for the idle callback to trigger.
cache_controller.ClientBecameNotBusy(std::move(busy));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
EXPECT_CALL(context_support, SetAggressivelyFreeResources(false));
task_runner->FastForwardBy(base::Seconds(5));
Mock::VerifyAndClearExpectations(&context_support);
// The Skia resource cache should now be empty.
EXPECT_EQ(gr_context->getResourceCachePurgeableBytes(), 0u);
// Set not-visible.
EXPECT_CALL(context_support, SetAggressivelyFreeResources(true));
cache_controller.ClientBecameNotVisible(std::move(visibility));
}
} // namespace
} // namespace viz
|