Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "components/viz/common/server_gpu_memory_buffer_manager.h" | |
| 6 | |
| 7 #include "base/test/scoped_task_environment.h" | |
| 8 #include "gpu/ipc/host/gpu_memory_buffer_support.h" | |
| 9 #include "services/ui/gpu/interfaces/gpu_service.mojom.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "ui/gfx/client_native_pixmap_factory.h" | |
| 12 | |
| 13 namespace viz { | |
|
danakj
2017/06/21 18:50:36
maybe throw everything inside a nested anon namesp
sadrul
2017/06/21 20:38:54
Yep, see next line :)
danakj
2017/06/21 21:44:08
Derp, oh I didn't see it closing at the bottom of
| |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class TestGpuService : public ui::mojom::GpuService { | |
| 18 public: | |
| 19 TestGpuService() {} | |
| 20 ~TestGpuService() override {} | |
| 21 | |
| 22 bool HasAllocationRequest(gfx::GpuMemoryBufferId id, int client_id) const { | |
| 23 for (const auto& req : allocation_requests_) { | |
| 24 if (req.id == id && req.client_id == client_id) | |
| 25 return true; | |
| 26 } | |
| 27 return false; | |
| 28 } | |
| 29 | |
| 30 bool HasDestructionRequest(gfx::GpuMemoryBufferId id, int client_id) const { | |
| 31 for (const auto& req : destruction_requests_) { | |
| 32 if (req.id == id && req.client_id == client_id) | |
| 33 return true; | |
| 34 } | |
| 35 return false; | |
| 36 } | |
| 37 | |
| 38 void SatisfyAllocationRequest(gfx::GpuMemoryBufferId id, int client_id) { | |
| 39 for (const auto& req : allocation_requests_) { | |
| 40 if (req.id == id && req.client_id == client_id) { | |
| 41 gfx::GpuMemoryBufferHandle handle; | |
| 42 handle.id = id; | |
| 43 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
| 44 req.callback.Run(handle); | |
| 45 return; | |
| 46 } | |
| 47 } | |
| 48 NOTREACHED(); | |
| 49 } | |
| 50 | |
| 51 // ui::mojom::GpuService: | |
| 52 void EstablishGpuChannel( | |
| 53 int32_t client_id, | |
| 54 uint64_t client_tracing_id, | |
| 55 bool is_gpu_host, | |
| 56 const EstablishGpuChannelCallback& callback) override {} | |
| 57 | |
| 58 void CloseChannel(int32_t client_id) override {} | |
| 59 | |
| 60 void CreateGpuMemoryBuffer( | |
| 61 gfx::GpuMemoryBufferId id, | |
| 62 const gfx::Size& size, | |
| 63 gfx::BufferFormat format, | |
| 64 gfx::BufferUsage usage, | |
| 65 int client_id, | |
| 66 gpu::SurfaceHandle surface_handle, | |
| 67 const CreateGpuMemoryBufferCallback& callback) override { | |
| 68 allocation_requests_.push_back( | |
| 69 {id, size, format, usage, client_id, callback}); | |
| 70 } | |
| 71 | |
| 72 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | |
| 73 int client_id, | |
| 74 const gpu::SyncToken& sync_token) override { | |
| 75 destruction_requests_.push_back({id, client_id}); | |
| 76 } | |
| 77 | |
| 78 void GetVideoMemoryUsageStats( | |
| 79 const GetVideoMemoryUsageStatsCallback& callback) override {} | |
| 80 | |
| 81 void RequestCompleteGpuInfo( | |
| 82 const RequestCompleteGpuInfoCallback& callback) override {} | |
| 83 | |
| 84 void LoadedShader(const std::string& data) override {} | |
| 85 | |
| 86 void DestroyingVideoSurface( | |
| 87 int32_t surface_id, | |
| 88 const DestroyingVideoSurfaceCallback& callback) override {} | |
| 89 | |
| 90 void WakeUpGpu() override {} | |
| 91 | |
| 92 void GpuSwitched() override {} | |
| 93 | |
| 94 void DestroyAllChannels() override {} | |
| 95 | |
| 96 void Crash() override {} | |
| 97 | |
| 98 void Hang() override {} | |
| 99 | |
| 100 void ThrowJavaException() override {} | |
| 101 | |
| 102 void Stop(const StopCallback& callback) override {} | |
| 103 | |
| 104 private: | |
| 105 struct AllocationRequest { | |
| 106 const gfx::GpuMemoryBufferId id; | |
| 107 const gfx::Size size; | |
| 108 const gfx::BufferFormat format; | |
| 109 const gfx::BufferUsage usage; | |
| 110 const int client_id; | |
| 111 const CreateGpuMemoryBufferCallback callback; | |
| 112 }; | |
| 113 std::vector<AllocationRequest> allocation_requests_; | |
| 114 | |
| 115 struct DestructionRequest { | |
| 116 const gfx::GpuMemoryBufferId id; | |
| 117 const int client_id; | |
| 118 }; | |
| 119 std::vector<DestructionRequest> destruction_requests_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(TestGpuService); | |
| 122 }; | |
| 123 | |
| 124 // It is necessary to install a custom pixmap factory which claims to support | |
| 125 // all native configurations, so that code that deals with this can be tested | |
| 126 // correctly. | |
| 127 class StubClientNativePixmapFactory : public gfx::ClientNativePixmapFactory { | |
|
danakj
2017/06/21 18:50:36
nit: I think Fake describes this more than Stub. S
sadrul
2017/06/21 20:38:54
Renamed to Fake
| |
| 128 public: | |
| 129 StubClientNativePixmapFactory() {} | |
| 130 ~StubClientNativePixmapFactory() override {} | |
| 131 | |
| 132 // gfx::ClientNativePixmapFactory: | |
| 133 bool IsConfigurationSupported(gfx::BufferFormat format, | |
| 134 gfx::BufferUsage usage) const override { | |
| 135 return true; | |
| 136 } | |
| 137 std::unique_ptr<gfx::ClientNativePixmap> ImportFromHandle( | |
| 138 const gfx::NativePixmapHandle& handle, | |
| 139 const gfx::Size& size, | |
| 140 gfx::BufferUsage usage) override { | |
| 141 NOTREACHED(); | |
| 142 return nullptr; | |
| 143 } | |
| 144 | |
| 145 private: | |
| 146 DISALLOW_COPY_AND_ASSIGN(StubClientNativePixmapFactory); | |
| 147 }; | |
| 148 | |
| 149 } // namespace | |
| 150 | |
| 151 class ServerGpuMemoryBufferManagerTest : public ::testing::Test { | |
| 152 public: | |
| 153 ServerGpuMemoryBufferManagerTest() {} | |
|
danakj
2017/06/21 18:50:36
nit: = default instead of {} whenever possible
sadrul
2017/06/21 20:38:54
Done.
| |
| 154 ~ServerGpuMemoryBufferManagerTest() override {} | |
| 155 | |
| 156 // ::testing::Test: | |
| 157 void SetUp() override { | |
| 158 gfx::ClientNativePixmapFactory::ResetInstance(); | |
| 159 gfx::ClientNativePixmapFactory::SetInstance(&pixmap_factory); | |
| 160 } | |
| 161 | |
| 162 void TearDown() override { gfx::ClientNativePixmapFactory::ResetInstance(); } | |
| 163 | |
| 164 private: | |
| 165 base::test::ScopedTaskEnvironment env; | |
|
danakj
2017/06/21 18:50:36
env_
sadrul
2017/06/21 20:38:54
Done.
| |
| 166 StubClientNativePixmapFactory pixmap_factory; | |
|
danakj
2017/06/21 18:50:35
pixmap_factory_
sadrul
2017/06/21 20:38:54
Done (sorry, moved from within the test body to he
| |
| 167 | |
| 168 DISALLOW_COPY_AND_ASSIGN(ServerGpuMemoryBufferManagerTest); | |
| 169 }; | |
| 170 | |
| 171 // Tests that allocation requests from a client that goes away before allocation | |
| 172 // completes are cleaned up correctly. | |
| 173 TEST_F(ServerGpuMemoryBufferManagerTest, AllocationRequestsForDestroyedClient) { | |
| 174 // Not all platforms support native configurations (currently only ozone and | |
|
danakj
2017/06/21 18:50:36
Can you #ifdef the return statement to only platfo
sadrul
2017/06/21 20:38:54
Done.
| |
| 175 // mac support it). Abort the test in those platforms. | |
| 176 if (gpu::GetNativeGpuMemoryBufferConfigurations().empty()) | |
| 177 return; | |
| 178 TestGpuService gpu_service; | |
| 179 ServerGpuMemoryBufferManager manager(&gpu_service, 1); | |
|
danakj
2017/06/21 18:50:36
Can you mention we're handing the mojo pointer dir
sadrul
2017/06/21 20:38:54
Done.
| |
| 180 | |
| 181 const auto buffer_id = static_cast<gfx::GpuMemoryBufferId>(1); | |
| 182 const int client_id = 2; | |
| 183 const gfx::Size size(10, 20); | |
| 184 const gfx::BufferFormat format = gfx::BufferFormat::RGBA_8888; | |
| 185 const gfx::BufferUsage usage = gfx::BufferUsage::GPU_READ; | |
| 186 manager.AllocateGpuMemoryBuffer( | |
| 187 buffer_id, client_id, size, format, usage, gpu::kNullSurfaceHandle, | |
| 188 base::BindOnce([](const gfx::GpuMemoryBufferHandle& handle) {})); | |
| 189 EXPECT_TRUE(gpu_service.HasAllocationRequest(buffer_id, client_id)); | |
| 190 EXPECT_FALSE(gpu_service.HasDestructionRequest(buffer_id, client_id)); | |
| 191 | |
| 192 // Destroy the client. Since no memory has been allocated yet, there will be | |
| 193 // no request for freeing memory. | |
| 194 manager.DestroyAllGpuMemoryBufferForClient(client_id); | |
| 195 EXPECT_TRUE(gpu_service.HasAllocationRequest(buffer_id, client_id)); | |
| 196 EXPECT_FALSE(gpu_service.HasDestructionRequest(buffer_id, client_id)); | |
| 197 | |
| 198 // When the host receives the allocated memory for the destroyed client, it | |
| 199 // should request the allocated memory to be freed. | |
| 200 gpu_service.SatisfyAllocationRequest(buffer_id, client_id); | |
| 201 EXPECT_TRUE(gpu_service.HasDestructionRequest(buffer_id, client_id)); | |
| 202 } | |
| 203 | |
| 204 } // namespace viz | |
| OLD | NEW |