transfer_cache_unittest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 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. #include <vector>
  5. #include "base/command_line.h"
  6. #include "base/threading/thread_task_runner_handle.h"
  7. #include "build/build_config.h"
  8. #include "cc/paint/image_transfer_cache_entry.h"
  9. #include "cc/paint/raw_memory_transfer_cache_entry.h"
  10. #include "cc/paint/transfer_cache_entry.h"
  11. #include "components/viz/test/test_gpu_service_holder.h"
  12. #include "components/viz/test/test_in_process_context_provider.h"
  13. #include "gpu/command_buffer/client/client_transfer_cache.h"
  14. #include "gpu/command_buffer/client/gles2_cmd_helper.h"
  15. #include "gpu/command_buffer/client/gles2_implementation.h"
  16. #include "gpu/command_buffer/client/raster_interface.h"
  17. #include "gpu/command_buffer/client/shared_memory_limits.h"
  18. #include "gpu/command_buffer/common/context_creation_attribs.h"
  19. #include "gpu/command_buffer/service/service_transfer_cache.h"
  20. #include "gpu/config/gpu_switches.h"
  21. #include "gpu/ipc/raster_in_process_context.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. #include "third_party/skia/include/core/SkImage.h"
  24. #include "ui/gl/gl_implementation.h"
  25. namespace cc {
  26. namespace {
  27. class TransferCacheTest : public testing::Test {
  28. public:
  29. TransferCacheTest() : test_client_entry_(std::vector<uint8_t>(100)) {}
  30. void SetUp() override {
  31. gpu::ContextCreationAttribs attribs;
  32. attribs.alpha_size = -1;
  33. attribs.depth_size = 24;
  34. attribs.stencil_size = 8;
  35. attribs.samples = 0;
  36. attribs.sample_buffers = 0;
  37. attribs.fail_if_major_perf_caveat = false;
  38. attribs.bind_generates_resource = false;
  39. // Enable OOP rasterization.
  40. attribs.enable_oop_rasterization = true;
  41. attribs.enable_raster_interface = true;
  42. attribs.enable_gles2_interface = false;
  43. context_ = std::make_unique<gpu::RasterInProcessContext>();
  44. auto result = context_->Initialize(
  45. viz::TestGpuServiceHolder::GetInstance()->task_executor(), attribs,
  46. gpu::SharedMemoryLimits(), &image_factory_, nullptr, nullptr);
  47. ASSERT_EQ(result, gpu::ContextResult::kSuccess);
  48. ASSERT_TRUE(context_->GetCapabilities().supports_oop_raster);
  49. }
  50. void TearDown() override { context_.reset(); }
  51. gpu::ServiceTransferCache* ServiceTransferCache() {
  52. return context_->GetTransferCacheForTest();
  53. }
  54. int decoder_id() { return context_->GetRasterDecoderIdForTest(); }
  55. gpu::raster::RasterInterface* ri() { return context_->GetImplementation(); }
  56. gpu::ContextSupport* ContextSupport() {
  57. return context_->GetContextSupport();
  58. }
  59. const ClientRawMemoryTransferCacheEntry& test_client_entry() const {
  60. return test_client_entry_;
  61. }
  62. void CreateEntry(const ClientTransferCacheEntry& entry) {
  63. auto* context_support = ContextSupport();
  64. uint32_t size = entry.SerializedSize();
  65. void* data = context_support->MapTransferCacheEntry(size);
  66. ASSERT_TRUE(data);
  67. entry.Serialize(base::make_span(static_cast<uint8_t*>(data), size));
  68. context_support->UnmapAndCreateTransferCacheEntry(entry.UnsafeType(),
  69. entry.Id());
  70. }
  71. private:
  72. viz::TestGpuMemoryBufferManager gpu_memory_buffer_manager_;
  73. viz::TestImageFactory image_factory_;
  74. std::unique_ptr<gpu::RasterInProcessContext> context_;
  75. gl::DisableNullDrawGLBindings enable_pixel_output_;
  76. ClientRawMemoryTransferCacheEntry test_client_entry_;
  77. };
  78. TEST_F(TransferCacheTest, Basic) {
  79. auto* service_cache = ServiceTransferCache();
  80. auto* context_support = ContextSupport();
  81. // Create an entry.
  82. const auto& entry = test_client_entry();
  83. CreateEntry(entry);
  84. ri()->Finish();
  85. // Validate service-side state.
  86. EXPECT_NE(nullptr,
  87. service_cache->GetEntry(gpu::ServiceTransferCache::EntryKey(
  88. decoder_id(), entry.Type(), entry.Id())));
  89. // Unlock on client side and flush to service.
  90. context_support->UnlockTransferCacheEntries(
  91. {{entry.UnsafeType(), entry.Id()}});
  92. ri()->Finish();
  93. // Re-lock on client side and validate state. No need to flush as lock is
  94. // local.
  95. EXPECT_TRUE(context_support->ThreadsafeLockTransferCacheEntry(
  96. entry.UnsafeType(), entry.Id()));
  97. // Delete on client side, flush, and validate that deletion reaches service.
  98. context_support->DeleteTransferCacheEntry(entry.UnsafeType(), entry.Id());
  99. ri()->Finish();
  100. EXPECT_EQ(nullptr,
  101. service_cache->GetEntry(gpu::ServiceTransferCache::EntryKey(
  102. decoder_id(), entry.Type(), entry.Id())));
  103. }
  104. TEST_F(TransferCacheTest, MemoryEviction) {
  105. auto* service_cache = ServiceTransferCache();
  106. auto* context_support = ContextSupport();
  107. const auto& entry = test_client_entry();
  108. // Create an entry.
  109. CreateEntry(entry);
  110. ri()->Finish();
  111. // Validate service-side state.
  112. EXPECT_NE(nullptr,
  113. service_cache->GetEntry(gpu::ServiceTransferCache::EntryKey(
  114. decoder_id(), entry.Type(), entry.Id())));
  115. // Unlock on client side and flush to service.
  116. context_support->UnlockTransferCacheEntries(
  117. {{entry.UnsafeType(), entry.Id()}});
  118. ri()->Finish();
  119. // Evict on the service side.
  120. service_cache->SetCacheSizeLimitForTesting(0);
  121. EXPECT_EQ(nullptr,
  122. service_cache->GetEntry(gpu::ServiceTransferCache::EntryKey(
  123. decoder_id(), entry.Type(), entry.Id())));
  124. // Try to re-lock on the client side. This should fail.
  125. EXPECT_FALSE(context_support->ThreadsafeLockTransferCacheEntry(
  126. entry.UnsafeType(), entry.Id()));
  127. }
  128. TEST_F(TransferCacheTest, CountEviction) {
  129. auto* service_cache = ServiceTransferCache();
  130. auto* context_support = ContextSupport();
  131. // Create 10 entries and leave them all unlocked.
  132. std::vector<std::unique_ptr<ClientRawMemoryTransferCacheEntry>> entries;
  133. for (int i = 0; i < 10; ++i) {
  134. entries.emplace_back(std::make_unique<ClientRawMemoryTransferCacheEntry>(
  135. std::vector<uint8_t>(4)));
  136. CreateEntry(*entries[i]);
  137. context_support->UnlockTransferCacheEntries(
  138. {{entries[i]->UnsafeType(), entries[i]->Id()}});
  139. ri()->Finish();
  140. }
  141. // These entries should be using up space.
  142. EXPECT_EQ(service_cache->cache_size_for_testing(), 40u);
  143. // Evict on the service side.
  144. service_cache->SetMaxCacheEntriesForTesting(5);
  145. // Half the entries should be evicted.
  146. EXPECT_EQ(service_cache->cache_size_for_testing(), 20u);
  147. }
  148. // This tests a size that is small enough that the transfer buffer is used
  149. // inside of RasterImplementation::MapTransferCacheEntry.
  150. TEST_F(TransferCacheTest, RawMemoryTransferSmall) {
  151. auto* service_cache = ServiceTransferCache();
  152. // Create an entry with some initialized data.
  153. std::vector<uint8_t> data;
  154. data.resize(100);
  155. for (size_t i = 0; i < data.size(); ++i) {
  156. data[i] = i;
  157. }
  158. // Add the entry to the transfer cache
  159. ClientRawMemoryTransferCacheEntry client_entry(data);
  160. CreateEntry(client_entry);
  161. ri()->Finish();
  162. // Validate service-side data matches.
  163. ServiceTransferCacheEntry* service_entry =
  164. service_cache->GetEntry(gpu::ServiceTransferCache::EntryKey(
  165. decoder_id(), client_entry.Type(), client_entry.Id()));
  166. EXPECT_EQ(service_entry->Type(), client_entry.Type());
  167. const std::vector<uint8_t> service_data =
  168. static_cast<ServiceRawMemoryTransferCacheEntry*>(service_entry)->data();
  169. EXPECT_EQ(data, service_data);
  170. }
  171. // This tests a size that is large enough that mapped memory is used inside
  172. // of RasterImplementation::MapTransferCacheEntry.
  173. TEST_F(TransferCacheTest, RawMemoryTransferLarge) {
  174. auto* service_cache = ServiceTransferCache();
  175. // Create an entry with some initialized data.
  176. std::vector<uint8_t> data;
  177. data.resize(1500);
  178. for (size_t i = 0; i < data.size(); ++i) {
  179. data[i] = i;
  180. }
  181. // Add the entry to the transfer cache
  182. ClientRawMemoryTransferCacheEntry client_entry(data);
  183. CreateEntry(client_entry);
  184. ri()->Finish();
  185. // Validate service-side data matches.
  186. ServiceTransferCacheEntry* service_entry =
  187. service_cache->GetEntry(gpu::ServiceTransferCache::EntryKey(
  188. decoder_id(), client_entry.Type(), client_entry.Id()));
  189. EXPECT_EQ(service_entry->Type(), client_entry.Type());
  190. const std::vector<uint8_t> service_data =
  191. static_cast<ServiceRawMemoryTransferCacheEntry*>(service_entry)->data();
  192. EXPECT_EQ(data, service_data);
  193. }
  194. } // namespace
  195. } // namespace cc