Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / vertex_attrib_manager_unittest.cc
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.
4
5 #include "gpu/command_buffer/service/vertex_attrib_manager.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/command_buffer/service/buffer_manager.h"
9 #include "gpu/command_buffer/service/error_state_mock.h"
10 #include "gpu/command_buffer/service/feature_info.h"
11 #include "gpu/command_buffer/service/gpu_service_test.h"
12 #include "gpu/command_buffer/service/test_helper.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gl/gl_mock.h"
15
16 using ::testing::Pointee;
17 using ::testing::_;
18
19 namespace gpu {
20 namespace gles2 {
21
22 class VertexAttribManagerTest : public GpuServiceTest {
23  public:
24   static const uint32 kNumVertexAttribs = 8;
25
26   VertexAttribManagerTest() {
27   }
28
29   ~VertexAttribManagerTest() override {}
30
31  protected:
32   void SetUp() override {
33     GpuServiceTest::SetUp();
34
35     for (uint32 ii = 0; ii < kNumVertexAttribs; ++ii) {
36       EXPECT_CALL(*gl_, VertexAttrib4f(ii, 0.0f, 0.0f, 0.0f, 1.0f))
37           .Times(1)
38           .RetiresOnSaturation();
39     }
40
41     manager_ = new VertexAttribManager();
42     manager_->Initialize(kNumVertexAttribs, true);
43   }
44
45   scoped_refptr<VertexAttribManager> manager_;
46 };
47
48 // GCC requires these declarations, but MSVC requires they not be present
49 #ifndef COMPILER_MSVC
50 const uint32 VertexAttribManagerTest::kNumVertexAttribs;
51 #endif
52
53 TEST_F(VertexAttribManagerTest, Basic) {
54   EXPECT_TRUE(manager_->GetVertexAttrib(kNumVertexAttribs) == NULL);
55   EXPECT_FALSE(manager_->HaveFixedAttribs());
56
57   const VertexAttribManager::VertexAttribList& enabled_attribs =
58       manager_->GetEnabledVertexAttribs();
59   EXPECT_EQ(0u, enabled_attribs.size());
60
61   for (uint32 ii = 0; ii < kNumVertexAttribs; ii += kNumVertexAttribs - 1) {
62     VertexAttrib* attrib = manager_->GetVertexAttrib(ii);
63     ASSERT_TRUE(attrib != NULL);
64     EXPECT_EQ(ii, attrib->index());
65     EXPECT_TRUE(attrib->buffer() == NULL);
66     EXPECT_EQ(0, attrib->offset());
67     EXPECT_EQ(4, attrib->size());
68     EXPECT_EQ(static_cast<GLenum>(GL_FLOAT), attrib->type());
69     EXPECT_EQ(GL_FALSE, attrib->normalized());
70     EXPECT_EQ(0, attrib->gl_stride());
71     EXPECT_FALSE(attrib->enabled());
72     manager_->Enable(ii, true);
73     EXPECT_TRUE(attrib->enabled());
74   }
75 }
76
77 TEST_F(VertexAttribManagerTest, Enable) {
78   const VertexAttribManager::VertexAttribList& enabled_attribs =
79       manager_->GetEnabledVertexAttribs();
80
81   VertexAttrib* attrib1 = manager_->GetVertexAttrib(1);
82   VertexAttrib* attrib2 = manager_->GetVertexAttrib(3);
83
84   manager_->Enable(1, true);
85   ASSERT_EQ(1u, enabled_attribs.size());
86   EXPECT_TRUE(attrib1->enabled());
87   manager_->Enable(3, true);
88   ASSERT_EQ(2u, enabled_attribs.size());
89   EXPECT_TRUE(attrib2->enabled());
90
91   manager_->Enable(1, false);
92   ASSERT_EQ(1u, enabled_attribs.size());
93   EXPECT_FALSE(attrib1->enabled());
94
95   manager_->Enable(3, false);
96   ASSERT_EQ(0u, enabled_attribs.size());
97   EXPECT_FALSE(attrib2->enabled());
98 }
99
100 TEST_F(VertexAttribManagerTest, SetAttribInfo) {
101   BufferManager buffer_manager(NULL, NULL);
102   buffer_manager.CreateBuffer(1, 2);
103   Buffer* buffer = buffer_manager.GetBuffer(1);
104   ASSERT_TRUE(buffer != NULL);
105
106   VertexAttrib* attrib = manager_->GetVertexAttrib(1);
107
108   manager_->SetAttribInfo(1, buffer, 3, GL_SHORT, GL_TRUE, 32, 32, 4);
109
110   EXPECT_EQ(buffer, attrib->buffer());
111   EXPECT_EQ(4, attrib->offset());
112   EXPECT_EQ(3, attrib->size());
113   EXPECT_EQ(static_cast<GLenum>(GL_SHORT), attrib->type());
114   EXPECT_EQ(GL_TRUE, attrib->normalized());
115   EXPECT_EQ(32, attrib->gl_stride());
116
117   // The VertexAttribManager must be destroyed before the BufferManager
118   // so it releases its buffers.
119   manager_ = NULL;
120   buffer_manager.Destroy(false);
121 }
122
123 TEST_F(VertexAttribManagerTest, HaveFixedAttribs) {
124   EXPECT_FALSE(manager_->HaveFixedAttribs());
125   manager_->SetAttribInfo(1, NULL, 4, GL_FIXED, GL_FALSE, 0, 16, 0);
126   EXPECT_TRUE(manager_->HaveFixedAttribs());
127   manager_->SetAttribInfo(3, NULL, 4, GL_FIXED, GL_FALSE, 0, 16, 0);
128   EXPECT_TRUE(manager_->HaveFixedAttribs());
129   manager_->SetAttribInfo(1, NULL, 4, GL_FLOAT, GL_FALSE, 0, 16, 0);
130   EXPECT_TRUE(manager_->HaveFixedAttribs());
131   manager_->SetAttribInfo(3, NULL, 4, GL_FLOAT, GL_FALSE, 0, 16, 0);
132   EXPECT_FALSE(manager_->HaveFixedAttribs());
133 }
134
135 TEST_F(VertexAttribManagerTest, CanAccess) {
136   MockErrorState error_state;
137   BufferManager buffer_manager(NULL, NULL);
138   buffer_manager.CreateBuffer(1, 2);
139   Buffer* buffer = buffer_manager.GetBuffer(1);
140   ASSERT_TRUE(buffer != NULL);
141
142   VertexAttrib* attrib = manager_->GetVertexAttrib(1);
143
144   EXPECT_TRUE(attrib->CanAccess(0));
145   manager_->Enable(1, true);
146   EXPECT_FALSE(attrib->CanAccess(0));
147
148   manager_->SetAttribInfo(1, buffer, 4, GL_FLOAT, GL_FALSE, 0, 16, 0);
149   EXPECT_FALSE(attrib->CanAccess(0));
150
151   EXPECT_TRUE(buffer_manager.SetTarget(buffer, GL_ARRAY_BUFFER));
152   TestHelper::DoBufferData(
153       gl_.get(), &error_state, &buffer_manager, buffer, 15, GL_STATIC_DRAW,
154       NULL, GL_NO_ERROR);
155
156   EXPECT_FALSE(attrib->CanAccess(0));
157   TestHelper::DoBufferData(
158       gl_.get(), &error_state, &buffer_manager, buffer, 16, GL_STATIC_DRAW,
159       NULL, GL_NO_ERROR);
160   EXPECT_TRUE(attrib->CanAccess(0));
161   EXPECT_FALSE(attrib->CanAccess(1));
162
163   manager_->SetAttribInfo(1, buffer, 4, GL_FLOAT, GL_FALSE, 0, 16, 1);
164   EXPECT_FALSE(attrib->CanAccess(0));
165
166   TestHelper::DoBufferData(
167       gl_.get(), &error_state, &buffer_manager, buffer, 32, GL_STATIC_DRAW,
168       NULL, GL_NO_ERROR);
169   EXPECT_TRUE(attrib->CanAccess(0));
170   EXPECT_FALSE(attrib->CanAccess(1));
171   manager_->SetAttribInfo(1, buffer, 4, GL_FLOAT, GL_FALSE, 0, 16, 0);
172   EXPECT_TRUE(attrib->CanAccess(1));
173   manager_->SetAttribInfo(1, buffer, 4, GL_FLOAT, GL_FALSE, 0, 20, 0);
174   EXPECT_TRUE(attrib->CanAccess(0));
175   EXPECT_FALSE(attrib->CanAccess(1));
176
177   // The VertexAttribManager must be destroyed before the BufferManager
178   // so it releases its buffers.
179   manager_ = NULL;
180   buffer_manager.Destroy(false);
181 }
182
183 TEST_F(VertexAttribManagerTest, Unbind) {
184   BufferManager buffer_manager(NULL, NULL);
185   buffer_manager.CreateBuffer(1, 2);
186   buffer_manager.CreateBuffer(3, 4);
187   Buffer* buffer1 = buffer_manager.GetBuffer(1);
188   Buffer* buffer2 = buffer_manager.GetBuffer(3);
189   ASSERT_TRUE(buffer1 != NULL);
190   ASSERT_TRUE(buffer2 != NULL);
191
192   VertexAttrib* attrib1 = manager_->GetVertexAttrib(1);
193   VertexAttrib* attrib3 = manager_->GetVertexAttrib(3);
194
195   // Attach to 2 buffers.
196   manager_->SetAttribInfo(1, buffer1, 3, GL_SHORT, GL_TRUE, 32, 32, 4);
197   manager_->SetAttribInfo(3, buffer1, 3, GL_SHORT, GL_TRUE, 32, 32, 4);
198   // Check they were attached.
199   EXPECT_EQ(buffer1, attrib1->buffer());
200   EXPECT_EQ(buffer1, attrib3->buffer());
201   // Unbind unattached buffer.
202   manager_->Unbind(buffer2);
203   // Should be no-op.
204   EXPECT_EQ(buffer1, attrib1->buffer());
205   EXPECT_EQ(buffer1, attrib3->buffer());
206   // Unbind buffer.
207   manager_->Unbind(buffer1);
208   // Check they were detached
209   EXPECT_TRUE(NULL == attrib1->buffer());
210   EXPECT_TRUE(NULL == attrib3->buffer());
211
212   // The VertexAttribManager must be destroyed before the BufferManager
213   // so it releases its buffers.
214   manager_ = NULL;
215   buffer_manager.Destroy(false);
216 }
217
218 // TODO(gman): Test ValidateBindings
219 // TODO(gman): Test ValidateBindings with client side arrays.
220
221 }  // namespace gles2
222 }  // namespace gpu
223
224