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