GrGpuResource.h revision 436293a3308d58ce494d9667bd13428dd6e35236
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
16f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.comclass GrContext;
1737dd331b20a92ce79cc26556e065dec98a66cb0bbsalomonclass GrGpu;
1837dd331b20a92ce79cc26556e065dec98a66cb0bbsalomonclass GrResourceCache2;
198fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com/**
2100b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base
2200b76bd750e668a6989dd497313e715d1b476fdcbsalomon * class to isolate the ref-cnting behavior and provide friendship without exposing all of
2300b76bd750e668a6989dd497313e715d1b476fdcbsalomon * GrGpuResource.
2400b76bd750e668a6989dd497313e715d1b476fdcbsalomon *
2500b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Gpu resources can have three types of refs:
2600b76bd750e668a6989dd497313e715d1b476fdcbsalomon *   1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls
2700b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      that read and write the resource via GrDrawTarget and by any object that must own a
2800b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      GrGpuResource and is itself owned (directly or indirectly) by Skia-client code.
29bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon *   2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read
3000b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      of the resource by the GPU as a result of a skia API call but hasn't executed it yet.
31bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon *   3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a
3200b76bd750e668a6989dd497313e715d1b476fdcbsalomon *      write to the resource by the GPU as a result of a skia API call but hasn't executed it yet.
3300b76bd750e668a6989dd497313e715d1b476fdcbsalomon *
3400b76bd750e668a6989dd497313e715d1b476fdcbsalomon * The latter two ref types are private and intended only for Gr core code.
35bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon *
36bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * When an item is purgable DERIVED:notifyIsPurgable() will be called (static poly morphism using
37bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * CRTP). GrIORef and GrGpuResource are separate classes for organizational reasons and to be
38bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * able to give access via friendship to only the functions related to pending IO operations.
3976b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com */
40bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomontemplate <typename DERIVED> class GrIORef : public SkNoncopyable {
418fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.compublic:
4245725db1d82615d43408ec488549aec6218f80e4bsalomon    SK_DECLARE_INST_COUNT_ROOT(GrIORef)
43c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
4400b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
4500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of
4600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
4700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // not intended to cross thread boundaries.
4800b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void ref() const {
499323b8b8e16df4adcd63ee8496a6382e8df535c9Brian Salomon        this->validate();
50bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        ++fRefCnt;
5100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
5200b76bd750e668a6989dd497313e715d1b476fdcbsalomon
5300b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void unref() const {
5400b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
5500b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fRefCnt;
56bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
5700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
5800b76bd750e668a6989dd497313e715d1b476fdcbsalomon
59c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void validate() const {
6000b76bd750e668a6989dd497313e715d1b476fdcbsalomon#ifdef SK_DEBUG
6100b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fRefCnt >= 0);
6200b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fPendingReads >= 0);
6300b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fPendingWrites >= 0);
6412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0);
65c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#endif
6600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
6700b76bd750e668a6989dd497313e715d1b476fdcbsalomon
6800b76bd750e668a6989dd497313e715d1b476fdcbsalomonprotected:
691e2530babb65a883a01df5ee87147432f6707ce3bsalomon    GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
7000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
7112299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    bool isPurgable() const { return !this->internalHasRef() && !this->internalHasPendingIO(); }
7212299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon
738d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
748d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
758d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
768d034a154fec81167ecb696c07da389b98cc02a7bsalomon
776d4488c5e03010c94200b3706631d34ec3201411bsalomon    bool internalHasRef() const { return SkToBool(fRefCnt); }
786d4488c5e03010c94200b3706631d34ec3201411bsalomon
7900b76bd750e668a6989dd497313e715d1b476fdcbsalomonprivate:
8000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void addPendingRead() const {
8100b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
8200b76bd750e668a6989dd497313e715d1b476fdcbsalomon        ++fPendingReads;
8300b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
8400b76bd750e668a6989dd497313e715d1b476fdcbsalomon
8500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void completedRead() const {
8600b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
8700b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fPendingReads;
88bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
8900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
9000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
9100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void addPendingWrite() const {
9200b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
9300b76bd750e668a6989dd497313e715d1b476fdcbsalomon        ++fPendingWrites;
9400b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
9500b76bd750e668a6989dd497313e715d1b476fdcbsalomon
9600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void completedWrite() const {
9700b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
9800b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fPendingWrites;
99bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
10000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
10100b76bd750e668a6989dd497313e715d1b476fdcbsalomon
10200b76bd750e668a6989dd497313e715d1b476fdcbsalomonprivate:
103bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    void didUnref() const {
10412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
10512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon            static_cast<const DERIVED*>(this)->notifyIsPurgable();
106bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        }
107bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    }
108bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
10900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fRefCnt;
11000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fPendingReads;
11100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fPendingWrites;
11200b76bd750e668a6989dd497313e715d1b476fdcbsalomon
113ac8d6193eaa29e02d1786fe56efa98eefee74e50bsalomon    // This class is used to manage conversion of refs to pending reads/writes.
114f96ba02513eadd9fa24d75396ec9f2d6682e464cbsalomon    friend class GrGpuResourceRef;
1151e2530babb65a883a01df5ee87147432f6707ce3bsalomon    friend class GrResourceCache2; // to check IO ref counts.
116bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
117bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    template <typename, GrIOType> friend class GrPendingIOResource;
11800b76bd750e668a6989dd497313e715d1b476fdcbsalomon};
11900b76bd750e668a6989dd497313e715d1b476fdcbsalomon
12000b76bd750e668a6989dd497313e715d1b476fdcbsalomon/**
12171cb0c241e439b6ed746b90294d0b6916644a644bsalomon * Base class for objects that can be kept in the GrResourceCache2.
12200b76bd750e668a6989dd497313e715d1b476fdcbsalomon */
123544fe2338ff7459dbd887549aca31b8fc4cde7f4bsalomonclass SK_API GrGpuResource : public GrIORef<GrGpuResource> {
12400b76bd750e668a6989dd497313e715d1b476fdcbsalomonpublic:
12500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    SK_DECLARE_INST_COUNT(GrGpuResource)
126977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com
1278fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
128089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Tests whether a object has been abandoned or released. All objects will
129089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * be in this state after their creating GrContext is destroyed or has
130089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * contextLost called. It's up to the client to test wasDestroyed() before
131089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * attempting to use an object if it holds refs on objects across
1328fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * ~GrContext, freeResources with the force flag, or contextLost.
1338fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
134089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * @return true if the object has been released or abandoned,
1358fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *         false otherwise.
1368fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
137089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    bool wasDestroyed() const { return NULL == fGpu; }
1388fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
139cee661af926cc977addc6e039b7022975a448acebsalomon@google.com    /**
140089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Retrieves the context that owns the object. Note that it is possible for
141089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * this to return NULL. When objects have been release()ed or abandon()ed
142089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * they no longer have an owning context. Destroying a GrContext
143089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * automatically releases all its resources.
144838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     */
1451f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    const GrContext* getContext() const;
1461f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrContext* getContext();
1471f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
148c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /**
149c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * Retrieves the amount of GPU memory used by this resource in bytes. It is
150c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * approximate since we aren't aware of additional padding or copies made
151c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * by the driver.
152c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     *
153c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * @return the amount of GPU memory used in bytes
154c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     */
15569ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    size_t gpuMemorySize() const {
15669ed47f42d4877c178fdc0031cb01af2966ae235bsalomon        if (kInvalidGpuMemorySize == fGpuMemorySize) {
15769ed47f42d4877c178fdc0031cb01af2966ae235bsalomon            fGpuMemorySize = this->onGpuMemorySize();
15869ed47f42d4877c178fdc0031cb01af2966ae235bsalomon            SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
15969ed47f42d4877c178fdc0031cb01af2966ae235bsalomon        }
16069ed47f42d4877c178fdc0031cb01af2966ae235bsalomon        return fGpuMemorySize;
16169ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    }
162c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
163728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /**
16452e9d63f7110ac691609660342cdab32082a4235bsalomon     * Gets an id that is unique for this GrGpuResource object. It is static in that it does
16552e9d63f7110ac691609660342cdab32082a4235bsalomon     * not change when the content of the GrGpuResource object changes. This will never return
166c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * 0.
167728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     */
168c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    uint32_t getUniqueID() const { return fUniqueID; }
169c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
170453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    /**
171453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon     * Internal-only helper class used for cache manipulations of the reosurce.
172453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon     */
173453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    class CacheAccess;
174453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    inline CacheAccess cacheAccess();
175453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    inline const CacheAccess cacheAccess() const;
176453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon
177436293a3308d58ce494d9667bd13428dd6e35236junov    /**
178436293a3308d58ce494d9667bd13428dd6e35236junov     * Removes references to objects in the underlying 3D API without freeing them.
179436293a3308d58ce494d9667bd13428dd6e35236junov     * Called by CacheAccess.
180436293a3308d58ce494d9667bd13428dd6e35236junov     * In general this method should not be called outside of skia. It was
181436293a3308d58ce494d9667bd13428dd6e35236junov     * made by public for a special case where it needs to be called in Blink
182436293a3308d58ce494d9667bd13428dd6e35236junov     * when a texture becomes unsafe to use after having been shared through
183436293a3308d58ce494d9667bd13428dd6e35236junov     * a texture mailbox.
184436293a3308d58ce494d9667bd13428dd6e35236junov     */
185436293a3308d58ce494d9667bd13428dd6e35236junov    void abandon();
186436293a3308d58ce494d9667bd13428dd6e35236junov
187c44be0e9e4cee5402909c06370a630eee188a8f3bsalomonprotected:
188169612621f00b3fe9f71014079991287d311751absalomon    // This must be called by every GrGpuObject. It should be called once the object is fully
189169612621f00b3fe9f71014079991287d311751absalomon    // initialized (i.e. not in a base class constructor).
190169612621f00b3fe9f71014079991287d311751absalomon    void registerWithCache();
191169612621f00b3fe9f71014079991287d311751absalomon
1926d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    GrGpuResource(GrGpu*, bool isWrapped);
1936d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    virtual ~GrGpuResource();
19476b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com
19576b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    GrGpu* getGpu() const { return fGpu; }
1968fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
19712299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    /** Overridden to free GPU resources in the backend API. */
19812299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    virtual void onRelease() { }
19912299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    /** Overridden to abandon any internal handles, ptrs, etc to backend API resources.
20012299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        This may be called when the underlying 3D context is no longer valid and so no
20112299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        backend API calls should be made. */
20212299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    virtual void onAbandon() { }
2038fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
20484c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    bool isWrapped() const { return SkToBool(kWrapped_Flag & fFlags); }
205a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com
206c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /**
20769ed47f42d4877c178fdc0031cb01af2966ae235bsalomon     * This entry point should be called whenever gpuMemorySize() should report a different size.
20869ed47f42d4877c178fdc0031cb01af2966ae235bsalomon     * The cache will call gpuMemorySize() to update the current size of the resource.
209c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     */
210c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void didChangeGpuMemorySize() const;
211c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
212744998e666073166307d2522847b2536000a7619bsalomon    /**
213744998e666073166307d2522847b2536000a7619bsalomon     * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
214744998e666073166307d2522847b2536000a7619bsalomon     * By default resources are not usable as scratch. This should only be called once.
215744998e666073166307d2522847b2536000a7619bsalomon     **/
216744998e666073166307d2522847b2536000a7619bsalomon    void setScratchKey(const GrResourceKey& scratchKey);
217744998e666073166307d2522847b2536000a7619bsalomon
2188fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comprivate:
21912299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    /**
22012299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon     * Frees the object in the underlying 3D API. Called by CacheAccess.
22112299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon     */
22212299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    void release();
22312299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon
22469ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    virtual size_t onGpuMemorySize() const = 0;
22569ed47f42d4877c178fdc0031cb01af2966ae235bsalomon
226453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    // See comments in CacheAccess.
227453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    bool setContentKey(const GrResourceKey& contentKey);
22884c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    void setBudgeted(bool countsAgainstBudget);
229bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    void notifyIsPurgable() const;
23010e23caea3106be125acea10a637789e5a15c728bsalomon    void removeScratchKey();
231bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
232515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG
2339474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com    friend class GrGpu; // for assert in GrGpu to access getGpu
2349474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com#endif
2358fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
236c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    static uint32_t CreateUniqueID();
237c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
238c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    // We're in an internal doubly linked list owned by GrResourceCache2
2396d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
2408fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
241c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon
24284c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
243728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    enum Flags {
2449ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com        /**
24584c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         * The resource counts against the resource cache's budget.
2469ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com         */
24784c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon        kBudgeted_Flag      = 0x1,
248728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com
24984c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon        /**
25084c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         * This object wraps a GPU object given to us by Skia's client. Skia will not free the
25184c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         * underlying backend API GPU resources when the GrGpuResource is destroyed. This also
25284c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         * implies that kBudgeted_Flag is not set.
25384c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         */
25484c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon        kWrapped_Flag       = 0x2,
255c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
25684c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon        /**
25784c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         * If set then fContentKey is valid and the resource is cached based on its content.
25884c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon         */
25984c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon        kContentKeySet_Flag = 0x4,
26084c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    };
261c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
2626d4488c5e03010c94200b3706631d34ec3201411bsalomon    // TODO(bsalomon): Remove GrResourceKey and use different simpler types for content and scratch
2636d4488c5e03010c94200b3706631d34ec3201411bsalomon    // keys.
264744998e666073166307d2522847b2536000a7619bsalomon    GrResourceKey           fScratchKey;
2656d4488c5e03010c94200b3706631d34ec3201411bsalomon    GrResourceKey           fContentKey;
26684c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon
26784c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    // This is not ref'ed but abandon() or release() will be called before the GrGpu object
26884c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    // is destroyed. Those calls set will this to NULL.
26984c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    GrGpu*                  fGpu;
27084c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    mutable size_t          fGpuMemorySize;
27184c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon
27284c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    uint32_t                fFlags;
27384c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    const uint32_t          fUniqueID;
274744998e666073166307d2522847b2536000a7619bsalomon
275bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    typedef GrIORef<GrGpuResource> INHERITED;
276bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable.
2778fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com};
2788fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2798fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#endif
280