• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_CACHED_OBJECT_H_
16 #define DAWNNATIVE_CACHED_OBJECT_H_
17 
18 #include <cstddef>
19 
20 namespace dawn_native {
21 
22     // Some objects are cached so that instead of creating new duplicate objects,
23     // we increase the refcount of an existing object.
24     // When an object is successfully created, the device should call
25     // SetIsCachedReference() and insert the object into the cache.
26     class CachedObject {
27       public:
28         bool IsCachedReference() const;
29 
30         // Functor necessary for the unordered_set<CachedObject*>-based cache.
31         struct HashFunc {
32             size_t operator()(const CachedObject* obj) const;
33         };
34 
35         size_t GetContentHash() const;
36         void SetContentHash(size_t contentHash);
37 
38       private:
39         friend class DeviceBase;
40         void SetIsCachedReference();
41 
42         bool mIsCachedReference = false;
43 
44         // Called by ObjectContentHasher upon creation to record the object.
45         virtual size_t ComputeContentHash() = 0;
46 
47         size_t mContentHash = 0;
48         bool mIsContentHashInitialized = false;
49     };
50 
51 }  // namespace dawn_native
52 
53 #endif  // DAWNNATIVE_CACHED_OBJECT_H_
54