1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/ipc/client/command_buffer_proxy_impl.h"
#include <limits>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/memory/raw_ref.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "gpu/command_buffer/client/gpu_control_client.h"
#include "gpu/command_buffer/common/context_creation_attribs.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/ipc/client/gpu_channel_host.h"
#include "gpu/ipc/common/gpu_channel.mojom.h"
#include "gpu/ipc/common/mock_command_buffer.h"
#include "gpu/ipc/common/mock_gpu_channel.h"
#include "gpu/ipc/common/surface_handle.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Matcher;
using ::testing::Return;
namespace gpu {
namespace {
// GpuChannelHost is expected to be created on the IO thread, and posts tasks to
// setup its IPC listener, so it must be created after the thread task runner
// handle is set. It expects Send to be called on any thread except IO thread,
// and posts tasks to the IO thread to ensure IPCs are sent in order, which is
// important for sync IPCs. But we override Send, so we can't test sync IPC
// behavior with this setup.
class TestGpuChannelHost : public GpuChannelHost {
public:
explicit TestGpuChannelHost(mojom::GpuChannel& gpu_channel)
: GpuChannelHost(0 /* channel_id */,
GPUInfo(),
GpuFeatureInfo(),
SharedImageCapabilities(),
mojo::ScopedMessagePipeHandle(
mojo::MessagePipeHandle(mojo::kInvalidHandleValue))),
gpu_channel_(gpu_channel) {}
mojom::GpuChannel& GetGpuChannel() override { return *gpu_channel_; }
protected:
~TestGpuChannelHost() override = default;
const raw_ref<mojom::GpuChannel> gpu_channel_;
};
class MockGpuControlClient : public GpuControlClient {
public:
MockGpuControlClient() = default;
virtual ~MockGpuControlClient() = default;
MOCK_METHOD0(OnGpuControlLostContext, void());
MOCK_METHOD0(OnGpuControlLostContextMaybeReentrant, void());
MOCK_METHOD2(OnGpuControlErrorMessage, void(const char*, int32_t));
MOCK_METHOD1(OnGpuSwitched, void(gl::GpuPreference));
MOCK_METHOD1(OnGpuControlReturnData, void(base::span<const uint8_t>));
};
class CommandBufferProxyImplTest
: public testing::WithParamInterface<std::tuple<bool, bool>>,
public testing::Test {
public:
CommandBufferProxyImplTest() {
std::vector<base::test::FeatureRef> enabled_features;
std::vector<base::test::FeatureRef> disabled_features;
(std::get<0>(GetParam()) ? enabled_features : disabled_features)
.push_back(features::kConditionallySkipGpuChannelFlush);
if (std::get<1>(GetParam())) {
enabled_features.push_back(features::kSyncPointGraphValidation);
} else {
disabled_features.push_back(features::kSyncPointGraphValidation);
}
feature_list_.InitWithFeatures(enabled_features, disabled_features);
skip_flush_if_possible_ = base::FeatureList::IsEnabled(
features::kConditionallySkipGpuChannelFlush);
channel_ = base::MakeRefCounted<TestGpuChannelHost>(mock_gpu_channel_);
}
~CommandBufferProxyImplTest() override {
// Release channel, and run any cleanup tasks it posts.
channel_ = nullptr;
base::RunLoop().RunUntilIdle();
}
std::unique_ptr<CommandBufferProxyImpl> CreateAndInitializeProxy(
MockCommandBuffer* mock_command_buffer = nullptr) {
auto proxy = std::make_unique<CommandBufferProxyImpl>(
channel_, 0 /* stream_id */,
base::SingleThreadTaskRunner::GetCurrentDefault());
// The Initialize() call below synchronously requests a new CommandBuffer
// using the channel's GpuControl interface. Simulate success, since we're
// not actually talking to the service in these tests.
EXPECT_CALL(mock_gpu_channel_, CreateCommandBuffer(_, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(Invoke(
[&](mojom::CreateCommandBufferParamsPtr params, int32_t routing_id,
base::UnsafeSharedMemoryRegion shared_state,
mojo::PendingAssociatedReceiver<mojom::CommandBuffer> receiver,
mojo::PendingAssociatedRemote<mojom::CommandBufferClient>
client,
ContextResult* result, Capabilities* capabilities,
GLCapabilities* gl_capabilities) -> bool {
// There's no real GpuChannel pipe for this endpoint to use, so
// give it its own dedicated pipe for these tests. This allows the
// CommandBufferProxyImpl to make calls on its CommandBuffer
// endpoint, which will send them to `mock_command_buffer` if
// provided by the test.
receiver.EnableUnassociatedUsage();
clients_.push_back(std::move(client));
if (mock_command_buffer)
mock_command_buffer->Bind(std::move(receiver));
*result = ContextResult::kSuccess;
return true;
}));
proxy->Initialize(nullptr, SchedulingPriority::kNormal,
ContextCreationAttribs(), GURL());
// Use an arbitrary valid shm_id. The command buffer doesn't use this
// directly, but not setting it triggers DCHECKs.
proxy->SetGetBuffer(1 /* shm_id */);
return proxy;
}
void ExpectOrderingBarrier(const mojom::DeferredRequest& request,
int32_t route_id,
int32_t put_offset) {
ASSERT_TRUE(request.params->is_command_buffer_request());
const auto& command_buffer_request =
*request.params->get_command_buffer_request();
ASSERT_TRUE(command_buffer_request.params->is_async_flush());
EXPECT_EQ(command_buffer_request.routing_id, route_id);
const auto& flush_request =
*command_buffer_request.params->get_async_flush();
EXPECT_EQ(flush_request.put_offset, put_offset);
}
void ExpectFlush(int count) {
if (skip_flush_if_possible_) {
// Under kConditionallySkipGpuChannelFlush the first flush call will be
// replaced by GetSharedMemoryForFlushId and later completely avoided
// using shared memory. In unit tests proper shared memory channels are
// never established so GetSharedMemory() is retried and fully replaces
// Flush()
EXPECT_CALL(mock_gpu_channel_,
GetSharedMemoryForFlushId(
Matcher<::base::ReadOnlySharedMemoryRegion*>(_)))
.Times(count);
} else {
EXPECT_CALL(mock_gpu_channel_, Flush())
.Times(count)
.WillRepeatedly(Return(true));
}
}
protected:
base::test::ScopedFeatureList feature_list_;
base::test::SingleThreadTaskEnvironment task_environment_;
MockGpuChannel mock_gpu_channel_;
bool skip_flush_if_possible_ = false;
scoped_refptr<TestGpuChannelHost> channel_;
std::vector<mojo::PendingAssociatedRemote<mojom::CommandBufferClient>>
clients_;
};
TEST_P(CommandBufferProxyImplTest, OrderingBarriersAreCoalescedWithFlush) {
auto proxy1 = CreateAndInitializeProxy();
auto proxy2 = CreateAndInitializeProxy();
EXPECT_CALL(mock_gpu_channel_, FlushDeferredRequests(_, _))
.Times(1)
.WillOnce(Invoke(
[&](std::vector<mojom::DeferredRequestPtr> requests, int32_t id) {
EXPECT_EQ(3u, requests.size());
ExpectOrderingBarrier(*requests[0], proxy1->route_id(), 10);
ExpectOrderingBarrier(*requests[1], proxy2->route_id(), 20);
ExpectOrderingBarrier(*requests[2], proxy1->route_id(), 50);
}));
proxy1->OrderingBarrier(10);
proxy2->OrderingBarrier(20);
proxy1->OrderingBarrier(30);
proxy1->OrderingBarrier(40);
proxy1->Flush(50);
// Once for each proxy.
EXPECT_CALL(mock_gpu_channel_, DestroyCommandBuffer(_))
.Times(2)
.WillRepeatedly(Return(true));
if (!features::IsSyncPointGraphValidationEnabled()) {
// Each proxy sends a sync GpuControl flush on disconnect.
ExpectFlush(2);
}
}
TEST_P(CommandBufferProxyImplTest, FlushPendingWorkFlushesOrderingBarriers) {
auto proxy1 = CreateAndInitializeProxy();
auto proxy2 = CreateAndInitializeProxy();
EXPECT_CALL(mock_gpu_channel_, FlushDeferredRequests(_, _))
.Times(1)
.WillOnce(Invoke(
[&](std::vector<mojom::DeferredRequestPtr> requests, int32_t id) {
EXPECT_EQ(3u, requests.size());
ExpectOrderingBarrier(*requests[0], proxy1->route_id(), 10);
ExpectOrderingBarrier(*requests[1], proxy2->route_id(), 20);
ExpectOrderingBarrier(*requests[2], proxy1->route_id(), 30);
}));
proxy1->OrderingBarrier(10);
proxy2->OrderingBarrier(20);
proxy1->OrderingBarrier(30);
proxy2->FlushPendingWork();
// Once for each proxy.
EXPECT_CALL(mock_gpu_channel_, DestroyCommandBuffer(_))
.Times(2)
.WillRepeatedly(Return(true));
if (!features::IsSyncPointGraphValidationEnabled()) {
// Each proxy sends a sync GpuControl flush on disconnect.
ExpectFlush(2);
}
}
TEST_P(CommandBufferProxyImplTest, EnsureWorkVisibleFlushesOrderingBarriers) {
auto proxy1 = CreateAndInitializeProxy();
auto proxy2 = CreateAndInitializeProxy();
// Ordering of these flush operations must be preserved.
{
::testing::InSequence in_sequence;
// First we expect to see a FlushDeferredRequests call.
EXPECT_CALL(mock_gpu_channel_, FlushDeferredRequests(_, _))
.Times(1)
.WillOnce(Invoke(
[&](std::vector<mojom::DeferredRequestPtr> requests, int32_t id) {
EXPECT_EQ(3u, requests.size());
ExpectOrderingBarrier(*requests[0], proxy1->route_id(), 10);
ExpectOrderingBarrier(*requests[1], proxy2->route_id(), 20);
ExpectOrderingBarrier(*requests[2], proxy1->route_id(), 30);
}));
if (!features::IsSyncPointGraphValidationEnabled()) {
// Next we expect a full `Flush()`.
ExpectFlush(1);
}
}
proxy1->OrderingBarrier(10);
proxy2->OrderingBarrier(20);
proxy1->OrderingBarrier(30);
proxy2->EnsureWorkVisible();
// Once for each proxy.
EXPECT_CALL(mock_gpu_channel_, DestroyCommandBuffer(_))
.Times(2)
.WillRepeatedly(Return(true));
if (!features::IsSyncPointGraphValidationEnabled()) {
// Each proxy sends a sync GpuControl flush on disconnect.
ExpectFlush(2);
}
}
TEST_P(CommandBufferProxyImplTest,
EnqueueDeferredMessageEnqueuesPendingOrderingBarriers) {
auto proxy1 = CreateAndInitializeProxy();
proxy1->OrderingBarrier(10);
proxy1->OrderingBarrier(20);
channel_->EnqueueDeferredMessage(
mojom::DeferredRequestParams::NewCommandBufferRequest(
mojom::DeferredCommandBufferRequest::New(
proxy1->route_id(), mojom::DeferredCommandBufferRequestParams::
NewDestroyTransferBuffer(3))),
/*sync_token_fences=*/{}, /*release_count=*/0);
// Make sure the above requests don't hit our mock yet.
base::RunLoop().RunUntilIdle();
// Now we can expect a FlushDeferredRequests to be elicited by the
// FlushPendingWork call below.
EXPECT_CALL(mock_gpu_channel_, FlushDeferredRequests(_, _))
.Times(1)
.WillOnce(Invoke(
[&](std::vector<mojom::DeferredRequestPtr> requests, int32_t id) {
EXPECT_EQ(2u, requests.size());
ExpectOrderingBarrier(*requests[0], proxy1->route_id(), 20);
ASSERT_TRUE(requests[1]->params->is_command_buffer_request());
auto& request = *requests[1]->params->get_command_buffer_request();
ASSERT_TRUE(request.params->is_destroy_transfer_buffer());
EXPECT_EQ(3, request.params->get_destroy_transfer_buffer());
}));
proxy1->FlushPendingWork();
EXPECT_CALL(mock_gpu_channel_, DestroyCommandBuffer(_))
.Times(1)
.WillOnce(Return(true));
if (!features::IsSyncPointGraphValidationEnabled()) {
// The proxy sends a sync GpuControl flush on disconnect.
ExpectFlush(1);
}
}
TEST_P(CommandBufferProxyImplTest, CreateTransferBufferOOM) {
auto gpu_control_client = std::unique_ptr<MockGpuControlClient>(
new testing::StrictMock<MockGpuControlClient>());
auto proxy = CreateAndInitializeProxy();
proxy->SetGpuControlClient(gpu_control_client.get());
// This is called once when the CommandBufferProxyImpl is destroyed.
EXPECT_CALL(*gpu_control_client, OnGpuControlLostContext())
.Times(1)
.RetiresOnSaturation();
// Passing kReturnNullOnOOM should not cause the context to be lost
EXPECT_CALL(*gpu_control_client, OnGpuControlLostContextMaybeReentrant())
.Times(0);
int32_t id = -1;
scoped_refptr<gpu::Buffer> transfer_buffer_oom = proxy->CreateTransferBuffer(
std::numeric_limits<uint32_t>::max(), &id, 0,
TransferBufferAllocationOption::kReturnNullOnOOM);
if (transfer_buffer_oom) {
// In this test, there's no guarantee allocating UINT32_MAX will definitely
// fail, but it is likely to OOM. If it didn't fail, return immediately.
// TODO(enga): Consider manually injecting an allocation failure to test this
// better.
return;
}
EXPECT_EQ(id, -1);
// Make a smaller buffer which should work.
scoped_refptr<gpu::Buffer> transfer_buffer =
proxy->CreateTransferBuffer(16, &id);
EXPECT_NE(transfer_buffer, nullptr);
EXPECT_NE(id, -1);
// Now, allocating with kLoseContextOnOOM should cause the context to be lost.
EXPECT_CALL(*gpu_control_client, OnGpuControlLostContextMaybeReentrant())
.Times(1)
.RetiresOnSaturation();
transfer_buffer_oom = proxy->CreateTransferBuffer(
std::numeric_limits<uint32_t>::max(), &id, 0,
TransferBufferAllocationOption::kLoseContextOnOOM);
EXPECT_CALL(mock_gpu_channel_, DestroyCommandBuffer(_))
.Times(1)
.WillOnce(Return(true));
if (!features::IsSyncPointGraphValidationEnabled()) {
// The proxy sends a sync GpuControl flush on disconnect.
ExpectFlush(1);
}
}
INSTANTIATE_TEST_SUITE_P(All,
CommandBufferProxyImplTest,
testing::Combine(testing::Values(false, true),
testing::Values(false, true)));
} // namespace
} // namespace gpu
|