blob: a3291e0c052376ed32ec2c96b724977c2faa0288 [file] [log] [blame]
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
bsalomon@google.com8fe72472011-03-30 21:26:44 +00006 */
7
8#ifndef GrResource_DEFINED
9#define GrResource_DEFINED
10
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000011#include "SkRefCnt.h"
bsalomon@google.com42619d82012-12-03 14:54:59 +000012#include "SkTInternalLList.h"
robertphillips@google.com9474ed02012-09-04 13:34:32 +000013
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014class GrGpu;
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000015class GrContext;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000016class GrResourceEntry;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000017
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000018/**
19 * Base class for the GPU resources created by a GrContext.
20 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000021class GrResource : public SkRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000022public:
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000023 SK_DECLARE_INST_COUNT(GrResource)
robertphillips@google.com977b9c82012-06-05 19:35:09 +000024
bsalomon@google.com8fe72472011-03-30 21:26:44 +000025 /**
26 * Frees the resource in the underlying 3D API. It must be safe to call this
27 * when the resource has been previously abandoned.
28 */
29 void release();
30
31 /**
32 * Removes references to objects in the underlying 3D API without freeing
33 * them. Used when the API context has been torn down before the GrContext.
34 */
35 void abandon();
36
37 /**
38 * Tests whether a resource has been abandoned or released. All resources
39 * will be in this state after their creating GrContext is destroyed or has
40 * contextLost called. It's up to the client to test isValid() before
41 * attempting to use a resource if it holds refs on resources across
42 * ~GrContext, freeResources with the force flag, or contextLost.
43 *
44 * @return true if the resource has been released or abandoned,
45 * false otherwise.
46 */
47 bool isValid() const { return NULL != fGpu; }
48
bsalomon@google.comcee661a2011-07-26 12:32:36 +000049 /**
50 * Retrieves the size of the object in GPU memory. This is approximate since
51 * we aren't aware of additional padding or copies made by the driver.
52 *
53 * @return the size of the buffer in bytes
54 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000055 virtual size_t sizeInBytes() const = 0;
bsalomon@google.comcee661a2011-07-26 12:32:36 +000056
bsalomon@google.com838f6e12013-01-23 21:37:01 +000057 /**
58 * Retrieves the context that owns the resource. Note that it is possible
59 * for this to return NULL. When resources have been release()ed or
60 * abandon()ed they no longer have an owning context. Destroying a
61 * GrContext automatically releases all its resources.
62 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000063 const GrContext* getContext() const;
64 GrContext* getContext();
65
66 void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
67 GrResourceEntry* getCacheEntry() { return fCacheEntry; }
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000068
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000069 void incDeferredRefCount() const { SkASSERT(fDeferredRefCount >= 0); ++fDeferredRefCount; }
70 void decDeferredRefCount() const { SkASSERT(fDeferredRefCount > 0); --fDeferredRefCount; }
robertphillips@google.com0255a5d2013-10-24 14:03:01 +000071 int getDeferredRefCount() const { return fDeferredRefCount; }
bsalomon@google.com838f6e12013-01-23 21:37:01 +000072
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073protected:
bsalomon@google.com72830222013-01-23 20:25:22 +000074 /**
75 * isWrapped indicates we have wrapped a client-created backend resource in a GrResource. If it
76 * is true then the client is responsible for the lifetime of the underlying backend resource.
77 * Otherwise, our onRelease() should free the resource.
78 */
79 GrResource(GrGpu* gpu, bool isWrapped);
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000080 virtual ~GrResource();
81
82 GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083
robertphillips@google.comd3645542012-09-05 18:37:39 +000084 // Derived classes should always call their parent class' onRelease
85 // and onAbandon methods in their overrides.
86 virtual void onRelease() {};
87 virtual void onAbandon() {};
bsalomon@google.com8fe72472011-03-30 21:26:44 +000088
bsalomon@google.coma2921122012-08-28 12:34:17 +000089 bool isInCache() const { return NULL != fCacheEntry; }
bsalomon@google.com72830222013-01-23 20:25:22 +000090 bool isWrapped() const { return kWrapped_Flag & fFlags; }
bsalomon@google.coma2921122012-08-28 12:34:17 +000091
bsalomon@google.com8fe72472011-03-30 21:26:44 +000092private:
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000093#ifdef SK_DEBUG
robertphillips@google.com9474ed02012-09-04 13:34:32 +000094 friend class GrGpu; // for assert in GrGpu to access getGpu
95#endif
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096
bsalomon@google.com42619d82012-12-03 14:54:59 +000097 // We're in an internal doubly linked list
98 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000099
bsalomon@google.com838f6e12013-01-23 21:37:01 +0000100 GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there
101 // are still live GrResources. It will call
102 // release() on all such resources in its
103 // destructor.
104 GrResourceEntry* fCacheEntry; // NULL if not in cache
105 mutable int fDeferredRefCount; // How many references in deferred drawing buffers.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000106
bsalomon@google.com72830222013-01-23 20:25:22 +0000107 enum Flags {
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000108 kWrapped_Flag = 0x1,
bsalomon@google.com72830222013-01-23 20:25:22 +0000109 };
110 uint32_t fFlags;
111
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000112 typedef SkRefCnt INHERITED;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113};
114
115#endif