GrGpuResource.h revision 515dcd36032997ce335daa0163c6d67e851bcad1
18fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com */
88fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#ifndef GrResource_DEFINED
118fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#define GrResource_DEFINED
128fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
138fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#include "GrRefCnt.h"
148fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1542619d8df206b0bcd36d952909d972b8961e75debsalomon@google.com#include "SkTInternalLList.h"
169474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com
178fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comclass GrGpu;
18f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.comclass GrContext;
191f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.comclass GrResourceEntry;
208fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2176b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com/**
2276b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com * Base class for the GPU resources created by a GrContext.
2376b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com */
248fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comclass GrResource : public GrRefCnt {
258fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.compublic:
264d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    SK_DECLARE_INST_COUNT(GrResource)
27977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com
288fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
298fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * Frees the resource in the underlying 3D API. It must be safe to call this
308fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * when the resource has been previously abandoned.
318fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
328fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void release();
338fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
348fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
358fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * Removes references to objects in the underlying 3D API without freeing
368fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * them. Used when the API context has been torn down before the GrContext.
378fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
388fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void abandon();
398fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
408fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
418fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * Tests whether a resource has been abandoned or released. All resources
428fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * will be in this state after their creating GrContext is destroyed or has
438fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * contextLost called. It's up to the client to test isValid() before
448fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * attempting to use a resource if it holds refs on resources across
458fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * ~GrContext, freeResources with the force flag, or contextLost.
468fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
478fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * @return true if the resource has been released or abandoned,
488fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *         false otherwise.
498fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
508fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    bool isValid() const { return NULL != fGpu; }
518fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
52cee661af926cc977addc6e039b7022975a448acebsalomon@google.com    /**
53cee661af926cc977addc6e039b7022975a448acebsalomon@google.com     * Retrieves the size of the object in GPU memory. This is approximate since
54cee661af926cc977addc6e039b7022975a448acebsalomon@google.com     * we aren't aware of additional padding or copies made by the driver.
55cee661af926cc977addc6e039b7022975a448acebsalomon@google.com     *
56cee661af926cc977addc6e039b7022975a448acebsalomon@google.com     * @return the size of the buffer in bytes
57cee661af926cc977addc6e039b7022975a448acebsalomon@google.com     */
581f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    virtual size_t sizeInBytes() const = 0;
59cee661af926cc977addc6e039b7022975a448acebsalomon@google.com
60838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com    /**
61838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     * Retrieves the context that owns the resource. Note that it is possible
62838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     * for this to return NULL. When resources have been release()ed or
63838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     * abandon()ed they no longer have an owning context. Destroying a
64838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     * GrContext automatically releases all its resources.
65838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com     */
661f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    const GrContext* getContext() const;
671f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrContext* getContext();
681f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
691f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
701f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrResourceEntry* getCacheEntry() { return fCacheEntry; }
71f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.com
72f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    void incDeferredRefCount() const { SkASSERT(fDeferredRefCount >= 0); ++fDeferredRefCount; }
73f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    void decDeferredRefCount() const { SkASSERT(fDeferredRefCount > 0); --fDeferredRefCount; }
74838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com
758fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comprotected:
76728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /**
77728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     * isWrapped indicates we have wrapped a client-created backend resource in a GrResource. If it
78728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     * is true then the client is responsible for the lifetime of the underlying backend resource.
79728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     * Otherwise, our onRelease() should free the resource.
80728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com     */
81728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    GrResource(GrGpu* gpu, bool isWrapped);
8276b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    virtual ~GrResource();
8376b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com
8476b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    GrGpu* getGpu() const { return fGpu; }
858fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
86d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    // Derived classes should always call their parent class' onRelease
87d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    // and onAbandon methods in their overrides.
88d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    virtual void onRelease() {};
89d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    virtual void onAbandon() {};
908fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
91a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com    bool isInCache() const { return NULL != fCacheEntry; }
92728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    bool isWrapped() const { return kWrapped_Flag & fFlags; }
93a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com
948fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comprivate:
95515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG
969474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com    friend class GrGpu; // for assert in GrGpu to access getGpu
979474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com#endif
988fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
9942619d8df206b0bcd36d952909d972b8961e75debsalomon@google.com    // We're in an internal doubly linked list
10042619d8df206b0bcd36d952909d972b8961e75debsalomon@google.com    SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource);
1018fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
102838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com    GrGpu*              fGpu;               // not reffed. The GrGpu can be deleted while there
103838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com                                            // are still live GrResources. It will call
104838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com                                            // release() on all such resources in its
105838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com                                            // destructor.
106838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com    GrResourceEntry*    fCacheEntry;        // NULL if not in cache
107838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com    mutable int         fDeferredRefCount;  // How many references in deferred drawing buffers.
1081f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
109728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    enum Flags {
1101f0f1a3b5ef4404da11ccf937ee270ea6ddb41e2bsalomon@google.com        kWrapped_Flag = 0x1,
111728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    };
112728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    uint32_t         fFlags;
113728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com
1148fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    typedef GrRefCnt INHERITED;
1158fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com};
1168fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1178fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#endif
118