1 // Copyright (c) 2015 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 "base/test/test_simple_task_runner.h"
6 #include "content/common/gpu/gpu_channel.h"
7 #include "content/common/gpu/gpu_channel_manager.h"
8 #include "content/common/gpu/gpu_channel_test_common.h"
9 #include "content/common/gpu/gpu_messages.h"
10 #include "ipc/ipc_test_sink.h"
14 class GpuChannelTest
: public GpuChannelTestCommon
{
16 GpuChannelTest() : GpuChannelTestCommon() {}
17 ~GpuChannelTest() override
{}
20 TEST_F(GpuChannelTest
, CreateViewCommandBuffer
) {
22 uint64 kClientTracingId
= 1;
24 ASSERT_TRUE(channel_manager());
26 EXPECT_TRUE(channel_manager()->OnMessageReceived(GpuMsg_EstablishChannel(
27 kClientId
, kClientTracingId
, false, false, false)));
28 GpuChannel
* channel
= channel_manager()->LookupChannel(kClientId
);
31 gfx::GLSurfaceHandle surface_handle
;
34 GPUCreateCommandBufferConfig init_params
;
35 init_params
.share_group_id
= MSG_ROUTING_NONE
;
36 init_params
.stream_id
= 0;
37 init_params
.stream_priority
= GpuStreamPriority::NORMAL
;
38 init_params
.attribs
= std::vector
<int>();
39 init_params
.active_url
= GURL();
40 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
41 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
42 surface_handle
, kSurfaceId
, kClientId
, init_params
, kRouteId
));
44 const IPC::Message
* msg
=
45 sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
48 base::Tuple
<CreateCommandBufferResult
> result
;
49 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
51 EXPECT_EQ(CREATE_COMMAND_BUFFER_SUCCEEDED
, base::get
<0>(result
));
53 sink()->ClearMessages();
55 GpuCommandBufferStub
* stub
= channel
->LookupCommandBuffer(kRouteId
);
59 TEST_F(GpuChannelTest
, IncompatibleStreamIds
) {
61 uint64 kClientTracingId
= 1;
63 ASSERT_TRUE(channel_manager());
65 EXPECT_TRUE(channel_manager()->OnMessageReceived(GpuMsg_EstablishChannel(
66 kClientId
, kClientTracingId
, false, false, false)));
67 GpuChannel
* channel
= channel_manager()->LookupChannel(kClientId
);
70 // Create first context.
71 int32 kSurfaceId1
= 1;
74 GPUCreateCommandBufferConfig init_params
;
75 init_params
.share_group_id
= MSG_ROUTING_NONE
;
76 init_params
.stream_id
= kStreamId1
;
77 init_params
.stream_priority
= GpuStreamPriority::NORMAL
;
78 init_params
.attribs
= std::vector
<int>();
79 init_params
.active_url
= GURL();
80 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
81 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
82 gfx::GLSurfaceHandle(), kSurfaceId1
, kClientId
, init_params
, kRouteId1
));
84 const IPC::Message
* msg
=
85 sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
88 base::Tuple
<CreateCommandBufferResult
> result
;
89 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
91 EXPECT_EQ(CREATE_COMMAND_BUFFER_SUCCEEDED
, base::get
<0>(result
));
93 sink()->ClearMessages();
95 GpuCommandBufferStub
* stub
= channel
->LookupCommandBuffer(kRouteId1
);
98 // Create second context in same share group but different stream.
99 int32 kSurfaceId2
= 2;
101 int32 kStreamId2
= 2;
103 init_params
.share_group_id
= kRouteId1
;
104 init_params
.stream_id
= kStreamId2
;
105 init_params
.stream_priority
= GpuStreamPriority::NORMAL
;
106 init_params
.attribs
= std::vector
<int>();
107 init_params
.active_url
= GURL();
108 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
109 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
110 gfx::GLSurfaceHandle(), kSurfaceId2
, kClientId
, init_params
, kRouteId2
));
112 msg
= sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
115 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
117 EXPECT_EQ(CREATE_COMMAND_BUFFER_FAILED
, base::get
<0>(result
));
119 sink()->ClearMessages();
121 stub
= channel
->LookupCommandBuffer(kRouteId2
);
125 TEST_F(GpuChannelTest
, IncompatibleStreamPriorities
) {
127 uint64 kClientTracingId
= 1;
129 ASSERT_TRUE(channel_manager());
131 EXPECT_TRUE(channel_manager()->OnMessageReceived(GpuMsg_EstablishChannel(
132 kClientId
, kClientTracingId
, false, false, false)));
133 GpuChannel
* channel
= channel_manager()->LookupChannel(kClientId
);
134 ASSERT_TRUE(channel
);
136 // Create first context.
137 int32 kSurfaceId1
= 1;
139 int32 kStreamId1
= 1;
140 GpuStreamPriority kStreamPriority1
= GpuStreamPriority::NORMAL
;
141 GPUCreateCommandBufferConfig init_params
;
142 init_params
.share_group_id
= MSG_ROUTING_NONE
;
143 init_params
.stream_id
= kStreamId1
;
144 init_params
.stream_priority
= kStreamPriority1
;
145 init_params
.attribs
= std::vector
<int>();
146 init_params
.active_url
= GURL();
147 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
148 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
149 gfx::GLSurfaceHandle(), kSurfaceId1
, kClientId
, init_params
, kRouteId1
));
151 const IPC::Message
* msg
=
152 sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
155 base::Tuple
<CreateCommandBufferResult
> result
;
156 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
158 EXPECT_EQ(CREATE_COMMAND_BUFFER_SUCCEEDED
, base::get
<0>(result
));
160 sink()->ClearMessages();
162 GpuCommandBufferStub
* stub
= channel
->LookupCommandBuffer(kRouteId1
);
165 // Create second context in same share group but different stream.
166 int32 kSurfaceId2
= 2;
168 int32 kStreamId2
= kStreamId1
;
169 GpuStreamPriority kStreamPriority2
= GpuStreamPriority::LOW
;
171 init_params
.share_group_id
= MSG_ROUTING_NONE
;
172 init_params
.stream_id
= kStreamId2
;
173 init_params
.stream_priority
= kStreamPriority2
;
174 init_params
.attribs
= std::vector
<int>();
175 init_params
.active_url
= GURL();
176 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
177 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
178 gfx::GLSurfaceHandle(), kSurfaceId2
, kClientId
, init_params
, kRouteId2
));
180 msg
= sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
183 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
185 EXPECT_EQ(CREATE_COMMAND_BUFFER_FAILED
, base::get
<0>(result
));
187 sink()->ClearMessages();
189 stub
= channel
->LookupCommandBuffer(kRouteId2
);
193 TEST_F(GpuChannelTest
, StreamLifetime
) {
195 uint64 kClientTracingId
= 1;
197 ASSERT_TRUE(channel_manager());
199 EXPECT_TRUE(channel_manager()->OnMessageReceived(GpuMsg_EstablishChannel(
200 kClientId
, kClientTracingId
, false, false, false)));
201 GpuChannel
* channel
= channel_manager()->LookupChannel(kClientId
);
202 ASSERT_TRUE(channel
);
204 // Create first context.
205 int32 kSurfaceId1
= 1;
207 int32 kStreamId1
= 1;
208 GpuStreamPriority kStreamPriority1
= GpuStreamPriority::NORMAL
;
209 GPUCreateCommandBufferConfig init_params
;
210 init_params
.share_group_id
= MSG_ROUTING_NONE
;
211 init_params
.stream_id
= kStreamId1
;
212 init_params
.stream_priority
= kStreamPriority1
;
213 init_params
.attribs
= std::vector
<int>();
214 init_params
.active_url
= GURL();
215 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
216 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
217 gfx::GLSurfaceHandle(), kSurfaceId1
, kClientId
, init_params
, kRouteId1
));
219 const IPC::Message
* msg
=
220 sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
223 base::Tuple
<CreateCommandBufferResult
> result
;
224 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
226 EXPECT_EQ(CREATE_COMMAND_BUFFER_SUCCEEDED
, base::get
<0>(result
));
228 sink()->ClearMessages();
230 GpuCommandBufferStub
* stub
= channel
->LookupCommandBuffer(kRouteId1
);
234 // GpuChannelHost always calls set_unblock(false) on messages sent to the
236 IPC::Message m
= GpuChannelMsg_DestroyCommandBuffer(kRouteId1
);
237 m
.set_unblock(false);
238 EXPECT_TRUE(channel
->filter()->OnMessageReceived(m
));
239 task_runner()->RunPendingTasks();
242 stub
= channel
->LookupCommandBuffer(kRouteId1
);
245 // Create second context in same share group but different stream.
246 int32 kSurfaceId2
= 2;
248 int32 kStreamId2
= 2;
249 GpuStreamPriority kStreamPriority2
= GpuStreamPriority::LOW
;
251 init_params
.share_group_id
= MSG_ROUTING_NONE
;
252 init_params
.stream_id
= kStreamId2
;
253 init_params
.stream_priority
= kStreamPriority2
;
254 init_params
.attribs
= std::vector
<int>();
255 init_params
.active_url
= GURL();
256 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
257 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
258 gfx::GLSurfaceHandle(), kSurfaceId2
, kClientId
, init_params
, kRouteId2
));
260 msg
= sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
263 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
265 EXPECT_EQ(CREATE_COMMAND_BUFFER_SUCCEEDED
, base::get
<0>(result
));
267 sink()->ClearMessages();
269 stub
= channel
->LookupCommandBuffer(kRouteId2
);
273 TEST_F(GpuChannelTest
, RealTimeStreamsDisallowed
) {
275 uint64 kClientTracingId
= 1;
276 bool allow_real_time_streams
= false;
278 ASSERT_TRUE(channel_manager());
280 EXPECT_TRUE(channel_manager()->OnMessageReceived(GpuMsg_EstablishChannel(
281 kClientId
, kClientTracingId
, false, false, allow_real_time_streams
)));
282 GpuChannel
* channel
= channel_manager()->LookupChannel(kClientId
);
283 ASSERT_TRUE(channel
);
285 // Create first context.
286 int32 kSurfaceId
= 1;
289 GpuStreamPriority kStreamPriority
= GpuStreamPriority::REAL_TIME
;
290 GPUCreateCommandBufferConfig init_params
;
291 init_params
.share_group_id
= MSG_ROUTING_NONE
;
292 init_params
.stream_id
= kStreamId
;
293 init_params
.stream_priority
= kStreamPriority
;
294 init_params
.attribs
= std::vector
<int>();
295 init_params
.active_url
= GURL();
296 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
297 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
298 gfx::GLSurfaceHandle(), kSurfaceId
, kClientId
, init_params
, kRouteId
));
300 const IPC::Message
* msg
=
301 sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
304 base::Tuple
<CreateCommandBufferResult
> result
;
305 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
307 EXPECT_EQ(CREATE_COMMAND_BUFFER_FAILED
, base::get
<0>(result
));
309 sink()->ClearMessages();
311 GpuCommandBufferStub
* stub
= channel
->LookupCommandBuffer(kRouteId
);
315 TEST_F(GpuChannelTest
, RealTimeStreamsAllowed
) {
317 uint64 kClientTracingId
= 1;
319 ASSERT_TRUE(channel_manager());
321 bool allow_real_time_streams
= true;
322 EXPECT_TRUE(channel_manager()->OnMessageReceived(GpuMsg_EstablishChannel(
323 kClientId
, kClientTracingId
, false, false, allow_real_time_streams
)));
324 GpuChannel
* channel
= channel_manager()->LookupChannel(kClientId
);
325 ASSERT_TRUE(channel
);
327 // Create first context.
328 int32 kSurfaceId
= 1;
331 GpuStreamPriority kStreamPriority
= GpuStreamPriority::REAL_TIME
;
332 GPUCreateCommandBufferConfig init_params
;
333 init_params
.share_group_id
= MSG_ROUTING_NONE
;
334 init_params
.stream_id
= kStreamId
;
335 init_params
.stream_priority
= kStreamPriority
;
336 init_params
.attribs
= std::vector
<int>();
337 init_params
.active_url
= GURL();
338 init_params
.gpu_preference
= gfx::PreferIntegratedGpu
;
339 channel_manager()->OnMessageReceived(GpuMsg_CreateViewCommandBuffer(
340 gfx::GLSurfaceHandle(), kSurfaceId
, kClientId
, init_params
, kRouteId
));
342 const IPC::Message
* msg
=
343 sink()->GetUniqueMessageMatching(GpuHostMsg_CommandBufferCreated::ID
);
346 base::Tuple
<CreateCommandBufferResult
> result
;
347 ASSERT_TRUE(GpuHostMsg_CommandBufferCreated::Read(msg
, &result
));
349 EXPECT_EQ(CREATE_COMMAND_BUFFER_SUCCEEDED
, base::get
<0>(result
));
351 sink()->ClearMessages();
353 GpuCommandBufferStub
* stub
= channel
->LookupCommandBuffer(kRouteId
);
357 } // namespace content