blob: e220e5f263c185e2ff3c0139e77d71029711da88 [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 {
41 SkASSERT(fResource->fScratchKey.isScratch());
42 return NULL == this->getContentKey() && !fResource->fScratchKey.isNullScratch();
43 }
44
45 /**
46 * If this resource can be used as a scratch resource this returns a valid scratch key.
47 * Otherwise it returns a key for which isNullScratch is true. The resource may currently be
48 * used as content resource rather than scratch. Check isScratch().
49 */
50 const GrResourceKey& getScratchKey() const { return fResource->fScratchKey; }
51
52 /**
bsalomon10e23ca2014-11-25 05:52:06 -080053 * If the resource has a scratch key, the key will be removed. Since scratch keys are installed
54 * at resource creation time, this means the resource will never again be used as scratch.
55 */
56 void removeScratchKey() const { fResource->removeScratchKey(); }
57
58 /**
bsalomon453cf402014-11-11 14:15:57 -080059 * If the resource is currently cached by a content key, the key is returned, otherwise NULL.
60 */
61 const GrResourceKey* getContentKey() const {
bsalomon84c8e622014-11-17 09:33:27 -080062 if (fResource->fFlags & GrGpuResource::kContentKeySet_Flag) {
bsalomon453cf402014-11-11 14:15:57 -080063 return &fResource->fContentKey;
64 }
65 return NULL;
66 }
67
bsalomon84c8e622014-11-17 09:33:27 -080068 /**
69 * Is the resource object wrapping an externally allocated GPU resource?
70 */
bsalomondace19e2014-11-17 07:34:06 -080071 bool isWrapped() const { return fResource->isWrapped(); }
72
bsalomon12299ab2014-11-14 13:33:09 -080073 /**
bsalomon84c8e622014-11-17 09:33:27 -080074 * Does the resource count against the resource budget?
75 */
76 bool isBudgeted() const {
77 bool ret = SkToBool(GrGpuResource::kBudgeted_Flag & fResource->fFlags);
78 SkASSERT(!(ret && fResource->isWrapped()));
79 return ret;
80 }
81
82 /**
bsalomon12299ab2014-11-14 13:33:09 -080083 * Called by the cache to delete the resource under normal circumstances.
84 */
85 void release() {
86 fResource->release();
87 if (fResource->isPurgable()) {
88 SkDELETE(fResource);
89 }
90 }
91
92 /**
93 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
94 */
95 void abandon() {
96 fResource->abandon();
97 if (fResource->isPurgable()) {
98 SkDELETE(fResource);
99 }
100 }
101
bsalomon453cf402014-11-11 14:15:57 -0800102private:
103 CacheAccess(GrGpuResource* resource) : fResource(resource) { }
104 CacheAccess(const CacheAccess& that) : fResource(that.fResource) { }
105 CacheAccess& operator=(const CacheAccess&); // unimpl
106
107 // No taking addresses of this type.
108 const CacheAccess* operator&() const;
109 CacheAccess* operator&();
110
111 GrGpuResource* fResource;
112
113 friend class GrGpuResource; // to construct/copy this type.
114};
115
116inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
117
118inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
119 return CacheAccess(const_cast<GrGpuResource*>(this));
120}
121
122#endif