1
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    /**
21     * Sets a unique key for the resource. If the resource was previously cached as scratch it will
22     * 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.
25     */
26    void setUniqueKey(const GrUniqueKey& key) { fResource->setUniqueKey(key); }
27
28    /** 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(); }
31
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
40     * already uncached. Furthermore, resources with unique keys cannot be made unbudgeted.
41     */
42    void makeUnbudgeted() { fResource->makeUnbudgeted(); }
43
44    /**
45     * Does the resource count against the resource budget?
46     */
47    SkBudgeted isBudgeted() const {
48        bool ret = GrGpuResource::kCached_LifeCycle == fResource->fLifeCycle;
49        SkASSERT(ret || !fResource->getUniqueKey().isValid());
50        return SkBudgeted(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
56     * used as a uniquely keyed resource rather than scratch. Check isScratch().
57     */
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
87