| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "cc/resources/tile_task_worker_pool.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/cancelable_callback.h" | |
| 11 #include "cc/base/unique_notifier.h" | |
| 12 #include "cc/resources/bitmap_tile_task_worker_pool.h" | |
| 13 #include "cc/resources/gpu_rasterizer.h" | |
| 14 #include "cc/resources/gpu_tile_task_worker_pool.h" | |
| 15 #include "cc/resources/one_copy_tile_task_worker_pool.h" | |
| 16 #include "cc/resources/picture_pile.h" | |
| 17 #include "cc/resources/picture_pile_impl.h" | |
| 18 #include "cc/resources/pixel_buffer_tile_task_worker_pool.h" | |
| 19 #include "cc/resources/raster_buffer.h" | |
| 20 #include "cc/resources/resource_pool.h" | |
| 21 #include "cc/resources/resource_provider.h" | |
| 22 #include "cc/resources/scoped_resource.h" | |
| 23 #include "cc/resources/tile_task_runner.h" | |
| 24 #include "cc/resources/zero_copy_tile_task_worker_pool.h" | |
| 25 #include "cc/test/fake_output_surface.h" | |
| 26 #include "cc/test/fake_output_surface_client.h" | |
| 27 #include "cc/test/fake_picture_pile_impl.h" | |
| 28 #include "cc/test/test_gpu_memory_buffer_manager.h" | |
| 29 #include "cc/test/test_shared_bitmap_manager.h" | |
| 30 #include "cc/test/test_task_graph_runner.h" | |
| 31 #include "cc/test/test_web_graphics_context_3d.h" | |
| 32 #include "gpu/GLES2/gl2extchromium.h" | |
| 33 #include "testing/gtest/include/gtest/gtest.h" | |
| 34 | |
| 35 namespace cc { | |
| 36 namespace { | |
| 37 | |
| 38 const size_t kMaxTransferBufferUsageBytes = 10000U; | |
| 39 // A resource of this dimension^2 * 4 must be greater than the above transfer | |
| 40 // buffer constant. | |
| 41 const size_t kLargeResourceDimension = 1000U; | |
| 42 | |
| 43 enum TileTaskWorkerPoolType { | |
| 44 TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER, | |
| 45 TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY, | |
| 46 TILE_TASK_WORKER_POOL_TYPE_ONE_COPY, | |
| 47 TILE_TASK_WORKER_POOL_TYPE_GPU, | |
| 48 TILE_TASK_WORKER_POOL_TYPE_BITMAP | |
| 49 }; | |
| 50 | |
| 51 class TestRasterTaskImpl : public RasterTask { | |
| 52 public: | |
| 53 typedef base::Callback<void(const RasterSource::SolidColorAnalysis& analysis, | |
| 54 bool was_canceled)> Reply; | |
| 55 | |
| 56 TestRasterTaskImpl(const Resource* resource, | |
| 57 const Reply& reply, | |
| 58 ImageDecodeTask::Vector* dependencies) | |
| 59 : RasterTask(resource, dependencies), | |
| 60 reply_(reply), | |
| 61 picture_pile_(FakePicturePileImpl::CreateEmptyPile(gfx::Size(1, 1), | |
| 62 gfx::Size(1, 1))) {} | |
| 63 | |
| 64 // Overridden from Task: | |
| 65 void RunOnWorkerThread() override { | |
| 66 raster_buffer_->Playback(picture_pile_.get(), gfx::Rect(0, 0, 1, 1), 1.0); | |
| 67 } | |
| 68 | |
| 69 // Overridden from TileTask: | |
| 70 void ScheduleOnOriginThread(TileTaskClient* client) override { | |
| 71 raster_buffer_ = client->AcquireBufferForRaster(resource()); | |
| 72 } | |
| 73 void CompleteOnOriginThread(TileTaskClient* client) override { | |
| 74 client->ReleaseBufferForRaster(raster_buffer_.Pass()); | |
| 75 } | |
| 76 void RunReplyOnOriginThread() override { | |
| 77 reply_.Run(RasterSource::SolidColorAnalysis(), !HasFinishedRunning()); | |
| 78 } | |
| 79 | |
| 80 protected: | |
| 81 ~TestRasterTaskImpl() override {} | |
| 82 | |
| 83 private: | |
| 84 const Reply reply_; | |
| 85 scoped_ptr<RasterBuffer> raster_buffer_; | |
| 86 scoped_refptr<PicturePileImpl> picture_pile_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(TestRasterTaskImpl); | |
| 89 }; | |
| 90 | |
| 91 class BlockingTestRasterTaskImpl : public TestRasterTaskImpl { | |
| 92 public: | |
| 93 BlockingTestRasterTaskImpl(const Resource* resource, | |
| 94 const Reply& reply, | |
| 95 base::Lock* lock, | |
| 96 ImageDecodeTask::Vector* dependencies) | |
| 97 : TestRasterTaskImpl(resource, reply, dependencies), lock_(lock) {} | |
| 98 | |
| 99 // Overridden from Task: | |
| 100 void RunOnWorkerThread() override { | |
| 101 base::AutoLock lock(*lock_); | |
| 102 TestRasterTaskImpl::RunOnWorkerThread(); | |
| 103 } | |
| 104 | |
| 105 // Overridden from TileTask: | |
| 106 void RunReplyOnOriginThread() override {} | |
| 107 | |
| 108 protected: | |
| 109 ~BlockingTestRasterTaskImpl() override {} | |
| 110 | |
| 111 private: | |
| 112 base::Lock* lock_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(BlockingTestRasterTaskImpl); | |
| 115 }; | |
| 116 | |
| 117 class TileTaskWorkerPoolTest | |
| 118 : public testing::TestWithParam<TileTaskWorkerPoolType>, | |
| 119 public TileTaskRunnerClient { | |
| 120 public: | |
| 121 struct RasterTaskResult { | |
| 122 unsigned id; | |
| 123 bool canceled; | |
| 124 }; | |
| 125 | |
| 126 typedef std::vector<scoped_refptr<RasterTask>> RasterTaskVector; | |
| 127 | |
| 128 enum NamedTaskSet { REQUIRED_FOR_ACTIVATION, REQUIRED_FOR_DRAW, ALL }; | |
| 129 | |
| 130 TileTaskWorkerPoolTest() | |
| 131 : context_provider_(TestContextProvider::Create()), | |
| 132 worker_context_provider_(TestContextProvider::Create()), | |
| 133 all_tile_tasks_finished_( | |
| 134 base::MessageLoopProxy::current().get(), | |
| 135 base::Bind(&TileTaskWorkerPoolTest::AllTileTasksFinished, | |
| 136 base::Unretained(this))), | |
| 137 timeout_seconds_(5), | |
| 138 timed_out_(false) {} | |
| 139 | |
| 140 // Overridden from testing::Test: | |
| 141 void SetUp() override { | |
| 142 switch (GetParam()) { | |
| 143 case TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER: | |
| 144 Create3dOutputSurfaceAndResourceProvider(); | |
| 145 tile_task_worker_pool_ = PixelBufferTileTaskWorkerPool::Create( | |
| 146 base::MessageLoopProxy::current().get(), &task_graph_runner_, | |
| 147 context_provider_.get(), resource_provider_.get(), | |
| 148 kMaxTransferBufferUsageBytes); | |
| 149 break; | |
| 150 case TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY: | |
| 151 Create3dOutputSurfaceAndResourceProvider(); | |
| 152 tile_task_worker_pool_ = ZeroCopyTileTaskWorkerPool::Create( | |
| 153 base::MessageLoopProxy::current().get(), &task_graph_runner_, | |
| 154 resource_provider_.get()); | |
| 155 break; | |
| 156 case TILE_TASK_WORKER_POOL_TYPE_ONE_COPY: | |
| 157 Create3dOutputSurfaceAndResourceProvider(); | |
| 158 staging_resource_pool_ = ResourcePool::Create(resource_provider_.get(), | |
| 159 GL_TEXTURE_2D); | |
| 160 tile_task_worker_pool_ = OneCopyTileTaskWorkerPool::Create( | |
| 161 base::MessageLoopProxy::current().get(), &task_graph_runner_, | |
| 162 context_provider_.get(), resource_provider_.get(), | |
| 163 staging_resource_pool_.get()); | |
| 164 break; | |
| 165 case TILE_TASK_WORKER_POOL_TYPE_GPU: | |
| 166 Create3dOutputSurfaceAndResourceProvider(); | |
| 167 rasterizer_ = GpuRasterizer::Create( | |
| 168 context_provider_.get(), resource_provider_.get(), false, false, 0); | |
| 169 tile_task_worker_pool_ = GpuTileTaskWorkerPool::Create( | |
| 170 base::MessageLoopProxy::current().get(), &task_graph_runner_, | |
| 171 static_cast<GpuRasterizer*>(rasterizer_.get())); | |
| 172 break; | |
| 173 case TILE_TASK_WORKER_POOL_TYPE_BITMAP: | |
| 174 CreateSoftwareOutputSurfaceAndResourceProvider(); | |
| 175 tile_task_worker_pool_ = BitmapTileTaskWorkerPool::Create( | |
| 176 base::MessageLoopProxy::current().get(), &task_graph_runner_, | |
| 177 resource_provider_.get()); | |
| 178 break; | |
| 179 } | |
| 180 | |
| 181 DCHECK(tile_task_worker_pool_); | |
| 182 tile_task_worker_pool_->AsTileTaskRunner()->SetClient(this); | |
| 183 } | |
| 184 | |
| 185 void TearDown() override { | |
| 186 tile_task_worker_pool_->AsTileTaskRunner()->Shutdown(); | |
| 187 tile_task_worker_pool_->AsTileTaskRunner()->CheckForCompletedTasks(); | |
| 188 } | |
| 189 | |
| 190 void AllTileTasksFinished() { | |
| 191 tile_task_worker_pool_->AsTileTaskRunner()->CheckForCompletedTasks(); | |
| 192 base::MessageLoop::current()->Quit(); | |
| 193 } | |
| 194 | |
| 195 // Overriden from TileTaskWorkerPoolClient: | |
| 196 void DidFinishRunningTileTasks(TaskSet task_set) override { | |
| 197 EXPECT_FALSE(completed_task_sets_[task_set]); | |
| 198 completed_task_sets_[task_set] = true; | |
| 199 if (task_set == ALL) { | |
| 200 EXPECT_TRUE((~completed_task_sets_).none()); | |
| 201 all_tile_tasks_finished_.Schedule(); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 TaskSetCollection TasksThatShouldBeForcedToComplete() const override { | |
| 206 return TaskSetCollection(); | |
| 207 } | |
| 208 | |
| 209 void RunMessageLoopUntilAllTasksHaveCompleted() { | |
| 210 if (timeout_seconds_) { | |
| 211 timeout_.Reset(base::Bind(&TileTaskWorkerPoolTest::OnTimeout, | |
| 212 base::Unretained(this))); | |
| 213 base::MessageLoopProxy::current()->PostDelayedTask( | |
| 214 FROM_HERE, timeout_.callback(), | |
| 215 base::TimeDelta::FromSeconds(timeout_seconds_)); | |
| 216 } | |
| 217 | |
| 218 base::MessageLoop::current()->Run(); | |
| 219 | |
| 220 timeout_.Cancel(); | |
| 221 | |
| 222 ASSERT_FALSE(timed_out_) << "Test timed out"; | |
| 223 } | |
| 224 | |
| 225 void ScheduleTasks() { | |
| 226 TileTaskQueue queue; | |
| 227 | |
| 228 for (RasterTaskVector::const_iterator it = tasks_.begin(); | |
| 229 it != tasks_.end(); ++it) { | |
| 230 TaskSetCollection task_sets; | |
| 231 task_sets[REQUIRED_FOR_ACTIVATION] = true; | |
| 232 task_sets[REQUIRED_FOR_DRAW] = true; | |
| 233 task_sets[ALL] = true; | |
| 234 queue.items.push_back(TileTaskQueue::Item(it->get(), task_sets)); | |
| 235 } | |
| 236 | |
| 237 completed_task_sets_.reset(); | |
| 238 tile_task_worker_pool_->AsTileTaskRunner()->ScheduleTasks(&queue); | |
| 239 } | |
| 240 | |
| 241 void AppendTask(unsigned id, const gfx::Size& size) { | |
| 242 scoped_ptr<ScopedResource> resource( | |
| 243 ScopedResource::Create(resource_provider_.get())); | |
| 244 resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE, | |
| 245 RGBA_8888); | |
| 246 const Resource* const_resource = resource.get(); | |
| 247 | |
| 248 ImageDecodeTask::Vector empty; | |
| 249 tasks_.push_back(new TestRasterTaskImpl( | |
| 250 const_resource, | |
| 251 base::Bind(&TileTaskWorkerPoolTest::OnTaskCompleted, | |
| 252 base::Unretained(this), base::Passed(&resource), id), | |
| 253 &empty)); | |
| 254 } | |
| 255 | |
| 256 void AppendTask(unsigned id) { AppendTask(id, gfx::Size(1, 1)); } | |
| 257 | |
| 258 void AppendBlockingTask(unsigned id, base::Lock* lock) { | |
| 259 const gfx::Size size(1, 1); | |
| 260 | |
| 261 scoped_ptr<ScopedResource> resource( | |
| 262 ScopedResource::Create(resource_provider_.get())); | |
| 263 resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE, | |
| 264 RGBA_8888); | |
| 265 const Resource* const_resource = resource.get(); | |
| 266 | |
| 267 ImageDecodeTask::Vector empty; | |
| 268 tasks_.push_back(new BlockingTestRasterTaskImpl( | |
| 269 const_resource, | |
| 270 base::Bind(&TileTaskWorkerPoolTest::OnTaskCompleted, | |
| 271 base::Unretained(this), base::Passed(&resource), id), | |
| 272 lock, &empty)); | |
| 273 } | |
| 274 | |
| 275 const std::vector<RasterTaskResult>& completed_tasks() const { | |
| 276 return completed_tasks_; | |
| 277 } | |
| 278 | |
| 279 void LoseContext(ContextProvider* context_provider) { | |
| 280 if (!context_provider) | |
| 281 return; | |
| 282 context_provider->ContextGL()->LoseContextCHROMIUM( | |
| 283 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB); | |
| 284 context_provider->ContextGL()->Flush(); | |
| 285 } | |
| 286 | |
| 287 private: | |
| 288 void Create3dOutputSurfaceAndResourceProvider() { | |
| 289 output_surface_ = FakeOutputSurface::Create3d( | |
| 290 context_provider_, worker_context_provider_).Pass(); | |
| 291 CHECK(output_surface_->BindToClient(&output_surface_client_)); | |
| 292 TestWebGraphicsContext3D* context3d = context_provider_->TestContext3d(); | |
| 293 context3d->set_support_sync_query(true); | |
| 294 resource_provider_ = ResourceProvider::Create(output_surface_.get(), NULL, | |
| 295 &gpu_memory_buffer_manager_, | |
| 296 NULL, 0, false, 1).Pass(); | |
| 297 } | |
| 298 | |
| 299 void CreateSoftwareOutputSurfaceAndResourceProvider() { | |
| 300 output_surface_ = FakeOutputSurface::CreateSoftware( | |
| 301 make_scoped_ptr(new SoftwareOutputDevice)); | |
| 302 CHECK(output_surface_->BindToClient(&output_surface_client_)); | |
| 303 resource_provider_ = | |
| 304 ResourceProvider::Create(output_surface_.get(), &shared_bitmap_manager_, | |
| 305 NULL, NULL, 0, false, 1).Pass(); | |
| 306 } | |
| 307 | |
| 308 void OnTaskCompleted(scoped_ptr<ScopedResource> resource, | |
| 309 unsigned id, | |
| 310 const RasterSource::SolidColorAnalysis& analysis, | |
| 311 bool was_canceled) { | |
| 312 RasterTaskResult result; | |
| 313 result.id = id; | |
| 314 result.canceled = was_canceled; | |
| 315 completed_tasks_.push_back(result); | |
| 316 } | |
| 317 | |
| 318 void OnTimeout() { | |
| 319 timed_out_ = true; | |
| 320 base::MessageLoop::current()->Quit(); | |
| 321 } | |
| 322 | |
| 323 protected: | |
| 324 scoped_refptr<TestContextProvider> context_provider_; | |
| 325 scoped_refptr<TestContextProvider> worker_context_provider_; | |
| 326 scoped_ptr<Rasterizer> rasterizer_; | |
| 327 FakeOutputSurfaceClient output_surface_client_; | |
| 328 scoped_ptr<FakeOutputSurface> output_surface_; | |
| 329 scoped_ptr<ResourceProvider> resource_provider_; | |
| 330 scoped_ptr<ResourcePool> staging_resource_pool_; | |
| 331 scoped_ptr<TileTaskWorkerPool> tile_task_worker_pool_; | |
| 332 TestGpuMemoryBufferManager gpu_memory_buffer_manager_; | |
| 333 TestSharedBitmapManager shared_bitmap_manager_; | |
| 334 TestTaskGraphRunner task_graph_runner_; | |
| 335 base::CancelableClosure timeout_; | |
| 336 UniqueNotifier all_tile_tasks_finished_; | |
| 337 int timeout_seconds_; | |
| 338 bool timed_out_; | |
| 339 RasterTaskVector tasks_; | |
| 340 std::vector<RasterTaskResult> completed_tasks_; | |
| 341 TaskSetCollection completed_task_sets_; | |
| 342 }; | |
| 343 | |
| 344 TEST_P(TileTaskWorkerPoolTest, Basic) { | |
| 345 AppendTask(0u); | |
| 346 AppendTask(1u); | |
| 347 ScheduleTasks(); | |
| 348 | |
| 349 RunMessageLoopUntilAllTasksHaveCompleted(); | |
| 350 | |
| 351 ASSERT_EQ(2u, completed_tasks().size()); | |
| 352 EXPECT_FALSE(completed_tasks()[0].canceled); | |
| 353 EXPECT_FALSE(completed_tasks()[1].canceled); | |
| 354 } | |
| 355 | |
| 356 TEST_P(TileTaskWorkerPoolTest, FailedMapResource) { | |
| 357 if (GetParam() == TILE_TASK_WORKER_POOL_TYPE_BITMAP) | |
| 358 return; | |
| 359 | |
| 360 TestWebGraphicsContext3D* context3d = context_provider_->TestContext3d(); | |
| 361 context3d->set_times_map_buffer_chromium_succeeds(0); | |
| 362 AppendTask(0u); | |
| 363 ScheduleTasks(); | |
| 364 | |
| 365 RunMessageLoopUntilAllTasksHaveCompleted(); | |
| 366 | |
| 367 ASSERT_EQ(1u, completed_tasks().size()); | |
| 368 EXPECT_FALSE(completed_tasks()[0].canceled); | |
| 369 } | |
| 370 | |
| 371 // This test checks that replacing a pending raster task with another does | |
| 372 // not prevent the DidFinishRunningTileTasks notification from being sent. | |
| 373 TEST_P(TileTaskWorkerPoolTest, FalseThrottling) { | |
| 374 base::Lock lock; | |
| 375 | |
| 376 // Schedule a task that is prevented from completing with a lock. | |
| 377 lock.Acquire(); | |
| 378 AppendBlockingTask(0u, &lock); | |
| 379 ScheduleTasks(); | |
| 380 | |
| 381 // Schedule another task to replace the still-pending task. Because the old | |
| 382 // task is not a throttled task in the new task set, it should not prevent | |
| 383 // DidFinishRunningTileTasks from getting signaled. | |
| 384 RasterTaskVector tasks; | |
| 385 tasks.swap(tasks_); | |
| 386 AppendTask(1u); | |
| 387 ScheduleTasks(); | |
| 388 | |
| 389 // Unblock the first task to allow the second task to complete. | |
| 390 lock.Release(); | |
| 391 | |
| 392 RunMessageLoopUntilAllTasksHaveCompleted(); | |
| 393 } | |
| 394 | |
| 395 TEST_P(TileTaskWorkerPoolTest, LargeResources) { | |
| 396 gfx::Size size(kLargeResourceDimension, kLargeResourceDimension); | |
| 397 | |
| 398 { | |
| 399 // Verify a resource of this size is larger than the transfer buffer. | |
| 400 scoped_ptr<ScopedResource> resource( | |
| 401 ScopedResource::Create(resource_provider_.get())); | |
| 402 resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE, | |
| 403 RGBA_8888); | |
| 404 EXPECT_GE(resource->bytes(), kMaxTransferBufferUsageBytes); | |
| 405 } | |
| 406 | |
| 407 AppendTask(0u, size); | |
| 408 AppendTask(1u, size); | |
| 409 AppendTask(2u, size); | |
| 410 ScheduleTasks(); | |
| 411 | |
| 412 // This will time out if a resource that is larger than the throttle limit | |
| 413 // never gets scheduled. | |
| 414 RunMessageLoopUntilAllTasksHaveCompleted(); | |
| 415 } | |
| 416 | |
| 417 TEST_P(TileTaskWorkerPoolTest, LostContext) { | |
| 418 LoseContext(output_surface_->context_provider()); | |
| 419 LoseContext(output_surface_->worker_context_provider()); | |
| 420 | |
| 421 AppendTask(0u); | |
| 422 AppendTask(1u); | |
| 423 ScheduleTasks(); | |
| 424 | |
| 425 RunMessageLoopUntilAllTasksHaveCompleted(); | |
| 426 | |
| 427 ASSERT_EQ(2u, completed_tasks().size()); | |
| 428 EXPECT_FALSE(completed_tasks()[0].canceled); | |
| 429 EXPECT_FALSE(completed_tasks()[1].canceled); | |
| 430 } | |
| 431 | |
| 432 INSTANTIATE_TEST_CASE_P( | |
| 433 TileTaskWorkerPoolTests, | |
| 434 TileTaskWorkerPoolTest, | |
| 435 ::testing::Values(TILE_TASK_WORKER_POOL_TYPE_PIXEL_BUFFER, | |
| 436 TILE_TASK_WORKER_POOL_TYPE_ZERO_COPY, | |
| 437 TILE_TASK_WORKER_POOL_TYPE_ONE_COPY, | |
| 438 TILE_TASK_WORKER_POOL_TYPE_GPU, | |
| 439 TILE_TASK_WORKER_POOL_TYPE_BITMAP)); | |
| 440 | |
| 441 } // namespace | |
| 442 } // namespace cc | |
| OLD | NEW |