Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_scheduler_unittest.cc
blobc1c2ef7f2d2aaf3e9a542284475aa89610b56b15
1 // Copyright (c) 2011 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.
5 #include "gpu/command_buffer/common/command_buffer_mock.h"
6 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
7 #include "gpu/command_buffer/service/gles2_cmd_decoder_mock.h"
8 #include "gpu/command_buffer/service/gpu_scheduler.h"
9 #include "gpu/command_buffer/service/mocks.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using testing::_;
14 using testing::DoAll;
15 using testing::Invoke;
16 using testing::NiceMock;
17 using testing::Return;
18 using testing::SetArgumentPointee;
19 using testing::StrictMock;
21 namespace gpu {
23 const size_t kRingBufferSize = 1024;
25 class GpuSchedulerTest : public testing::Test {
26 protected:
27 static const int32 kTransferBufferId = 123;
29 void SetUp() override {
30 scoped_ptr<base::SharedMemory> shared_memory(new ::base::SharedMemory);
31 shared_memory->CreateAndMapAnonymous(kRingBufferSize);
32 buffer_ = static_cast<int32*>(shared_memory->memory());
33 shared_memory_buffer_ =
34 MakeBufferFromSharedMemory(shared_memory.Pass(), kRingBufferSize);
35 memset(buffer_, 0, kRingBufferSize);
37 command_buffer_.reset(new MockCommandBuffer);
39 CommandBuffer::State default_state;
40 ON_CALL(*command_buffer_.get(), GetLastState())
41 .WillByDefault(Return(default_state));
42 ON_CALL(*command_buffer_.get(), GetPutOffset())
43 .WillByDefault(Return(0));
45 decoder_.reset(new gles2::MockGLES2Decoder());
46 // Install FakeDoCommands handler so we can use individual DoCommand()
47 // expectations.
48 EXPECT_CALL(*decoder_, DoCommands(_, _, _, _)).WillRepeatedly(
49 Invoke(decoder_.get(), &gles2::MockGLES2Decoder::FakeDoCommands));
51 scheduler_.reset(new gpu::GpuScheduler(command_buffer_.get(),
52 decoder_.get(),
53 decoder_.get()));
54 EXPECT_CALL(*command_buffer_, GetTransferBuffer(kTransferBufferId))
55 .WillOnce(Return(shared_memory_buffer_));
56 EXPECT_CALL(*command_buffer_, SetGetOffset(0));
57 EXPECT_TRUE(scheduler_->SetGetBuffer(kTransferBufferId));
60 void TearDown() override {
61 // Ensure that any unexpected tasks posted by the GPU scheduler are executed
62 // in order to fail the test.
63 base::MessageLoop::current()->RunUntilIdle();
66 error::Error GetError() {
67 return command_buffer_->GetLastState().error;
70 scoped_ptr<MockCommandBuffer> command_buffer_;
71 scoped_refptr<Buffer> shared_memory_buffer_;
72 int32* buffer_;
73 scoped_ptr<gles2::MockGLES2Decoder> decoder_;
74 scoped_ptr<GpuScheduler> scheduler_;
77 TEST_F(GpuSchedulerTest, SchedulerDoesNothingIfRingBufferIsEmpty) {
78 CommandBuffer::State state;
80 EXPECT_CALL(*command_buffer_, GetLastState())
81 .WillRepeatedly(Return(state));
83 EXPECT_CALL(*command_buffer_, SetParseError(_))
84 .Times(0);
86 scheduler_->PutChanged();
89 TEST_F(GpuSchedulerTest, GetSetBuffer) {
90 CommandBuffer::State state;
92 // Set the get offset to something not 0.
93 EXPECT_CALL(*command_buffer_, SetGetOffset(2));
94 scheduler_->SetGetOffset(2);
95 EXPECT_EQ(2, scheduler_->GetGetOffset());
97 // Set the buffer.
98 EXPECT_CALL(*command_buffer_, GetTransferBuffer(kTransferBufferId))
99 .WillOnce(Return(shared_memory_buffer_));
100 EXPECT_CALL(*command_buffer_, SetGetOffset(0));
101 EXPECT_TRUE(scheduler_->SetGetBuffer(kTransferBufferId));
103 // Check the get offset was reset.
104 EXPECT_EQ(0, scheduler_->GetGetOffset());
107 TEST_F(GpuSchedulerTest, ProcessesOneCommand) {
108 CommandHeader* header = reinterpret_cast<CommandHeader*>(&buffer_[0]);
109 header[0].command = 7;
110 header[0].size = 2;
111 buffer_[1] = 123;
113 CommandBuffer::State state;
115 EXPECT_CALL(*command_buffer_, GetLastState())
116 .WillRepeatedly(Return(state));
117 EXPECT_CALL(*command_buffer_, GetPutOffset())
118 .WillRepeatedly(Return(2));
119 EXPECT_CALL(*command_buffer_, SetGetOffset(2));
121 EXPECT_CALL(*decoder_, DoCommand(7, 1, &buffer_[0]))
122 .WillOnce(Return(error::kNoError));
124 EXPECT_CALL(*command_buffer_, SetParseError(_))
125 .Times(0);
127 scheduler_->PutChanged();
130 TEST_F(GpuSchedulerTest, ProcessesTwoCommands) {
131 CommandHeader* header = reinterpret_cast<CommandHeader*>(&buffer_[0]);
132 header[0].command = 7;
133 header[0].size = 2;
134 buffer_[1] = 123;
135 header[2].command = 8;
136 header[2].size = 1;
138 CommandBuffer::State state;
140 EXPECT_CALL(*command_buffer_, GetLastState())
141 .WillRepeatedly(Return(state));
142 EXPECT_CALL(*command_buffer_, GetPutOffset())
143 .WillRepeatedly(Return(3));
145 EXPECT_CALL(*decoder_, DoCommand(7, 1, &buffer_[0]))
146 .WillOnce(Return(error::kNoError));
148 EXPECT_CALL(*decoder_, DoCommand(8, 0, &buffer_[2]))
149 .WillOnce(Return(error::kNoError));
150 EXPECT_CALL(*command_buffer_, SetGetOffset(3));
152 scheduler_->PutChanged();
155 TEST_F(GpuSchedulerTest, SetsErrorCodeOnCommandBuffer) {
156 CommandHeader* header = reinterpret_cast<CommandHeader*>(&buffer_[0]);
157 header[0].command = 7;
158 header[0].size = 1;
160 CommandBuffer::State state;
162 EXPECT_CALL(*command_buffer_, GetLastState())
163 .WillRepeatedly(Return(state));
164 EXPECT_CALL(*command_buffer_, GetPutOffset())
165 .WillRepeatedly(Return(1));
167 EXPECT_CALL(*decoder_, DoCommand(7, 0, &buffer_[0]))
168 .WillOnce(Return(
169 error::kUnknownCommand));
170 EXPECT_CALL(*command_buffer_, SetGetOffset(1));
172 EXPECT_CALL(*command_buffer_, SetContextLostReason(_));
173 EXPECT_CALL(*decoder_, GetContextLostReason())
174 .WillOnce(Return(error::kUnknown));
175 EXPECT_CALL(*command_buffer_,
176 SetParseError(error::kUnknownCommand));
178 scheduler_->PutChanged();
181 TEST_F(GpuSchedulerTest, ProcessCommandsDoesNothingAfterError) {
182 CommandBuffer::State state;
183 state.error = error::kGenericError;
185 EXPECT_CALL(*command_buffer_, GetLastState())
186 .WillRepeatedly(Return(state));
188 scheduler_->PutChanged();
191 TEST_F(GpuSchedulerTest, CanGetAddressOfSharedMemory) {
192 EXPECT_CALL(*command_buffer_.get(), GetTransferBuffer(7))
193 .WillOnce(Return(shared_memory_buffer_));
195 EXPECT_EQ(&buffer_[0], scheduler_->GetSharedMemoryBuffer(7)->memory());
198 ACTION_P2(SetPointee, address, value) {
199 *address = value;
202 TEST_F(GpuSchedulerTest, CanGetSizeOfSharedMemory) {
203 EXPECT_CALL(*command_buffer_.get(), GetTransferBuffer(7))
204 .WillOnce(Return(shared_memory_buffer_));
206 EXPECT_EQ(kRingBufferSize, scheduler_->GetSharedMemoryBuffer(7)->size());
209 TEST_F(GpuSchedulerTest, SetTokenForwardsToCommandBuffer) {
210 EXPECT_CALL(*command_buffer_, SetToken(7));
211 scheduler_->set_token(7);
214 } // namespace gpu