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"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class CommandBufferSharedTest
: public testing::Test
{
17 void SetUp() override
{
18 shared_state_
.reset(new CommandBufferSharedState());
19 shared_state_
->Initialize();
22 scoped_ptr
<CommandBufferSharedState
> shared_state_
;
25 TEST_F(CommandBufferSharedTest
, TestBasic
) {
26 CommandBuffer::State state
;
28 shared_state_
->Read(&state
);
30 EXPECT_LT(state
.generation
, 0x80000000);
31 EXPECT_EQ(state
.get_offset
, 0);
32 EXPECT_EQ(state
.token
, -1);
33 EXPECT_EQ(state
.error
, gpu::error::kNoError
);
34 EXPECT_EQ(state
.context_lost_reason
, gpu::error::kUnknown
);
37 static const int kSize
= 100000;
39 void WriteToState(int32
*buffer
,
40 CommandBufferSharedState
* shared_state
) {
41 CommandBuffer::State state
;
42 for (int i
= 0; i
< kSize
; i
++) {
44 state
.get_offset
= i
+ 1;
45 state
.generation
= i
+ 2;
46 state
.error
= static_cast<gpu::error::Error
>(i
+ 3);
47 // Ensure that the producer doesn't update the buffer until after the
48 // consumer reads from it.
49 EXPECT_EQ(buffer
[i
], 0);
51 shared_state
->Write(state
);
55 TEST_F(CommandBufferSharedTest
, TestConsistency
) {
56 scoped_ptr
<int32
[]> buffer
;
57 buffer
.reset(new int32
[kSize
]);
58 base::Thread
consumer("Reader Thread");
60 memset(buffer
.get(), 0, kSize
* sizeof(int32
));
63 consumer
.message_loop()->PostTask(
64 FROM_HERE
, base::Bind(&WriteToState
, buffer
.get(),
65 shared_state_
.get()));
67 CommandBuffer::State last_state
;
69 CommandBuffer::State state
= last_state
;
71 shared_state_
->Read(&state
);
73 if (state
.generation
< last_state
.generation
)
76 if (state
.get_offset
>= 1) {
77 buffer
[state
.get_offset
- 1] = 1;
78 // Check that the state is consistent
79 EXPECT_LE(last_state
.token
, state
.token
);
80 EXPECT_LE(last_state
.generation
, state
.generation
);
82 EXPECT_EQ(state
.token
, state
.get_offset
- 2);
83 EXPECT_EQ(state
.generation
,
84 static_cast<unsigned int>(state
.get_offset
) + 1);
85 EXPECT_EQ(state
.error
, state
.get_offset
+ 2);
87 if (state
.get_offset
== kSize
)