OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImageFilter.h" | 8 #include "SkImageFilter.h" |
9 #include "SkImageFilterCacheKey.h" | 9 #include "SkImageFilterCacheKey.h" |
10 | 10 |
11 #include "SkBitmap.h" | 11 #include "SkCanvas.h" |
12 #include "SkBitmapDevice.h" | |
13 #include "SkChecksum.h" | 12 #include "SkChecksum.h" |
14 #include "SkFuzzLogging.h" | 13 #include "SkFuzzLogging.h" |
15 #include "SkLocalMatrixImageFilter.h" | 14 #include "SkLocalMatrixImageFilter.h" |
16 #include "SkMatrixImageFilter.h" | 15 #include "SkMatrixImageFilter.h" |
17 #include "SkOncePtr.h" | 16 #include "SkOncePtr.h" |
18 #include "SkReadBuffer.h" | 17 #include "SkReadBuffer.h" |
19 #include "SkRect.h" | 18 #include "SkRect.h" |
20 #include "SkSpecialImage.h" | 19 #include "SkSpecialImage.h" |
21 #include "SkSpecialSurface.h" | 20 #include "SkSpecialSurface.h" |
22 #include "SkTDynamicHash.h" | 21 #include "SkTDynamicHash.h" |
23 #include "SkTInternalLList.h" | 22 #include "SkTInternalLList.h" |
24 #include "SkValidationUtils.h" | 23 #include "SkValidationUtils.h" |
25 #include "SkWriteBuffer.h" | 24 #include "SkWriteBuffer.h" |
26 #if SK_SUPPORT_GPU | 25 #if SK_SUPPORT_GPU |
27 #include "GrContext.h" | 26 #include "GrContext.h" |
28 #include "GrDrawContext.h" | 27 #include "GrDrawContext.h" |
29 #include "SkGrPixelRef.h" | |
30 #include "SkGr.h" | |
31 #endif | 28 #endif |
32 | 29 |
33 #ifdef SK_BUILD_FOR_IOS | 30 #ifdef SK_BUILD_FOR_IOS |
34 enum { kDefaultCacheSize = 2 * 1024 * 1024 }; | 31 enum { kDefaultCacheSize = 2 * 1024 * 1024 }; |
35 #else | 32 #else |
36 enum { kDefaultCacheSize = 128 * 1024 * 1024 }; | 33 enum { kDefaultCacheSize = 128 * 1024 * 1024 }; |
37 #endif | 34 #endif |
38 | 35 |
39 #ifndef SK_IGNORE_TO_STRING | 36 #ifndef SK_IGNORE_TO_STRING |
40 void SkImageFilter::CropRect::toString(SkString* str) const { | 37 void SkImageFilter::CropRect::toString(SkString* str) const { |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 ~CacheImpl() override { | 457 ~CacheImpl() override { |
461 SkTDynamicHash<Value, Key>::Iter iter(&fLookup); | 458 SkTDynamicHash<Value, Key>::Iter iter(&fLookup); |
462 | 459 |
463 while (!iter.done()) { | 460 while (!iter.done()) { |
464 Value* v = &*iter; | 461 Value* v = &*iter; |
465 ++iter; | 462 ++iter; |
466 delete v; | 463 delete v; |
467 } | 464 } |
468 } | 465 } |
469 struct Value { | 466 struct Value { |
470 Value(const Key& key, const SkBitmap& bitmap, const SkIPoint& offset) | |
471 : fKey(key), fBitmap(bitmap), fOffset(offset) {} | |
472 Value(const Key& key, SkSpecialImage* image, const SkIPoint& offset) | 467 Value(const Key& key, SkSpecialImage* image, const SkIPoint& offset) |
473 : fKey(key), fImage(SkRef(image)), fOffset(offset) {} | 468 : fKey(key), fImage(SkRef(image)), fOffset(offset) {} |
474 | 469 |
475 Key fKey; | 470 Key fKey; |
476 SkBitmap fBitmap; | |
477 SkAutoTUnref<SkSpecialImage> fImage; | 471 SkAutoTUnref<SkSpecialImage> fImage; |
478 SkIPoint fOffset; | 472 SkIPoint fOffset; |
479 static const Key& GetKey(const Value& v) { | 473 static const Key& GetKey(const Value& v) { |
480 return v.fKey; | 474 return v.fKey; |
481 } | 475 } |
482 static uint32_t Hash(const Key& key) { | 476 static uint32_t Hash(const Key& key) { |
483 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); | 477 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); |
484 } | 478 } |
485 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); | 479 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); |
486 }; | 480 }; |
487 | 481 |
488 bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const override
{ | |
489 SkAutoMutexAcquire mutex(fMutex); | |
490 if (Value* v = fLookup.find(key)) { | |
491 *result = v->fBitmap; | |
492 *offset = v->fOffset; | |
493 if (v != fLRU.head()) { | |
494 fLRU.remove(v); | |
495 fLRU.addToHead(v); | |
496 } | |
497 return true; | |
498 } | |
499 return false; | |
500 } | |
501 | |
502 SkSpecialImage* get(const Key& key, SkIPoint* offset) const override { | 482 SkSpecialImage* get(const Key& key, SkIPoint* offset) const override { |
503 SkAutoMutexAcquire mutex(fMutex); | 483 SkAutoMutexAcquire mutex(fMutex); |
504 if (Value* v = fLookup.find(key)) { | 484 if (Value* v = fLookup.find(key)) { |
505 *offset = v->fOffset; | 485 *offset = v->fOffset; |
506 if (v != fLRU.head()) { | 486 if (v != fLRU.head()) { |
507 fLRU.remove(v); | 487 fLRU.remove(v); |
508 fLRU.addToHead(v); | 488 fLRU.addToHead(v); |
509 } | 489 } |
510 return v->fImage; | 490 return v->fImage; |
511 } | 491 } |
512 return nullptr; | 492 return nullptr; |
513 } | 493 } |
514 | 494 |
515 void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) ove
rride { | |
516 SkAutoMutexAcquire mutex(fMutex); | |
517 if (Value* v = fLookup.find(key)) { | |
518 this->removeInternal(v); | |
519 } | |
520 Value* v = new Value(key, result, offset); | |
521 fLookup.add(v); | |
522 fLRU.addToHead(v); | |
523 fCurrentBytes += result.getSize(); | |
524 while (fCurrentBytes > fMaxBytes) { | |
525 Value* tail = fLRU.tail(); | |
526 SkASSERT(tail); | |
527 if (tail == v) { | |
528 break; | |
529 } | |
530 this->removeInternal(tail); | |
531 } | |
532 } | |
533 | |
534 void set(const Key& key, SkSpecialImage* image, const SkIPoint& offset) over
ride { | 495 void set(const Key& key, SkSpecialImage* image, const SkIPoint& offset) over
ride { |
535 SkAutoMutexAcquire mutex(fMutex); | 496 SkAutoMutexAcquire mutex(fMutex); |
536 if (Value* v = fLookup.find(key)) { | 497 if (Value* v = fLookup.find(key)) { |
537 this->removeInternal(v); | 498 this->removeInternal(v); |
538 } | 499 } |
539 Value* v = new Value(key, image, offset); | 500 Value* v = new Value(key, image, offset); |
540 fLookup.add(v); | 501 fLookup.add(v); |
541 fLRU.addToHead(v); | 502 fLRU.addToHead(v); |
542 fCurrentBytes += image->getSize(); | 503 fCurrentBytes += image->getSize(); |
543 while (fCurrentBytes > fMaxBytes) { | 504 while (fCurrentBytes > fMaxBytes) { |
(...skipping 20 matching lines...) Expand all Loading... |
564 for (int i = 0; i < count; i++) { | 525 for (int i = 0; i < count; i++) { |
565 if (Value* v = fLookup.find(keys[i])) { | 526 if (Value* v = fLookup.find(keys[i])) { |
566 this->removeInternal(v); | 527 this->removeInternal(v); |
567 } | 528 } |
568 } | 529 } |
569 } | 530 } |
570 | 531 |
571 SkDEBUGCODE(int count() const override { return fLookup.count(); }) | 532 SkDEBUGCODE(int count() const override { return fLookup.count(); }) |
572 private: | 533 private: |
573 void removeInternal(Value* v) { | 534 void removeInternal(Value* v) { |
574 if (v->fImage) { | 535 SkASSERT(v->fImage); |
575 fCurrentBytes -= v->fImage->getSize(); | 536 fCurrentBytes -= v->fImage->getSize(); |
576 } else { | |
577 fCurrentBytes -= v->fBitmap.getSize(); | |
578 } | |
579 fLRU.remove(v); | 537 fLRU.remove(v); |
580 fLookup.remove(v->fKey); | 538 fLookup.remove(v->fKey); |
581 delete v; | 539 delete v; |
582 } | 540 } |
583 private: | 541 private: |
584 SkTDynamicHash<Value, Key> fLookup; | 542 SkTDynamicHash<Value, Key> fLookup; |
585 mutable SkTInternalLList<Value> fLRU; | 543 mutable SkTInternalLList<Value> fLRU; |
586 size_t fMaxBytes; | 544 size_t fMaxBytes; |
587 size_t fCurrentBytes; | 545 size_t fCurrentBytes; |
588 mutable SkMutex fMutex; | 546 mutable SkMutex fMutex; |
589 }; | 547 }; |
590 | 548 |
591 } // namespace | 549 } // namespace |
592 | 550 |
593 SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) { | 551 SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) { |
594 return new CacheImpl(maxBytes); | 552 return new CacheImpl(maxBytes); |
595 } | 553 } |
596 | 554 |
597 SK_DECLARE_STATIC_ONCE_PTR(SkImageFilter::Cache, cache); | 555 SK_DECLARE_STATIC_ONCE_PTR(SkImageFilter::Cache, cache); |
598 SkImageFilter::Cache* SkImageFilter::Cache::Get() { | 556 SkImageFilter::Cache* SkImageFilter::Cache::Get() { |
599 return cache.get([]{ return SkImageFilter::Cache::Create(kDefaultCacheSize);
}); | 557 return cache.get([]{ return SkImageFilter::Cache::Create(kDefaultCacheSize);
}); |
600 } | 558 } |
601 | 559 |
602 void SkImageFilter::PurgeCache() { | 560 void SkImageFilter::PurgeCache() { |
603 Cache::Get()->purge(); | 561 Cache::Get()->purge(); |
604 } | 562 } |
605 | 563 |
OLD | NEW |