blob: 475317ddb21242ea4c016f178de0e34e96a33a51 [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 */
27 bool setContentKey(const GrResourceKey& contentKey) {
28 return fResource->setContentKey(contentKey);
29 }
30
31 /**
bsalomon84c8e622014-11-17 09:33:27 -080032 * Changes whether the resource counts against the resource cache budget.
33 */
34 void setBudgeted(bool countsAgainstBudget) { fResource->setBudgeted(countsAgainstBudget); }
35
36 /**
bsalomon453cf402014-11-11 14:15:57 -080037 * Is the resource currently cached as scratch? This means it has a valid scratch key and does
38 * not have a content key.
39 */
40 bool isScratch() const {
bsalomon7775c852014-12-30 12:50:52 -080041 return NULL == this->getContentKey() && fResource->fScratchKey.isValid();
bsalomon453cf402014-11-11 14:15:57 -080042 }
43
44 /**
45 * If this resource can be used as a scratch resource this returns a valid scratch key.
46 * Otherwise it returns a key for which isNullScratch is true. The resource may currently be
bsalomon7775c852014-12-30 12:50:52 -080047 * used as a content resource rather than scratch. Check isScratch().
bsalomon453cf402014-11-11 14:15:57 -080048 */
bsalomon7775c852014-12-30 12:50:52 -080049 const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; }
bsalomon453cf402014-11-11 14:15:57 -080050
51 /**
bsalomon10e23ca2014-11-25 05:52:06 -080052 * If the resource has a scratch key, the key will be removed. Since scratch keys are installed
53 * at resource creation time, this means the resource will never again be used as scratch.
54 */
55 void removeScratchKey() const { fResource->removeScratchKey(); }
56
57 /**
bsalomon453cf402014-11-11 14:15:57 -080058 * If the resource is currently cached by a content key, the key is returned, otherwise NULL.
59 */
60 const GrResourceKey* getContentKey() const {
bsalomon84c8e622014-11-17 09:33:27 -080061 if (fResource->fFlags & GrGpuResource::kContentKeySet_Flag) {
bsalomon453cf402014-11-11 14:15:57 -080062 return &fResource->fContentKey;
63 }
64 return NULL;
65 }
66
bsalomon84c8e622014-11-17 09:33:27 -080067 /**
68 * Is the resource object wrapping an externally allocated GPU resource?
69 */
bsalomondace19e2014-11-17 07:34:06 -080070 bool isWrapped() const { return fResource->isWrapped(); }
71
bsalomon12299ab2014-11-14 13:33:09 -080072 /**
bsalomon84c8e622014-11-17 09:33:27 -080073 * Does the resource count against the resource budget?
74 */
75 bool isBudgeted() const {
76 bool ret = SkToBool(GrGpuResource::kBudgeted_Flag & fResource->fFlags);
77 SkASSERT(!(ret && fResource->isWrapped()));
78 return ret;
79 }
80
81 /**
bsalomon12299ab2014-11-14 13:33:09 -080082 * Called by the cache to delete the resource under normal circumstances.
83 */
84 void release() {
85 fResource->release();
86 if (fResource->isPurgable()) {
87 SkDELETE(fResource);
88 }
89 }
90
91 /**
92 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
93 */
94 void abandon() {
95 fResource->abandon();
96 if (fResource->isPurgable()) {
97 SkDELETE(fResource);
98 }
99 }
100
bsalomon453cf402014-11-11 14:15:57 -0800101private:
102 CacheAccess(GrGpuResource* resource) : fResource(resource) { }
103 CacheAccess(const CacheAccess& that) : fResource(that.fResource) { }
104 CacheAccess& operator=(const CacheAccess&); // unimpl
105
106 // No taking addresses of this type.
107 const CacheAccess* operator&() const;
108 CacheAccess* operator&();
109
110 GrGpuResource* fResource;
111
112 friend class GrGpuResource; // to construct/copy this type.
113};
114
115inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
116
117inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
118 return CacheAccess(const_cast<GrGpuResource*>(this));
119}
120
121#endif