blob: b18949c2621c639f533a6935ec2be4e42a10897e [file] [log] [blame]
bsalomon453cf402014-11-11 14:15:57 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrGpuResource.h"
12#include "src/gpu/GrGpuResourcePriv.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013
14namespace skiatest {
15 class Reporter;
16}
bsalomon453cf402014-11-11 14:15:57 -080017
18/**
bsalomon3582d3e2015-02-13 14:20:05 -080019 * This class allows GrResourceCache increased privileged access to GrGpuResource objects.
bsalomon453cf402014-11-11 14:15:57 -080020 */
21class GrGpuResource::CacheAccess {
bsalomon3582d3e2015-02-13 14:20:05 -080022private:
Brian Salomon01ceae92019-04-02 11:49:54 -040023 /** The cache is allowed to go from no refs to 1 ref. */
24 void ref() { fResource->addInitialRef(); }
25
bsalomon453cf402014-11-11 14:15:57 -080026 /**
bsalomonc2f35b72015-01-23 07:19:22 -080027 * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
bsalomon8718aaf2015-02-19 07:24:21 -080028 * key, and does not have a unique key.
bsalomon453cf402014-11-11 14:15:57 -080029 */
30 bool isScratch() const {
bsalomon8718aaf2015-02-19 07:24:21 -080031 return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
Brian Salomonfa2ebea2019-01-24 15:58:58 -050032 GrBudgetedType::kBudgeted == fResource->resourcePriv().budgetedType();
bsalomon453cf402014-11-11 14:15:57 -080033 }
34
bsalomon10e23ca2014-11-25 05:52:06 -080035 /**
bsalomon12299ab2014-11-14 13:33:09 -080036 * Called by the cache to delete the resource under normal circumstances.
37 */
38 void release() {
39 fResource->release();
Robert Phillipsbf8bf832019-08-30 13:13:44 -040040 if (!fResource->hasRef()) {
halcanary385fe4d2015-08-26 13:07:48 -070041 delete fResource;
bsalomon12299ab2014-11-14 13:33:09 -080042 }
43 }
44
45 /**
46 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
47 */
48 void abandon() {
49 fResource->abandon();
Robert Phillipsbf8bf832019-08-30 13:13:44 -040050 if (!fResource->hasRef()) {
halcanary385fe4d2015-08-26 13:07:48 -070051 delete fResource;
bsalomon12299ab2014-11-14 13:33:09 -080052 }
53 }
54
bsalomonf99e9612015-02-19 08:24:16 -080055 /** Called by the cache to assign a new unique key. */
56 void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
57
Robert Phillipsaee18c92019-09-06 11:48:27 -040058 /** Is the resource ref'ed */
Brian Salomon2c791fc2019-04-02 11:52:03 -040059 bool hasRef() const { return fResource->hasRef(); }
60
bsalomonf99e9612015-02-19 08:24:16 -080061 /** Called by the cache to make the unique key invalid. */
62 void removeUniqueKey() { fResource->fUniqueKey.reset(); }
63
bsalomon9f2d1572015-02-17 11:47:40 -080064 uint32_t timestamp() const { return fResource->fTimestamp; }
65 void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
66
Brian Salomon5e150852017-03-22 14:53:13 -040067 void setTimeWhenResourceBecomePurgeable() {
68 SkASSERT(fResource->isPurgeable());
69 fResource->fTimeWhenBecamePurgeable = GrStdSteadyClock::now();
70 }
bsalomone2e87f32016-09-22 12:42:11 -070071 /**
Brian Salomon5e150852017-03-22 14:53:13 -040072 * Called by the cache to determine whether this resource should be purged based on the length
73 * of time it has been available for purging.
74 */
75 GrStdSteadyClock::time_point timeWhenResourceBecamePurgeable() {
76 SkASSERT(fResource->isPurgeable());
77 return fResource->fTimeWhenBecamePurgeable;
78 }
bsalomone2e87f32016-09-22 12:42:11 -070079
bsalomon9f2d1572015-02-17 11:47:40 -080080 int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
81
bsalomon3582d3e2015-02-13 14:20:05 -080082 CacheAccess(GrGpuResource* resource) : fResource(resource) {}
83 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
bsalomon453cf402014-11-11 14:15:57 -080084 CacheAccess& operator=(const CacheAccess&); // unimpl
85
86 // No taking addresses of this type.
Brian Salomon01ceae92019-04-02 11:49:54 -040087 const CacheAccess* operator&() const = delete;
88 CacheAccess* operator&() = delete;
bsalomon453cf402014-11-11 14:15:57 -080089
90 GrGpuResource* fResource;
91
92 friend class GrGpuResource; // to construct/copy this type.
bsalomon3582d3e2015-02-13 14:20:05 -080093 friend class GrResourceCache; // to use this type
94 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
bsalomon453cf402014-11-11 14:15:57 -080095};
96
97inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
98
99inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
100 return CacheAccess(const_cast<GrGpuResource*>(this));
101}
102
103#endif