blob: ba07c20f011e009de700f27d205fef89dd4e86c4 [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001
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
bsalomon744998e2014-08-28 09:54:34 -070012#include "GrGpuResource.h"
13#include "GrResourceKey.h"
bsalomon8b79d232014-11-10 10:19:06 -080014#include "SkRefCnt.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070015#include "SkTInternalLList.h"
bsalomon744998e2014-08-28 09:54:34 -070016#include "SkTMultiMap.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070017
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 */
23class GrResourceCache2 {
24public:
25 GrResourceCache2() : fCount(0) {};
26 ~GrResourceCache2();
27
bsalomonbcf0a522014-10-08 08:40:09 -070028 void insertResource(GrGpuResource*);
bsalomonc8dc1f72014-08-21 13:02:13 -070029
bsalomonbcf0a522014-10-08 08:40:09 -070030 void removeResource(GrGpuResource*);
bsalomonc8dc1f72014-08-21 13:02:13 -070031
bsalomon8b79d232014-11-10 10:19:06 -080032 // 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.
bsalomon6d4488c2014-11-11 07:27:16 -080036 bool didSetContentKey(GrGpuResource*);
bsalomon8b79d232014-11-10 10:19:06 -080037
bsalomonc8dc1f72014-08-21 13:02:13 -070038 void abandonAll();
39
40 void releaseAll();
41
bsalomon000f8292014-10-15 19:04:14 -070042 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);
bsalomon8b79d232014-11-10 10:19:06 -080049
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 }
bsalomonbcf0a522014-10-08 08:40:09 -070067
bsalomonc8dc1f72014-08-21 13:02:13 -070068private:
bsalomon16961262014-08-26 14:01:07 -070069#ifdef SK_DEBUG
bsalomon37dd3312014-11-03 08:47:23 -080070 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r); }
bsalomon16961262014-08-26 14:01:07 -070071#endif
72
bsalomonbcf0a522014-10-08 08:40:09 -070073 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -070074
bsalomon744998e2014-08-28 09:54:34 -070075 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
bsalomon8b79d232014-11-10 10:19:06 -080084 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
bsalomon744998e2014-08-28 09:54:34 -070093 int fCount;
94 SkTInternalLList<GrGpuResource> fResources;
95 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -080096 ScratchMap fScratchMap;
97 // This holds all resources that have content keys.
98 ContentHash fContentHash;
bsalomonc8dc1f72014-08-21 13:02:13 -070099};
100
101#endif