| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #ifndef GrResourceCache2_DEFINED | 9 #ifndef GrResourceCache2_DEFINED |
| 10 #define GrResourceCache2_DEFINED | 10 #define GrResourceCache2_DEFINED |
| 11 | 11 |
| 12 #include "GrGpuResource.h" | 12 #include "GrGpuResource.h" |
| 13 #include "GrResourceKey.h" | 13 #include "GrResourceKey.h" |
| 14 #include "SkRefCnt.h" |
| 14 #include "SkTInternalLList.h" | 15 #include "SkTInternalLList.h" |
| 15 #include "SkTMultiMap.h" | 16 #include "SkTMultiMap.h" |
| 16 | 17 |
| 17 /** | 18 /** |
| 18 * Eventual replacement for GrResourceCache. Currently it simply holds a list | 19 * Eventual replacement for GrResourceCache. Currently it simply holds a list |
| 19 * of all GrGpuResource objects for a GrContext. It is used to invalidate all | 20 * of all GrGpuResource objects for a GrContext. It is used to invalidate all |
| 20 * the resources when necessary. | 21 * the resources when necessary. |
| 21 */ | 22 */ |
| 22 class GrResourceCache2 { | 23 class GrResourceCache2 { |
| 23 public: | 24 public: |
| 24 GrResourceCache2() : fCount(0) {}; | 25 GrResourceCache2() : fCount(0) {}; |
| 25 ~GrResourceCache2(); | 26 ~GrResourceCache2(); |
| 26 | 27 |
| 27 void insertResource(GrGpuResource*); | 28 void insertResource(GrGpuResource*); |
| 28 | 29 |
| 29 void removeResource(GrGpuResource*); | 30 void removeResource(GrGpuResource*); |
| 30 | 31 |
| 32 void willRemoveContentKey(const GrGpuResource*); |
| 33 |
| 34 // This currently returns a bool and fails when an existing resource has a k
ey that collides |
| 35 // with the new content key. In the future it will null out the content key
for the existing |
| 36 // resource. The failure is a temporary measure taken because duties are spl
it between two |
| 37 // cache objects currently. |
| 38 bool didAddContentKey(GrGpuResource*); |
| 39 |
| 31 void abandonAll(); | 40 void abandonAll(); |
| 32 | 41 |
| 33 void releaseAll(); | 42 void releaseAll(); |
| 34 | 43 |
| 35 enum { | 44 enum { |
| 36 /** Preferentially returns scratch resources with no pending IO. */ | 45 /** Preferentially returns scratch resources with no pending IO. */ |
| 37 kPreferNoPendingIO_ScratchFlag = 0x1, | 46 kPreferNoPendingIO_ScratchFlag = 0x1, |
| 38 /** Will not return any resources that match but have pending IO. */ | 47 /** Will not return any resources that match but have pending IO. */ |
| 39 kRequireNoPendingIO_ScratchFlag = 0x2, | 48 kRequireNoPendingIO_ScratchFlag = 0x2, |
| 40 }; | 49 }; |
| 41 GrGpuResource* findAndRefScratchResource(const GrResourceKey& scratchKey, ui
nt32_t flags = 0); | 50 GrGpuResource* findAndRefScratchResource(const GrResourceKey& scratchKey, ui
nt32_t flags = 0); |
| 51 |
| 52 #ifdef SK_DEBUG |
| 53 // This is not particularly fast and only used for validation, so debug only
. |
| 54 int countScratchEntriesForKey(const GrResourceKey& scratchKey) const { |
| 55 SkASSERT(scratchKey.isScratch()); |
| 56 return fScratchMap.countForKey(scratchKey); |
| 57 } |
| 58 #endif |
| 59 |
| 60 GrGpuResource* findAndRefContentResource(const GrResourceKey& contentKey) { |
| 61 SkASSERT(!contentKey.isScratch()); |
| 62 return SkSafeRef(fContentHash.find(contentKey)); |
| 63 } |
| 64 |
| 65 bool hasContentKey(const GrResourceKey& contentKey) const { |
| 66 SkASSERT(!contentKey.isScratch()); |
| 67 return SkToBool(fContentHash.find(contentKey)); |
| 68 } |
| 42 | 69 |
| 43 private: | 70 private: |
| 44 #ifdef SK_DEBUG | 71 #ifdef SK_DEBUG |
| 45 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r)
; } | 72 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r)
; } |
| 46 #endif | 73 #endif |
| 47 | 74 |
| 48 class AvailableForScratchUse; | 75 class AvailableForScratchUse; |
| 49 | 76 |
| 50 struct ScratchMapTraits { | 77 struct ScratchMapTraits { |
| 51 static const GrResourceKey& GetKey(const GrGpuResource& r) { | 78 static const GrResourceKey& GetKey(const GrGpuResource& r) { |
| 52 return r.getScratchKey(); | 79 return r.getScratchKey(); |
| 53 } | 80 } |
| 54 | 81 |
| 55 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } | 82 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
| 56 }; | 83 }; |
| 57 typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchM
ap; | 84 typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchM
ap; |
| 58 | 85 |
| 86 struct ContentHashTraits { |
| 87 static const GrResourceKey& GetKey(const GrGpuResource& r) { |
| 88 return *r.getContentKey(); |
| 89 } |
| 90 |
| 91 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
| 92 }; |
| 93 typedef SkTDynamicHash<GrGpuResource, GrResourceKey, ContentHashTraits> Cont
entHash; |
| 94 |
| 59 int fCount; | 95 int fCount; |
| 60 SkTInternalLList<GrGpuResource> fResources; | 96 SkTInternalLList<GrGpuResource> fResources; |
| 61 // This map holds all resources that can be used as scratch resources. | 97 // This map holds all resources that can be used as scratch resources. |
| 62 ScratchMap fScratchMap; | 98 ScratchMap fScratchMap; |
| 99 // This holds all resources that have content keys. |
| 100 ContentHash fContentHash; |
| 63 }; | 101 }; |
| 64 | 102 |
| 65 #endif | 103 #endif |
| OLD | NEW |