Flutter Engine
The Flutter Engine
GrGpuResourceCacheAccess.h
Go to the documentation of this file.
1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrGpuResourceCacheAccess_DEFINED
9#define GrGpuResourceCacheAccess_DEFINED
10
13
14namespace skiatest {
15 class Reporter;
16} // namespace skiatest
17
18/**
19 * This class allows GrResourceCache increased privileged access to GrGpuResource objects.
20 */
22private:
23 /** The cache is allowed to go from no refs to 1 ref. */
24 void ref() { fResource->addInitialRef(); }
25
26 /**
27 * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
28 * key, and does not have a unique key.
29 */
30 bool isScratch() const {
31 return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
33 }
34
35 bool isUsableAsScratch() const {
36 return this->isScratch() && !fResource->internalHasRef();
37 }
38
39 /**
40 * Called by the cache to delete the resource under normal circumstances.
41 */
42 void release() {
43 fResource->release();
44 if (!fResource->hasRef() && fResource->hasNoCommandBufferUsages()) {
45 delete fResource;
46 }
47 }
48
49 /**
50 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
51 */
52 void abandon() {
53 fResource->abandon();
54 if (!fResource->hasRef() && fResource->hasNoCommandBufferUsages()) {
55 delete fResource;
56 }
57 }
58
59 /** Called by the cache to assign a new unique key. */
60 void setUniqueKey(const skgpu::UniqueKey& key) { fResource->fUniqueKey = key; }
61
62 /** Is the resource ref'ed */
63 bool hasRef() const { return fResource->hasRef(); }
64 bool hasRefOrCommandBufferUsage() const {
65 return this->hasRef() || !fResource->hasNoCommandBufferUsages();
66 }
67
68 /** Called by the cache to make the unique key invalid. */
69 void removeUniqueKey() { fResource->fUniqueKey.reset(); }
70
71 uint32_t timestamp() const { return fResource->fTimestamp; }
72 void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
73
74 void setTimeWhenResourceBecomePurgeable() {
75 SkASSERT(fResource->isPurgeable());
76 fResource->fTimeWhenBecamePurgeable = skgpu::StdSteadyClock::now();
77 }
78 /**
79 * Called by the cache to determine whether this resource should be purged based on the length
80 * of time it has been available for purging.
81 */
82 skgpu::StdSteadyClock::time_point timeWhenResourceBecamePurgeable() {
83 SkASSERT(fResource->isPurgeable());
84 return fResource->fTimeWhenBecamePurgeable;
85 }
86
87 int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
88
90 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
91 CacheAccess& operator=(const CacheAccess&) = delete;
92
93 // No taking addresses of this type.
94 const CacheAccess* operator&() const = delete;
95 CacheAccess* operator&() = delete;
96
97 GrGpuResource* fResource;
98
99 friend class GrGpuResource; // to construct/copy this type.
100 friend class GrResourceCache; // to use this type
101 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
102};
103
105
106inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { // NOLINT(readability-const-return-type)
107 return CacheAccess(const_cast<GrGpuResource*>(this));
108}
109
110#endif
reporter
Definition: FontMgrTest.cpp:39
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkString resource(SkPDFResourceType type, int index)
friend void test_unbudgeted_to_scratch(skiatest::Reporter *reporter)
GrBudgetedType budgetedType() const
const skgpu::UniqueKey & getUniqueKey() const
ResourcePriv resourcePriv()
void addInitialRef() const
Definition: GrGpuResource.h:94
bool internalHasRef() const
Definition: GrGpuResource.h:88
bool isValid() const
Definition: ResourceKey.h:55