1/*
2 * Copyright 2014 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 GrCacheable_DEFINED
9#define GrCacheable_DEFINED
10
11#include "SkRefCnt.h"
12
13class GrResourceCacheEntry;
14
15/**
16 * Base class for objects that can be kept in the GrResourceCache.
17 */
18class GrCacheable : public SkRefCnt {
19public:
20    SK_DECLARE_INST_COUNT(GrCacheable)
21
22    /**
23     * Retrieves the amount of GPU memory used by this resource in bytes. It is
24     * approximate since we aren't aware of additional padding or copies made
25     * by the driver.
26     *
27     * @return the amount of GPU memory used in bytes
28     */
29    virtual size_t gpuMemorySize() const = 0;
30
31    /**
32     * Checks whether the GPU memory allocated to this resource is still in effect.
33     * It can become invalid if its context is destroyed or lost, in which case it
34     * should no longer count against the GrResourceCache budget.
35     *
36     * @return true if this resource is still holding GPU memory
37     *         false otherwise.
38     */
39    virtual bool isValidOnGpu() const = 0;
40
41    void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; }
42    GrResourceCacheEntry* getCacheEntry() { return fCacheEntry; }
43
44    /**
45     * Gets an id that is unique for this GrCacheable object. It is static in that it does
46     * not change when the content of the GrCacheable object changes. This will never return
47     * 0.
48     */
49    uint32_t getGenerationID() const;
50
51protected:
52    GrCacheable()
53        : fCacheEntry(NULL)
54        , fGenID(0) {}
55
56    bool isInCache() const { return NULL != fCacheEntry; }
57
58    /**
59     * This entry point should be called whenever gpuMemorySize() begins
60     * reporting a different size. If the object is in the cache, it will call
61     * gpuMemorySize() immediately and pass the new size on to the resource
62     * cache.
63     */
64    void didChangeGpuMemorySize() const;
65
66private:
67    GrResourceCacheEntry*   fCacheEntry;  // NULL if not in cache
68    mutable uint32_t        fGenID;
69
70    typedef SkRefCnt INHERITED;
71};
72
73#endif
74