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 // This file contains the tests for the CommandBufferSharedState class.
7 #include "gpu/command_buffer/common/command_buffer_shared.h"
10 #include "base/location.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class CommandBufferSharedTest
: public testing::Test
{
20 void SetUp() override
{
21 shared_state_
.reset(new CommandBufferSharedState());
22 shared_state_
->Initialize();
25 scoped_ptr
<CommandBufferSharedState
> shared_state_
;
28 TEST_F(CommandBufferSharedTest
, TestBasic
) {
29 CommandBuffer::State state
;
31 shared_state_
->Read(&state
);
33 EXPECT_LT(state
.generation
, 0x80000000);
34 EXPECT_EQ(state
.get_offset
, 0);
35 EXPECT_EQ(state
.token
, -1);
36 EXPECT_EQ(state
.error
, gpu::error::kNoError
);
37 EXPECT_EQ(state
.context_lost_reason
, gpu::error::kUnknown
);
40 static const int kSize
= 100000;
42 void WriteToState(int32
*buffer
,
43 CommandBufferSharedState
* shared_state
) {
44 CommandBuffer::State state
;
45 for (int i
= 0; i
< kSize
; i
++) {
47 state
.get_offset
= i
+ 1;
48 state
.generation
= i
+ 2;
49 state
.error
= static_cast<gpu::error::Error
>(i
+ 3);
50 // Ensure that the producer doesn't update the buffer until after the
51 // consumer reads from it.
52 EXPECT_EQ(buffer
[i
], 0);
54 shared_state
->Write(state
);
58 TEST_F(CommandBufferSharedTest
, TestConsistency
) {
59 scoped_ptr
<int32
[]> buffer
;
60 buffer
.reset(new int32
[kSize
]);
61 base::Thread
consumer("Reader Thread");
63 memset(buffer
.get(), 0, kSize
* sizeof(int32
));
66 consumer
.task_runner()->PostTask(
67 FROM_HERE
, base::Bind(&WriteToState
, buffer
.get(), shared_state_
.get()));
69 CommandBuffer::State last_state
;
71 CommandBuffer::State state
= last_state
;
73 shared_state_
->Read(&state
);
75 if (state
.generation
< last_state
.generation
)
78 if (state
.get_offset
>= 1) {
79 buffer
[state
.get_offset
- 1] = 1;
80 // Check that the state is consistent
81 EXPECT_LE(last_state
.token
, state
.token
);
82 EXPECT_LE(last_state
.generation
, state
.generation
);
84 EXPECT_EQ(state
.token
, state
.get_offset
- 2);
85 EXPECT_EQ(state
.generation
,
86 static_cast<unsigned int>(state
.get_offset
) + 1);
87 EXPECT_EQ(state
.error
, state
.get_offset
+ 2);
89 if (state
.get_offset
== kSize
)