OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/gpu/shader_disk_cache.h" | 5 #include "content/browser/gpu/shader_disk_cache.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
11 #include "gpu/command_buffer/common/constants.h" | 11 #include "gpu/command_buffer/common/constants.h" |
12 #include "net/base/cache_type.h" | 12 #include "net/base/cache_type.h" |
13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 static const base::FilePath::CharType kGpuCachePath[] = | 20 static const base::FilePath::CharType kGpuCachePath[] = |
21 FILE_PATH_LITERAL("GPUCache"); | 21 FILE_PATH_LITERAL("GPUCache"); |
22 | 22 |
23 static ShaderCacheFactory* factory_instance = nullptr; | |
24 | |
25 } // namespace | 23 } // namespace |
26 | 24 |
27 // ShaderDiskCacheEntry handles the work of caching/updating the cached | 25 // ShaderDiskCacheEntry handles the work of caching/updating the cached |
28 // shaders. | 26 // shaders. |
29 class ShaderDiskCacheEntry : public base::ThreadChecker { | 27 class ShaderDiskCacheEntry : public base::ThreadChecker { |
30 public: | 28 public: |
31 ShaderDiskCacheEntry(ShaderDiskCache* cache, | 29 ShaderDiskCacheEntry(ShaderDiskCache* cache, |
32 const std::string& key, | 30 const std::string& key, |
33 const std::string& shader); | 31 const std::string& shader); |
34 ~ShaderDiskCacheEntry(); | 32 ~ShaderDiskCacheEntry(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 std::unique_ptr<disk_cache::Backend::Iterator> iter_; | 91 std::unique_ptr<disk_cache::Backend::Iterator> iter_; |
94 scoped_refptr<net::IOBufferWithSize> buf_; | 92 scoped_refptr<net::IOBufferWithSize> buf_; |
95 disk_cache::Entry* entry_; | 93 disk_cache::Entry* entry_; |
96 base::WeakPtrFactory<ShaderDiskReadHelper> weak_ptr_factory_; | 94 base::WeakPtrFactory<ShaderDiskReadHelper> weak_ptr_factory_; |
97 | 95 |
98 DISALLOW_COPY_AND_ASSIGN(ShaderDiskReadHelper); | 96 DISALLOW_COPY_AND_ASSIGN(ShaderDiskReadHelper); |
99 }; | 97 }; |
100 | 98 |
101 class ShaderClearHelper : public base::ThreadChecker { | 99 class ShaderClearHelper : public base::ThreadChecker { |
102 public: | 100 public: |
103 ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache, | 101 ShaderClearHelper(ShaderCacheFactory* factory, |
| 102 scoped_refptr<ShaderDiskCache> cache, |
104 const base::FilePath& path, | 103 const base::FilePath& path, |
105 const base::Time& delete_begin, | 104 const base::Time& delete_begin, |
106 const base::Time& delete_end, | 105 const base::Time& delete_end, |
107 const base::Closure& callback); | 106 const base::Closure& callback); |
108 ~ShaderClearHelper(); | 107 ~ShaderClearHelper(); |
109 | 108 |
110 void Clear(); | 109 void Clear(); |
111 | 110 |
112 private: | 111 private: |
113 enum OpType { | 112 enum OpType { |
114 TERMINATE, | 113 TERMINATE, |
115 VERIFY_CACHE_SETUP, | 114 VERIFY_CACHE_SETUP, |
116 DELETE_CACHE | 115 DELETE_CACHE |
117 }; | 116 }; |
118 | 117 |
119 void DoClearShaderCache(int rv); | 118 void DoClearShaderCache(int rv); |
120 | 119 |
| 120 ShaderCacheFactory* factory_; |
121 scoped_refptr<ShaderDiskCache> cache_; | 121 scoped_refptr<ShaderDiskCache> cache_; |
122 OpType op_type_; | 122 OpType op_type_; |
123 base::FilePath path_; | 123 base::FilePath path_; |
124 base::Time delete_begin_; | 124 base::Time delete_begin_; |
125 base::Time delete_end_; | 125 base::Time delete_end_; |
126 base::Closure callback_; | 126 base::Closure callback_; |
127 base::WeakPtrFactory<ShaderClearHelper> weak_ptr_factory_; | 127 base::WeakPtrFactory<ShaderClearHelper> weak_ptr_factory_; |
128 | 128 |
129 DISALLOW_COPY_AND_ASSIGN(ShaderClearHelper); | 129 DISALLOW_COPY_AND_ASSIGN(ShaderClearHelper); |
130 }; | 130 }; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 int ShaderDiskReadHelper::IterationComplete(int rv) { | 312 int ShaderDiskReadHelper::IterationComplete(int rv) { |
313 DCHECK(CalledOnValidThread()); | 313 DCHECK(CalledOnValidThread()); |
314 iter_.reset(); | 314 iter_.reset(); |
315 op_type_ = TERMINATE; | 315 op_type_ = TERMINATE; |
316 return net::OK; | 316 return net::OK; |
317 } | 317 } |
318 | 318 |
319 //////////////////////////////////////////////////////////////////////////////// | 319 //////////////////////////////////////////////////////////////////////////////// |
320 // ShaderClearHelper | 320 // ShaderClearHelper |
321 | 321 |
322 ShaderClearHelper::ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache, | 322 ShaderClearHelper::ShaderClearHelper(ShaderCacheFactory* factory, |
| 323 scoped_refptr<ShaderDiskCache> cache, |
323 const base::FilePath& path, | 324 const base::FilePath& path, |
324 const base::Time& delete_begin, | 325 const base::Time& delete_begin, |
325 const base::Time& delete_end, | 326 const base::Time& delete_end, |
326 const base::Closure& callback) | 327 const base::Closure& callback) |
327 : cache_(std::move(cache)), | 328 : factory_(factory), |
| 329 cache_(std::move(cache)), |
328 op_type_(VERIFY_CACHE_SETUP), | 330 op_type_(VERIFY_CACHE_SETUP), |
329 path_(path), | 331 path_(path), |
330 delete_begin_(delete_begin), | 332 delete_begin_(delete_begin), |
331 delete_end_(delete_end), | 333 delete_end_(delete_end), |
332 callback_(callback), | 334 callback_(callback), |
333 weak_ptr_factory_(this) {} | 335 weak_ptr_factory_(this) {} |
334 | 336 |
335 ShaderClearHelper::~ShaderClearHelper() { | 337 ShaderClearHelper::~ShaderClearHelper() { |
336 DCHECK(CalledOnValidThread()); | 338 DCHECK(CalledOnValidThread()); |
337 } | 339 } |
(...skipping 15 matching lines...) Expand all Loading... |
353 break; | 355 break; |
354 case DELETE_CACHE: | 356 case DELETE_CACHE: |
355 rv = cache_->Clear(delete_begin_, delete_end_, | 357 rv = cache_->Clear(delete_begin_, delete_end_, |
356 base::Bind(&ShaderClearHelper::DoClearShaderCache, | 358 base::Bind(&ShaderClearHelper::DoClearShaderCache, |
357 weak_ptr_factory_.GetWeakPtr())); | 359 weak_ptr_factory_.GetWeakPtr())); |
358 op_type_ = TERMINATE; | 360 op_type_ = TERMINATE; |
359 break; | 361 break; |
360 case TERMINATE: | 362 case TERMINATE: |
361 callback_.Run(); | 363 callback_.Run(); |
362 // Calling CacheCleared() destroys |this|. | 364 // Calling CacheCleared() destroys |this|. |
363 ShaderCacheFactory::GetInstance()->CacheCleared(path_); | 365 factory_->CacheCleared(path_); |
364 rv = net::ERR_IO_PENDING; // Break the loop. | 366 rv = net::ERR_IO_PENDING; // Break the loop. |
365 break; | 367 break; |
366 } | 368 } |
367 } | 369 } |
368 } | 370 } |
369 | 371 |
370 //////////////////////////////////////////////////////////////////////////////// | 372 //////////////////////////////////////////////////////////////////////////////// |
371 // ShaderCacheFactory | 373 // ShaderCacheFactory |
372 | 374 |
373 // static | |
374 void ShaderCacheFactory::InitInstance( | |
375 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
376 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner) { | |
377 if (task_runner->BelongsToCurrentThread()) { | |
378 CreateFactoryInstance(std::move(cache_task_runner)); | |
379 } else { | |
380 task_runner->PostTask(FROM_HERE, | |
381 base::Bind(&ShaderCacheFactory::CreateFactoryInstance, | |
382 std::move(cache_task_runner))); | |
383 } | |
384 } | |
385 | |
386 // static | |
387 ShaderCacheFactory* ShaderCacheFactory::GetInstance() { | |
388 DCHECK(!factory_instance || factory_instance->CalledOnValidThread()); | |
389 return factory_instance; | |
390 } | |
391 | |
392 ShaderCacheFactory::ShaderCacheFactory( | 375 ShaderCacheFactory::ShaderCacheFactory( |
393 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner) | 376 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner) |
394 : cache_task_runner_(std::move(cache_task_runner)) {} | 377 : cache_task_runner_(std::move(cache_task_runner)) {} |
395 | 378 |
396 ShaderCacheFactory::~ShaderCacheFactory() { | 379 ShaderCacheFactory::~ShaderCacheFactory() { |
397 } | 380 } |
398 | 381 |
399 // static | |
400 void ShaderCacheFactory::CreateFactoryInstance( | |
401 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner) { | |
402 DCHECK(!factory_instance); | |
403 factory_instance = new ShaderCacheFactory(std::move(cache_task_runner)); | |
404 } | |
405 | |
406 void ShaderCacheFactory::SetCacheInfo(int32_t client_id, | 382 void ShaderCacheFactory::SetCacheInfo(int32_t client_id, |
407 const base::FilePath& path) { | 383 const base::FilePath& path) { |
408 DCHECK(CalledOnValidThread()); | 384 DCHECK(CalledOnValidThread()); |
409 client_id_to_path_map_[client_id] = path; | 385 client_id_to_path_map_[client_id] = path; |
410 } | 386 } |
411 | 387 |
412 void ShaderCacheFactory::RemoveCacheInfo(int32_t client_id) { | 388 void ShaderCacheFactory::RemoveCacheInfo(int32_t client_id) { |
413 DCHECK(CalledOnValidThread()); | 389 DCHECK(CalledOnValidThread()); |
414 client_id_to_path_map_.erase(client_id); | 390 client_id_to_path_map_.erase(client_id); |
415 } | 391 } |
416 | 392 |
417 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::Get(int32_t client_id) { | 393 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::Get(int32_t client_id) { |
418 DCHECK(CalledOnValidThread()); | 394 DCHECK(CalledOnValidThread()); |
419 ClientIdToPathMap::iterator iter = client_id_to_path_map_.find(client_id); | 395 ClientIdToPathMap::iterator iter = client_id_to_path_map_.find(client_id); |
420 if (iter == client_id_to_path_map_.end()) | 396 if (iter == client_id_to_path_map_.end()) |
421 return NULL; | 397 return NULL; |
422 return ShaderCacheFactory::GetByPath(iter->second); | 398 return ShaderCacheFactory::GetByPath(iter->second); |
423 } | 399 } |
424 | 400 |
425 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::GetByPath( | 401 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::GetByPath( |
426 const base::FilePath& path) { | 402 const base::FilePath& path) { |
427 DCHECK(CalledOnValidThread()); | 403 DCHECK(CalledOnValidThread()); |
428 ShaderCacheMap::iterator iter = shader_cache_map_.find(path); | 404 ShaderCacheMap::iterator iter = shader_cache_map_.find(path); |
429 if (iter != shader_cache_map_.end()) | 405 if (iter != shader_cache_map_.end()) |
430 return iter->second; | 406 return iter->second; |
431 | 407 |
432 ShaderDiskCache* cache = new ShaderDiskCache(path); | 408 ShaderDiskCache* cache = new ShaderDiskCache(this, path); |
433 cache->Init(cache_task_runner_); | 409 cache->Init(cache_task_runner_); |
434 return cache; | 410 return cache; |
435 } | 411 } |
436 | 412 |
437 void ShaderCacheFactory::AddToCache(const base::FilePath& key, | 413 void ShaderCacheFactory::AddToCache(const base::FilePath& key, |
438 ShaderDiskCache* cache) { | 414 ShaderDiskCache* cache) { |
439 DCHECK(CalledOnValidThread()); | 415 DCHECK(CalledOnValidThread()); |
440 shader_cache_map_[key] = cache; | 416 shader_cache_map_[key] = cache; |
441 } | 417 } |
442 | 418 |
443 void ShaderCacheFactory::RemoveFromCache(const base::FilePath& key) { | 419 void ShaderCacheFactory::RemoveFromCache(const base::FilePath& key) { |
444 DCHECK(CalledOnValidThread()); | 420 DCHECK(CalledOnValidThread()); |
445 shader_cache_map_.erase(key); | 421 shader_cache_map_.erase(key); |
446 } | 422 } |
447 | 423 |
448 void ShaderCacheFactory::ClearByPath(const base::FilePath& path, | 424 void ShaderCacheFactory::ClearByPath(const base::FilePath& path, |
449 const base::Time& delete_begin, | 425 const base::Time& delete_begin, |
450 const base::Time& delete_end, | 426 const base::Time& delete_end, |
451 const base::Closure& callback) { | 427 const base::Closure& callback) { |
452 DCHECK(CalledOnValidThread()); | 428 DCHECK(CalledOnValidThread()); |
453 DCHECK(!callback.is_null()); | 429 DCHECK(!callback.is_null()); |
454 | 430 |
455 auto helper = base::MakeUnique<ShaderClearHelper>( | 431 auto helper = base::MakeUnique<ShaderClearHelper>( |
456 GetByPath(path), path, delete_begin, delete_end, callback); | 432 this, GetByPath(path), path, delete_begin, delete_end, callback); |
457 | 433 |
458 // We could receive requests to clear the same path with different | 434 // We could receive requests to clear the same path with different |
459 // begin/end times. So, we keep a list of requests. If we haven't seen this | 435 // begin/end times. So, we keep a list of requests. If we haven't seen this |
460 // path before we kick off the clear and add it to the list. If we have see it | 436 // path before we kick off the clear and add it to the list. If we have see it |
461 // already, then we already have a clear running. We add this clear to the | 437 // already, then we already have a clear running. We add this clear to the |
462 // list and wait for any previous clears to finish. | 438 // list and wait for any previous clears to finish. |
463 ShaderClearMap::iterator iter = shader_clear_map_.find(path); | 439 ShaderClearMap::iterator iter = shader_clear_map_.find(path); |
464 if (iter != shader_clear_map_.end()) { | 440 if (iter != shader_clear_map_.end()) { |
465 iter->second.push(std::move(helper)); | 441 iter->second.push(std::move(helper)); |
466 return; | 442 return; |
(...skipping 25 matching lines...) Expand all Loading... |
492 iter->second.front()->Clear(); | 468 iter->second.front()->Clear(); |
493 return; | 469 return; |
494 } | 470 } |
495 | 471 |
496 shader_clear_map_.erase(iter); | 472 shader_clear_map_.erase(iter); |
497 } | 473 } |
498 | 474 |
499 //////////////////////////////////////////////////////////////////////////////// | 475 //////////////////////////////////////////////////////////////////////////////// |
500 // ShaderDiskCache | 476 // ShaderDiskCache |
501 | 477 |
502 ShaderDiskCache::ShaderDiskCache(const base::FilePath& cache_path) | 478 ShaderDiskCache::ShaderDiskCache(ShaderCacheFactory* factory, |
503 : cache_available_(false), | 479 const base::FilePath& cache_path) |
| 480 : factory_(factory), |
| 481 cache_available_(false), |
504 cache_path_(cache_path), | 482 cache_path_(cache_path), |
505 is_initialized_(false) { | 483 is_initialized_(false) { |
506 ShaderCacheFactory::GetInstance()->AddToCache(cache_path_, this); | 484 factory_->AddToCache(cache_path_, this); |
507 } | 485 } |
508 | 486 |
509 ShaderDiskCache::~ShaderDiskCache() { | 487 ShaderDiskCache::~ShaderDiskCache() { |
510 ShaderCacheFactory::GetInstance()->RemoveFromCache(cache_path_); | 488 factory_->RemoveFromCache(cache_path_); |
511 } | 489 } |
512 | 490 |
513 void ShaderDiskCache::Init( | 491 void ShaderDiskCache::Init( |
514 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner) { | 492 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner) { |
515 if (is_initialized_) { | 493 if (is_initialized_) { |
516 NOTREACHED(); // can't initialize disk cache twice. | 494 NOTREACHED(); // can't initialize disk cache twice. |
517 return; | 495 return; |
518 } | 496 } |
519 is_initialized_ = true; | 497 is_initialized_ = true; |
520 | 498 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 int ShaderDiskCache::SetCacheCompleteCallback( | 575 int ShaderDiskCache::SetCacheCompleteCallback( |
598 const net::CompletionCallback& callback) { | 576 const net::CompletionCallback& callback) { |
599 if (entries_.empty()) { | 577 if (entries_.empty()) { |
600 return net::OK; | 578 return net::OK; |
601 } | 579 } |
602 cache_complete_callback_ = callback; | 580 cache_complete_callback_ = callback; |
603 return net::ERR_IO_PENDING; | 581 return net::ERR_IO_PENDING; |
604 } | 582 } |
605 | 583 |
606 } // namespace content | 584 } // namespace content |
607 | |
OLD | NEW |