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 GrGpuResourceCacheAccess_DEFINED
10 #define GrGpuResourceCacheAccess_DEFINED
11 
12 #include "GrGpuResource.h"
13 #include "GrGpuResourcePriv.h"
14 
15 namespace skiatest {
16     class Reporter;
17 }
18 
19 /**
20  * This class allows GrResourceCache increased privileged access to GrGpuResource objects.
21  */
22 class GrGpuResource::CacheAccess {
23 private:
24     /**
25      * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
26      * key, and does not have a unique key.
27      */
isScratch()28     bool isScratch() const {
29         return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
30                 fResource->resourcePriv().isBudgeted();
31     }
32 
33     /**
34      * Is the resource object wrapping an externally allocated GPU resource?
35      */
isWrapped()36     bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResource->fLifeCycle; }
37 
38     /**
39      * Called by the cache to delete the resource under normal circumstances.
40      */
release()41     void release() {
42         fResource->release();
43         if (fResource->isPurgeable()) {
44             SkDELETE(fResource);
45         }
46     }
47 
48     /**
49      * Called by the cache to delete the resource when the backend 3D context is no longer valid.
50      */
abandon()51     void abandon() {
52         fResource->abandon();
53         if (fResource->isPurgeable()) {
54             SkDELETE(fResource);
55         }
56     }
57 
58     /** Called by the cache to assign a new unique key. */
setUniqueKey(const GrUniqueKey & key)59     void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
60 
61     /** Called by the cache to make the unique key invalid. */
removeUniqueKey()62     void removeUniqueKey() { fResource->fUniqueKey.reset(); }
63 
timestamp()64     uint32_t timestamp() const { return fResource->fTimestamp; }
setTimestamp(uint32_t ts)65     void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
66 
accessCacheIndex()67     int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
68 
CacheAccess(GrGpuResource * resource)69     CacheAccess(GrGpuResource* resource) : fResource(resource) {}
CacheAccess(const CacheAccess & that)70     CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
71     CacheAccess& operator=(const CacheAccess&); // unimpl
72 
73     // No taking addresses of this type.
74     const CacheAccess* operator&() const;
75     CacheAccess* operator&();
76 
77     GrGpuResource* fResource;
78 
79     friend class GrGpuResource; // to construct/copy this type.
80     friend class GrResourceCache; // to use this type
81     friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
82 };
83 
cacheAccess()84 inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
85 
cacheAccess()86 inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
87     return CacheAccess(const_cast<GrGpuResource*>(this));
88 }
89 
90 #endif
91