blob: ce3b597184179824e2037702f551e3c0feea823e [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:
bsalomon453cf402014-11-11 14:15:57 -080023 /**
bsalomonc2f35b72015-01-23 07:19:22 -080024 * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
bsalomon8718aaf2015-02-19 07:24:21 -080025 * key, and does not have a unique key.
bsalomon453cf402014-11-11 14:15:57 -080026 */
27 bool isScratch() const {
bsalomon8718aaf2015-02-19 07:24:21 -080028 return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
bsalomon5ec26ae2016-02-25 08:33:02 -080029 SkBudgeted::kYes == fResource->resourcePriv().isBudgeted();
bsalomon453cf402014-11-11 14:15:57 -080030 }
31
bsalomon10e23ca2014-11-25 05:52:06 -080032 /**
Greg Daniel2268ad22018-11-15 09:27:38 -050033 * Even if the resource has a unique key should we still try to purge it as soon as possible.
34 */
35 bool shouldPurgeImmediately() const { return fResource->fShouldPurgeImmediately; }
36
37 /**
Brian Salomon614c1a82018-12-19 15:42:06 -050038 * Called by GrResourceCache when a resource becomes purgeable regardless of whether the cache
39 * has decided to keep the resource ot purge it immediately.
40 */
41 void becamePurgeable() { fResource->becamePurgeable(); }
42
43 /**
bsalomon12299ab2014-11-14 13:33:09 -080044 * Called by the cache to delete the resource under normal circumstances.
45 */
46 void release() {
47 fResource->release();
halcanary385fe4d2015-08-26 13:07:48 -070048 if (fResource->isPurgeable()) {
49 delete fResource;
bsalomon12299ab2014-11-14 13:33:09 -080050 }
51 }
52
53 /**
54 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
55 */
56 void abandon() {
57 fResource->abandon();
halcanary385fe4d2015-08-26 13:07:48 -070058 if (fResource->isPurgeable()) {
59 delete fResource;
bsalomon12299ab2014-11-14 13:33:09 -080060 }
61 }
62
bsalomonf99e9612015-02-19 08:24:16 -080063 /** Called by the cache to assign a new unique key. */
64 void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
65
66 /** Called by the cache to make the unique key invalid. */
67 void removeUniqueKey() { fResource->fUniqueKey.reset(); }
68
bsalomon9f2d1572015-02-17 11:47:40 -080069 uint32_t timestamp() const { return fResource->fTimestamp; }
70 void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
71
Brian Salomon5e150852017-03-22 14:53:13 -040072 void setTimeWhenResourceBecomePurgeable() {
73 SkASSERT(fResource->isPurgeable());
74 fResource->fTimeWhenBecamePurgeable = GrStdSteadyClock::now();
75 }
bsalomone2e87f32016-09-22 12:42:11 -070076 /**
Brian Salomon5e150852017-03-22 14:53:13 -040077 * Called by the cache to determine whether this resource should be purged based on the length
78 * of time it has been available for purging.
79 */
80 GrStdSteadyClock::time_point timeWhenResourceBecamePurgeable() {
81 SkASSERT(fResource->isPurgeable());
82 return fResource->fTimeWhenBecamePurgeable;
83 }
bsalomone2e87f32016-09-22 12:42:11 -070084
bsalomon9f2d1572015-02-17 11:47:40 -080085 int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
86
bsalomon3582d3e2015-02-13 14:20:05 -080087 CacheAccess(GrGpuResource* resource) : fResource(resource) {}
88 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
bsalomon453cf402014-11-11 14:15:57 -080089 CacheAccess& operator=(const CacheAccess&); // unimpl
90
91 // No taking addresses of this type.
92 const CacheAccess* operator&() const;
93 CacheAccess* operator&();
94
95 GrGpuResource* fResource;
96
97 friend class GrGpuResource; // to construct/copy this type.
bsalomon3582d3e2015-02-13 14:20:05 -080098 friend class GrResourceCache; // to use this type
99 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
bsalomon453cf402014-11-11 14:15:57 -0800100};
101
102inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
103
104inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
105 return CacheAccess(const_cast<GrGpuResource*>(this));
106}
107
108#endif