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                 SkBudgeted::kYes == fResource->resourcePriv().isBudgeted();
31     }
32 
33     /**
34      * Is the resource object wrapping an externally allocated GPU resource?
35      */
isExternal()36     bool isExternal() const { return fResource->isExternal(); }
37 
38     /**
39      * Is the resource object wrapping an externally allocated GPU resource that Skia has not taken
40      * ownership of.
41      */
isBorrowed()42     bool isBorrowed() const { return GrGpuResource::kBorrowed_LifeCycle == fResource->fLifeCycle; }
43 
44     /**
45      * Is the resource object wrapping an externally allocated GPU resource that Skia has taken
46      * ownership of.
47      */
isAdopted()48     bool isAdopted() const { return GrGpuResource::kAdopted_LifeCycle == fResource->fLifeCycle; }
49 
50     /**
51      * Called by the cache to delete the resource under normal circumstances.
52      */
release()53     void release() {
54         fResource->release();
55         if (fResource->isPurgeable()) {
56             delete fResource;
57         }
58     }
59 
60     /**
61      * Called by the cache to delete the resource when the backend 3D context is no longer valid.
62      */
abandon()63     void abandon() {
64         fResource->abandon();
65         if (fResource->isPurgeable()) {
66             delete fResource;
67         }
68     }
69 
70     /** Called by the cache to assign a new unique key. */
setUniqueKey(const GrUniqueKey & key)71     void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
72 
73     /** Called by the cache to make the unique key invalid. */
removeUniqueKey()74     void removeUniqueKey() { fResource->fUniqueKey.reset(); }
75 
timestamp()76     uint32_t timestamp() const { return fResource->fTimestamp; }
setTimestamp(uint32_t ts)77     void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
78 
accessCacheIndex()79     int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
80 
CacheAccess(GrGpuResource * resource)81     CacheAccess(GrGpuResource* resource) : fResource(resource) {}
CacheAccess(const CacheAccess & that)82     CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
83     CacheAccess& operator=(const CacheAccess&); // unimpl
84 
85     // No taking addresses of this type.
86     const CacheAccess* operator&() const;
87     CacheAccess* operator&();
88 
89     GrGpuResource* fResource;
90 
91     friend class GrGpuResource; // to construct/copy this type.
92     friend class GrResourceCache; // to use this type
93     friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
94 };
95 
cacheAccess()96 inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
97 
cacheAccess()98 inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
99     return CacheAccess(const_cast<GrGpuResource*>(this));
100 }
101 
102 #endif
103