| 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" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 ShaderLoadedCallback shader_loaded_callback_; | 91 ShaderLoadedCallback shader_loaded_callback_; |
| 92 OpType op_type_; | 92 OpType op_type_; |
| 93 std::unique_ptr<disk_cache::Backend::Iterator> iter_; | 93 std::unique_ptr<disk_cache::Backend::Iterator> iter_; |
| 94 scoped_refptr<net::IOBufferWithSize> buf_; | 94 scoped_refptr<net::IOBufferWithSize> buf_; |
| 95 disk_cache::Entry* entry_; | 95 disk_cache::Entry* entry_; |
| 96 base::WeakPtrFactory<ShaderDiskReadHelper> weak_ptr_factory_; | 96 base::WeakPtrFactory<ShaderDiskReadHelper> weak_ptr_factory_; |
| 97 | 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ShaderDiskReadHelper); | 98 DISALLOW_COPY_AND_ASSIGN(ShaderDiskReadHelper); |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 class ShaderClearHelper : public base::RefCounted<ShaderClearHelper>, | 101 class ShaderClearHelper : public base::ThreadChecker { |
| 102 public base::SupportsWeakPtr<ShaderClearHelper>, | |
| 103 public base::ThreadChecker { | |
| 104 public: | 102 public: |
| 105 ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache, | 103 ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache, |
| 106 const base::FilePath& path, | 104 const base::FilePath& path, |
| 107 const base::Time& delete_begin, | 105 const base::Time& delete_begin, |
| 108 const base::Time& delete_end, | 106 const base::Time& delete_end, |
| 109 const base::Closure& callback); | 107 const base::Closure& callback); |
| 108 ~ShaderClearHelper(); |
| 109 |
| 110 void Clear(); | 110 void Clear(); |
| 111 | 111 |
| 112 private: | 112 private: |
| 113 friend class base::RefCounted<ShaderClearHelper>; | |
| 114 | |
| 115 enum OpType { | 113 enum OpType { |
| 116 TERMINATE, | 114 TERMINATE, |
| 117 VERIFY_CACHE_SETUP, | 115 VERIFY_CACHE_SETUP, |
| 118 DELETE_CACHE | 116 DELETE_CACHE |
| 119 }; | 117 }; |
| 120 | 118 |
| 121 ~ShaderClearHelper(); | |
| 122 | |
| 123 void DoClearShaderCache(int rv); | 119 void DoClearShaderCache(int rv); |
| 124 | 120 |
| 125 scoped_refptr<ShaderDiskCache> cache_; | 121 scoped_refptr<ShaderDiskCache> cache_; |
| 126 OpType op_type_; | 122 OpType op_type_; |
| 127 base::FilePath path_; | 123 base::FilePath path_; |
| 128 base::Time delete_begin_; | 124 base::Time delete_begin_; |
| 129 base::Time delete_end_; | 125 base::Time delete_end_; |
| 130 base::Closure callback_; | 126 base::Closure callback_; |
| 127 base::WeakPtrFactory<ShaderClearHelper> weak_ptr_factory_; |
| 131 | 128 |
| 132 DISALLOW_COPY_AND_ASSIGN(ShaderClearHelper); | 129 DISALLOW_COPY_AND_ASSIGN(ShaderClearHelper); |
| 133 }; | 130 }; |
| 134 | 131 |
| 135 //////////////////////////////////////////////////////////////////////////////// | 132 //////////////////////////////////////////////////////////////////////////////// |
| 136 // ShaderDiskCacheEntry | 133 // ShaderDiskCacheEntry |
| 137 | 134 |
| 138 ShaderDiskCacheEntry::ShaderDiskCacheEntry(ShaderDiskCache* cache, | 135 ShaderDiskCacheEntry::ShaderDiskCacheEntry(ShaderDiskCache* cache, |
| 139 const std::string& key, | 136 const std::string& key, |
| 140 const std::string& shader) | 137 const std::string& shader) |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 case READ_COMPLETE: | 255 case READ_COMPLETE: |
| 259 rv = ReadComplete(rv); | 256 rv = ReadComplete(rv); |
| 260 break; | 257 break; |
| 261 case ITERATION_FINISHED: | 258 case ITERATION_FINISHED: |
| 262 rv = IterationComplete(rv); | 259 rv = IterationComplete(rv); |
| 263 break; | 260 break; |
| 264 case TERMINATE: | 261 case TERMINATE: |
| 265 cache_->ReadComplete(); | 262 cache_->ReadComplete(); |
| 266 rv = net::ERR_IO_PENDING; // break the loop | 263 rv = net::ERR_IO_PENDING; // break the loop |
| 267 break; | 264 break; |
| 268 default: | |
| 269 NOTREACHED(); // Invalid state for read helper | |
| 270 rv = net::ERR_FAILED; | |
| 271 break; | |
| 272 } | 265 } |
| 273 } while (rv != net::ERR_IO_PENDING); | 266 } while (rv != net::ERR_IO_PENDING); |
| 274 } | 267 } |
| 275 | 268 |
| 276 int ShaderDiskReadHelper::OpenNextEntry() { | 269 int ShaderDiskReadHelper::OpenNextEntry() { |
| 277 DCHECK(CalledOnValidThread()); | 270 DCHECK(CalledOnValidThread()); |
| 278 op_type_ = OPEN_NEXT_COMPLETE; | 271 op_type_ = OPEN_NEXT_COMPLETE; |
| 279 if (!iter_) | 272 if (!iter_) |
| 280 iter_ = cache_->backend()->CreateIterator(); | 273 iter_ = cache_->backend()->CreateIterator(); |
| 281 return iter_->OpenNextEntry(&entry_, | 274 return iter_->OpenNextEntry(&entry_, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 DCHECK(CalledOnValidThread()); | 313 DCHECK(CalledOnValidThread()); |
| 321 iter_.reset(); | 314 iter_.reset(); |
| 322 op_type_ = TERMINATE; | 315 op_type_ = TERMINATE; |
| 323 return net::OK; | 316 return net::OK; |
| 324 } | 317 } |
| 325 | 318 |
| 326 //////////////////////////////////////////////////////////////////////////////// | 319 //////////////////////////////////////////////////////////////////////////////// |
| 327 // ShaderClearHelper | 320 // ShaderClearHelper |
| 328 | 321 |
| 329 ShaderClearHelper::ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache, | 322 ShaderClearHelper::ShaderClearHelper(scoped_refptr<ShaderDiskCache> cache, |
| 330 const base::FilePath& path, | 323 const base::FilePath& path, |
| 331 const base::Time& delete_begin, | 324 const base::Time& delete_begin, |
| 332 const base::Time& delete_end, | 325 const base::Time& delete_end, |
| 333 const base::Closure& callback) | 326 const base::Closure& callback) |
| 334 : cache_(cache), | 327 : cache_(std::move(cache)), |
| 335 op_type_(VERIFY_CACHE_SETUP), | 328 op_type_(VERIFY_CACHE_SETUP), |
| 336 path_(path), | 329 path_(path), |
| 337 delete_begin_(delete_begin), | 330 delete_begin_(delete_begin), |
| 338 delete_end_(delete_end), | 331 delete_end_(delete_end), |
| 339 callback_(callback) { | 332 callback_(callback), |
| 340 } | 333 weak_ptr_factory_(this) {} |
| 341 | 334 |
| 342 ShaderClearHelper::~ShaderClearHelper() { | 335 ShaderClearHelper::~ShaderClearHelper() { |
| 343 DCHECK(CalledOnValidThread()); | 336 DCHECK(CalledOnValidThread()); |
| 344 } | 337 } |
| 345 | 338 |
| 346 void ShaderClearHelper::Clear() { | 339 void ShaderClearHelper::Clear() { |
| 347 DCHECK(CalledOnValidThread()); | 340 DCHECK(CalledOnValidThread()); |
| 348 DoClearShaderCache(net::OK); | 341 DoClearShaderCache(net::OK); |
| 349 } | 342 } |
| 350 | 343 |
| 351 void ShaderClearHelper::DoClearShaderCache(int rv) { | 344 void ShaderClearHelper::DoClearShaderCache(int rv) { |
| 352 DCHECK(CalledOnValidThread()); | 345 DCHECK(CalledOnValidThread()); |
| 353 | |
| 354 // Hold a ref to ourselves so when we do the CacheCleared call we don't get | |
| 355 // auto-deleted when our ref count drops to zero. | |
| 356 scoped_refptr<ShaderClearHelper> helper = this; | |
| 357 | |
| 358 while (rv != net::ERR_IO_PENDING) { | 346 while (rv != net::ERR_IO_PENDING) { |
| 359 switch (op_type_) { | 347 switch (op_type_) { |
| 360 case VERIFY_CACHE_SETUP: | 348 case VERIFY_CACHE_SETUP: |
| 361 rv = cache_->SetAvailableCallback( | 349 rv = cache_->SetAvailableCallback( |
| 362 base::Bind(&ShaderClearHelper::DoClearShaderCache, AsWeakPtr())); | 350 base::Bind(&ShaderClearHelper::DoClearShaderCache, |
| 351 weak_ptr_factory_.GetWeakPtr())); |
| 363 op_type_ = DELETE_CACHE; | 352 op_type_ = DELETE_CACHE; |
| 364 break; | 353 break; |
| 365 case DELETE_CACHE: | 354 case DELETE_CACHE: |
| 366 rv = cache_->Clear( | 355 rv = cache_->Clear(delete_begin_, delete_end_, |
| 367 delete_begin_, delete_end_, | 356 base::Bind(&ShaderClearHelper::DoClearShaderCache, |
| 368 base::Bind(&ShaderClearHelper::DoClearShaderCache, AsWeakPtr())); | 357 weak_ptr_factory_.GetWeakPtr())); |
| 369 op_type_ = TERMINATE; | 358 op_type_ = TERMINATE; |
| 370 break; | 359 break; |
| 371 case TERMINATE: | 360 case TERMINATE: |
| 361 callback_.Run(); |
| 362 // Calling CacheCleared() destroys |this|. |
| 372 ShaderCacheFactory::GetInstance()->CacheCleared(path_); | 363 ShaderCacheFactory::GetInstance()->CacheCleared(path_); |
| 373 callback_.Run(); | |
| 374 rv = net::ERR_IO_PENDING; // Break the loop. | 364 rv = net::ERR_IO_PENDING; // Break the loop. |
| 375 break; | 365 break; |
| 376 default: | |
| 377 NOTREACHED(); // Invalid state provided. | |
| 378 op_type_ = TERMINATE; | |
| 379 break; | |
| 380 } | 366 } |
| 381 } | 367 } |
| 382 } | 368 } |
| 383 | 369 |
| 384 //////////////////////////////////////////////////////////////////////////////// | 370 //////////////////////////////////////////////////////////////////////////////// |
| 385 // ShaderCacheFactory | 371 // ShaderCacheFactory |
| 386 | 372 |
| 387 // static | 373 // static |
| 388 void ShaderCacheFactory::InitInstance( | 374 void ShaderCacheFactory::InitInstance( |
| 389 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 375 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 client_id_to_path_map_[client_id] = path; | 409 client_id_to_path_map_[client_id] = path; |
| 424 } | 410 } |
| 425 | 411 |
| 426 void ShaderCacheFactory::RemoveCacheInfo(int32_t client_id) { | 412 void ShaderCacheFactory::RemoveCacheInfo(int32_t client_id) { |
| 427 DCHECK(CalledOnValidThread()); | 413 DCHECK(CalledOnValidThread()); |
| 428 client_id_to_path_map_.erase(client_id); | 414 client_id_to_path_map_.erase(client_id); |
| 429 } | 415 } |
| 430 | 416 |
| 431 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::Get(int32_t client_id) { | 417 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::Get(int32_t client_id) { |
| 432 DCHECK(CalledOnValidThread()); | 418 DCHECK(CalledOnValidThread()); |
| 433 ClientIdToPathMap::iterator iter = | 419 ClientIdToPathMap::iterator iter = client_id_to_path_map_.find(client_id); |
| 434 client_id_to_path_map_.find(client_id); | |
| 435 if (iter == client_id_to_path_map_.end()) | 420 if (iter == client_id_to_path_map_.end()) |
| 436 return NULL; | 421 return NULL; |
| 437 return ShaderCacheFactory::GetByPath(iter->second); | 422 return ShaderCacheFactory::GetByPath(iter->second); |
| 438 } | 423 } |
| 439 | 424 |
| 440 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::GetByPath( | 425 scoped_refptr<ShaderDiskCache> ShaderCacheFactory::GetByPath( |
| 441 const base::FilePath& path) { | 426 const base::FilePath& path) { |
| 442 DCHECK(CalledOnValidThread()); | 427 DCHECK(CalledOnValidThread()); |
| 443 ShaderCacheMap::iterator iter = shader_cache_map_.find(path); | 428 ShaderCacheMap::iterator iter = shader_cache_map_.find(path); |
| 444 if (iter != shader_cache_map_.end()) | 429 if (iter != shader_cache_map_.end()) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 460 shader_cache_map_.erase(key); | 445 shader_cache_map_.erase(key); |
| 461 } | 446 } |
| 462 | 447 |
| 463 void ShaderCacheFactory::ClearByPath(const base::FilePath& path, | 448 void ShaderCacheFactory::ClearByPath(const base::FilePath& path, |
| 464 const base::Time& delete_begin, | 449 const base::Time& delete_begin, |
| 465 const base::Time& delete_end, | 450 const base::Time& delete_end, |
| 466 const base::Closure& callback) { | 451 const base::Closure& callback) { |
| 467 DCHECK(CalledOnValidThread()); | 452 DCHECK(CalledOnValidThread()); |
| 468 DCHECK(!callback.is_null()); | 453 DCHECK(!callback.is_null()); |
| 469 | 454 |
| 470 scoped_refptr<ShaderClearHelper> helper = new ShaderClearHelper( | 455 auto helper = base::MakeUnique<ShaderClearHelper>( |
| 471 GetByPath(path), path, delete_begin, delete_end, callback); | 456 GetByPath(path), path, delete_begin, delete_end, callback); |
| 472 | 457 |
| 473 // We could receive requests to clear the same path with different | 458 // We could receive requests to clear the same path with different |
| 474 // begin/end times. So, we keep a list of requests. If we haven't seen this | 459 // begin/end times. So, we keep a list of requests. If we haven't seen this |
| 475 // path before we kick off the clear and add it to the list. If we have see it | 460 // path before we kick off the clear and add it to the list. If we have see it |
| 476 // already, then we already have a clear running. We add this clear to the | 461 // already, then we already have a clear running. We add this clear to the |
| 477 // list and wait for any previous clears to finish. | 462 // list and wait for any previous clears to finish. |
| 478 ShaderClearMap::iterator iter = shader_clear_map_.find(path); | 463 ShaderClearMap::iterator iter = shader_clear_map_.find(path); |
| 479 if (iter != shader_clear_map_.end()) { | 464 if (iter != shader_clear_map_.end()) { |
| 480 iter->second.push(helper); | 465 iter->second.push(std::move(helper)); |
| 481 return; | 466 return; |
| 482 } | 467 } |
| 483 | 468 |
| 469 // Insert the helper in the map before calling Clear(), since it can lead to a |
| 470 // call back into CacheCleared(). |
| 471 ShaderClearHelper* helper_ptr = helper.get(); |
| 484 shader_clear_map_.insert( | 472 shader_clear_map_.insert( |
| 485 std::pair<base::FilePath, ShaderClearQueue>(path, ShaderClearQueue())); | 473 std::pair<base::FilePath, ShaderClearQueue>(path, ShaderClearQueue())); |
| 486 shader_clear_map_[path].push(helper); | 474 shader_clear_map_[path].push(std::move(helper)); |
| 487 helper->Clear(); | 475 helper_ptr->Clear(); |
| 488 } | 476 } |
| 489 | 477 |
| 490 void ShaderCacheFactory::CacheCleared(const base::FilePath& path) { | 478 void ShaderCacheFactory::CacheCleared(const base::FilePath& path) { |
| 491 DCHECK(CalledOnValidThread()); | 479 DCHECK(CalledOnValidThread()); |
| 492 | 480 |
| 493 ShaderClearMap::iterator iter = shader_clear_map_.find(path); | 481 ShaderClearMap::iterator iter = shader_clear_map_.find(path); |
| 494 if (iter == shader_clear_map_.end()) { | 482 if (iter == shader_clear_map_.end()) { |
| 495 LOG(ERROR) << "Completed clear but missing clear helper."; | 483 LOG(ERROR) << "Completed clear but missing clear helper."; |
| 496 return; | 484 return; |
| 497 } | 485 } |
| 498 | 486 |
| 499 iter->second.pop(); | 487 iter->second.pop(); |
| 500 | 488 |
| 501 // If there are remaining items in the list we trigger the Clear on the | 489 // If there are remaining items in the list we trigger the Clear on the |
| 502 // next one. | 490 // next one. |
| 503 if (!iter->second.empty()) { | 491 if (!iter->second.empty()) { |
| 504 iter->second.front()->Clear(); | 492 iter->second.front()->Clear(); |
| 505 return; | 493 return; |
| 506 } | 494 } |
| 507 | 495 |
| 508 shader_clear_map_.erase(path); | 496 shader_clear_map_.erase(iter); |
| 509 } | 497 } |
| 510 | 498 |
| 511 //////////////////////////////////////////////////////////////////////////////// | 499 //////////////////////////////////////////////////////////////////////////////// |
| 512 // ShaderDiskCache | 500 // ShaderDiskCache |
| 513 | 501 |
| 514 ShaderDiskCache::ShaderDiskCache(const base::FilePath& cache_path) | 502 ShaderDiskCache::ShaderDiskCache(const base::FilePath& cache_path) |
| 515 : cache_available_(false), | 503 : cache_available_(false), |
| 516 cache_path_(cache_path), | 504 cache_path_(cache_path), |
| 517 is_initialized_(false) { | 505 is_initialized_(false) { |
| 518 ShaderCacheFactory::GetInstance()->AddToCache(cache_path_, this); | 506 ShaderCacheFactory::GetInstance()->AddToCache(cache_path_, this); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 const net::CompletionCallback& callback) { | 598 const net::CompletionCallback& callback) { |
| 611 if (entries_.empty()) { | 599 if (entries_.empty()) { |
| 612 return net::OK; | 600 return net::OK; |
| 613 } | 601 } |
| 614 cache_complete_callback_ = callback; | 602 cache_complete_callback_ = callback; |
| 615 return net::ERR_IO_PENDING; | 603 return net::ERR_IO_PENDING; |
| 616 } | 604 } |
| 617 | 605 |
| 618 } // namespace content | 606 } // namespace content |
| 619 | 607 |
| OLD | NEW |