GrGpuResource.h revision 63c992f6c05ea728b5386de61d279f10eb7e08d9
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"
135756aff40921e700dc40f2a1757291a64acddeaajunov#include "SkData.h"
14c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#include "SkInstCnt.h"
1542619d8df206b0bcd36d952909d972b8961e75debsalomon@google.com#include "SkTInternalLList.h"
169474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com
17f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.comclass GrContext;
1837dd331b20a92ce79cc26556e065dec98a66cb0bbsalomonclass GrGpu;
1937dd331b20a92ce79cc26556e065dec98a66cb0bbsalomonclass GrResourceCache2;
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.
256f07665768dc84453316e7b2bbd6049576764cb1mtklein *
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 *
3763c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon * When an item is purgeable DERIVED:notifyIsPurgeable() 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:
436f07665768dc84453316e7b2bbd6049576764cb1mtklein    SK_DECLARE_INST_COUNT(GrIORef)
44c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
4500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
4600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of
4700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
4800b76bd750e668a6989dd497313e715d1b476fdcbsalomon    // not intended to cross thread boundaries.
4900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void ref() const {
509323b8b8e16df4adcd63ee8496a6382e8df535c9Brian Salomon        this->validate();
51bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        ++fRefCnt;
5200b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
5300b76bd750e668a6989dd497313e715d1b476fdcbsalomon
5400b76bd750e668a6989dd497313e715d1b476fdcbsalomon    void unref() const {
5500b76bd750e668a6989dd497313e715d1b476fdcbsalomon        this->validate();
5600b76bd750e668a6989dd497313e715d1b476fdcbsalomon        --fRefCnt;
57bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        this->didUnref();
5800b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
5900b76bd750e668a6989dd497313e715d1b476fdcbsalomon
60c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void validate() const {
6100b76bd750e668a6989dd497313e715d1b476fdcbsalomon#ifdef SK_DEBUG
6200b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fRefCnt >= 0);
6300b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fPendingReads >= 0);
6400b76bd750e668a6989dd497313e715d1b476fdcbsalomon        SkASSERT(fPendingWrites >= 0);
6512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0);
66c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#endif
6700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    }
6800b76bd750e668a6989dd497313e715d1b476fdcbsalomon
6900b76bd750e668a6989dd497313e715d1b476fdcbsalomonprotected:
701e2530babb65a883a01df5ee87147432f6707ce3bsalomon    GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
7100b76bd750e668a6989dd497313e715d1b476fdcbsalomon
7263c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon    bool isPurgeable() const { return !this->internalHasRef() && !this->internalHasPendingIO(); }
7312299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon
748d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
758d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
768d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
778d034a154fec81167ecb696c07da389b98cc02a7bsalomon
786d4488c5e03010c94200b3706631d34ec3201411bsalomon    bool internalHasRef() const { return SkToBool(fRefCnt); }
796d4488c5e03010c94200b3706631d34ec3201411bsalomon
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 {
10512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
10663c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon            static_cast<const DERIVED*>(this)->notifyIsPurgeable();
107bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon        }
108bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    }
109bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
11000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fRefCnt;
11100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fPendingReads;
11200b76bd750e668a6989dd497313e715d1b476fdcbsalomon    mutable int32_t fPendingWrites;
11300b76bd750e668a6989dd497313e715d1b476fdcbsalomon
114ac8d6193eaa29e02d1786fe56efa98eefee74e50bsalomon    // This class is used to manage conversion of refs to pending reads/writes.
115f96ba02513eadd9fa24d75396ec9f2d6682e464cbsalomon    friend class GrGpuResourceRef;
1161e2530babb65a883a01df5ee87147432f6707ce3bsalomon    friend class GrResourceCache2; // to check IO ref counts.
117bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
118bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    template <typename, GrIOType> friend class GrPendingIOResource;
11900b76bd750e668a6989dd497313e715d1b476fdcbsalomon};
12000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
12100b76bd750e668a6989dd497313e715d1b476fdcbsalomon/**
12271cb0c241e439b6ed746b90294d0b6916644a644bsalomon * Base class for objects that can be kept in the GrResourceCache2.
12300b76bd750e668a6989dd497313e715d1b476fdcbsalomon */
124544fe2338ff7459dbd887549aca31b8fc4cde7f4bsalomonclass SK_API GrGpuResource : public GrIORef<GrGpuResource> {
12500b76bd750e668a6989dd497313e715d1b476fdcbsalomonpublic:
12600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    SK_DECLARE_INST_COUNT(GrGpuResource)
127977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com
1285236cf480daf82b2f36e42795abdbbc915533a59bsalomon    enum LifeCycle {
1295236cf480daf82b2f36e42795abdbbc915533a59bsalomon        /**
1305236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * The resource is cached and owned by Skia. Resources with this status may be kept alive
1315236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * by the cache as either scratch or content resources even when there are no refs to them.
1325236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * The cache may release them whenever there are no refs.
1335236cf480daf82b2f36e42795abdbbc915533a59bsalomon         */
1345236cf480daf82b2f36e42795abdbbc915533a59bsalomon        kCached_LifeCycle,
1355236cf480daf82b2f36e42795abdbbc915533a59bsalomon        /**
1365236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * The resource is uncached. As soon as there are no more refs to it, it is released. Under
1375236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * the hood the cache may opaquely recycle it as a cached resource.
1385236cf480daf82b2f36e42795abdbbc915533a59bsalomon         */
1395236cf480daf82b2f36e42795abdbbc915533a59bsalomon        kUncached_LifeCycle,
1405236cf480daf82b2f36e42795abdbbc915533a59bsalomon        /**
1415236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * Similar to uncached, but Skia does not manage the lifetime of the underlying backend
1425236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * 3D API object(s). The client is responsible for freeing those. Used to inject client-
1435236cf480daf82b2f36e42795abdbbc915533a59bsalomon         * created GPU resources into Skia (e.g. to render to a client-created texture).
1445236cf480daf82b2f36e42795abdbbc915533a59bsalomon         */
1455236cf480daf82b2f36e42795abdbbc915533a59bsalomon        kWrapped_LifeCycle,
1465236cf480daf82b2f36e42795abdbbc915533a59bsalomon    };
1475236cf480daf82b2f36e42795abdbbc915533a59bsalomon
1488fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
149089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Tests whether a object has been abandoned or released. All objects will
150089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * be in this state after their creating GrContext is destroyed or has
151089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * contextLost called. It's up to the client to test wasDestroyed() before
152089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * attempting to use an object if it holds refs on objects across
1538fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * ~GrContext, freeResources with the force flag, or contextLost.
1548fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
155089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * @return true if the object has been released or abandoned,
1568fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *         false otherwise.
1578fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
158089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    bool wasDestroyed() const { return NULL == fGpu; }
1598fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
160cee661af926cc977addc6e039b7022975a448acebsalomon@google.com    /**
161089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * Retrieves the context that owns the object. Note that it is possible for
162089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * this to return NULL. When objects have been release()ed or abandon()ed
163089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * they no longer have an owning context. Destroying a GrContext
164089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     * automatically releases all its resources.
165838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     */
1661f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    const GrContext* getContext() const;
1671f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrContext* getContext();
1681f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
169c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /**
170c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * Retrieves the amount of GPU memory used by this resource in bytes. It is
171c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * approximate since we aren't aware of additional padding or copies made
172c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * by the driver.
173c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     *
174c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * @return the amount of GPU memory used in bytes
175c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     */
17669ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    size_t gpuMemorySize() const {
17769ed47f42d4877c178fdc0031cb01af2966ae235bsalomon        if (kInvalidGpuMemorySize == fGpuMemorySize) {
17869ed47f42d4877c178fdc0031cb01af2966ae235bsalomon            fGpuMemorySize = this->onGpuMemorySize();
17969ed47f42d4877c178fdc0031cb01af2966ae235bsalomon            SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
18069ed47f42d4877c178fdc0031cb01af2966ae235bsalomon        }
18169ed47f42d4877c178fdc0031cb01af2966ae235bsalomon        return fGpuMemorySize;
18269ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    }
183c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
184728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /**
18552e9d63f7110ac691609660342cdab32082a4235bsalomon     * Gets an id that is unique for this GrGpuResource object. It is static in that it does
18652e9d63f7110ac691609660342cdab32082a4235bsalomon     * not change when the content of the GrGpuResource object changes. This will never return
187c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     * 0.
188728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     */
189c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    uint32_t getUniqueID() const { return fUniqueID; }
190c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
191453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    /**
1925756aff40921e700dc40f2a1757291a64acddeaajunov     * Attach a custom data object to this resource. The data will remain attached
1935756aff40921e700dc40f2a1757291a64acddeaajunov     * for the lifetime of this resource (until it is abandoned or released).
1945756aff40921e700dc40f2a1757291a64acddeaajunov     * Takes a ref on data. Previously attached data, if any, is unrefed.
1955756aff40921e700dc40f2a1757291a64acddeaajunov     * Returns the data argument, for convenience.
1965756aff40921e700dc40f2a1757291a64acddeaajunov     */
1975756aff40921e700dc40f2a1757291a64acddeaajunov    const SkData* setCustomData(const SkData* data);
1985756aff40921e700dc40f2a1757291a64acddeaajunov
1995756aff40921e700dc40f2a1757291a64acddeaajunov    /**
2005756aff40921e700dc40f2a1757291a64acddeaajunov     * Returns the custom data object that was attached to this resource by
2015756aff40921e700dc40f2a1757291a64acddeaajunov     * calling setCustomData.
2025756aff40921e700dc40f2a1757291a64acddeaajunov     */
2035756aff40921e700dc40f2a1757291a64acddeaajunov    const SkData* getCustomData() const { return fData.get(); }
2045756aff40921e700dc40f2a1757291a64acddeaajunov
2055756aff40921e700dc40f2a1757291a64acddeaajunov    /**
206453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon     * Internal-only helper class used for cache manipulations of the reosurce.
207453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon     */
208453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    class CacheAccess;
209453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    inline CacheAccess cacheAccess();
210453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    inline const CacheAccess cacheAccess() const;
211453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon
212436293a3308d58ce494d9667bd13428dd6e35236junov    /**
213436293a3308d58ce494d9667bd13428dd6e35236junov     * Removes references to objects in the underlying 3D API without freeing them.
214436293a3308d58ce494d9667bd13428dd6e35236junov     * Called by CacheAccess.
215436293a3308d58ce494d9667bd13428dd6e35236junov     * In general this method should not be called outside of skia. It was
216436293a3308d58ce494d9667bd13428dd6e35236junov     * made by public for a special case where it needs to be called in Blink
217436293a3308d58ce494d9667bd13428dd6e35236junov     * when a texture becomes unsafe to use after having been shared through
218436293a3308d58ce494d9667bd13428dd6e35236junov     * a texture mailbox.
219436293a3308d58ce494d9667bd13428dd6e35236junov     */
220436293a3308d58ce494d9667bd13428dd6e35236junov    void abandon();
221436293a3308d58ce494d9667bd13428dd6e35236junov
222c44be0e9e4cee5402909c06370a630eee188a8f3bsalomonprotected:
223169612621f00b3fe9f71014079991287d311751absalomon    // This must be called by every GrGpuObject. It should be called once the object is fully
224169612621f00b3fe9f71014079991287d311751absalomon    // initialized (i.e. not in a base class constructor).
225169612621f00b3fe9f71014079991287d311751absalomon    void registerWithCache();
226169612621f00b3fe9f71014079991287d311751absalomon
2275236cf480daf82b2f36e42795abdbbc915533a59bsalomon    GrGpuResource(GrGpu*, LifeCycle);
2286d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    virtual ~GrGpuResource();
22976b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com
23076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    GrGpu* getGpu() const { return fGpu; }
2318fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
23212299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    /** Overridden to free GPU resources in the backend API. */
23312299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    virtual void onRelease() { }
23412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    /** Overridden to abandon any internal handles, ptrs, etc to backend API resources.
23512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        This may be called when the underlying 3D context is no longer valid and so no
23612299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon        backend API calls should be made. */
23712299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    virtual void onAbandon() { }
2388fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2395236cf480daf82b2f36e42795abdbbc915533a59bsalomon    bool isWrapped() const { return kWrapped_LifeCycle == fLifeCycle; }
240a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com
241c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /**
24269ed47f42d4877c178fdc0031cb01af2966ae235bsalomon     * This entry point should be called whenever gpuMemorySize() should report a different size.
24369ed47f42d4877c178fdc0031cb01af2966ae235bsalomon     * The cache will call gpuMemorySize() to update the current size of the resource.
244c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon     */
245c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void didChangeGpuMemorySize() const;
246c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
247744998e666073166307d2522847b2536000a7619bsalomon    /**
248744998e666073166307d2522847b2536000a7619bsalomon     * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
249744998e666073166307d2522847b2536000a7619bsalomon     * By default resources are not usable as scratch. This should only be called once.
250744998e666073166307d2522847b2536000a7619bsalomon     **/
2517775c85611c734a2af709b3a9c127939a4296c48bsalomon    void setScratchKey(const GrScratchKey& scratchKey);
252744998e666073166307d2522847b2536000a7619bsalomon
2538fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comprivate:
25412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    /**
25512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon     * Frees the object in the underlying 3D API. Called by CacheAccess.
25612299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon     */
25712299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon    void release();
25812299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon
25969ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    virtual size_t onGpuMemorySize() const = 0;
26069ed47f42d4877c178fdc0031cb01af2966ae235bsalomon
261453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon    // See comments in CacheAccess.
26224db3b1c35fb935660229da164fc5ad31977387fbsalomon    bool setContentKey(const GrContentKey& contentKey);
26363c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon    void notifyIsPurgeable() const;
26410e23caea3106be125acea10a637789e5a15c728bsalomon    void removeScratchKey();
265afe3005be3392e43bc51eb7eb2017eefaed85ad1bsalomon    void makeBudgeted();
266c2f35b750a57d7dc0b8053a98279631d1ccb9b56bsalomon    void makeUnbudgeted();
267bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
268515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG
2699474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com    friend class GrGpu; // for assert in GrGpu to access getGpu
2709474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com#endif
2718fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
272c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    static uint32_t CreateUniqueID();
273c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
274c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    // We're in an internal doubly linked list owned by GrResourceCache2
2756d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
2768fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
277c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon
27884c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
27924db3b1c35fb935660229da164fc5ad31977387fbsalomon    GrScratchKey                fScratchKey;
28024db3b1c35fb935660229da164fc5ad31977387fbsalomon    GrContentKey                fContentKey;
28184c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon
28284c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    // This is not ref'ed but abandon() or release() will be called before the GrGpu object
28384c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon    // is destroyed. Those calls set will this to NULL.
28424db3b1c35fb935660229da164fc5ad31977387fbsalomon    GrGpu*                      fGpu;
28524db3b1c35fb935660229da164fc5ad31977387fbsalomon    mutable size_t              fGpuMemorySize;
28684c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon
28724db3b1c35fb935660229da164fc5ad31977387fbsalomon    LifeCycle                   fLifeCycle;
28824db3b1c35fb935660229da164fc5ad31977387fbsalomon    const uint32_t              fUniqueID;
289744998e666073166307d2522847b2536000a7619bsalomon
29024db3b1c35fb935660229da164fc5ad31977387fbsalomon    SkAutoTUnref<const SkData>  fData;
2915756aff40921e700dc40f2a1757291a64acddeaajunov
292bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    typedef GrIORef<GrGpuResource> INHERITED;
29363c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon    friend class GrIORef<GrGpuResource>; // to access notifyIsPurgeable.
2948fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com};
2958fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2968fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#endif
297