blob: 474438fa9ce29407c044feb5df030a40e9a7d8cb [file] [log] [blame]
bsalomon453cf402014-11-11 14:15:57 -08001
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
14/**
15 * This class allows code internal to Skia privileged access to manage the cache keys of a
16 * GrGpuResource object.
17 */
18class GrGpuResource::CacheAccess {
19public:
20 /**
21 * Sets a content key for the resource. If the resource was previously cached as scratch it will
22 * be converted to a content resource. Currently this may only be called once per resource. It
23 * fails if there is already a resource with the same content key. TODO: make this supplant the
24 * resource that currently is using the content key, allow resources' content keys to change,
25 * and allow removal of a content key to convert a resource back to scratch.
26 */
bsalomon24db3b12015-01-23 04:24:04 -080027 bool setContentKey(const GrContentKey& contentKey) {
bsalomon453cf402014-11-11 14:15:57 -080028 return fResource->setContentKey(contentKey);
29 }
30
31 /**
bsalomonc2f35b72015-01-23 07:19:22 -080032 * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
33 * key, and does not have a content key.
bsalomon453cf402014-11-11 14:15:57 -080034 */
35 bool isScratch() const {
bsalomonc2f35b72015-01-23 07:19:22 -080036 return !this->getContentKey().isValid() && fResource->fScratchKey.isValid() &&
37 this->isBudgeted();
bsalomon453cf402014-11-11 14:15:57 -080038 }
39
40 /**
41 * If this resource can be used as a scratch resource this returns a valid scratch key.
42 * Otherwise it returns a key for which isNullScratch is true. The resource may currently be
bsalomon7775c852014-12-30 12:50:52 -080043 * used as a content resource rather than scratch. Check isScratch().
bsalomon453cf402014-11-11 14:15:57 -080044 */
bsalomon7775c852014-12-30 12:50:52 -080045 const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; }
bsalomon453cf402014-11-11 14:15:57 -080046
47 /**
bsalomon10e23ca2014-11-25 05:52:06 -080048 * If the resource has a scratch key, the key will be removed. Since scratch keys are installed
49 * at resource creation time, this means the resource will never again be used as scratch.
50 */
51 void removeScratchKey() const { fResource->removeScratchKey(); }
52
53 /**
bsalomon453cf402014-11-11 14:15:57 -080054 * If the resource is currently cached by a content key, the key is returned, otherwise NULL.
55 */
bsalomon24db3b12015-01-23 04:24:04 -080056 const GrContentKey& getContentKey() const { return fResource->fContentKey; }
bsalomon453cf402014-11-11 14:15:57 -080057
bsalomon84c8e622014-11-17 09:33:27 -080058 /**
59 * Is the resource object wrapping an externally allocated GPU resource?
60 */
bsalomon5236cf42015-01-14 10:42:08 -080061 bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResource->fLifeCycle; }
bsalomondace19e2014-11-17 07:34:06 -080062
bsalomon12299ab2014-11-14 13:33:09 -080063 /**
bsalomon84c8e622014-11-17 09:33:27 -080064 * Does the resource count against the resource budget?
65 */
bsalomonc2f35b72015-01-23 07:19:22 -080066 bool isBudgeted() const {
67 bool ret = GrGpuResource::kCached_LifeCycle == fResource->fLifeCycle;
68 SkASSERT(ret || !this->getContentKey().isValid());
69 return ret;
70 }
bsalomon84c8e622014-11-17 09:33:27 -080071
72 /**
bsalomonafe30052015-01-16 07:32:33 -080073 * If the resource is uncached make it cached. Has no effect on resources that are wrapped or
74 * already cached.
75 */
76 void makeBudgeted() { fResource->makeBudgeted(); }
77
78 /**
bsalomonc2f35b72015-01-23 07:19:22 -080079 * If the resource is cached make it uncached. Has no effect on resources that are wrapped or
80 * already uncached. Furthermore, resources with content keys cannot be made unbudgeted.
81 */
82 void makeUnbudgeted() { fResource->makeUnbudgeted(); }
83
84 /**
bsalomon12299ab2014-11-14 13:33:09 -080085 * Called by the cache to delete the resource under normal circumstances.
86 */
87 void release() {
88 fResource->release();
89 if (fResource->isPurgable()) {
90 SkDELETE(fResource);
91 }
92 }
93
94 /**
95 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
96 */
97 void abandon() {
98 fResource->abandon();
99 if (fResource->isPurgable()) {
100 SkDELETE(fResource);
101 }
102 }
103
bsalomon453cf402014-11-11 14:15:57 -0800104private:
105 CacheAccess(GrGpuResource* resource) : fResource(resource) { }
106 CacheAccess(const CacheAccess& that) : fResource(that.fResource) { }
107 CacheAccess& operator=(const CacheAccess&); // unimpl
108
109 // No taking addresses of this type.
110 const CacheAccess* operator&() const;
111 CacheAccess* operator&();
112
113 GrGpuResource* fResource;
114
115 friend class GrGpuResource; // to construct/copy this type.
116};
117
118inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
119
120inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
121 return CacheAccess(const_cast<GrGpuResource*>(this));
122}
123
124#endif