blob: affc753f9b1b8ea819e480cfbeef4112acef0d85 [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
11#include "GrGpuResource.h"
12
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:
Chris Dalton5d2de082017-12-19 10:40:23 -070019 SkDEBUGCODE(bool hasPendingIO_debugOnly() const { return fResource->internalHasPendingIO(); })
20
bsalomon3582d3e2015-02-13 14:20:05 -080021 /**
bsalomon8718aaf2015-02-19 07:24:21 -080022 * Sets a unique key for the resource. If the resource was previously cached as scratch it will
bsalomonf99e9612015-02-19 08:24:16 -080023 * be converted to a uniquely-keyed resource. If the key is invalid then this is equivalent to
24 * removeUniqueKey(). If another resource is using the key then its unique key is removed and
25 * this resource takes over the key.
bsalomon3582d3e2015-02-13 14:20:05 -080026 */
bsalomonf99e9612015-02-19 08:24:16 -080027 void setUniqueKey(const GrUniqueKey& key) { fResource->setUniqueKey(key); }
bsalomon3582d3e2015-02-13 14:20:05 -080028
bsalomonf99e9612015-02-19 08:24:16 -080029 /** Removes the unique key from a resource. If the resource has a scratch key, it may be
30 preserved for recycling as scratch. */
31 void removeUniqueKey() { fResource->removeUniqueKey(); }
bsalomon3582d3e2015-02-13 14:20:05 -080032
33 /**
34 * If the resource is uncached make it cached. Has no effect on resources that are wrapped or
35 * already cached.
36 */
37 void makeBudgeted() { fResource->makeBudgeted(); }
38
39 /**
40 * If the resource is cached make it uncached. Has no effect on resources that are wrapped or
bsalomon8718aaf2015-02-19 07:24:21 -080041 * already uncached. Furthermore, resources with unique keys cannot be made unbudgeted.
bsalomon3582d3e2015-02-13 14:20:05 -080042 */
43 void makeUnbudgeted() { fResource->makeUnbudgeted(); }
44
45 /**
46 * Does the resource count against the resource budget?
47 */
bsalomon5ec26ae2016-02-25 08:33:02 -080048 SkBudgeted isBudgeted() const {
kkinnunen2e6055b2016-04-22 01:48:29 -070049 bool ret = SkBudgeted::kYes == fResource->fBudgeted;
Brian Osman0562eb92017-05-08 11:16:39 -040050 SkASSERT(ret || !fResource->getUniqueKey().isValid() || fResource->fRefsWrappedObjects);
bsalomon5ec26ae2016-02-25 08:33:02 -080051 return SkBudgeted(ret);
bsalomon3582d3e2015-02-13 14:20:05 -080052 }
53
bsalomonb2c01332016-02-26 10:37:26 -080054 /**
55 * Is the resource object wrapping an externally allocated GPU resource?
56 */
kkinnunen2e6055b2016-04-22 01:48:29 -070057 bool refsWrappedObjects() const { return fResource->fRefsWrappedObjects; }
bsalomonb2c01332016-02-26 10:37:26 -080058
59 /**
bsalomon3582d3e2015-02-13 14:20:05 -080060 * If this resource can be used as a scratch resource this returns a valid scratch key.
61 * Otherwise it returns a key for which isNullScratch is true. The resource may currently be
bsalomon8718aaf2015-02-19 07:24:21 -080062 * used as a uniquely keyed resource rather than scratch. Check isScratch().
bsalomon3582d3e2015-02-13 14:20:05 -080063 */
64 const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; }
65
66 /**
67 * If the resource has a scratch key, the key will be removed. Since scratch keys are installed
68 * at resource creation time, this means the resource will never again be used as scratch.
69 */
70 void removeScratchKey() const { fResource->removeScratchKey(); }
71
72protected:
73 ResourcePriv(GrGpuResource* resource) : fResource(resource) { }
74 ResourcePriv(const ResourcePriv& that) : fResource(that.fResource) {}
75 ResourcePriv& operator=(const CacheAccess&); // unimpl
76
77 // No taking addresses of this type.
78 const ResourcePriv* operator&() const;
79 ResourcePriv* operator&();
80
81 GrGpuResource* fResource;
82
83 friend class GrGpuResource; // to construct/copy this type.
84};
85
86inline GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() { return ResourcePriv(this); }
87
88inline const GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() const {
89 return ResourcePriv(const_cast<GrGpuResource*>(this));
90}
91
92#endif