OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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 |
| 5 #include "gpu/command_buffer/service/service_discardable_manager.h" |
| 6 |
| 7 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h" |
| 8 #include "gpu/command_buffer/service/gpu_service_test.h" |
| 9 #include "gpu/command_buffer/service/mailbox_manager.h" |
| 10 #include "gpu/command_buffer/service/memory_tracking.h" |
| 11 #include "gpu/command_buffer/service/mocks.h" |
| 12 #include "gpu/command_buffer/service/test_helper.h" |
| 13 #include "gpu/command_buffer/service/texture_manager.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/gl/gl_image_stub.h" |
| 16 #include "ui/gl/gl_mock.h" |
| 17 #include "ui/gl/gl_switches.h" |
| 18 |
| 19 using ::testing::Pointee; |
| 20 using ::testing::_; |
| 21 |
| 22 namespace gpu { |
| 23 namespace gles2 { |
| 24 namespace { |
| 25 |
| 26 ServiceDiscardableHandle CreateLockedHandleForTesting() { |
| 27 const int32_t kHandleLockedStart = 2; |
| 28 |
| 29 std::unique_ptr<base::SharedMemory> shared_mem(new base::SharedMemory); |
| 30 shared_mem->CreateAndMapAnonymous(sizeof(uint32_t)); |
| 31 scoped_refptr<gpu::Buffer> buffer = |
| 32 MakeBufferFromSharedMemory(std::move(shared_mem), sizeof(uint32_t)); |
| 33 *reinterpret_cast<uint32_t*>(buffer->memory()) = kHandleLockedStart; |
| 34 |
| 35 return ServiceDiscardableHandle(buffer, 0, 0); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 class ServiceDiscardableManagerTest : public GpuServiceTest { |
| 41 public: |
| 42 ServiceDiscardableManagerTest() {} |
| 43 ~ServiceDiscardableManagerTest() override {} |
| 44 |
| 45 protected: |
| 46 void SetUp() override { |
| 47 GpuServiceTest::SetUp(); |
| 48 decoder_.reset(new MockGLES2Decoder()); |
| 49 feature_info_ = new FeatureInfo(); |
| 50 context_group_ = scoped_refptr<ContextGroup>(new ContextGroup( |
| 51 gpu_preferences_, nullptr, nullptr, nullptr, nullptr, feature_info_, |
| 52 false, nullptr, nullptr, GpuFeatureInfo(), &discardable_manager_)); |
| 53 TestHelper::SetupContextGroupInitExpectations( |
| 54 gl_.get(), DisallowedFeatures(), "", "", CONTEXT_TYPE_OPENGLES2, false); |
| 55 context_group_->Initialize(decoder_.get(), CONTEXT_TYPE_OPENGLES2, |
| 56 DisallowedFeatures()); |
| 57 texture_manager_ = context_group_->texture_manager(); |
| 58 } |
| 59 |
| 60 void TearDown() override { |
| 61 context_group_->Destroy(decoder_.get(), false); |
| 62 context_group_ = nullptr; |
| 63 EXPECT_EQ(0u, discardable_manager_.NumCacheEntriesForTesting()); |
| 64 GpuServiceTest::TearDown(); |
| 65 } |
| 66 |
| 67 ServiceDiscardableManager discardable_manager_; |
| 68 GpuPreferences gpu_preferences_; |
| 69 scoped_refptr<FeatureInfo> feature_info_; |
| 70 TextureManager* texture_manager_; |
| 71 std::unique_ptr<MockGLES2Decoder> decoder_; |
| 72 scoped_refptr<gles2::ContextGroup> context_group_; |
| 73 }; |
| 74 |
| 75 TEST_F(ServiceDiscardableManagerTest, BasicUsage) { |
| 76 const GLuint kClientId = 1; |
| 77 const GLuint kServiceId = 2; |
| 78 const size_t texture_size = 4 * 1024 * 1024; |
| 79 |
| 80 texture_manager_->CreateTexture(kClientId, kServiceId); |
| 81 auto handle = CreateLockedHandleForTesting(); |
| 82 |
| 83 discardable_manager_.InsertLockedTexture(kClientId, texture_size, |
| 84 texture_manager_, handle); |
| 85 EXPECT_EQ(1u, discardable_manager_.NumCacheEntriesForTesting()); |
| 86 EXPECT_TRUE(discardable_manager_.IsEntryLockedForTesting(kClientId, |
| 87 texture_manager_)); |
| 88 gles2::TextureRef* texture_to_unbind; |
| 89 EXPECT_TRUE(discardable_manager_.UnlockTexture(kClientId, texture_manager_, |
| 90 &texture_to_unbind)); |
| 91 EXPECT_NE(nullptr, texture_to_unbind); |
| 92 EXPECT_FALSE(discardable_manager_.IsEntryLockedForTesting(kClientId, |
| 93 texture_manager_)); |
| 94 } |
| 95 |
| 96 TEST_F(ServiceDiscardableManagerTest, UnlockInvalid) { |
| 97 const GLuint kClientId = 1; |
| 98 gles2::TextureRef* texture_to_unbind; |
| 99 EXPECT_FALSE(discardable_manager_.UnlockTexture(kClientId, texture_manager_, |
| 100 &texture_to_unbind)); |
| 101 EXPECT_EQ(nullptr, texture_to_unbind); |
| 102 } |
| 103 |
| 104 TEST_F(ServiceDiscardableManagerTest, Limits) { |
| 105 const size_t texture_size = 64 * 1024 * 1024; |
| 106 |
| 107 // Create 4 textures, this should fill up the discardable cache. |
| 108 for (int i = 1; i < 5; ++i) { |
| 109 texture_manager_->CreateTexture(i, i); |
| 110 auto handle = CreateLockedHandleForTesting(); |
| 111 discardable_manager_.InsertLockedTexture(i, texture_size, texture_manager_, |
| 112 handle); |
| 113 } |
| 114 |
| 115 gles2::TextureRef* texture_to_unbind; |
| 116 EXPECT_TRUE(discardable_manager_.UnlockTexture(3, texture_manager_, |
| 117 &texture_to_unbind)); |
| 118 EXPECT_NE(nullptr, texture_to_unbind); |
| 119 EXPECT_TRUE(discardable_manager_.UnlockTexture(1, texture_manager_, |
| 120 &texture_to_unbind)); |
| 121 EXPECT_NE(nullptr, texture_to_unbind); |
| 122 EXPECT_TRUE(discardable_manager_.UnlockTexture(2, texture_manager_, |
| 123 &texture_to_unbind)); |
| 124 EXPECT_NE(nullptr, texture_to_unbind); |
| 125 EXPECT_TRUE(discardable_manager_.UnlockTexture(4, texture_manager_, |
| 126 &texture_to_unbind)); |
| 127 EXPECT_NE(nullptr, texture_to_unbind); |
| 128 |
| 129 // Allocate four more textures - the previous 4 should be evicted / deleted. |
| 130 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(3))).RetiresOnSaturation(); |
| 131 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(1))).RetiresOnSaturation(); |
| 132 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(2))).RetiresOnSaturation(); |
| 133 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(4))).RetiresOnSaturation(); |
| 134 |
| 135 for (int i = 5; i < 9; ++i) { |
| 136 texture_manager_->CreateTexture(i, i); |
| 137 auto handle = CreateLockedHandleForTesting(); |
| 138 discardable_manager_.InsertLockedTexture(i, texture_size, texture_manager_, |
| 139 handle); |
| 140 } |
| 141 |
| 142 // Delete these last 4 textures. |
| 143 for (int i = 5; i < 9; ++i) { |
| 144 discardable_manager_.OnTextureDeleted(i, texture_manager_); |
| 145 } |
| 146 } |
| 147 |
| 148 TEST_F(ServiceDiscardableManagerTest, TextureSizeChanged) { |
| 149 const GLuint kClientId = 1; |
| 150 const GLuint kServiceId = 2; |
| 151 const size_t texture_size = 4 * 1024 * 1024; |
| 152 |
| 153 texture_manager_->CreateTexture(kClientId, kServiceId); |
| 154 TextureRef* texture_ref = texture_manager_->GetTexture(kClientId); |
| 155 auto handle = CreateLockedHandleForTesting(); |
| 156 discardable_manager_.InsertLockedTexture(kClientId, 0, texture_manager_, |
| 157 handle); |
| 158 EXPECT_EQ(0u, discardable_manager_.TotalSizeForTesting()); |
| 159 texture_manager_->SetTarget(texture_ref, GL_TEXTURE_2D); |
| 160 texture_manager_->SetLevelInfo(texture_ref, GL_TEXTURE_2D, 0, GL_RGBA, 1024, |
| 161 1024, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 162 gfx::Rect(1024, 1024)); |
| 163 EXPECT_EQ(texture_size, discardable_manager_.TotalSizeForTesting()); |
| 164 } |
| 165 |
| 166 } // namespace gles2 |
| 167 } // namespace gpu |
OLD | NEW |