GrGpuResource.h revision bcf0a52d4f4221b158e68a06ba0c4cc4db011060
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * Copyright 2014 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
68fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com */
78fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
86d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon#ifndef GrGpuResource_DEFINED
96d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon#define GrGpuResource_DEFINED
108fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
11bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon#include "GrResourceKey.h"
12bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon#include "GrTypesPriv.h"
13c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#include "SkInstCnt.h"
1442619d8df206b0bcd36d952909d972b8961e75debsalomon@google.com#include "SkTInternalLList.h"
159474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com
16c44be0e9e4cee5402909c06370a630eee188a8f3bsalomonclass GrResourceCacheEntry;
17c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomonclass GrResourceCache2;
188fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comclass GrGpu;
19f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.comclass GrContext;
208fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2176b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com/**
2200b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base
2300b76bd750e668a6989dd497313e715d1b476fdcbsalomon * class to isolate the ref-cnting behavior and provide friendship without exposing all of
2400b76bd750e668a6989dd497313e715d1b476fdcbsalomon * GrGpuResource.
2500b76bd750e668a6989dd497313e715d1b476fdcbsalomon *
2600b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Gpu resources can have three types of refs:
2700b76bd750e668a6989dd497313e715d1b476fdcbsalomon *   1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls
2800b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      that read and write the resource via GrDrawTarget and by any object that must own a
2900b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      GrGpuResource and is itself owned (directly or indirectly) by Skia-client code.
30bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon *   2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read
3100b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      of the resource by the GPU as a result of a skia API call but hasn't executed it yet.
32bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon *   3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a
3300b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      write to the resource by the GPU as a result of a skia API call but hasn't executed it yet.
3400b76bd750e668a6989dd497313e715d1b476fdcbsalomon *
3500b76bd750e668a6989dd497313e715d1b476fdcbsalomon * The latter two ref types are private and intended only for Gr core code.
36bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon *
37bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * When an item is purgable DERIVED:notifyIsPurgable() will be called (static poly morphism using
38bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * CRTP). GrIORef and GrGpuResource are separate classes for organizational reasons and to be
39bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * able to give access via friendship to only the functions related to pending IO operations.
4076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com */
41bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomontemplate <typename DERIVED> class GrIORef : public SkNoncopyable {
428fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.compublic:
4345725db1d82615d43408ec488549aec6218f80e4bsalomon    SK_DECLARE_INST_COUNT_ROOT(GrIORef)
4445725db1d82615d43408ec488549aec6218f80e4bsalomon    virtual ~GrIORef();
45c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
4600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
4700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of
4800b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
4900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // not intended to cross thread boundaries.
5000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void ref() const {
519323b8b8e16df4adcd63ee8496a6382e8df535c9Brian Salomon        this->validate();
52bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        ++fRefCnt;
5300b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
5400b76bd750e668a6989dd497313e715d1b476fdcbsalomon
5500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void unref() const {
5600b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
5700b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fRefCnt;
58bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
5900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
6000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
61bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    bool isPurgable() const { return this->reffedOnlyByCache() && !this->internalHasPendingIO(); }
62bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    bool reffedOnlyByCache() const { return 1 == fRefCnt; }
6300b76bd750e668a6989dd497313e715d1b476fdcbsalomon
64c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void validate() const {
6500b76bd750e668a6989dd497313e715d1b476fdcbsalomon#ifdef SK_DEBUG
6600b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fRefCnt >= 0);
6700b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fPendingReads >= 0);
6800b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fPendingWrites >= 0);
6900b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0);
70c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#endif
7100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
7200b76bd750e668a6989dd497313e715d1b476fdcbsalomon
7300b76bd750e668a6989dd497313e715d1b476fdcbsalomonprotected:
74bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0), fIsScratch(kNo_IsScratch) { }
7500b76bd750e668a6989dd497313e715d1b476fdcbsalomon
768d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
778d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
788d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
798d034a154fec81167ecb696c07da389b98cc02a7bsalomon
8000b76bd750e668a6989dd497313e715d1b476fdcbsalomonprivate:
8100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void addPendingRead() const {
8200b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
8300b76bd750e668a6989dd497313e715d1b476fdcbsalomon        ++fPendingReads;
8400b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
8500b76bd750e668a6989dd497313e715d1b476fdcbsalomon
8600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void completedRead() const {
8700b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
8800b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fPendingReads;
89bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
9000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
9100b76bd750e668a6989dd497313e715d1b476fdcbsalomon
9200b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void addPendingWrite() const {
9300b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
9400b76bd750e668a6989dd497313e715d1b476fdcbsalomon        ++fPendingWrites;
9500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
9600b76bd750e668a6989dd497313e715d1b476fdcbsalomon
9700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void completedWrite() const {
9800b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
9900b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fPendingWrites;
100bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
10100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
10200b76bd750e668a6989dd497313e715d1b476fdcbsalomon
10300b76bd750e668a6989dd497313e715d1b476fdcbsalomonprivate:
104bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    void didUnref() const {
105bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        if (0 == fPendingReads && 0 == fPendingWrites) {
106bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon            if (0 == fRefCnt) {
107bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon                SkDELETE(this);
108bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon            } else if (1 == fRefCnt) {
109bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon                // The one ref is the cache's
110bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon                static_cast<const DERIVED*>(this)->notifyIsPurgable();
111bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon            }
112bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        }
113bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    }
114bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
11500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fRefCnt;
11600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fPendingReads;
11700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fPendingWrites;
11800b76bd750e668a6989dd497313e715d1b476fdcbsalomon
119ac8d6193eaa29e02d1786fe56efa98eefee74e50bsalomon    // This class is used to manage conversion of refs to pending reads/writes.
120f96ba02513eadd9fa24d75396ec9f2d6682e464cbsalomon    friend class GrGpuResourceRef;
121bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
122bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    // This is temporary until GrResourceCache is fully replaced by GrResourceCache2.
123bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    enum IsScratch {
124bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        kNo_IsScratch,
125bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        kYes_IsScratch
126bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    } fIsScratch;
127bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
128bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    friend class GrContext; // to set the above field.
129bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    friend class GrResourceCache; // to check the above field.
130bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    friend class GrResourceCache2; // to check the above field.
131bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
132bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    template <typename, GrIOType> friend class GrPendingIOResource;
13300b76bd750e668a6989dd497313e715d1b476fdcbsalomon};
13400b76bd750e668a6989dd497313e715d1b476fdcbsalomon
13500b76bd750e668a6989dd497313e715d1b476fdcbsalomon/**
13600b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Base class for objects that can be kept in the GrResourceCache.
13700b76bd750e668a6989dd497313e715d1b476fdcbsalomon */
138bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomonclass GrGpuResource : public GrIORef<GrGpuResource> {
13900b76bd750e668a6989dd497313e715d1b476fdcbsalomonpublic:
14000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    SK_DECLARE_INST_COUNT(GrGpuResource)
141977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com
1428fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
143089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Frees the object in the underlying 3D API. It must be safe to call this
144089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * when the object has been previously abandoned.
1458fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
1468fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void release();
1478fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1488fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
1498fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * Removes references to objects in the underlying 3D API without freeing
1508fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * them. Used when the API context has been torn down before the GrContext.
1518fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
1528fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void abandon();
1538fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1548fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
155089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Tests whether a object has been abandoned or released. All objects will
156089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * be in this state after their creating GrContext is destroyed or has
157089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * contextLost called. It's up to the client to test wasDestroyed() before
158089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * attempting to use an object if it holds refs on objects across
1598fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * ~GrContext, freeResources with the force flag, or contextLost.
1608fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
161089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * @return true if the object has been released or abandoned,
1628fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *         false otherwise.
1638fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
164089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    bool wasDestroyed() const { return NULL == fGpu; }
1658fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
166cee661af926cc977addc6e039b7022975a448acebsalomon@google.com    /**
167089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Retrieves the context that owns the object. Note that it is possible for
168089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * this to return NULL. When objects have been release()ed or abandon()ed
169089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * they no longer have an owning context. Destroying a GrContext
170089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * automatically releases all its resources.
171838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     */
1721f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    const GrContext* getContext() const;
1731f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrContext* getContext();
1741f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
175c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /**
176c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * Retrieves the amount of GPU memory used by this resource in bytes. It is
177c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * approximate since we aren't aware of additional padding or copies made
178c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * by the driver.
179c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     *
180c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * @return the amount of GPU memory used in bytes
181c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     */
182c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    virtual size_t gpuMemorySize() const = 0;
183c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
184c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; }
185bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    GrResourceCacheEntry* getCacheEntry() const { return fCacheEntry; }
186089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org
187744998e666073166307d2522847b2536000a7619bsalomon    /**
188744998e666073166307d2522847b2536000a7619bsalomon     * If this resource can be used as a scratch resource this returns a valid
189744998e666073166307d2522847b2536000a7619bsalomon     * scratch key. Otherwise it returns a key for which isNullScratch is true.
190744998e666073166307d2522847b2536000a7619bsalomon     */
191744998e666073166307d2522847b2536000a7619bsalomon    const GrResourceKey& getScratchKey() const { return fScratchKey; }
192744998e666073166307d2522847b2536000a7619bsalomon
193728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /**
19452e9d63f7110ac691609660342cdab32082a4235bsalomon     * Gets an id that is unique for this GrGpuResource object. It is static in that it does
19552e9d63f7110ac691609660342cdab32082a4235bsalomon     * not change when the content of the GrGpuResource object changes. This will never return
196c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * 0.
197728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     */
198c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    uint32_t getUniqueID() const { return fUniqueID; }
199c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
200c44be0e9e4cee5402909c06370a630eee188a8f3bsalomonprotected:
201169612621f00b3fe9f71014079991287d311751absalomon    // This must be called by every GrGpuObject. It should be called once the object is fully
202169612621f00b3fe9f71014079991287d311751absalomon    // initialized (i.e. not in a base class constructor).
203169612621f00b3fe9f71014079991287d311751absalomon    void registerWithCache();
204169612621f00b3fe9f71014079991287d311751absalomon
2056d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    GrGpuResource(GrGpu*, bool isWrapped);
2066d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    virtual ~GrGpuResource();
20776b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com
20849f085dddff10473b6ebf832a974288300224e60bsalomon    bool isInCache() const { return SkToBool(fCacheEntry); }
209c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
21076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    GrGpu* getGpu() const { return fGpu; }
2118fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
212d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    // Derived classes should always call their parent class' onRelease
213d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    // and onAbandon methods in their overrides.
214d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    virtual void onRelease() {};
215d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    virtual void onAbandon() {};
2168fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2179ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com    bool isWrapped() const { return kWrapped_FlagBit & fFlags; }
218a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com
219c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /**
220c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * This entry point should be called whenever gpuMemorySize() begins
221c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * reporting a different size. If the object is in the cache, it will call
222c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * gpuMemorySize() immediately and pass the new size on to the resource
223c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * cache.
224c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     */
225c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void didChangeGpuMemorySize() const;
226c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
227744998e666073166307d2522847b2536000a7619bsalomon    /**
228744998e666073166307d2522847b2536000a7619bsalomon     * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
229744998e666073166307d2522847b2536000a7619bsalomon     * By default resources are not usable as scratch. This should only be called once.
230744998e666073166307d2522847b2536000a7619bsalomon     **/
231744998e666073166307d2522847b2536000a7619bsalomon    void setScratchKey(const GrResourceKey& scratchKey);
232744998e666073166307d2522847b2536000a7619bsalomon
2338fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comprivate:
234bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    void notifyIsPurgable() const;
235bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
236515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG
2379474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com    friend class GrGpu; // for assert in GrGpu to access getGpu
2389474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com#endif
2398fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
240c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    static uint32_t CreateUniqueID();
241c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
242c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    // We're in an internal doubly linked list owned by GrResourceCache2
2436d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
2448fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
245c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    // This is not ref'ed but abandon() or release() will be called before the GrGpu object
246c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    // is destroyed. Those calls set will this to NULL.
247c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    GrGpu* fGpu;
248c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon
249728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    enum Flags {
2509ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com        /**
251089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org         * This object wraps a GPU object given to us by the user.
252b77f0f4ae560e97cc4cd2758752d955549017c3cskia.committer@gmail.com         * Lifetime management is left up to the user (i.e., we will not
2539ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com         * free it).
2549ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com         */
2559ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com        kWrapped_FlagBit         = 0x1,
256728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    };
257728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com
258c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    uint32_t                fFlags;
259c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
260c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    GrResourceCacheEntry*   fCacheEntry;  // NULL if not in cache
261c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    const uint32_t          fUniqueID;
262c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
263744998e666073166307d2522847b2536000a7619bsalomon    GrResourceKey           fScratchKey;
264744998e666073166307d2522847b2536000a7619bsalomon
265bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    typedef GrIORef<GrGpuResource> INHERITED;
266bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable.
2678fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com};
2688fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2698fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#endif
270