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