blob: 3c3786d05359086954c0e23c6cc478b58987c208 [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 /**
bsalomon453cf402014-11-11 14:15:57 -080032 * Is the resource currently cached as scratch? This means it has a valid scratch key and does
33 * not have a content key.
34 */
35 bool isScratch() const {
36 SkASSERT(fResource->fScratchKey.isScratch());
37 return NULL == this->getContentKey() && !fResource->fScratchKey.isNullScratch();
38 }
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
43 * used as content resource rather than scratch. Check isScratch().
44 */
45 const GrResourceKey& getScratchKey() const { return fResource->fScratchKey; }
46
47 /**
48 * If the resource is currently cached by a content key, the key is returned, otherwise NULL.
49 */
50 const GrResourceKey* getContentKey() const {
51 if (fResource->fContentKeySet) {
52 return &fResource->fContentKey;
53 }
54 return NULL;
55 }
56
bsalomon12299ab2014-11-14 13:33:09 -080057 /**
58 * Called by the cache to delete the resource under normal circumstances.
59 */
60 void release() {
61 fResource->release();
62 if (fResource->isPurgable()) {
63 SkDELETE(fResource);
64 }
65 }
66
67 /**
68 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
69 */
70 void abandon() {
71 fResource->abandon();
72 if (fResource->isPurgable()) {
73 SkDELETE(fResource);
74 }
75 }
76
bsalomon453cf402014-11-11 14:15:57 -080077private:
78 CacheAccess(GrGpuResource* resource) : fResource(resource) { }
79 CacheAccess(const CacheAccess& that) : fResource(that.fResource) { }
80 CacheAccess& operator=(const CacheAccess&); // unimpl
81
82 // No taking addresses of this type.
83 const CacheAccess* operator&() const;
84 CacheAccess* operator&();
85
86 GrGpuResource* fResource;
87
88 friend class GrGpuResource; // to construct/copy this type.
89};
90
91inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
92
93inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
94 return CacheAccess(const_cast<GrGpuResource*>(this));
95}
96
97#endif