blob: fc1e5935b3f56a6a19c1a015251a0a4b2f4d9beb [file] [log] [blame]
bsalomon3582d3e2015-02-13 14:20:05 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrGpuResourcePriv_DEFINED
9#define GrGpuResourcePriv_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012
13/**
14 * This class allows code internal to Skia privileged access to manage the cache keys and budget
15 * status of a GrGpuResource object.
16 */
17class GrGpuResource::ResourcePriv {
18public:
19 /**
bsalomon8718aaf2015-02-19 07:24:21 -080020 * Sets a unique key for the resource. If the resource was previously cached as scratch it will
bsalomonf99e9612015-02-19 08:24:16 -080021 * be converted to a uniquely-keyed resource. If the key is invalid then this is equivalent to
22 * removeUniqueKey(). If another resource is using the key then its unique key is removed and
23 * this resource takes over the key.
bsalomon3582d3e2015-02-13 14:20:05 -080024 */
bsalomonf99e9612015-02-19 08:24:16 -080025 void setUniqueKey(const GrUniqueKey& key) { fResource->setUniqueKey(key); }
bsalomon3582d3e2015-02-13 14:20:05 -080026
bsalomonf99e9612015-02-19 08:24:16 -080027 /** Removes the unique key from a resource. If the resource has a scratch key, it may be
28 preserved for recycling as scratch. */
29 void removeUniqueKey() { fResource->removeUniqueKey(); }
bsalomon3582d3e2015-02-13 14:20:05 -080030
31 /**
32 * If the resource is uncached make it cached. Has no effect on resources that are wrapped or
33 * already cached.
34 */
35 void makeBudgeted() { fResource->makeBudgeted(); }
36
37 /**
38 * If the resource is cached make it uncached. Has no effect on resources that are wrapped or
bsalomon8718aaf2015-02-19 07:24:21 -080039 * already uncached. Furthermore, resources with unique keys cannot be made unbudgeted.
bsalomon3582d3e2015-02-13 14:20:05 -080040 */
41 void makeUnbudgeted() { fResource->makeUnbudgeted(); }
42
43 /**
Brian Salomonfa2ebea2019-01-24 15:58:58 -050044 * Get the resource's budgeted-type which indicates whether it counts against the resource cache
45 * budget and if not whether it is allowed to be cached.
bsalomon3582d3e2015-02-13 14:20:05 -080046 */
Brian Salomonfa2ebea2019-01-24 15:58:58 -050047 GrBudgetedType budgetedType() const {
48 SkASSERT(GrBudgetedType::kBudgeted == fResource->fBudgetedType ||
49 !fResource->getUniqueKey().isValid() || fResource->fRefsWrappedObjects);
50 return fResource->fBudgetedType;
bsalomon3582d3e2015-02-13 14:20:05 -080051 }
52
bsalomonb2c01332016-02-26 10:37:26 -080053 /**
54 * Is the resource object wrapping an externally allocated GPU resource?
55 */
kkinnunen2e6055b2016-04-22 01:48:29 -070056 bool refsWrappedObjects() const { return fResource->fRefsWrappedObjects; }
bsalomonb2c01332016-02-26 10:37:26 -080057
58 /**
bsalomon3582d3e2015-02-13 14:20:05 -080059 * If this resource can be used as a scratch resource this returns a valid scratch key.
60 * Otherwise it returns a key for which isNullScratch is true. The resource may currently be
bsalomon8718aaf2015-02-19 07:24:21 -080061 * used as a uniquely keyed resource rather than scratch. Check isScratch().
bsalomon3582d3e2015-02-13 14:20:05 -080062 */
63 const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; }
64
65 /**
66 * If the resource has a scratch key, the key will be removed. Since scratch keys are installed
67 * at resource creation time, this means the resource will never again be used as scratch.
68 */
69 void removeScratchKey() const { fResource->removeScratchKey(); }
70
Brian Salomon614c1a82018-12-19 15:42:06 -050071 bool isPurgeable() const { return fResource->isPurgeable(); }
72
Robert Phillipsbf8bf832019-08-30 13:13:44 -040073 bool hasRef() const { return fResource->hasRef(); }
Brian Salomon9bc76d92019-01-24 12:18:33 -050074
bsalomon3582d3e2015-02-13 14:20:05 -080075protected:
76 ResourcePriv(GrGpuResource* resource) : fResource(resource) { }
77 ResourcePriv(const ResourcePriv& that) : fResource(that.fResource) {}
78 ResourcePriv& operator=(const CacheAccess&); // unimpl
79
80 // No taking addresses of this type.
81 const ResourcePriv* operator&() const;
82 ResourcePriv* operator&();
83
84 GrGpuResource* fResource;
85
86 friend class GrGpuResource; // to construct/copy this type.
87};
88
89inline GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() { return ResourcePriv(this); }
90
91inline const GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() const {
92 return ResourcePriv(const_cast<GrGpuResource*>(this));
93}
94
95#endif