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 // This file contains unit tests for gles2 commmands
10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "gpu/command_buffer/common/gles2_cmd_format.h"
18 class GLES2FormatTest
: public testing::Test
{
20 static const unsigned char kInitialValue
= 0xBD;
22 virtual void SetUp() {
23 memset(buffer_
, kInitialValue
, sizeof(buffer_
));
26 virtual void TearDown() {
31 return static_cast<T
*>(static_cast<void*>(&buffer_
));
34 void CheckBytesWritten(
35 const void* end
, size_t expected_size
, size_t written_size
) {
36 size_t actual_size
= static_cast<const unsigned char*>(end
) -
37 GetBufferAs
<const unsigned char>();
38 EXPECT_LT(actual_size
, sizeof(buffer_
));
39 EXPECT_GT(actual_size
, 0u);
40 EXPECT_EQ(expected_size
, actual_size
);
41 EXPECT_EQ(kInitialValue
, buffer_
[written_size
]);
42 EXPECT_NE(kInitialValue
, buffer_
[written_size
- 1]);
45 void CheckBytesWrittenMatchesExpectedSize(
46 const void* end
, size_t expected_size
) {
47 CheckBytesWritten(end
, expected_size
, expected_size
);
51 unsigned char buffer_
[1024];
54 void SignalCompletion(uint32
* assigned_async_token_ptr
,
56 AsyncUploadSync
* sync
) {
57 EXPECT_EQ(async_token
, *assigned_async_token_ptr
);
58 sync
->SetAsyncUploadToken(async_token
);
61 TEST(GLES2FormatAsyncUploadSyncTest
, AsyncUploadSync
) {
62 const size_t kSize
= 10;
63 const size_t kCount
= 1000;
65 base::Thread
thread("GLES2FormatUploadSyncTest - Fake Upload Thread");
68 // Run the same test 50 times so we retest the wrap as well.
69 for (size_t test_run
= 0; test_run
< 50; ++test_run
) {
73 uint32 buffer_tokens
[kSize
];
74 memset(buffer_tokens
, 0, sizeof(buffer_tokens
));
76 // Start with a token large enough so that we'll wrap.
77 uint32 async_token
= std::numeric_limits
<uint32
>::max() - kCount
/ 2;
79 // Set initial async token.
80 sync
.SetAsyncUploadToken(async_token
);
82 for (size_t i
= 0; i
< kCount
; ++i
) {
83 size_t buffer
= i
% kSize
;
85 // Loop until previous async token has passed if any was set.
86 while (buffer_tokens
[buffer
] &&
87 !sync
.HasAsyncUploadTokenPassed(buffer_tokens
[buffer
]))
88 base::PlatformThread::YieldCurrentThread();
90 // Next token, skip 0.
95 // Set the buffer's associated token.
96 buffer_tokens
[buffer
] = async_token
;
98 // Set the async upload token on the fake upload thread and assert that
99 // the associated buffer still has the given token.
100 thread
.message_loop()->PostTask(FROM_HERE
,
101 base::Bind(&SignalCompletion
,
102 &buffer_tokens
[buffer
],
107 // Flush the thread message loop before starting again.
108 base::WaitableEvent
waitable(false, false);
109 thread
.message_loop()->PostTask(FROM_HERE
,
110 base::Bind(&base::WaitableEvent::Signal
,
111 base::Unretained(&waitable
)));
116 // GCC requires these declarations, but MSVC requires they not be present
118 const unsigned char GLES2FormatTest::kInitialValue
;
121 #include "gpu/command_buffer/common/gles2_cmd_format_test_autogen.h"