1 // Copyright 2013 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 "cc/raster/tile_task_worker_pool.h"
7 #include "base/test/test_simple_task_runner.h"
8 #include "base/time/time.h"
9 #include "cc/debug/lap_timer.h"
10 #include "cc/output/context_provider.h"
11 #include "cc/raster/bitmap_tile_task_worker_pool.h"
12 #include "cc/raster/gpu_rasterizer.h"
13 #include "cc/raster/gpu_tile_task_worker_pool.h"
14 #include "cc/raster/one_copy_tile_task_worker_pool.h"
15 #include "cc/raster/pixel_buffer_tile_task_worker_pool.h"
16 #include "cc/raster/raster_buffer.h"
17 #include "cc/raster/tile_task_runner.h"
18 #include "cc/raster/zero_copy_tile_task_worker_pool.h"
19 #include "cc/resources/resource_pool.h"
20 #include "cc/resources/resource_provider.h"
21 #include "cc/resources/scoped_resource.h"
22 #include "cc/test/fake_output_surface.h"
23 #include "cc/test/fake_output_surface_client.h"
24 #include "cc/test/fake_resource_provider.h"
25 #include "cc/test/test_context_support.h"
26 #include "cc/test/test_gpu_memory_buffer_manager.h"
27 #include "cc/test/test_shared_bitmap_manager.h"
28 #include "cc/test/test_web_graphics_context_3d.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "testing/perf/perf_test.h"
31 #include "third_party/khronos/GLES2/gl2.h"
32 #include "third_party/skia/include/gpu/GrContext.h"
33 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
38 class PerfGLES2Interface
: public gpu::gles2::GLES2InterfaceStub
{
39 // Overridden from gpu::gles2::GLES2Interface:
40 GLuint
CreateImageCHROMIUM(ClientBuffer buffer
,
43 GLenum internalformat
) override
{
46 void GenBuffers(GLsizei n
, GLuint
* buffers
) override
{
47 for (GLsizei i
= 0; i
< n
; ++i
)
50 void GenTextures(GLsizei n
, GLuint
* textures
) override
{
51 for (GLsizei i
= 0; i
< n
; ++i
)
54 void GetIntegerv(GLenum pname
, GLint
* params
) override
{
55 if (pname
== GL_MAX_TEXTURE_SIZE
)
58 void GenQueriesEXT(GLsizei n
, GLuint
* queries
) override
{
59 for (GLsizei i
= 0; i
< n
; ++i
)
62 void GetQueryObjectuivEXT(GLuint query
,
64 GLuint
* params
) override
{
65 if (pname
== GL_QUERY_RESULT_AVAILABLE_EXT
)
70 class PerfContextProvider
: public ContextProvider
{
72 PerfContextProvider() : context_gl_(new PerfGLES2Interface
) {}
74 bool BindToCurrentThread() override
{ return true; }
75 Capabilities
ContextCapabilities() override
{
76 Capabilities capabilities
;
77 capabilities
.gpu
.image
= true;
78 capabilities
.gpu
.sync_query
= true;
81 gpu::gles2::GLES2Interface
* ContextGL() override
{ return context_gl_
.get(); }
82 gpu::ContextSupport
* ContextSupport() override
{ return &support_
; }
83 class GrContext
* GrContext() override
{
85 return gr_context_
.get();
87 skia::RefPtr
<const GrGLInterface
> null_interface
=
88 skia::AdoptRef(GrGLCreateNullInterface());
89 gr_context_
= skia::AdoptRef(GrContext::Create(
91 reinterpret_cast<GrBackendContext
>(null_interface
.get())));
92 return gr_context_
.get();
94 void InvalidateGrContext(uint32_t state
) override
{
96 gr_context_
.get()->resetContext(state
);
98 void SetupLock() override
{}
99 base::Lock
* GetLock() override
{ return &context_lock_
; }
100 void VerifyContexts() override
{}
101 void DeleteCachedResources() override
{}
102 bool DestroyedOnMainThread() override
{ return false; }
103 void SetLostContextCallback(const LostContextCallback
& cb
) override
{}
104 void SetMemoryPolicyChangedCallback(
105 const MemoryPolicyChangedCallback
& cb
) override
{}
108 ~PerfContextProvider() override
{}
110 scoped_ptr
<PerfGLES2Interface
> context_gl_
;
111 skia::RefPtr
<class GrContext
> gr_context_
;
112 TestContextSupport support_
;
113 base::Lock context_lock_
;
116 enum TileTaskWorkerPoolType
{
117 TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER
,
118 TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY
,
119 TILE_TASK_WORKER_POOL_TYPE_ONE_COPY
,
120 TILE_TASK_WORKER_POOL_TYPE_GPU
,
121 TILE_TASK_WORKER_POOL_TYPE_BITMAP
124 static const int kTimeLimitMillis
= 2000;
125 static const int kWarmupRuns
= 5;
126 static const int kTimeCheckInterval
= 10;
128 class PerfImageDecodeTaskImpl
: public ImageDecodeTask
{
130 PerfImageDecodeTaskImpl() {}
132 // Overridden from Task:
133 void RunOnWorkerThread() override
{}
135 // Overridden from TileTask:
136 void ScheduleOnOriginThread(TileTaskClient
* client
) override
{}
137 void CompleteOnOriginThread(TileTaskClient
* client
) override
{}
138 void RunReplyOnOriginThread() override
{ Reset(); }
142 did_complete_
= false;
146 ~PerfImageDecodeTaskImpl() override
{}
149 DISALLOW_COPY_AND_ASSIGN(PerfImageDecodeTaskImpl
);
152 class PerfRasterTaskImpl
: public RasterTask
{
154 PerfRasterTaskImpl(scoped_ptr
<ScopedResource
> resource
,
155 ImageDecodeTask::Vector
* dependencies
)
156 : RasterTask(resource
.get(), dependencies
), resource_(resource
.Pass()) {}
158 // Overridden from Task:
159 void RunOnWorkerThread() override
{}
161 // Overridden from TileTask:
162 void ScheduleOnOriginThread(TileTaskClient
* client
) override
{
163 // No tile ids are given to support partial updates.
164 raster_buffer_
= client
->AcquireBufferForRaster(resource(), 0, 0);
166 void CompleteOnOriginThread(TileTaskClient
* client
) override
{
167 client
->ReleaseBufferForRaster(raster_buffer_
.Pass());
169 void RunReplyOnOriginThread() override
{ Reset(); }
173 did_complete_
= false;
177 ~PerfRasterTaskImpl() override
{}
180 scoped_ptr
<ScopedResource
> resource_
;
181 scoped_ptr
<RasterBuffer
> raster_buffer_
;
183 DISALLOW_COPY_AND_ASSIGN(PerfRasterTaskImpl
);
186 class TileTaskWorkerPoolPerfTestBase
{
188 typedef std::vector
<scoped_refptr
<RasterTask
>> RasterTaskVector
;
190 enum NamedTaskSet
{ REQUIRED_FOR_ACTIVATION
, REQUIRED_FOR_DRAW
, ALL
};
192 TileTaskWorkerPoolPerfTestBase()
193 : context_provider_(make_scoped_refptr(new PerfContextProvider
)),
194 task_runner_(new base::TestSimpleTaskRunner
),
195 task_graph_runner_(new TaskGraphRunner
),
197 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
198 kTimeCheckInterval
) {}
200 void CreateImageDecodeTasks(unsigned num_image_decode_tasks
,
201 ImageDecodeTask::Vector
* image_decode_tasks
) {
202 for (unsigned i
= 0; i
< num_image_decode_tasks
; ++i
)
203 image_decode_tasks
->push_back(new PerfImageDecodeTaskImpl
);
206 void CreateRasterTasks(unsigned num_raster_tasks
,
207 const ImageDecodeTask::Vector
& image_decode_tasks
,
208 RasterTaskVector
* raster_tasks
) {
209 const gfx::Size
size(1, 1);
211 for (unsigned i
= 0; i
< num_raster_tasks
; ++i
) {
212 scoped_ptr
<ScopedResource
> resource(
213 ScopedResource::Create(resource_provider_
.get()));
214 resource
->Allocate(size
, ResourceProvider::TEXTURE_HINT_IMMUTABLE
,
217 ImageDecodeTask::Vector dependencies
= image_decode_tasks
;
218 raster_tasks
->push_back(
219 new PerfRasterTaskImpl(resource
.Pass(), &dependencies
));
223 void BuildTileTaskQueue(TileTaskQueue
* queue
,
224 const RasterTaskVector
& raster_tasks
) {
225 for (size_t i
= 0u; i
< raster_tasks
.size(); ++i
) {
226 bool required_for_activation
= (i
% 2) == 0;
227 TaskSetCollection task_set_collection
;
228 task_set_collection
[ALL
] = true;
229 task_set_collection
[REQUIRED_FOR_ACTIVATION
] = required_for_activation
;
230 queue
->items
.push_back(
231 TileTaskQueue::Item(raster_tasks
[i
].get(), task_set_collection
));
236 scoped_refptr
<ContextProvider
> context_provider_
;
237 FakeOutputSurfaceClient output_surface_client_
;
238 scoped_ptr
<FakeOutputSurface
> output_surface_
;
239 scoped_ptr
<ResourceProvider
> resource_provider_
;
240 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
241 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
245 class TileTaskWorkerPoolPerfTest
246 : public TileTaskWorkerPoolPerfTestBase
,
247 public testing::TestWithParam
<TileTaskWorkerPoolType
>,
248 public TileTaskRunnerClient
{
250 // Overridden from testing::Test:
251 void SetUp() override
{
252 switch (GetParam()) {
253 case TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER
:
254 Create3dOutputSurfaceAndResourceProvider();
255 tile_task_worker_pool_
= PixelBufferTileTaskWorkerPool::Create(
256 task_runner_
.get(), task_graph_runner_
.get(),
257 context_provider_
.get(), resource_provider_
.get(),
258 std::numeric_limits
<size_t>::max());
260 case TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY
:
261 Create3dOutputSurfaceAndResourceProvider();
262 tile_task_worker_pool_
= ZeroCopyTileTaskWorkerPool::Create(
263 task_runner_
.get(), task_graph_runner_
.get(),
264 resource_provider_
.get());
266 case TILE_TASK_WORKER_POOL_TYPE_ONE_COPY
:
267 Create3dOutputSurfaceAndResourceProvider();
268 tile_task_worker_pool_
= OneCopyTileTaskWorkerPool::Create(
269 task_runner_
.get(), task_graph_runner_
.get(),
270 context_provider_
.get(), resource_provider_
.get(),
271 std::numeric_limits
<int>::max(), false,
272 std::numeric_limits
<int>::max());
274 case TILE_TASK_WORKER_POOL_TYPE_GPU
:
275 Create3dOutputSurfaceAndResourceProvider();
276 tile_task_worker_pool_
= GpuTileTaskWorkerPool::Create(
277 task_runner_
.get(), task_graph_runner_
.get(),
278 context_provider_
.get(), resource_provider_
.get(), false, 0);
280 case TILE_TASK_WORKER_POOL_TYPE_BITMAP
:
281 CreateSoftwareOutputSurfaceAndResourceProvider();
282 tile_task_worker_pool_
= BitmapTileTaskWorkerPool::Create(
283 task_runner_
.get(), task_graph_runner_
.get(),
284 resource_provider_
.get());
288 DCHECK(tile_task_worker_pool_
);
289 tile_task_worker_pool_
->AsTileTaskRunner()->SetClient(this);
291 void TearDown() override
{
292 tile_task_worker_pool_
->AsTileTaskRunner()->Shutdown();
293 tile_task_worker_pool_
->AsTileTaskRunner()->CheckForCompletedTasks();
296 // Overriden from TileTaskRunnerClient:
297 void DidFinishRunningTileTasks(TaskSet task_set
) override
{
298 tile_task_worker_pool_
->AsTileTaskRunner()->CheckForCompletedTasks();
300 TaskSetCollection
TasksThatShouldBeForcedToComplete() const override
{
301 return TaskSetCollection();
304 void RunMessageLoopUntilAllTasksHaveCompleted() {
305 task_graph_runner_
->RunUntilIdle();
306 task_runner_
->RunUntilIdle();
309 void RunScheduleTasksTest(const std::string
& test_name
,
310 unsigned num_raster_tasks
,
311 unsigned num_image_decode_tasks
) {
312 ImageDecodeTask::Vector image_decode_tasks
;
313 RasterTaskVector raster_tasks
;
314 CreateImageDecodeTasks(num_image_decode_tasks
, &image_decode_tasks
);
315 CreateRasterTasks(num_raster_tasks
, image_decode_tasks
, &raster_tasks
);
317 // Avoid unnecessary heap allocations by reusing the same queue.
323 BuildTileTaskQueue(&queue
, raster_tasks
);
324 tile_task_worker_pool_
->AsTileTaskRunner()->ScheduleTasks(&queue
);
325 tile_task_worker_pool_
->AsTileTaskRunner()->CheckForCompletedTasks();
327 } while (!timer_
.HasTimeLimitExpired());
330 tile_task_worker_pool_
->AsTileTaskRunner()->ScheduleTasks(&empty
);
331 RunMessageLoopUntilAllTasksHaveCompleted();
333 perf_test::PrintResult("schedule_tasks", TestModifierString(), test_name
,
334 timer_
.LapsPerSecond(), "runs/s", true);
337 void RunScheduleAlternateTasksTest(const std::string
& test_name
,
338 unsigned num_raster_tasks
,
339 unsigned num_image_decode_tasks
) {
340 const size_t kNumVersions
= 2;
341 ImageDecodeTask::Vector image_decode_tasks
[kNumVersions
];
342 RasterTaskVector raster_tasks
[kNumVersions
];
343 for (size_t i
= 0; i
< kNumVersions
; ++i
) {
344 CreateImageDecodeTasks(num_image_decode_tasks
, &image_decode_tasks
[i
]);
345 CreateRasterTasks(num_raster_tasks
, image_decode_tasks
[i
],
349 // Avoid unnecessary heap allocations by reusing the same queue.
356 BuildTileTaskQueue(&queue
, raster_tasks
[count
% kNumVersions
]);
357 tile_task_worker_pool_
->AsTileTaskRunner()->ScheduleTasks(&queue
);
358 tile_task_worker_pool_
->AsTileTaskRunner()->CheckForCompletedTasks();
361 } while (!timer_
.HasTimeLimitExpired());
364 tile_task_worker_pool_
->AsTileTaskRunner()->ScheduleTasks(&empty
);
365 RunMessageLoopUntilAllTasksHaveCompleted();
367 perf_test::PrintResult("schedule_alternate_tasks", TestModifierString(),
368 test_name
, timer_
.LapsPerSecond(), "runs/s", true);
371 void RunScheduleAndExecuteTasksTest(const std::string
& test_name
,
372 unsigned num_raster_tasks
,
373 unsigned num_image_decode_tasks
) {
374 ImageDecodeTask::Vector image_decode_tasks
;
375 RasterTaskVector raster_tasks
;
376 CreateImageDecodeTasks(num_image_decode_tasks
, &image_decode_tasks
);
377 CreateRasterTasks(num_raster_tasks
, image_decode_tasks
, &raster_tasks
);
379 // Avoid unnecessary heap allocations by reusing the same queue.
385 BuildTileTaskQueue(&queue
, raster_tasks
);
386 tile_task_worker_pool_
->AsTileTaskRunner()->ScheduleTasks(&queue
);
387 RunMessageLoopUntilAllTasksHaveCompleted();
389 } while (!timer_
.HasTimeLimitExpired());
392 tile_task_worker_pool_
->AsTileTaskRunner()->ScheduleTasks(&empty
);
393 RunMessageLoopUntilAllTasksHaveCompleted();
395 perf_test::PrintResult("schedule_and_execute_tasks", TestModifierString(),
396 test_name
, timer_
.LapsPerSecond(), "runs/s", true);
400 void Create3dOutputSurfaceAndResourceProvider() {
401 output_surface_
= FakeOutputSurface::Create3d(context_provider_
).Pass();
402 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
403 resource_provider_
= FakeResourceProvider::Create(
404 output_surface_
.get(), nullptr, &gpu_memory_buffer_manager_
);
407 void CreateSoftwareOutputSurfaceAndResourceProvider() {
408 output_surface_
= FakeOutputSurface::CreateSoftware(
409 make_scoped_ptr(new SoftwareOutputDevice
));
410 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
411 resource_provider_
= FakeResourceProvider::Create(
412 output_surface_
.get(), &shared_bitmap_manager_
, nullptr);
415 std::string
TestModifierString() const {
416 switch (GetParam()) {
417 case TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER
:
418 return std::string("_pixel_tile_task_worker_pool");
419 case TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY
:
420 return std::string("_zero_copy_tile_task_worker_pool");
421 case TILE_TASK_WORKER_POOL_TYPE_ONE_COPY
:
422 return std::string("_one_copy_tile_task_worker_pool");
423 case TILE_TASK_WORKER_POOL_TYPE_GPU
:
424 return std::string("_gpu_tile_task_worker_pool");
425 case TILE_TASK_WORKER_POOL_TYPE_BITMAP
:
426 return std::string("_bitmap_tile_task_worker_pool");
429 return std::string();
432 scoped_ptr
<TileTaskWorkerPool
> tile_task_worker_pool_
;
433 TestGpuMemoryBufferManager gpu_memory_buffer_manager_
;
434 TestSharedBitmapManager shared_bitmap_manager_
;
437 TEST_P(TileTaskWorkerPoolPerfTest
, ScheduleTasks
) {
438 RunScheduleTasksTest("1_0", 1, 0);
439 RunScheduleTasksTest("32_0", 32, 0);
440 RunScheduleTasksTest("1_1", 1, 1);
441 RunScheduleTasksTest("32_1", 32, 1);
442 RunScheduleTasksTest("1_4", 1, 4);
443 RunScheduleTasksTest("32_4", 32, 4);
446 TEST_P(TileTaskWorkerPoolPerfTest
, ScheduleAlternateTasks
) {
447 RunScheduleAlternateTasksTest("1_0", 1, 0);
448 RunScheduleAlternateTasksTest("32_0", 32, 0);
449 RunScheduleAlternateTasksTest("1_1", 1, 1);
450 RunScheduleAlternateTasksTest("32_1", 32, 1);
451 RunScheduleAlternateTasksTest("1_4", 1, 4);
452 RunScheduleAlternateTasksTest("32_4", 32, 4);
455 TEST_P(TileTaskWorkerPoolPerfTest
, ScheduleAndExecuteTasks
) {
456 RunScheduleAndExecuteTasksTest("1_0", 1, 0);
457 RunScheduleAndExecuteTasksTest("32_0", 32, 0);
458 RunScheduleAndExecuteTasksTest("1_1", 1, 1);
459 RunScheduleAndExecuteTasksTest("32_1", 32, 1);
460 RunScheduleAndExecuteTasksTest("1_4", 1, 4);
461 RunScheduleAndExecuteTasksTest("32_4", 32, 4);
464 INSTANTIATE_TEST_CASE_P(
465 TileTaskWorkerPoolPerfTests
,
466 TileTaskWorkerPoolPerfTest
,
467 ::testing::Values(TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER
,
468 TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY
,
469 TILE_TASK_WORKER_POOL_TYPE_ONE_COPY
,
470 TILE_TASK_WORKER_POOL_TYPE_GPU
,
471 TILE_TASK_WORKER_POOL_TYPE_BITMAP
));
473 class TileTaskWorkerPoolCommonPerfTest
: public TileTaskWorkerPoolPerfTestBase
,
474 public testing::Test
{
476 // Overridden from testing::Test:
477 void SetUp() override
{
478 output_surface_
= FakeOutputSurface::Create3d(context_provider_
).Pass();
479 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
481 FakeResourceProvider::Create(output_surface_
.get(), nullptr);
484 void RunBuildTileTaskQueueTest(const std::string
& test_name
,
485 unsigned num_raster_tasks
,
486 unsigned num_image_decode_tasks
) {
487 ImageDecodeTask::Vector image_decode_tasks
;
488 RasterTaskVector raster_tasks
;
489 CreateImageDecodeTasks(num_image_decode_tasks
, &image_decode_tasks
);
490 CreateRasterTasks(num_raster_tasks
, image_decode_tasks
, &raster_tasks
);
492 // Avoid unnecessary heap allocations by reusing the same queue.
498 BuildTileTaskQueue(&queue
, raster_tasks
);
500 } while (!timer_
.HasTimeLimitExpired());
502 perf_test::PrintResult("build_raster_task_queue", "", test_name
,
503 timer_
.LapsPerSecond(), "runs/s", true);
507 TEST_F(TileTaskWorkerPoolCommonPerfTest
, BuildTileTaskQueue
) {
508 RunBuildTileTaskQueueTest("1_0", 1, 0);
509 RunBuildTileTaskQueueTest("32_0", 32, 0);
510 RunBuildTileTaskQueueTest("1_1", 1, 1);
511 RunBuildTileTaskQueueTest("32_1", 32, 1);
512 RunBuildTileTaskQueueTest("1_4", 1, 4);
513 RunBuildTileTaskQueueTest("32_4", 32, 4);