blob: c55bb0777494baa3a9508107baf55d79ee86c23d [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 /**
53 * If the resource is currently cached by a content key, the key is returned, otherwise NULL.
54 */
55 const GrResourceKey* getContentKey() const {
bsalomon84c8e622014-11-17 09:33:27 -080056 if (fResource->fFlags & GrGpuResource::kContentKeySet_Flag) {
bsalomon453cf402014-11-11 14:15:57 -080057 return &fResource->fContentKey;
58 }
59 return NULL;
60 }
61
bsalomon84c8e622014-11-17 09:33:27 -080062 /**
63 * Is the resource object wrapping an externally allocated GPU resource?
64 */
bsalomondace19e2014-11-17 07:34:06 -080065 bool isWrapped() const { return fResource->isWrapped(); }
66
bsalomon12299ab2014-11-14 13:33:09 -080067 /**
bsalomon84c8e622014-11-17 09:33:27 -080068 * Does the resource count against the resource budget?
69 */
70 bool isBudgeted() const {
71 bool ret = SkToBool(GrGpuResource::kBudgeted_Flag & fResource->fFlags);
72 SkASSERT(!(ret && fResource->isWrapped()));
73 return ret;
74 }
75
76 /**
bsalomon12299ab2014-11-14 13:33:09 -080077 * Called by the cache to delete the resource under normal circumstances.
78 */
79 void release() {
80 fResource->release();
81 if (fResource->isPurgable()) {
82 SkDELETE(fResource);
83 }
84 }
85
86 /**
87 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
88 */
89 void abandon() {
90 fResource->abandon();
91 if (fResource->isPurgable()) {
92 SkDELETE(fResource);
93 }
94 }
95
bsalomon453cf402014-11-11 14:15:57 -080096private:
97 CacheAccess(GrGpuResource* resource) : fResource(resource) { }
98 CacheAccess(const CacheAccess& that) : fResource(that.fResource) { }
99 CacheAccess& operator=(const CacheAccess&); // unimpl
100
101 // No taking addresses of this type.
102 const CacheAccess* operator&() const;
103 CacheAccess* operator&();
104
105 GrGpuResource* fResource;
106
107 friend class GrGpuResource; // to construct/copy this type.
108};
109
110inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
111
112inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
113 return CacheAccess(const_cast<GrGpuResource*>(this));
114}
115
116#endif