Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / client / cmd_buffer_helper_test.cc
blob859c99553b9bc037595f13d350ea759c9b45407e
1 // Copyright (c) 2012 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 // Tests for the Command Buffer Helper.
7 #include <list>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/linked_ptr.h"
12 #include "gpu/command_buffer/client/cmd_buffer_helper.h"
13 #include "gpu/command_buffer/service/command_buffer_service.h"
14 #include "gpu/command_buffer/service/gpu_scheduler.h"
15 #include "gpu/command_buffer/service/mocks.h"
16 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace gpu {
21 using testing::Return;
22 using testing::Mock;
23 using testing::Truly;
24 using testing::Sequence;
25 using testing::DoAll;
26 using testing::Invoke;
27 using testing::_;
29 const int32 kTotalNumCommandEntries = 32;
30 const int32 kCommandBufferSizeBytes =
31 kTotalNumCommandEntries * sizeof(CommandBufferEntry);
32 const int32 kUnusedCommandId = 5; // we use 0 and 2 currently.
34 // Override CommandBufferService::Flush() to lock flushing and simulate
35 // the buffer becoming full in asynchronous mode.
36 class CommandBufferServiceLocked : public CommandBufferService {
37 public:
38 explicit CommandBufferServiceLocked(
39 TransferBufferManagerInterface* transfer_buffer_manager)
40 : CommandBufferService(transfer_buffer_manager),
41 flush_locked_(false),
42 last_flush_(-1),
43 flush_count_(0) {}
44 ~CommandBufferServiceLocked() override {}
46 void Flush(int32 put_offset) override {
47 flush_count_++;
48 if (!flush_locked_) {
49 last_flush_ = -1;
50 CommandBufferService::Flush(put_offset);
51 } else {
52 last_flush_ = put_offset;
56 void LockFlush() { flush_locked_ = true; }
58 void UnlockFlush() { flush_locked_ = false; }
60 int FlushCount() { return flush_count_; }
62 void WaitForGetOffsetInRange(int32 start, int32 end) override {
63 if (last_flush_ != -1) {
64 CommandBufferService::Flush(last_flush_);
65 last_flush_ = -1;
67 CommandBufferService::WaitForGetOffsetInRange(start, end);
70 private:
71 bool flush_locked_;
72 int last_flush_;
73 int flush_count_;
74 DISALLOW_COPY_AND_ASSIGN(CommandBufferServiceLocked);
77 // Test fixture for CommandBufferHelper test - Creates a CommandBufferHelper,
78 // using a CommandBufferEngine with a mock AsyncAPIInterface for its interface
79 // (calling it directly, not through the RPC mechanism).
80 class CommandBufferHelperTest : public testing::Test {
81 protected:
82 virtual void SetUp() {
83 api_mock_.reset(new AsyncAPIMock(true));
85 // ignore noops in the mock - we don't want to inspect the internals of the
86 // helper.
87 EXPECT_CALL(*api_mock_, DoCommand(cmd::kNoop, _, _))
88 .WillRepeatedly(Return(error::kNoError));
91 TransferBufferManager* manager = new TransferBufferManager();
92 transfer_buffer_manager_ = manager;
93 EXPECT_TRUE(manager->Initialize());
95 command_buffer_.reset(
96 new CommandBufferServiceLocked(transfer_buffer_manager_.get()));
97 EXPECT_TRUE(command_buffer_->Initialize());
99 gpu_scheduler_.reset(new GpuScheduler(
100 command_buffer_.get(), api_mock_.get(), NULL));
101 command_buffer_->SetPutOffsetChangeCallback(base::Bind(
102 &GpuScheduler::PutChanged, base::Unretained(gpu_scheduler_.get())));
103 command_buffer_->SetGetBufferChangeCallback(base::Bind(
104 &GpuScheduler::SetGetBuffer, base::Unretained(gpu_scheduler_.get())));
106 api_mock_->set_engine(gpu_scheduler_.get());
108 helper_.reset(new CommandBufferHelper(command_buffer_.get()));
109 helper_->Initialize(kCommandBufferSizeBytes);
111 test_command_next_id_ = kUnusedCommandId;
114 virtual void TearDown() {
115 // If the GpuScheduler posts any tasks, this forces them to run.
116 base::MessageLoop::current()->RunUntilIdle();
117 test_command_args_.clear();
120 const CommandParser* GetParser() const {
121 return gpu_scheduler_->parser();
124 int32 ImmediateEntryCount() const { return helper_->immediate_entry_count_; }
126 // Adds a command to the buffer through the helper, while adding it as an
127 // expected call on the API mock.
128 void AddCommandWithExpect(error::Error _return,
129 unsigned int command,
130 int arg_count,
131 CommandBufferEntry *args) {
132 CommandHeader header;
133 header.size = arg_count + 1;
134 header.command = command;
135 CommandBufferEntry* cmds =
136 static_cast<CommandBufferEntry*>(helper_->GetSpace(arg_count + 1));
137 CommandBufferOffset put = 0;
138 cmds[put++].value_header = header;
139 for (int ii = 0; ii < arg_count; ++ii) {
140 cmds[put++] = args[ii];
143 EXPECT_CALL(*api_mock_, DoCommand(command, arg_count,
144 Truly(AsyncAPIMock::IsArgs(arg_count, args))))
145 .InSequence(sequence_)
146 .WillOnce(Return(_return));
149 void AddUniqueCommandWithExpect(error::Error _return, int cmd_size) {
150 EXPECT_GE(cmd_size, 1);
151 EXPECT_LT(cmd_size, kTotalNumCommandEntries);
152 int arg_count = cmd_size - 1;
154 // Allocate array for args.
155 linked_ptr<std::vector<CommandBufferEntry> > args_ptr(
156 new std::vector<CommandBufferEntry>(arg_count ? arg_count : 1));
158 for (int32 ii = 0; ii < arg_count; ++ii) {
159 (*args_ptr)[ii].value_uint32 = 0xF00DF00D + ii;
162 // Add command and save args in test_command_args_ until the test completes.
163 AddCommandWithExpect(
164 _return, test_command_next_id_++, arg_count, &(*args_ptr)[0]);
165 test_command_args_.insert(test_command_args_.end(), args_ptr);
168 void TestCommandWrappingFull(int32 cmd_size, int32 start_commands) {
169 const int32 num_args = cmd_size - 1;
170 EXPECT_EQ(kTotalNumCommandEntries % cmd_size, 0);
172 std::vector<CommandBufferEntry> args(num_args);
173 for (int32 ii = 0; ii < num_args; ++ii) {
174 args[ii].value_uint32 = ii + 1;
177 // Initially insert commands up to start_commands and Finish().
178 for (int32 ii = 0; ii < start_commands; ++ii) {
179 AddCommandWithExpect(
180 error::kNoError, ii + kUnusedCommandId, num_args, &args[0]);
182 helper_->Finish();
184 EXPECT_EQ(GetParser()->put(),
185 (start_commands * cmd_size) % kTotalNumCommandEntries);
186 EXPECT_EQ(GetParser()->get(),
187 (start_commands * cmd_size) % kTotalNumCommandEntries);
189 // Lock flushing to force the buffer to get full.
190 command_buffer_->LockFlush();
192 // Add enough commands to over fill the buffer.
193 for (int32 ii = 0; ii < kTotalNumCommandEntries / cmd_size + 2; ++ii) {
194 AddCommandWithExpect(error::kNoError,
195 start_commands + ii + kUnusedCommandId,
196 num_args,
197 &args[0]);
200 // Flush all commands.
201 command_buffer_->UnlockFlush();
202 helper_->Finish();
204 // Check that the commands did happen.
205 Mock::VerifyAndClearExpectations(api_mock_.get());
207 // Check the error status.
208 EXPECT_EQ(error::kNoError, GetError());
211 // Checks that the buffer from put to put+size is free in the parser.
212 void CheckFreeSpace(CommandBufferOffset put, unsigned int size) {
213 CommandBufferOffset parser_put = GetParser()->put();
214 CommandBufferOffset parser_get = GetParser()->get();
215 CommandBufferOffset limit = put + size;
216 if (parser_get > parser_put) {
217 // "busy" buffer wraps, so "free" buffer is between put (inclusive) and
218 // get (exclusive).
219 EXPECT_LE(parser_put, put);
220 EXPECT_GT(parser_get, limit);
221 } else {
222 // "busy" buffer does not wrap, so the "free" buffer is the top side (from
223 // put to the limit) and the bottom side (from 0 to get).
224 if (put >= parser_put) {
225 // we're on the top side, check we are below the limit.
226 EXPECT_GE(kTotalNumCommandEntries, limit);
227 } else {
228 // we're on the bottom side, check we are below get.
229 EXPECT_GT(parser_get, limit);
234 int32 GetGetOffset() {
235 return command_buffer_->GetLastState().get_offset;
238 int32 GetPutOffset() {
239 return command_buffer_->GetPutOffset();
242 int32 GetHelperGetOffset() { return helper_->get_offset(); }
244 int32 GetHelperPutOffset() { return helper_->put_; }
246 uint32 GetHelperFlushGeneration() { return helper_->flush_generation(); }
248 error::Error GetError() {
249 return command_buffer_->GetLastState().error;
252 CommandBufferOffset get_helper_put() { return helper_->put_; }
254 scoped_ptr<AsyncAPIMock> api_mock_;
255 scoped_refptr<TransferBufferManagerInterface> transfer_buffer_manager_;
256 scoped_ptr<CommandBufferServiceLocked> command_buffer_;
257 scoped_ptr<GpuScheduler> gpu_scheduler_;
258 scoped_ptr<CommandBufferHelper> helper_;
259 std::list<linked_ptr<std::vector<CommandBufferEntry> > > test_command_args_;
260 unsigned int test_command_next_id_;
261 Sequence sequence_;
264 // Checks immediate_entry_count_ changes based on 'usable' state.
265 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesNotUsable) {
266 // Auto flushing mode is tested separately.
267 helper_->SetAutomaticFlushes(false);
268 EXPECT_EQ(helper_->usable(), true);
269 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
270 helper_->ClearUsable();
271 EXPECT_EQ(ImmediateEntryCount(), 0);
274 // Checks immediate_entry_count_ changes based on RingBuffer state.
275 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesNoRingBuffer) {
276 helper_->SetAutomaticFlushes(false);
277 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
278 helper_->FreeRingBuffer();
279 EXPECT_EQ(ImmediateEntryCount(), 0);
282 // Checks immediate_entry_count_ calc when Put >= Get and Get == 0.
283 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesGetAtZero) {
284 // No internal auto flushing.
285 helper_->SetAutomaticFlushes(false);
286 command_buffer_->LockFlush();
288 // Start at Get = Put = 0.
289 EXPECT_EQ(GetHelperPutOffset(), 0);
290 EXPECT_EQ(GetHelperGetOffset(), 0);
292 // Immediate count should be 1 less than the end of the buffer.
293 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
294 AddUniqueCommandWithExpect(error::kNoError, 2);
295 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 3);
297 helper_->Finish();
299 // Check that the commands did happen.
300 Mock::VerifyAndClearExpectations(api_mock_.get());
302 // Check the error status.
303 EXPECT_EQ(error::kNoError, GetError());
306 // Checks immediate_entry_count_ calc when Put >= Get and Get > 0.
307 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesGetInMiddle) {
308 // No internal auto flushing.
309 helper_->SetAutomaticFlushes(false);
310 command_buffer_->LockFlush();
312 // Move to Get = Put = 2.
313 AddUniqueCommandWithExpect(error::kNoError, 2);
314 helper_->Finish();
315 EXPECT_EQ(GetHelperPutOffset(), 2);
316 EXPECT_EQ(GetHelperGetOffset(), 2);
318 // Immediate count should be up to the end of the buffer.
319 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 2);
320 AddUniqueCommandWithExpect(error::kNoError, 2);
321 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 4);
323 helper_->Finish();
325 // Check that the commands did happen.
326 Mock::VerifyAndClearExpectations(api_mock_.get());
328 // Check the error status.
329 EXPECT_EQ(error::kNoError, GetError());
332 // Checks immediate_entry_count_ calc when Put < Get.
333 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesGetBeforePut) {
334 // Move to Get = kTotalNumCommandEntries / 4, Put = 0.
335 const int kInitGetOffset = kTotalNumCommandEntries / 4;
336 helper_->SetAutomaticFlushes(false);
337 command_buffer_->LockFlush();
338 AddUniqueCommandWithExpect(error::kNoError, kInitGetOffset);
339 helper_->Finish();
340 AddUniqueCommandWithExpect(error::kNoError,
341 kTotalNumCommandEntries - kInitGetOffset);
343 // Flush instead of Finish will let Put wrap without the command buffer
344 // immediately processing the data between Get and Put.
345 helper_->Flush();
347 EXPECT_EQ(GetHelperGetOffset(), kInitGetOffset);
348 EXPECT_EQ(GetHelperPutOffset(), 0);
350 // Immediate count should be up to Get - 1.
351 EXPECT_EQ(ImmediateEntryCount(), kInitGetOffset - 1);
352 AddUniqueCommandWithExpect(error::kNoError, 2);
353 EXPECT_EQ(ImmediateEntryCount(), kInitGetOffset - 3);
355 helper_->Finish();
356 // Check that the commands did happen.
357 Mock::VerifyAndClearExpectations(api_mock_.get());
359 // Check the error status.
360 EXPECT_EQ(error::kNoError, GetError());
363 // Checks immediate_entry_count_ calc when automatic flushing is enabled.
364 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesAutoFlushing) {
365 command_buffer_->LockFlush();
367 // Start at Get = Put = 0.
368 EXPECT_EQ(GetHelperPutOffset(), 0);
369 EXPECT_EQ(GetHelperGetOffset(), 0);
371 // Without auto flushes, up to kTotalNumCommandEntries - 1 is available.
372 helper_->SetAutomaticFlushes(false);
373 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
375 // With auto flushes, and Get == Last Put,
376 // up to kTotalNumCommandEntries / kAutoFlushSmall is available.
377 helper_->SetAutomaticFlushes(true);
378 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries / kAutoFlushSmall);
380 // With auto flushes, and Get != Last Put,
381 // up to kTotalNumCommandEntries / kAutoFlushBig is available.
382 AddUniqueCommandWithExpect(error::kNoError, 2);
383 helper_->Flush();
384 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries / kAutoFlushBig);
386 helper_->Finish();
387 // Check that the commands did happen.
388 Mock::VerifyAndClearExpectations(api_mock_.get());
390 // Check the error status.
391 EXPECT_EQ(error::kNoError, GetError());
394 // Checks immediate_entry_count_ calc when automatic flushing is enabled, and
395 // we allocate commands over the immediate_entry_count_ size.
396 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesOverFlushLimit) {
397 // Lock internal flushing.
398 command_buffer_->LockFlush();
400 // Start at Get = Put = 0.
401 EXPECT_EQ(GetHelperPutOffset(), 0);
402 EXPECT_EQ(GetHelperGetOffset(), 0);
404 // Pre-check ImmediateEntryCount is limited with automatic flushing enabled.
405 helper_->SetAutomaticFlushes(true);
406 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries / kAutoFlushSmall);
408 // Add a command larger than ImmediateEntryCount().
409 AddUniqueCommandWithExpect(error::kNoError, ImmediateEntryCount() + 1);
411 // ImmediateEntryCount() should now be 0, to force a flush check on the next
412 // command.
413 EXPECT_EQ(ImmediateEntryCount(), 0);
415 // Add a command when ImmediateEntryCount() == 0.
416 AddUniqueCommandWithExpect(error::kNoError, ImmediateEntryCount() + 1);
418 helper_->Finish();
419 // Check that the commands did happen.
420 Mock::VerifyAndClearExpectations(api_mock_.get());
422 // Check the error status.
423 EXPECT_EQ(error::kNoError, GetError());
426 // Checks that commands in the buffer are properly executed, and that the
427 // status/error stay valid.
428 TEST_F(CommandBufferHelperTest, TestCommandProcessing) {
429 // Check initial state of the engine - it should have been configured by the
430 // helper.
431 EXPECT_TRUE(GetParser() != NULL);
432 EXPECT_EQ(error::kNoError, GetError());
433 EXPECT_EQ(0, GetGetOffset());
435 // Add 3 commands through the helper
436 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 0, NULL);
438 CommandBufferEntry args1[2];
439 args1[0].value_uint32 = 3;
440 args1[1].value_float = 4.f;
441 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 2, args1);
443 CommandBufferEntry args2[2];
444 args2[0].value_uint32 = 5;
445 args2[1].value_float = 6.f;
446 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 2, args2);
448 // Wait until it's done.
449 helper_->Finish();
450 // Check that the engine has no more work to do.
451 EXPECT_TRUE(GetParser()->IsEmpty());
453 // Check that the commands did happen.
454 Mock::VerifyAndClearExpectations(api_mock_.get());
456 // Check the error status.
457 EXPECT_EQ(error::kNoError, GetError());
460 // Checks that commands in the buffer are properly executed when wrapping the
461 // buffer, and that the status/error stay valid.
462 TEST_F(CommandBufferHelperTest, TestCommandWrapping) {
463 // Add num_commands * commands of size 3 through the helper to make sure we
464 // do wrap. kTotalNumCommandEntries must not be a multiple of 3.
465 static_assert(kTotalNumCommandEntries % 3 != 0,
466 "kTotalNumCommandEntries must not be a multiple of 3");
467 const int kNumCommands = (kTotalNumCommandEntries / 3) * 2;
468 CommandBufferEntry args1[2];
469 args1[0].value_uint32 = 5;
470 args1[1].value_float = 4.f;
472 for (int i = 0; i < kNumCommands; ++i) {
473 AddCommandWithExpect(error::kNoError, kUnusedCommandId + i, 2, args1);
476 helper_->Finish();
477 // Check that the commands did happen.
478 Mock::VerifyAndClearExpectations(api_mock_.get());
480 // Check the error status.
481 EXPECT_EQ(error::kNoError, GetError());
484 // Checks the case where the command inserted exactly matches the space left in
485 // the command buffer.
486 TEST_F(CommandBufferHelperTest, TestCommandWrappingExactMultiple) {
487 const int32 kCommandSize = kTotalNumCommandEntries / 2;
488 const size_t kNumArgs = kCommandSize - 1;
489 static_assert(kTotalNumCommandEntries % kCommandSize == 0,
490 "kTotalNumCommandEntries should be a multiple of kCommandSize");
491 CommandBufferEntry args1[kNumArgs];
492 for (size_t ii = 0; ii < kNumArgs; ++ii) {
493 args1[ii].value_uint32 = ii + 1;
496 for (unsigned int i = 0; i < 5; ++i) {
497 AddCommandWithExpect(
498 error::kNoError, i + kUnusedCommandId, kNumArgs, args1);
501 helper_->Finish();
502 // Check that the commands did happen.
503 Mock::VerifyAndClearExpectations(api_mock_.get());
505 // Check the error status.
506 EXPECT_EQ(error::kNoError, GetError());
509 // Checks exact wrapping condition with Get = 0.
510 TEST_F(CommandBufferHelperTest, TestCommandWrappingFullAtStart) {
511 TestCommandWrappingFull(2, 0);
514 // Checks exact wrapping condition with 0 < Get < kTotalNumCommandEntries.
515 TEST_F(CommandBufferHelperTest, TestCommandWrappingFullInMiddle) {
516 TestCommandWrappingFull(2, 1);
519 // Checks exact wrapping condition with Get = kTotalNumCommandEntries.
520 // Get should wrap back to 0, but making sure.
521 TEST_F(CommandBufferHelperTest, TestCommandWrappingFullAtEnd) {
522 TestCommandWrappingFull(2, kTotalNumCommandEntries / 2);
525 // Checks that asking for available entries work, and that the parser
526 // effectively won't use that space.
527 TEST_F(CommandBufferHelperTest, TestAvailableEntries) {
528 CommandBufferEntry args[2];
529 args[0].value_uint32 = 3;
530 args[1].value_float = 4.f;
532 // Add 2 commands through the helper - 8 entries
533 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 1, 0, NULL);
534 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 2, 0, NULL);
535 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 3, 2, args);
536 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 4, 2, args);
538 // Ask for 5 entries.
539 helper_->WaitForAvailableEntries(5);
541 CommandBufferOffset put = get_helper_put();
542 CheckFreeSpace(put, 5);
544 // Add more commands.
545 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 5, 2, args);
547 // Wait until everything is done done.
548 helper_->Finish();
550 // Check that the commands did happen.
551 Mock::VerifyAndClearExpectations(api_mock_.get());
553 // Check the error status.
554 EXPECT_EQ(error::kNoError, GetError());
557 // Checks that the InsertToken/WaitForToken work.
558 TEST_F(CommandBufferHelperTest, TestToken) {
559 CommandBufferEntry args[2];
560 args[0].value_uint32 = 3;
561 args[1].value_float = 4.f;
563 // Add a first command.
564 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 3, 2, args);
565 // keep track of the buffer position.
566 CommandBufferOffset command1_put = get_helper_put();
567 int32 token = helper_->InsertToken();
569 EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
570 .WillOnce(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
571 Return(error::kNoError)));
572 // Add another command.
573 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 4, 2, args);
574 helper_->WaitForToken(token);
575 // check that the get pointer is beyond the first command.
576 EXPECT_LE(command1_put, GetGetOffset());
577 helper_->Finish();
579 // Check that the commands did happen.
580 Mock::VerifyAndClearExpectations(api_mock_.get());
582 // Check the error status.
583 EXPECT_EQ(error::kNoError, GetError());
586 // Checks WaitForToken doesn't Flush if token is already read.
587 TEST_F(CommandBufferHelperTest, TestWaitForTokenFlush) {
588 CommandBufferEntry args[2];
589 args[0].value_uint32 = 3;
590 args[1].value_float = 4.f;
592 // Add a first command.
593 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 3, 2, args);
594 int32 token = helper_->InsertToken();
596 EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
597 .WillOnce(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
598 Return(error::kNoError)));
600 int flush_count = command_buffer_->FlushCount();
602 // Test that waiting for pending token causes a Flush.
603 helper_->WaitForToken(token);
604 EXPECT_EQ(command_buffer_->FlushCount(), flush_count + 1);
606 // Test that we don't Flush repeatedly.
607 helper_->WaitForToken(token);
608 EXPECT_EQ(command_buffer_->FlushCount(), flush_count + 1);
610 // Add another command.
611 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 4, 2, args);
613 // Test that we don't Flush repeatedly even if commands are pending.
614 helper_->WaitForToken(token);
615 EXPECT_EQ(command_buffer_->FlushCount(), flush_count + 1);
617 helper_->Finish();
619 // Check that the commands did happen.
620 Mock::VerifyAndClearExpectations(api_mock_.get());
622 // Check the error status.
623 EXPECT_EQ(error::kNoError, GetError());
626 TEST_F(CommandBufferHelperTest, FreeRingBuffer) {
627 EXPECT_TRUE(helper_->HaveRingBuffer());
629 // Test freeing ring buffer.
630 helper_->FreeRingBuffer();
631 EXPECT_FALSE(helper_->HaveRingBuffer());
633 // Test that InsertToken allocates a new one
634 int32 token = helper_->InsertToken();
635 EXPECT_TRUE(helper_->HaveRingBuffer());
636 EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
637 .WillOnce(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
638 Return(error::kNoError)));
639 helper_->WaitForToken(token);
640 helper_->FreeRingBuffer();
641 EXPECT_FALSE(helper_->HaveRingBuffer());
643 // Test that WaitForAvailableEntries allocates a new one
644 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 0, NULL);
645 EXPECT_TRUE(helper_->HaveRingBuffer());
646 helper_->Finish();
647 helper_->FreeRingBuffer();
648 EXPECT_FALSE(helper_->HaveRingBuffer());
650 // Check that the commands did happen.
651 Mock::VerifyAndClearExpectations(api_mock_.get());
654 TEST_F(CommandBufferHelperTest, Noop) {
655 for (int ii = 1; ii < 4; ++ii) {
656 CommandBufferOffset put_before = get_helper_put();
657 helper_->Noop(ii);
658 CommandBufferOffset put_after = get_helper_put();
659 EXPECT_EQ(ii, put_after - put_before);
663 TEST_F(CommandBufferHelperTest, IsContextLost) {
664 EXPECT_FALSE(helper_->IsContextLost());
665 command_buffer_->SetParseError(error::kGenericError);
666 EXPECT_TRUE(helper_->IsContextLost());
669 // Checks helper's 'flush generation' updates.
670 TEST_F(CommandBufferHelperTest, TestFlushGeneration) {
671 // Explicit flushing only.
672 helper_->SetAutomaticFlushes(false);
674 // Generation should change after Flush() but not before.
675 uint32 gen1, gen2, gen3;
677 gen1 = GetHelperFlushGeneration();
678 AddUniqueCommandWithExpect(error::kNoError, 2);
679 gen2 = GetHelperFlushGeneration();
680 helper_->Flush();
681 gen3 = GetHelperFlushGeneration();
682 EXPECT_EQ(gen2, gen1);
683 EXPECT_NE(gen3, gen2);
685 // Generation should change after Finish() but not before.
686 gen1 = GetHelperFlushGeneration();
687 AddUniqueCommandWithExpect(error::kNoError, 2);
688 gen2 = GetHelperFlushGeneration();
689 helper_->Finish();
690 gen3 = GetHelperFlushGeneration();
691 EXPECT_EQ(gen2, gen1);
692 EXPECT_NE(gen3, gen2);
694 helper_->Finish();
696 // Check that the commands did happen.
697 Mock::VerifyAndClearExpectations(api_mock_.get());
699 // Check the error status.
700 EXPECT_EQ(error::kNoError, GetError());
703 TEST_F(CommandBufferHelperTest, TestOrderingBarrierFlushGeneration) {
704 // Explicit flushing only.
705 helper_->SetAutomaticFlushes(false);
707 // Generation should change after OrderingBarrier() but not before.
708 uint32 gen1, gen2, gen3;
710 gen1 = GetHelperFlushGeneration();
711 AddUniqueCommandWithExpect(error::kNoError, 2);
712 gen2 = GetHelperFlushGeneration();
713 helper_->OrderingBarrier();
714 gen3 = GetHelperFlushGeneration();
715 EXPECT_EQ(gen2, gen1);
716 EXPECT_NE(gen3, gen2);
718 helper_->Finish();
720 // Check that the commands did happen.
721 Mock::VerifyAndClearExpectations(api_mock_.get());
723 // Check the error status.
724 EXPECT_EQ(error::kNoError, GetError());
727 // Expect Flush() to always call CommandBuffer::Flush().
728 TEST_F(CommandBufferHelperTest, TestFlushToCommandBuffer) {
729 // Explicit flushing only.
730 helper_->SetAutomaticFlushes(false);
732 int flush_count1, flush_count2, flush_count3;
734 flush_count1 = command_buffer_->FlushCount();
735 AddUniqueCommandWithExpect(error::kNoError, 2);
736 helper_->Flush();
737 flush_count2 = command_buffer_->FlushCount();
738 helper_->Flush();
739 flush_count3 = command_buffer_->FlushCount();
741 EXPECT_EQ(flush_count2, flush_count1 + 1);
742 EXPECT_EQ(flush_count3, flush_count2 + 1);
745 // Expect OrderingBarrier() to always call CommandBuffer::OrderingBarrier().
746 TEST_F(CommandBufferHelperTest, TestOrderingBarrierToCommandBuffer) {
747 // Explicit flushing only.
748 helper_->SetAutomaticFlushes(false);
750 int flush_count1, flush_count2, flush_count3;
752 flush_count1 = command_buffer_->FlushCount();
753 AddUniqueCommandWithExpect(error::kNoError, 2);
754 helper_->OrderingBarrier();
755 flush_count2 = command_buffer_->FlushCount();
756 helper_->OrderingBarrier();
757 flush_count3 = command_buffer_->FlushCount();
759 EXPECT_EQ(flush_count2, flush_count1 + 1);
760 EXPECT_EQ(flush_count3, flush_count2 + 1);
763 } // namespace gpu