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.
4
5// This file contains unit tests for gles2 commmands
6
7#include <limits>
8
9#include "base/bind.h"
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"
14
15namespace gpu {
16namespace gles2 {
17
18class GLES2FormatTest : public testing::Test {
19 protected:
20  static const unsigned char kInitialValue = 0xBD;
21
22  virtual void SetUp() {
23    memset(buffer_, kInitialValue, sizeof(buffer_));
24  }
25
26  virtual void TearDown() {
27  }
28
29  template <typename T>
30  T* GetBufferAs() {
31    return static_cast<T*>(static_cast<void*>(&buffer_));
32  }
33
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]);
43  }
44
45  void CheckBytesWrittenMatchesExpectedSize(
46      const void* end, size_t expected_size) {
47    CheckBytesWritten(end, expected_size, expected_size);
48  }
49
50 private:
51  unsigned char buffer_[1024];
52};
53
54void SignalCompletion(uint32* assigned_async_token_ptr,
55                      uint32 async_token,
56                      AsyncUploadSync* sync) {
57  EXPECT_EQ(async_token, *assigned_async_token_ptr);
58  sync->SetAsyncUploadToken(async_token);
59}
60
61TEST(GLES2FormatAsyncUploadSyncTest, AsyncUploadSync) {
62  const size_t kSize = 10;
63  const size_t kCount = 1000;
64
65  base::Thread thread("GLES2FormatUploadSyncTest - Fake Upload Thread");
66  thread.Start();
67
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) {
70    AsyncUploadSync sync;
71    sync.Reset();
72
73    uint32 buffer_tokens[kSize];
74    memset(buffer_tokens, 0, sizeof(buffer_tokens));
75
76    // Start with a token large enough so that we'll wrap.
77    uint32 async_token = std::numeric_limits<uint32>::max() - kCount / 2;
78
79    // Set initial async token.
80    sync.SetAsyncUploadToken(async_token);
81
82    for (size_t i = 0; i < kCount; ++i) {
83      size_t buffer = i % kSize;
84
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();
89
90      // Next token, skip 0.
91      async_token++;
92      if (async_token == 0)
93        async_token++;
94
95      // Set the buffer's associated token.
96      buffer_tokens[buffer] = async_token;
97
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],
103                                                 async_token,
104                                                 &sync));
105    }
106
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)));
112    waitable.Wait();
113  }
114}
115
116// GCC requires these declarations, but MSVC requires they not be present
117#ifndef _MSC_VER
118const unsigned char GLES2FormatTest::kInitialValue;
119#endif
120
121#include "gpu/command_buffer/common/gles2_cmd_format_test_autogen.h"
122
123}  // namespace gles2
124}  // namespace gpu
125
126