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 discardable_manager_ = scoped_refptr<ServiceDiscardableManager>( |
| 49 new ServiceDiscardableManager()); |
| 50 decoder_.reset(new MockGLES2Decoder()); |
| 51 feature_info_ = new FeatureInfo(); |
| 52 context_group_ = scoped_refptr<ContextGroup>(new ContextGroup( |
| 53 gpu_preferences_, NULL, NULL, NULL, NULL, feature_info_, false, nullptr, |
| 54 nullptr, GpuFeatureInfo())); |
| 55 TestHelper::SetupContextGroupInitExpectations( |
| 56 gl_.get(), DisallowedFeatures(), "", "", CONTEXT_TYPE_OPENGLES2, false); |
| 57 context_group_->Initialize(decoder_.get(), CONTEXT_TYPE_OPENGLES2, |
| 58 DisallowedFeatures()); |
| 59 texture_manager_ = context_group_->texture_manager(); |
| 60 } |
| 61 |
| 62 void TearDown() override { |
| 63 context_group_->Destroy(decoder_.get(), false); |
| 64 context_group_ = nullptr; |
| 65 GpuServiceTest::TearDown(); |
| 66 } |
| 67 |
| 68 scoped_refptr<ServiceDiscardableManager> discardable_manager_; |
| 69 GpuPreferences gpu_preferences_; |
| 70 scoped_refptr<FeatureInfo> feature_info_; |
| 71 TextureManager* texture_manager_; |
| 72 std::unique_ptr<MockGLES2Decoder> decoder_; |
| 73 scoped_refptr<gles2::ContextGroup> context_group_; |
| 74 }; |
| 75 |
| 76 TEST_F(ServiceDiscardableManagerTest, BasicUsage) { |
| 77 const GLuint kClientId = 1; |
| 78 const GLuint kServiceId = 2; |
| 79 const size_t texture_size = 64 * 1024 * 1024; |
| 80 |
| 81 texture_manager_->CreateTexture(kClientId, kServiceId); |
| 82 auto handle = CreateLockedHandleForTesting(); |
| 83 |
| 84 discardable_manager_->InsertLockedTexture(kClientId, texture_size, |
| 85 context_group_.get(), handle); |
| 86 EXPECT_TRUE( |
| 87 discardable_manager_->UnlockTexture(kClientId, context_group_.get())); |
| 88 discardable_manager_->OnTextureDeleted(kClientId, context_group_.get()); |
| 89 } |
| 90 |
| 91 TEST_F(ServiceDiscardableManagerTest, UnlockInvalid) { |
| 92 const GLuint kClientId = 1; |
| 93 EXPECT_FALSE( |
| 94 discardable_manager_->UnlockTexture(kClientId, context_group_.get())); |
| 95 } |
| 96 |
| 97 TEST_F(ServiceDiscardableManagerTest, Limits) { |
| 98 const size_t texture_size = 64 * 1024 * 1024; |
| 99 |
| 100 // Create 4 textures, this should fill up the discardable cache. |
| 101 for (int i = 1; i < 5; ++i) { |
| 102 texture_manager_->CreateTexture(i, i); |
| 103 auto handle = CreateLockedHandleForTesting(); |
| 104 discardable_manager_->InsertLockedTexture(i, texture_size, |
| 105 context_group_.get(), handle); |
| 106 } |
| 107 |
| 108 // Unlock textures in an arbitrary order, 2, 0, 1, 3, in order to test MRU. |
| 109 { |
| 110 testing::InSequence force_sequence; |
| 111 EXPECT_TRUE(discardable_manager_->UnlockTexture(3, context_group_.get())); |
| 112 EXPECT_TRUE(discardable_manager_->UnlockTexture(1, context_group_.get())); |
| 113 EXPECT_TRUE(discardable_manager_->UnlockTexture(2, context_group_.get())); |
| 114 EXPECT_TRUE(discardable_manager_->UnlockTexture(4, context_group_.get())); |
| 115 } |
| 116 |
| 117 // Allocate four more textures - the previous 4 should be evicted / deleted. |
| 118 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(3))).RetiresOnSaturation(); |
| 119 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(1))).RetiresOnSaturation(); |
| 120 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(2))).RetiresOnSaturation(); |
| 121 EXPECT_CALL(*gl_, DeleteTextures(1, Pointee(4))).RetiresOnSaturation(); |
| 122 |
| 123 for (int i = 5; i < 9; ++i) { |
| 124 texture_manager_->CreateTexture(i, i); |
| 125 auto handle = CreateLockedHandleForTesting(); |
| 126 discardable_manager_->InsertLockedTexture(i, texture_size, |
| 127 context_group_.get(), handle); |
| 128 } |
| 129 |
| 130 // Delete these last 4 textures. |
| 131 for (int i = 5; i < 9; ++i) { |
| 132 discardable_manager_->OnTextureDeleted(i, context_group_.get()); |
| 133 } |
| 134 } |
| 135 |
| 136 } // namespace gles2 |
| 137 } // namespace gpu |
OLD | NEW |