1 // Copyright (c) 2014 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 #include "gpu/command_buffer/service/valuebuffer_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "gpu/command_buffer/common/gles2_cmd_format.h"
11 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
12 #include "gpu/command_buffer/common/value_state.h"
13 #include "gpu/command_buffer/service/common_decoder.h"
14 #include "gpu/command_buffer/service/feature_info.h"
15 #include "gpu/command_buffer/service/gpu_service_test.h"
16 #include "gpu/command_buffer/service/mocks.h"
17 #include "gpu/command_buffer/service/test_helper.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gl/gl_bindings.h"
21 #include "ui/gl/gl_mock.h"
26 class MockSubscriptionRefSetObserver
: public SubscriptionRefSet::Observer
{
28 MOCK_METHOD1(OnAddSubscription
, void(unsigned int target
));
29 MOCK_METHOD1(OnRemoveSubscription
, void(unsigned int target
));
32 class ValuebufferManagerTest
: public GpuServiceTest
{
34 ValuebufferManagerTest() {}
35 ~ValuebufferManagerTest() override
{}
37 void SetUp() override
{
38 GpuServiceTest::SetUp();
39 subscription_ref_set_
= new SubscriptionRefSet();
40 pending_state_map_
= new ValueStateMap();
41 subscription_ref_set_
->AddObserver(&mock_observer_
);
42 manager_
.reset(new ValuebufferManager(subscription_ref_set_
.get(),
43 pending_state_map_
.get()));
46 void TearDown() override
{
48 subscription_ref_set_
->RemoveObserver(&mock_observer_
);
49 GpuServiceTest::TearDown();
53 MockSubscriptionRefSetObserver mock_observer_
;
55 scoped_refptr
<SubscriptionRefSet
> subscription_ref_set_
;
56 scoped_refptr
<ValueStateMap
> pending_state_map_
;
57 scoped_ptr
<ValuebufferManager
> manager_
;
60 TEST_F(ValuebufferManagerTest
, Basic
) {
61 const GLuint kClient1Id
= 1;
62 const GLuint kClient2Id
= 2;
63 // Check we can create a Valuebuffer
64 manager_
->CreateValuebuffer(kClient1Id
);
65 Valuebuffer
* valuebuffer0
= manager_
->GetValuebuffer(kClient1Id
);
66 ASSERT_TRUE(valuebuffer0
!= NULL
);
67 EXPECT_EQ(kClient1Id
, valuebuffer0
->client_id());
68 // Check we get nothing for a non-existent Valuebuffer.
69 // Check trying to a remove non-existent Valuebuffer does not crash
70 manager_
->RemoveValuebuffer(kClient2Id
);
71 // Check we can't get the renderbuffer after we remove it.
72 manager_
->RemoveValuebuffer(kClient1Id
);
73 EXPECT_TRUE(manager_
->GetValuebuffer(kClient1Id
) == NULL
);
76 TEST_F(ValuebufferManagerTest
, Destroy
) {
77 const GLuint kClient1Id
= 1;
78 // Check we can create Valuebuffer.
79 manager_
->CreateValuebuffer(kClient1Id
);
80 Valuebuffer
* valuebuffer0
= manager_
->GetValuebuffer(kClient1Id
);
81 ASSERT_TRUE(valuebuffer0
!= NULL
);
82 EXPECT_EQ(kClient1Id
, valuebuffer0
->client_id());
84 // Check the resources were released.
85 Valuebuffer
* valuebuffer1
= manager_
->GetValuebuffer(kClient1Id
);
86 ASSERT_TRUE(valuebuffer1
== NULL
);
89 TEST_F(ValuebufferManagerTest
, ValueBuffer
) {
90 const GLuint kClient1Id
= 1;
91 // Check we can create a Valuebuffer
92 manager_
->CreateValuebuffer(kClient1Id
);
93 Valuebuffer
* valuebuffer0
= manager_
->GetValuebuffer(kClient1Id
);
94 ASSERT_TRUE(valuebuffer0
!= NULL
);
95 EXPECT_EQ(kClient1Id
, valuebuffer0
->client_id());
96 EXPECT_FALSE(valuebuffer0
->IsValid());
99 TEST_F(ValuebufferManagerTest
, UpdateState
) {
100 const GLuint kClient1Id
= 1;
101 ValueState valuestate1
;
102 valuestate1
.int_value
[0] = 111;
103 ValueState valuestate2
;
104 valuestate2
.int_value
[0] = 222;
105 manager_
->CreateValuebuffer(kClient1Id
);
106 Valuebuffer
* valuebuffer0
= manager_
->GetValuebuffer(kClient1Id
);
107 ASSERT_TRUE(valuebuffer0
!= NULL
);
108 EXPECT_EQ(kClient1Id
, valuebuffer0
->client_id());
109 valuebuffer0
->AddSubscription(GL_MOUSE_POSITION_CHROMIUM
);
110 ASSERT_TRUE(valuebuffer0
->GetState(GL_MOUSE_POSITION_CHROMIUM
) == NULL
);
111 pending_state_map_
->UpdateState(GL_MOUSE_POSITION_CHROMIUM
, valuestate1
);
112 manager_
->UpdateValuebufferState(valuebuffer0
);
113 const ValueState
* new_state1
=
114 valuebuffer0
->GetState(GL_MOUSE_POSITION_CHROMIUM
);
115 ASSERT_TRUE(new_state1
!= NULL
);
116 ASSERT_TRUE(new_state1
->int_value
[0] == 111);
117 // Ensure state changes
118 pending_state_map_
->UpdateState(GL_MOUSE_POSITION_CHROMIUM
, valuestate2
);
119 manager_
->UpdateValuebufferState(valuebuffer0
);
120 const ValueState
* new_state2
=
121 valuebuffer0
->GetState(GL_MOUSE_POSITION_CHROMIUM
);
122 ASSERT_TRUE(new_state2
!= NULL
);
123 ASSERT_TRUE(new_state2
->int_value
[0] == 222);
126 TEST_F(ValuebufferManagerTest
, NotifySubscriptionRefs
) {
127 const GLuint kClientId1
= 1;
128 const GLuint kClientId2
= 2;
129 manager_
->CreateValuebuffer(kClientId1
);
130 Valuebuffer
* valuebuffer1
= manager_
->GetValuebuffer(kClientId1
);
131 ASSERT_TRUE(valuebuffer1
!= NULL
);
132 manager_
->CreateValuebuffer(kClientId2
);
133 Valuebuffer
* valuebuffer2
= manager_
->GetValuebuffer(kClientId2
);
134 ASSERT_TRUE(valuebuffer2
!= NULL
);
135 EXPECT_CALL(mock_observer_
, OnAddSubscription(GL_MOUSE_POSITION_CHROMIUM
))
137 valuebuffer1
->AddSubscription(GL_MOUSE_POSITION_CHROMIUM
);
138 EXPECT_CALL(mock_observer_
, OnAddSubscription(GL_MOUSE_POSITION_CHROMIUM
))
140 valuebuffer2
->AddSubscription(GL_MOUSE_POSITION_CHROMIUM
);
141 EXPECT_CALL(mock_observer_
, OnRemoveSubscription(GL_MOUSE_POSITION_CHROMIUM
))
143 valuebuffer1
->RemoveSubscription(GL_MOUSE_POSITION_CHROMIUM
);
144 // Ensure the manager still thinks a buffer has a reference to the
145 // subscription target.
146 EXPECT_CALL(mock_observer_
, OnRemoveSubscription(GL_MOUSE_POSITION_CHROMIUM
))
148 valuebuffer2
->RemoveSubscription(GL_MOUSE_POSITION_CHROMIUM
);