blob: 9424c40850a22bbf42c438e9544f45ceecf169cc [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 void willRemoveContentKey(const GrGpuResource*);
33
34 // This currently returns a bool and fails when an existing resource has a key 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 split between two
37 // cache objects currently.
38 bool didAddContentKey(GrGpuResource*);
39
bsalomonc8dc1f72014-08-21 13:02:13 -070040 void abandonAll();
41
42 void releaseAll();
43
bsalomon000f8292014-10-15 19:04:14 -070044 enum {
45 /** Preferentially returns scratch resources with no pending IO. */
46 kPreferNoPendingIO_ScratchFlag = 0x1,
47 /** Will not return any resources that match but have pending IO. */
48 kRequireNoPendingIO_ScratchFlag = 0x2,
49 };
50 GrGpuResource* findAndRefScratchResource(const GrResourceKey& scratchKey, uint32_t flags = 0);
bsalomon8b79d232014-11-10 10:19:06 -080051
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 }
bsalomonbcf0a522014-10-08 08:40:09 -070069
bsalomonc8dc1f72014-08-21 13:02:13 -070070private:
bsalomon16961262014-08-26 14:01:07 -070071#ifdef SK_DEBUG
bsalomon37dd3312014-11-03 08:47:23 -080072 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r); }
bsalomon16961262014-08-26 14:01:07 -070073#endif
74
bsalomonbcf0a522014-10-08 08:40:09 -070075 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -070076
bsalomon744998e2014-08-28 09:54:34 -070077 struct ScratchMapTraits {
78 static const GrResourceKey& GetKey(const GrGpuResource& r) {
79 return r.getScratchKey();
80 }
81
82 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
83 };
84 typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchMap;
85
bsalomon8b79d232014-11-10 10:19:06 -080086 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> ContentHash;
94
bsalomon744998e2014-08-28 09:54:34 -070095 int fCount;
96 SkTInternalLList<GrGpuResource> fResources;
97 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -080098 ScratchMap fScratchMap;
99 // This holds all resources that have content keys.
100 ContentHash fContentHash;
bsalomonc8dc1f72014-08-21 13:02:13 -0700101};
102
103#endif