blob: 5324dccc22ad2842d76dbda15df381cadc54022e [file] [log] [blame]
bsalomon3582d3e2015-02-13 14:20:05 -08001
2/*
3 * Copyright 2015 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 GrGpuResourcePriv_DEFINED
10#define GrGpuResourcePriv_DEFINED
11
12#include "GrGpuResource.h"
13
14/**
15 * This class allows code internal to Skia privileged access to manage the cache keys and budget
16 * status of a GrGpuResource object.
17 */
18class GrGpuResource::ResourcePriv {
19public:
20 /**
bsalomon8718aaf2015-02-19 07:24:21 -080021 * Sets a unique key for the resource. If the resource was previously cached as scratch it will
bsalomonf99e9612015-02-19 08:24:16 -080022 * be converted to a uniquely-keyed resource. If the key is invalid then this is equivalent to
23 * removeUniqueKey(). If another resource is using the key then its unique key is removed and
24 * this resource takes over the key.
bsalomon3582d3e2015-02-13 14:20:05 -080025 */
bsalomonf99e9612015-02-19 08:24:16 -080026 void setUniqueKey(const GrUniqueKey& key) { fResource->setUniqueKey(key); }
bsalomon3582d3e2015-02-13 14:20:05 -080027
bsalomonf99e9612015-02-19 08:24:16 -080028 /** Removes the unique key from a resource. If the resource has a scratch key, it may be
29 preserved for recycling as scratch. */
30 void removeUniqueKey() { fResource->removeUniqueKey(); }
bsalomon3582d3e2015-02-13 14:20:05 -080031
32 /**
33 * If the resource is uncached make it cached. Has no effect on resources that are wrapped or
34 * already cached.
35 */
36 void makeBudgeted() { fResource->makeBudgeted(); }
37
38 /**
39 * If the resource is cached make it uncached. Has no effect on resources that are wrapped or
bsalomon8718aaf2015-02-19 07:24:21 -080040 * already uncached. Furthermore, resources with unique keys cannot be made unbudgeted.
bsalomon3582d3e2015-02-13 14:20:05 -080041 */
42 void makeUnbudgeted() { fResource->makeUnbudgeted(); }
43
44 /**
45 * Does the resource count against the resource budget?
46 */
47 bool isBudgeted() const {
48 bool ret = GrGpuResource::kCached_LifeCycle == fResource->fLifeCycle;
bsalomon8718aaf2015-02-19 07:24:21 -080049 SkASSERT(ret || !fResource->getUniqueKey().isValid());
bsalomon3582d3e2015-02-13 14:20:05 -080050 return ret;
51 }
52
53 /**
54 * If this resource can be used as a scratch resource this returns a valid scratch key.
55 * Otherwise it returns a key for which isNullScratch is true. The resource may currently be
bsalomon8718aaf2015-02-19 07:24:21 -080056 * used as a uniquely keyed resource rather than scratch. Check isScratch().
bsalomon3582d3e2015-02-13 14:20:05 -080057 */
58 const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; }
59
60 /**
61 * If the resource has a scratch key, the key will be removed. Since scratch keys are installed
62 * at resource creation time, this means the resource will never again be used as scratch.
63 */
64 void removeScratchKey() const { fResource->removeScratchKey(); }
65
66protected:
67 ResourcePriv(GrGpuResource* resource) : fResource(resource) { }
68 ResourcePriv(const ResourcePriv& that) : fResource(that.fResource) {}
69 ResourcePriv& operator=(const CacheAccess&); // unimpl
70
71 // No taking addresses of this type.
72 const ResourcePriv* operator&() const;
73 ResourcePriv* operator&();
74
75 GrGpuResource* fResource;
76
77 friend class GrGpuResource; // to construct/copy this type.
78};
79
80inline GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() { return ResourcePriv(this); }
81
82inline const GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() const {
83 return ResourcePriv(const_cast<GrGpuResource*>(this));
84}
85
86#endif