blob: dd01b72ff0780041d892bafcc6067b57b88a47e0 [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
11#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
13
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();
Brian Salomon9bc76d92019-01-24 12:18:33 -050040 if (!fResource->hasRefOrPendingIO()) {
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();
Brian Salomon9bc76d92019-01-24 12:18:33 -050050 if (!fResource->hasRefOrPendingIO()) {
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
58 /** Called by the cache to make the unique key invalid. */
59 void removeUniqueKey() { fResource->fUniqueKey.reset(); }
60
bsalomon9f2d1572015-02-17 11:47:40 -080061 uint32_t timestamp() const { return fResource->fTimestamp; }
62 void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
63
Brian Salomon5e150852017-03-22 14:53:13 -040064 void setTimeWhenResourceBecomePurgeable() {
65 SkASSERT(fResource->isPurgeable());
66 fResource->fTimeWhenBecamePurgeable = GrStdSteadyClock::now();
67 }
bsalomone2e87f32016-09-22 12:42:11 -070068 /**
Brian Salomon5e150852017-03-22 14:53:13 -040069 * Called by the cache to determine whether this resource should be purged based on the length
70 * of time it has been available for purging.
71 */
72 GrStdSteadyClock::time_point timeWhenResourceBecamePurgeable() {
73 SkASSERT(fResource->isPurgeable());
74 return fResource->fTimeWhenBecamePurgeable;
75 }
bsalomone2e87f32016-09-22 12:42:11 -070076
bsalomon9f2d1572015-02-17 11:47:40 -080077 int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
78
bsalomon3582d3e2015-02-13 14:20:05 -080079 CacheAccess(GrGpuResource* resource) : fResource(resource) {}
80 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
bsalomon453cf402014-11-11 14:15:57 -080081 CacheAccess& operator=(const CacheAccess&); // unimpl
82
83 // No taking addresses of this type.
Brian Salomon01ceae92019-04-02 11:49:54 -040084 const CacheAccess* operator&() const = delete;
85 CacheAccess* operator&() = delete;
bsalomon453cf402014-11-11 14:15:57 -080086
87 GrGpuResource* fResource;
88
89 friend class GrGpuResource; // to construct/copy this type.
bsalomon3582d3e2015-02-13 14:20:05 -080090 friend class GrResourceCache; // to use this type
91 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
bsalomon453cf402014-11-11 14:15:57 -080092};
93
94inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
95
96inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
97 return CacheAccess(const_cast<GrGpuResource*>(this));
98}
99
100#endif