blob: 1cc958799d44a538674cdb2573e368a8c058b8cd [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"
bsalomon453cf402014-11-11 14:15:57 -080013#include "GrGpuResourceCacheAccess.h"
bsalomon744998e2014-08-28 09:54:34 -070014#include "GrResourceKey.h"
bsalomon8b79d232014-11-10 10:19:06 -080015#include "SkRefCnt.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070016#include "SkTInternalLList.h"
bsalomon744998e2014-08-28 09:54:34 -070017#include "SkTMultiMap.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070018
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 */
24class GrResourceCache2 {
25public:
26 GrResourceCache2() : fCount(0) {};
27 ~GrResourceCache2();
28
bsalomonbcf0a522014-10-08 08:40:09 -070029 void insertResource(GrGpuResource*);
bsalomonc8dc1f72014-08-21 13:02:13 -070030
bsalomonbcf0a522014-10-08 08:40:09 -070031 void removeResource(GrGpuResource*);
bsalomonc8dc1f72014-08-21 13:02:13 -070032
bsalomon8b79d232014-11-10 10:19:06 -080033 // 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.
bsalomon6d4488c2014-11-11 07:27:16 -080037 bool didSetContentKey(GrGpuResource*);
bsalomon8b79d232014-11-10 10:19:06 -080038
bsalomonc8dc1f72014-08-21 13:02:13 -070039 void abandonAll();
40
41 void releaseAll();
42
bsalomon000f8292014-10-15 19:04:14 -070043 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);
bsalomon8b79d232014-11-10 10:19:06 -080050
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 }
bsalomonbcf0a522014-10-08 08:40:09 -070068
bsalomonc8dc1f72014-08-21 13:02:13 -070069private:
bsalomon16961262014-08-26 14:01:07 -070070#ifdef SK_DEBUG
bsalomon37dd3312014-11-03 08:47:23 -080071 bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r); }
bsalomon16961262014-08-26 14:01:07 -070072#endif
73
bsalomonbcf0a522014-10-08 08:40:09 -070074 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -070075
bsalomon744998e2014-08-28 09:54:34 -070076 struct ScratchMapTraits {
77 static const GrResourceKey& GetKey(const GrGpuResource& r) {
bsalomon453cf402014-11-11 14:15:57 -080078 return r.cacheAccess().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -070079 }
80
81 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
82 };
83 typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchMap;
84
bsalomon8b79d232014-11-10 10:19:06 -080085 struct ContentHashTraits {
86 static const GrResourceKey& GetKey(const GrGpuResource& r) {
bsalomon453cf402014-11-11 14:15:57 -080087 return *r.cacheAccess().getContentKey();
bsalomon8b79d232014-11-10 10:19:06 -080088 }
89
90 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
91 };
92 typedef SkTDynamicHash<GrGpuResource, GrResourceKey, ContentHashTraits> ContentHash;
93
bsalomon744998e2014-08-28 09:54:34 -070094 int fCount;
95 SkTInternalLList<GrGpuResource> fResources;
96 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -080097 ScratchMap fScratchMap;
98 // This holds all resources that have content keys.
99 ContentHash fContentHash;
bsalomonc8dc1f72014-08-21 13:02:13 -0700100};
101
102#endif