Index: gpu/command_buffer/service/service_discardable_manager_unittest.cc |
diff --git a/gpu/command_buffer/service/service_discardable_manager_unittest.cc b/gpu/command_buffer/service/service_discardable_manager_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..78f812d191cc1989210b5c5e9a9d66feff1742e0 |
--- /dev/null |
+++ b/gpu/command_buffer/service/service_discardable_manager_unittest.cc |
@@ -0,0 +1,167 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "gpu/command_buffer/service/service_discardable_manager.h" |
+ |
+#include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h" |
+#include "gpu/command_buffer/service/gpu_service_test.h" |
+#include "gpu/command_buffer/service/mailbox_manager.h" |
+#include "gpu/command_buffer/service/memory_tracking.h" |
+#include "gpu/command_buffer/service/mocks.h" |
+#include "gpu/command_buffer/service/test_helper.h" |
+#include "gpu/command_buffer/service/texture_manager.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gl/gl_image_stub.h" |
+#include "ui/gl/gl_mock.h" |
+#include "ui/gl/gl_switches.h" |
+ |
+using ::testing::Pointee; |
+using ::testing::_; |
+ |
+namespace gpu { |
+namespace gles2 { |
+namespace { |
+ |
+ServiceDiscardableHandle CreateLockedHandleForTesting() { |
+ const int32_t kHandleLockedStart = 2; |
+ |
+ std::unique_ptr<base::SharedMemory> shared_mem(new base::SharedMemory); |
+ shared_mem->CreateAndMapAnonymous(sizeof(uint32_t)); |
+ scoped_refptr<gpu::Buffer> buffer = |
+ MakeBufferFromSharedMemory(std::move(shared_mem), sizeof(uint32_t)); |
+ *reinterpret_cast<uint32_t*>(buffer->memory()) = kHandleLockedStart; |
+ |
+ return ServiceDiscardableHandle(buffer, 0, 0); |
+} |
+ |
+} // namespace |
+ |
+class ServiceDiscardableManagerTest : public GpuServiceTest { |
+ public: |
+ ServiceDiscardableManagerTest() {} |
+ ~ServiceDiscardableManagerTest() override {} |
+ |
+ protected: |
+ void SetUp() override { |
+ GpuServiceTest::SetUp(); |
+ decoder_.reset(new MockGLES2Decoder()); |
+ feature_info_ = new FeatureInfo(); |
+ context_group_ = scoped_refptr<ContextGroup>(new ContextGroup( |
+ gpu_preferences_, nullptr, nullptr, nullptr, nullptr, feature_info_, |
+ false, nullptr, nullptr, GpuFeatureInfo(), &discardable_manager_)); |
+ TestHelper::SetupContextGroupInitExpectations( |
+ gl_.get(), DisallowedFeatures(), "", "", CONTEXT_TYPE_OPENGLES2, false); |
+ context_group_->Initialize(decoder_.get(), CONTEXT_TYPE_OPENGLES2, |
+ DisallowedFeatures()); |
+ texture_manager_ = context_group_->texture_manager(); |
+ } |
+ |
+ void TearDown() override { |
+ context_group_->Destroy(decoder_.get(), false); |
+ context_group_ = nullptr; |
+ EXPECT_EQ(0u, discardable_manager_.NumCacheEntriesForTesting()); |
+ GpuServiceTest::TearDown(); |
+ } |
+ |
+ ServiceDiscardableManager discardable_manager_; |
+ GpuPreferences gpu_preferences_; |
+ scoped_refptr<FeatureInfo> feature_info_; |
+ TextureManager* texture_manager_; |
+ std::unique_ptr<MockGLES2Decoder> decoder_; |
+ scoped_refptr<gles2::ContextGroup> context_group_; |
+}; |
+ |
+TEST_F(ServiceDiscardableManagerTest, BasicUsage) { |
+ const GLuint kClientId = 1; |
+ const GLuint kServiceId = 2; |
+ const size_t texture_size = 4 * 1024 * 1024; |
+ |
+ texture_manager_->CreateTexture(kClientId, kServiceId); |
+ auto handle = CreateLockedHandleForTesting(); |
+ |
+ discardable_manager_.InsertLockedTexture(kClientId, texture_size, |
+ texture_manager_, handle); |
+ EXPECT_EQ(1u, discardable_manager_.NumCacheEntriesForTesting()); |
+ EXPECT_TRUE(discardable_manager_.IsEntryLockedForTesting(kClientId, |
+ texture_manager_)); |
+ gles2::TextureRef* texture_to_unbind; |
+ EXPECT_TRUE(discardable_manager_.UnlockTexture(kClientId, texture_manager_, |
+ &texture_to_unbind)); |
+ EXPECT_NE(nullptr, texture_to_unbind); |
+ EXPECT_FALSE(discardable_manager_.IsEntryLockedForTesting(kClientId, |
+ texture_manager_)); |
+} |
+ |
+TEST_F(ServiceDiscardableManagerTest, UnlockInvalid) { |
+ const GLuint kClientId = 1; |
+ gles2::TextureRef* texture_to_unbind; |
+ EXPECT_FALSE(discardable_manager_.UnlockTexture(kClientId, texture_manager_, |
+ &texture_to_unbind)); |
+ EXPECT_EQ(nullptr, texture_to_unbind); |
+} |
+ |
+TEST_F(ServiceDiscardableManagerTest, Limits) { |
+ const size_t texture_size = 64 * 1024 * 1024; |
+ |
+ // Create 4 textures, this should fill up the discardable cache. |
+ for (int i = 1; i < 5; ++i) { |
+ texture_manager_->CreateTexture(i, i); |
+ auto handle = CreateLockedHandleForTesting(); |
+ discardable_manager_.InsertLockedTexture(i, texture_size, texture_manager_, |
+ handle); |
+ } |
+ |
+ gles2::TextureRef* texture_to_unbind; |
+ EXPECT_TRUE(discardable_manager_.UnlockTexture(3, texture_manager_, |
+ &texture_to_unbind)); |
+ EXPECT_NE(nullptr, texture_to_unbind); |
+ EXPECT_TRUE(discardable_manager_.UnlockTexture(1, texture_manager_, |
+ &texture_to_unbind)); |
+ EXPECT_NE(nullptr, texture_to_unbind); |
+ EXPECT_TRUE(discardable_manager_.UnlockTexture(2, texture_manager_, |
+ &texture_to_unbind)); |
+ EXPECT_NE(nullptr, texture_to_unbind); |
+ EXPECT_TRUE(discardable_manager_.UnlockTexture(4, texture_manager_, |
+ &texture_to_unbind)); |
+ EXPECT_NE(nullptr, texture_to_unbind); |
+ |
+ // Allocate four more textures - the previous 4 should be evicted / deleted. |
+ EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(3))).RetiresOnSaturation(); |
+ EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(1))).RetiresOnSaturation(); |
+ EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(2))).RetiresOnSaturation(); |
+ EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(4))).RetiresOnSaturation(); |
+ |
+ for (int i = 5; i < 9; ++i) { |
+ texture_manager_->CreateTexture(i, i); |
+ auto handle = CreateLockedHandleForTesting(); |
+ discardable_manager_.InsertLockedTexture(i, texture_size, texture_manager_, |
+ handle); |
+ } |
+ |
+ // Delete these last 4 textures. |
+ for (int i = 5; i < 9; ++i) { |
+ discardable_manager_.OnTextureDeleted(i, texture_manager_); |
+ } |
+} |
+ |
+TEST_F(ServiceDiscardableManagerTest, TextureSizeChanged) { |
+ const GLuint kClientId = 1; |
+ const GLuint kServiceId = 2; |
+ const size_t texture_size = 4 * 1024 * 1024; |
+ |
+ texture_manager_->CreateTexture(kClientId, kServiceId); |
+ TextureRef* texture_ref = texture_manager_->GetTexture(kClientId); |
+ auto handle = CreateLockedHandleForTesting(); |
+ discardable_manager_.InsertLockedTexture(kClientId, 0, texture_manager_, |
+ handle); |
+ EXPECT_EQ(0u, discardable_manager_.TotalSizeForTesting()); |
+ texture_manager_->SetTarget(texture_ref, GL_TEXTURE_2D); |
+ texture_manager_->SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1024, |
+ 1024, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, |
+ gfx::Rect(1024, 1024)); |
+ EXPECT_EQ(texture_size, discardable_manager_.TotalSizeForTesting()); |
+} |
+ |
+} // namespace gles2 |
+} // namespace gpu |