Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gpu / command_buffer / client / cmd_buffer_helper_test.cc
blobc2d1361555a0e5afbe0317980f7e0007316bc54b
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 "base/message_loop/message_loop.h"
13 #include "gpu/command_buffer/client/cmd_buffer_helper.h"
14 #include "gpu/command_buffer/service/command_buffer_service.h"
15 #include "gpu/command_buffer/service/gpu_scheduler.h"
16 #include "gpu/command_buffer/service/mocks.h"
17 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 #if defined(OS_MACOSX)
21 #include "base/mac/scoped_nsautorelease_pool.h"
22 #endif
24 namespace gpu {
26 using testing::Return;
27 using testing::Mock;
28 using testing::Truly;
29 using testing::Sequence;
30 using testing::DoAll;
31 using testing::Invoke;
32 using testing::_;
34 const int32 kTotalNumCommandEntries = 32;
35 const int32 kCommandBufferSizeBytes =
36 kTotalNumCommandEntries * sizeof(CommandBufferEntry);
37 const int32 kUnusedCommandId = 5; // we use 0 and 2 currently.
39 // Override CommandBufferService::Flush() to lock flushing and simulate
40 // the buffer becoming full in asynchronous mode.
41 class CommandBufferServiceLocked : public CommandBufferService {
42 public:
43 explicit CommandBufferServiceLocked(
44 TransferBufferManagerInterface* transfer_buffer_manager)
45 : CommandBufferService(transfer_buffer_manager),
46 flush_locked_(false),
47 last_flush_(-1),
48 flush_count_(0) {}
49 virtual ~CommandBufferServiceLocked() {}
51 virtual void Flush(int32 put_offset) OVERRIDE {
52 flush_count_++;
53 if (!flush_locked_) {
54 last_flush_ = -1;
55 CommandBufferService::Flush(put_offset);
56 } else {
57 last_flush_ = put_offset;
61 void LockFlush() { flush_locked_ = true; }
63 void UnlockFlush() { flush_locked_ = false; }
65 int FlushCount() { return flush_count_; }
67 virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE {
68 if (last_flush_ != -1) {
69 CommandBufferService::Flush(last_flush_);
70 last_flush_ = -1;
72 CommandBufferService::WaitForGetOffsetInRange(start, end);
75 private:
76 bool flush_locked_;
77 int last_flush_;
78 int flush_count_;
79 DISALLOW_COPY_AND_ASSIGN(CommandBufferServiceLocked);
82 // Test fixture for CommandBufferHelper test - Creates a CommandBufferHelper,
83 // using a CommandBufferEngine with a mock AsyncAPIInterface for its interface
84 // (calling it directly, not through the RPC mechanism).
85 class CommandBufferHelperTest : public testing::Test {
86 protected:
87 virtual void SetUp() {
88 api_mock_.reset(new AsyncAPIMock);
89 // ignore noops in the mock - we don't want to inspect the internals of the
90 // helper.
91 EXPECT_CALL(*api_mock_, DoCommand(cmd::kNoop, _, _))
92 .WillRepeatedly(Return(error::kNoError));
95 TransferBufferManager* manager = new TransferBufferManager();
96 transfer_buffer_manager_.reset(manager);
97 EXPECT_TRUE(manager->Initialize());
99 command_buffer_.reset(
100 new CommandBufferServiceLocked(transfer_buffer_manager_.get()));
101 EXPECT_TRUE(command_buffer_->Initialize());
103 gpu_scheduler_.reset(new GpuScheduler(
104 command_buffer_.get(), api_mock_.get(), NULL));
105 command_buffer_->SetPutOffsetChangeCallback(base::Bind(
106 &GpuScheduler::PutChanged, base::Unretained(gpu_scheduler_.get())));
107 command_buffer_->SetGetBufferChangeCallback(base::Bind(
108 &GpuScheduler::SetGetBuffer, base::Unretained(gpu_scheduler_.get())));
110 api_mock_->set_engine(gpu_scheduler_.get());
112 helper_.reset(new CommandBufferHelper(command_buffer_.get()));
113 helper_->Initialize(kCommandBufferSizeBytes);
115 test_command_next_id_ = kUnusedCommandId;
118 virtual void TearDown() {
119 // If the GpuScheduler posts any tasks, this forces them to run.
120 base::MessageLoop::current()->RunUntilIdle();
121 test_command_args_.clear();
124 const CommandParser* GetParser() const {
125 return gpu_scheduler_->parser();
128 int32 ImmediateEntryCount() const { return helper_->immediate_entry_count_; }
130 // Adds a command to the buffer through the helper, while adding it as an
131 // expected call on the API mock.
132 void AddCommandWithExpect(error::Error _return,
133 unsigned int command,
134 int arg_count,
135 CommandBufferEntry *args) {
136 CommandHeader header;
137 header.size = arg_count + 1;
138 header.command = command;
139 CommandBufferEntry* cmds =
140 static_cast<CommandBufferEntry*>(helper_->GetSpace(arg_count + 1));
141 CommandBufferOffset put = 0;
142 cmds[put++].value_header = header;
143 for (int ii = 0; ii < arg_count; ++ii) {
144 cmds[put++] = args[ii];
147 EXPECT_CALL(*api_mock_, DoCommand(command, arg_count,
148 Truly(AsyncAPIMock::IsArgs(arg_count, args))))
149 .InSequence(sequence_)
150 .WillOnce(Return(_return));
153 void AddUniqueCommandWithExpect(error::Error _return, int cmd_size) {
154 EXPECT_GE(cmd_size, 1);
155 EXPECT_LT(cmd_size, kTotalNumCommandEntries);
156 int arg_count = cmd_size - 1;
158 // Allocate array for args.
159 linked_ptr<std::vector<CommandBufferEntry> > args_ptr(
160 new std::vector<CommandBufferEntry>(arg_count ? arg_count : 1));
162 for (int32 ii = 0; ii < arg_count; ++ii) {
163 (*args_ptr)[ii].value_uint32 = 0xF00DF00D + ii;
166 // Add command and save args in test_command_args_ until the test completes.
167 AddCommandWithExpect(
168 _return, test_command_next_id_++, arg_count, &(*args_ptr)[0]);
169 test_command_args_.insert(test_command_args_.end(), args_ptr);
172 void TestCommandWrappingFull(int32 cmd_size, int32 start_commands) {
173 const int32 num_args = cmd_size - 1;
174 EXPECT_EQ(kTotalNumCommandEntries % cmd_size, 0);
176 std::vector<CommandBufferEntry> args(num_args);
177 for (int32 ii = 0; ii < num_args; ++ii) {
178 args[ii].value_uint32 = ii + 1;
181 // Initially insert commands up to start_commands and Finish().
182 for (int32 ii = 0; ii < start_commands; ++ii) {
183 AddCommandWithExpect(
184 error::kNoError, ii + kUnusedCommandId, num_args, &args[0]);
186 helper_->Finish();
188 EXPECT_EQ(GetParser()->put(),
189 (start_commands * cmd_size) % kTotalNumCommandEntries);
190 EXPECT_EQ(GetParser()->get(),
191 (start_commands * cmd_size) % kTotalNumCommandEntries);
193 // Lock flushing to force the buffer to get full.
194 command_buffer_->LockFlush();
196 // Add enough commands to over fill the buffer.
197 for (int32 ii = 0; ii < kTotalNumCommandEntries / cmd_size + 2; ++ii) {
198 AddCommandWithExpect(error::kNoError,
199 start_commands + ii + kUnusedCommandId,
200 num_args,
201 &args[0]);
204 // Flush all commands.
205 command_buffer_->UnlockFlush();
206 helper_->Finish();
208 // Check that the commands did happen.
209 Mock::VerifyAndClearExpectations(api_mock_.get());
211 // Check the error status.
212 EXPECT_EQ(error::kNoError, GetError());
215 // Checks that the buffer from put to put+size is free in the parser.
216 void CheckFreeSpace(CommandBufferOffset put, unsigned int size) {
217 CommandBufferOffset parser_put = GetParser()->put();
218 CommandBufferOffset parser_get = GetParser()->get();
219 CommandBufferOffset limit = put + size;
220 if (parser_get > parser_put) {
221 // "busy" buffer wraps, so "free" buffer is between put (inclusive) and
222 // get (exclusive).
223 EXPECT_LE(parser_put, put);
224 EXPECT_GT(parser_get, limit);
225 } else {
226 // "busy" buffer does not wrap, so the "free" buffer is the top side (from
227 // put to the limit) and the bottom side (from 0 to get).
228 if (put >= parser_put) {
229 // we're on the top side, check we are below the limit.
230 EXPECT_GE(kTotalNumCommandEntries, limit);
231 } else {
232 // we're on the bottom side, check we are below get.
233 EXPECT_GT(parser_get, limit);
238 int32 GetGetOffset() {
239 return command_buffer_->GetLastState().get_offset;
242 int32 GetPutOffset() {
243 return command_buffer_->GetLastState().put_offset;
246 int32 GetHelperGetOffset() { return helper_->get_offset(); }
248 int32 GetHelperPutOffset() { return helper_->put_; }
250 uint32 GetHelperFlushGeneration() { return helper_->flush_generation(); }
252 error::Error GetError() {
253 return command_buffer_->GetLastState().error;
256 CommandBufferOffset get_helper_put() { return helper_->put_; }
258 #if defined(OS_MACOSX)
259 base::mac::ScopedNSAutoreleasePool autorelease_pool_;
260 #endif
261 base::MessageLoop message_loop_;
262 scoped_ptr<AsyncAPIMock> api_mock_;
263 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
264 scoped_ptr<CommandBufferServiceLocked> command_buffer_;
265 scoped_ptr<GpuScheduler> gpu_scheduler_;
266 scoped_ptr<CommandBufferHelper> helper_;
267 std::list<linked_ptr<std::vector<CommandBufferEntry> > > test_command_args_;
268 unsigned int test_command_next_id_;
269 Sequence sequence_;
272 // Checks immediate_entry_count_ changes based on 'usable' state.
273 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesNotUsable) {
274 // Auto flushing mode is tested separately.
275 helper_->SetAutomaticFlushes(false);
276 EXPECT_EQ(helper_->usable(), true);
277 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
278 helper_->ClearUsable();
279 EXPECT_EQ(ImmediateEntryCount(), 0);
282 // Checks immediate_entry_count_ changes based on RingBuffer state.
283 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesNoRingBuffer) {
284 helper_->SetAutomaticFlushes(false);
285 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
286 helper_->FreeRingBuffer();
287 EXPECT_EQ(ImmediateEntryCount(), 0);
290 // Checks immediate_entry_count_ calc when Put >= Get and Get == 0.
291 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesGetAtZero) {
292 // No internal auto flushing.
293 helper_->SetAutomaticFlushes(false);
294 command_buffer_->LockFlush();
296 // Start at Get = Put = 0.
297 EXPECT_EQ(GetHelperPutOffset(), 0);
298 EXPECT_EQ(GetHelperGetOffset(), 0);
300 // Immediate count should be 1 less than the end of the buffer.
301 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
302 AddUniqueCommandWithExpect(error::kNoError, 2);
303 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 3);
305 helper_->Finish();
307 // Check that the commands did happen.
308 Mock::VerifyAndClearExpectations(api_mock_.get());
310 // Check the error status.
311 EXPECT_EQ(error::kNoError, GetError());
314 // Checks immediate_entry_count_ calc when Put >= Get and Get > 0.
315 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesGetInMiddle) {
316 // No internal auto flushing.
317 helper_->SetAutomaticFlushes(false);
318 command_buffer_->LockFlush();
320 // Move to Get = Put = 2.
321 AddUniqueCommandWithExpect(error::kNoError, 2);
322 helper_->Finish();
323 EXPECT_EQ(GetHelperPutOffset(), 2);
324 EXPECT_EQ(GetHelperGetOffset(), 2);
326 // Immediate count should be up to the end of the buffer.
327 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 2);
328 AddUniqueCommandWithExpect(error::kNoError, 2);
329 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 4);
331 helper_->Finish();
333 // Check that the commands did happen.
334 Mock::VerifyAndClearExpectations(api_mock_.get());
336 // Check the error status.
337 EXPECT_EQ(error::kNoError, GetError());
340 // Checks immediate_entry_count_ calc when Put < Get.
341 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesGetBeforePut) {
342 // Move to Get = kTotalNumCommandEntries / 4, Put = 0.
343 const int kInitGetOffset = kTotalNumCommandEntries / 4;
344 helper_->SetAutomaticFlushes(false);
345 command_buffer_->LockFlush();
346 AddUniqueCommandWithExpect(error::kNoError, kInitGetOffset);
347 helper_->Finish();
348 AddUniqueCommandWithExpect(error::kNoError,
349 kTotalNumCommandEntries - kInitGetOffset);
351 // Flush instead of Finish will let Put wrap without the command buffer
352 // immediately processing the data between Get and Put.
353 helper_->Flush();
355 EXPECT_EQ(GetHelperGetOffset(), kInitGetOffset);
356 EXPECT_EQ(GetHelperPutOffset(), 0);
358 // Immediate count should be up to Get - 1.
359 EXPECT_EQ(ImmediateEntryCount(), kInitGetOffset - 1);
360 AddUniqueCommandWithExpect(error::kNoError, 2);
361 EXPECT_EQ(ImmediateEntryCount(), kInitGetOffset - 3);
363 helper_->Finish();
364 // Check that the commands did happen.
365 Mock::VerifyAndClearExpectations(api_mock_.get());
367 // Check the error status.
368 EXPECT_EQ(error::kNoError, GetError());
371 // Checks immediate_entry_count_ calc when automatic flushing is enabled.
372 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesAutoFlushing) {
373 command_buffer_->LockFlush();
375 // Start at Get = Put = 0.
376 EXPECT_EQ(GetHelperPutOffset(), 0);
377 EXPECT_EQ(GetHelperGetOffset(), 0);
379 // Without auto flushes, up to kTotalNumCommandEntries - 1 is available.
380 helper_->SetAutomaticFlushes(false);
381 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries - 1);
383 // With auto flushes, and Get == Last Put,
384 // up to kTotalNumCommandEntries / kAutoFlushSmall is available.
385 helper_->SetAutomaticFlushes(true);
386 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries / kAutoFlushSmall);
388 // With auto flushes, and Get != Last Put,
389 // up to kTotalNumCommandEntries / kAutoFlushBig is available.
390 AddUniqueCommandWithExpect(error::kNoError, 2);
391 helper_->Flush();
392 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries / kAutoFlushBig);
394 helper_->Finish();
395 // Check that the commands did happen.
396 Mock::VerifyAndClearExpectations(api_mock_.get());
398 // Check the error status.
399 EXPECT_EQ(error::kNoError, GetError());
402 // Checks immediate_entry_count_ calc when automatic flushing is enabled, and
403 // we allocate commands over the immediate_entry_count_ size.
404 TEST_F(CommandBufferHelperTest, TestCalcImmediateEntriesOverFlushLimit) {
405 // Lock internal flushing.
406 command_buffer_->LockFlush();
408 // Start at Get = Put = 0.
409 EXPECT_EQ(GetHelperPutOffset(), 0);
410 EXPECT_EQ(GetHelperGetOffset(), 0);
412 // Pre-check ImmediateEntryCount is limited with automatic flushing enabled.
413 helper_->SetAutomaticFlushes(true);
414 EXPECT_EQ(ImmediateEntryCount(), kTotalNumCommandEntries / kAutoFlushSmall);
416 // Add a command larger than ImmediateEntryCount().
417 AddUniqueCommandWithExpect(error::kNoError, ImmediateEntryCount() + 1);
419 // ImmediateEntryCount() should now be 0, to force a flush check on the next
420 // command.
421 EXPECT_EQ(ImmediateEntryCount(), 0);
423 // Add a command when ImmediateEntryCount() == 0.
424 AddUniqueCommandWithExpect(error::kNoError, ImmediateEntryCount() + 1);
426 helper_->Finish();
427 // Check that the commands did happen.
428 Mock::VerifyAndClearExpectations(api_mock_.get());
430 // Check the error status.
431 EXPECT_EQ(error::kNoError, GetError());
434 // Checks that commands in the buffer are properly executed, and that the
435 // status/error stay valid.
436 TEST_F(CommandBufferHelperTest, TestCommandProcessing) {
437 // Check initial state of the engine - it should have been configured by the
438 // helper.
439 EXPECT_TRUE(GetParser() != NULL);
440 EXPECT_EQ(error::kNoError, GetError());
441 EXPECT_EQ(0, GetGetOffset());
443 // Add 3 commands through the helper
444 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 0, NULL);
446 CommandBufferEntry args1[2];
447 args1[0].value_uint32 = 3;
448 args1[1].value_float = 4.f;
449 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 2, args1);
451 CommandBufferEntry args2[2];
452 args2[0].value_uint32 = 5;
453 args2[1].value_float = 6.f;
454 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 2, args2);
456 // Wait until it's done.
457 helper_->Finish();
458 // Check that the engine has no more work to do.
459 EXPECT_TRUE(GetParser()->IsEmpty());
461 // Check that the commands did happen.
462 Mock::VerifyAndClearExpectations(api_mock_.get());
464 // Check the error status.
465 EXPECT_EQ(error::kNoError, GetError());
468 // Checks that commands in the buffer are properly executed when wrapping the
469 // buffer, and that the status/error stay valid.
470 TEST_F(CommandBufferHelperTest, TestCommandWrapping) {
471 // Add num_commands * commands of size 3 through the helper to make sure we
472 // do wrap. kTotalNumCommandEntries must not be a multiple of 3.
473 COMPILE_ASSERT(kTotalNumCommandEntries % 3 != 0,
474 Is_multiple_of_num_command_entries);
475 const int kNumCommands = (kTotalNumCommandEntries / 3) * 2;
476 CommandBufferEntry args1[2];
477 args1[0].value_uint32 = 5;
478 args1[1].value_float = 4.f;
480 for (int i = 0; i < kNumCommands; ++i) {
481 AddCommandWithExpect(error::kNoError, kUnusedCommandId + i, 2, args1);
484 helper_->Finish();
485 // Check that the commands did happen.
486 Mock::VerifyAndClearExpectations(api_mock_.get());
488 // Check the error status.
489 EXPECT_EQ(error::kNoError, GetError());
492 // Checks the case where the command inserted exactly matches the space left in
493 // the command buffer.
494 TEST_F(CommandBufferHelperTest, TestCommandWrappingExactMultiple) {
495 const int32 kCommandSize = kTotalNumCommandEntries / 2;
496 const size_t kNumArgs = kCommandSize - 1;
497 COMPILE_ASSERT(kTotalNumCommandEntries % kCommandSize == 0,
498 Not_multiple_of_num_command_entries);
499 CommandBufferEntry args1[kNumArgs];
500 for (size_t ii = 0; ii < kNumArgs; ++ii) {
501 args1[ii].value_uint32 = ii + 1;
504 for (unsigned int i = 0; i < 5; ++i) {
505 AddCommandWithExpect(
506 error::kNoError, i + kUnusedCommandId, kNumArgs, args1);
509 helper_->Finish();
510 // Check that the commands did happen.
511 Mock::VerifyAndClearExpectations(api_mock_.get());
513 // Check the error status.
514 EXPECT_EQ(error::kNoError, GetError());
517 // Checks exact wrapping condition with Get = 0.
518 TEST_F(CommandBufferHelperTest, TestCommandWrappingFullAtStart) {
519 TestCommandWrappingFull(2, 0);
522 // Checks exact wrapping condition with 0 < Get < kTotalNumCommandEntries.
523 TEST_F(CommandBufferHelperTest, TestCommandWrappingFullInMiddle) {
524 TestCommandWrappingFull(2, 1);
527 // Checks exact wrapping condition with Get = kTotalNumCommandEntries.
528 // Get should wrap back to 0, but making sure.
529 TEST_F(CommandBufferHelperTest, TestCommandWrappingFullAtEnd) {
530 TestCommandWrappingFull(2, kTotalNumCommandEntries / 2);
533 // Checks that asking for available entries work, and that the parser
534 // effectively won't use that space.
535 TEST_F(CommandBufferHelperTest, TestAvailableEntries) {
536 CommandBufferEntry args[2];
537 args[0].value_uint32 = 3;
538 args[1].value_float = 4.f;
540 // Add 2 commands through the helper - 8 entries
541 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 1, 0, NULL);
542 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 2, 0, NULL);
543 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 3, 2, args);
544 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 4, 2, args);
546 // Ask for 5 entries.
547 helper_->WaitForAvailableEntries(5);
549 CommandBufferOffset put = get_helper_put();
550 CheckFreeSpace(put, 5);
552 // Add more commands.
553 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 5, 2, args);
555 // Wait until everything is done done.
556 helper_->Finish();
558 // Check that the commands did happen.
559 Mock::VerifyAndClearExpectations(api_mock_.get());
561 // Check the error status.
562 EXPECT_EQ(error::kNoError, GetError());
565 // Checks that the InsertToken/WaitForToken work.
566 TEST_F(CommandBufferHelperTest, TestToken) {
567 CommandBufferEntry args[2];
568 args[0].value_uint32 = 3;
569 args[1].value_float = 4.f;
571 // Add a first command.
572 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 3, 2, args);
573 // keep track of the buffer position.
574 CommandBufferOffset command1_put = get_helper_put();
575 int32 token = helper_->InsertToken();
577 EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
578 .WillOnce(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
579 Return(error::kNoError)));
580 // Add another command.
581 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 4, 2, args);
582 helper_->WaitForToken(token);
583 // check that the get pointer is beyond the first command.
584 EXPECT_LE(command1_put, GetGetOffset());
585 helper_->Finish();
587 // Check that the commands did happen.
588 Mock::VerifyAndClearExpectations(api_mock_.get());
590 // Check the error status.
591 EXPECT_EQ(error::kNoError, GetError());
594 // Checks WaitForToken doesn't Flush if token is already read.
595 TEST_F(CommandBufferHelperTest, TestWaitForTokenFlush) {
596 CommandBufferEntry args[2];
597 args[0].value_uint32 = 3;
598 args[1].value_float = 4.f;
600 // Add a first command.
601 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 3, 2, args);
602 int32 token = helper_->InsertToken();
604 EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
605 .WillOnce(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
606 Return(error::kNoError)));
608 int flush_count = command_buffer_->FlushCount();
610 // Test that waiting for pending token causes a Flush.
611 helper_->WaitForToken(token);
612 EXPECT_EQ(command_buffer_->FlushCount(), flush_count + 1);
614 // Test that we don't Flush repeatedly.
615 helper_->WaitForToken(token);
616 EXPECT_EQ(command_buffer_->FlushCount(), flush_count + 1);
618 // Add another command.
619 AddCommandWithExpect(error::kNoError, kUnusedCommandId + 4, 2, args);
621 // Test that we don't Flush repeatedly even if commands are pending.
622 helper_->WaitForToken(token);
623 EXPECT_EQ(command_buffer_->FlushCount(), flush_count + 1);
625 helper_->Finish();
627 // Check that the commands did happen.
628 Mock::VerifyAndClearExpectations(api_mock_.get());
630 // Check the error status.
631 EXPECT_EQ(error::kNoError, GetError());
634 TEST_F(CommandBufferHelperTest, FreeRingBuffer) {
635 EXPECT_TRUE(helper_->HaveRingBuffer());
637 // Test freeing ring buffer.
638 helper_->FreeRingBuffer();
639 EXPECT_FALSE(helper_->HaveRingBuffer());
641 // Test that InsertToken allocates a new one
642 int32 token = helper_->InsertToken();
643 EXPECT_TRUE(helper_->HaveRingBuffer());
644 EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
645 .WillOnce(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
646 Return(error::kNoError)));
647 helper_->WaitForToken(token);
648 helper_->FreeRingBuffer();
649 EXPECT_FALSE(helper_->HaveRingBuffer());
651 // Test that WaitForAvailableEntries allocates a new one
652 AddCommandWithExpect(error::kNoError, kUnusedCommandId, 0, NULL);
653 EXPECT_TRUE(helper_->HaveRingBuffer());
654 helper_->Finish();
655 helper_->FreeRingBuffer();
656 EXPECT_FALSE(helper_->HaveRingBuffer());
658 // Check that the commands did happen.
659 Mock::VerifyAndClearExpectations(api_mock_.get());
662 TEST_F(CommandBufferHelperTest, Noop) {
663 for (int ii = 1; ii < 4; ++ii) {
664 CommandBufferOffset put_before = get_helper_put();
665 helper_->Noop(ii);
666 CommandBufferOffset put_after = get_helper_put();
667 EXPECT_EQ(ii, put_after - put_before);
671 TEST_F(CommandBufferHelperTest, IsContextLost) {
672 EXPECT_FALSE(helper_->IsContextLost());
673 command_buffer_->SetParseError(error::kGenericError);
674 EXPECT_TRUE(helper_->IsContextLost());
677 // Checks helper's 'flush generation' updates.
678 TEST_F(CommandBufferHelperTest, TestFlushGeneration) {
679 // Explicit flushing only.
680 helper_->SetAutomaticFlushes(false);
682 // Generation should change after Flush() but not before.
683 uint32 gen1, gen2, gen3;
685 gen1 = GetHelperFlushGeneration();
686 AddUniqueCommandWithExpect(error::kNoError, 2);
687 gen2 = GetHelperFlushGeneration();
688 helper_->Flush();
689 gen3 = GetHelperFlushGeneration();
690 EXPECT_EQ(gen2, gen1);
691 EXPECT_NE(gen3, gen2);
693 // Generation should change after Finish() but not before.
694 gen1 = GetHelperFlushGeneration();
695 AddUniqueCommandWithExpect(error::kNoError, 2);
696 gen2 = GetHelperFlushGeneration();
697 helper_->Finish();
698 gen3 = GetHelperFlushGeneration();
699 EXPECT_EQ(gen2, gen1);
700 EXPECT_NE(gen3, gen2);
702 helper_->Finish();
704 // Check that the commands did happen.
705 Mock::VerifyAndClearExpectations(api_mock_.get());
707 // Check the error status.
708 EXPECT_EQ(error::kNoError, GetError());
711 } // namespace gpu