Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrGpuResource.h
1 /*
2  * Copyright 2014 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.
6  */
7
8 #ifndef GrGpuResource_DEFINED
9 #define GrGpuResource_DEFINED
10
11 #include "GrResourceKey.h"
12 #include "GrTypesPriv.h"
13 #include "SkInstCnt.h"
14 #include "SkTInternalLList.h"
15
16 class GrContext;
17 class GrGpu;
18 class GrResourceCache2;
19 class GrResourceCacheEntry;
20
21 /**
22  * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base
23  * class to isolate the ref-cnting behavior and provide friendship without exposing all of
24  * GrGpuResource.
25  * 
26  * Gpu resources can have three types of refs:
27  *   1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls
28  *      that read and write the resource via GrDrawTarget and by any object that must own a
29  *      GrGpuResource and is itself owned (directly or indirectly) by Skia-client code.
30  *   2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read
31  *      of the resource by the GPU as a result of a skia API call but hasn't executed it yet.
32  *   3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a
33  *      write to the resource by the GPU as a result of a skia API call but hasn't executed it yet.
34  *
35  * The latter two ref types are private and intended only for Gr core code.
36  *
37  * When an item is purgable DERIVED:notifyIsPurgable() will be called (static poly morphism using
38  * CRTP). GrIORef and GrGpuResource are separate classes for organizational reasons and to be
39  * able to give access via friendship to only the functions related to pending IO operations.
40  */
41 template <typename DERIVED> class GrIORef : public SkNoncopyable {
42 public:
43     SK_DECLARE_INST_COUNT_ROOT(GrIORef)
44
45     // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
46     // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of
47     // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
48     // not intended to cross thread boundaries.
49     void ref() const {
50         this->validate();
51         ++fRefCnt;
52     }
53
54     void unref() const {
55         this->validate();
56         --fRefCnt;
57         this->didUnref();
58     }
59
60     bool isPurgable() const { return this->reffedOnlyByCache() && !this->internalHasPendingIO(); }
61     bool reffedOnlyByCache() const { return 1 == fRefCnt; }
62
63     void validate() const {
64 #ifdef SK_DEBUG
65         SkASSERT(fRefCnt >= 0);
66         SkASSERT(fPendingReads >= 0);
67         SkASSERT(fPendingWrites >= 0);
68         SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0);
69 #endif
70     }
71
72 protected:
73     GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
74
75     bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
76     bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
77     bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
78
79 private:
80     void addPendingRead() const {
81         this->validate();
82         ++fPendingReads;
83     }
84
85     void completedRead() const {
86         this->validate();
87         --fPendingReads;
88         this->didUnref();
89     }
90
91     void addPendingWrite() const {
92         this->validate();
93         ++fPendingWrites;
94     }
95
96     void completedWrite() const {
97         this->validate();
98         --fPendingWrites;
99         this->didUnref();
100     }
101
102 private:
103     void didUnref() const {
104         if (0 == fPendingReads && 0 == fPendingWrites) {
105             if (0 == fRefCnt) {
106                 // Must call derived destructor since this is not a virtual class.
107                 SkDELETE(static_cast<const DERIVED*>(this));
108             } else if (1 == fRefCnt) {
109                 // The one ref is the cache's
110                 static_cast<const DERIVED*>(this)->notifyIsPurgable();
111             }
112         }
113     }
114
115     mutable int32_t fRefCnt;
116     mutable int32_t fPendingReads;
117     mutable int32_t fPendingWrites;
118
119     // This class is used to manage conversion of refs to pending reads/writes.
120     friend class GrGpuResourceRef;
121     friend class GrResourceCache2; // to check IO ref counts.
122
123     template <typename, GrIOType> friend class GrPendingIOResource;
124 };
125
126 /**
127  * Base class for objects that can be kept in the GrResourceCache.
128  */
129 class SK_API GrGpuResource : public GrIORef<GrGpuResource> {
130 public:
131     SK_DECLARE_INST_COUNT(GrGpuResource)
132
133     /**
134      * Frees the object in the underlying 3D API. It must be safe to call this
135      * when the object has been previously abandoned.
136      */
137     void release();
138
139     /**
140      * Removes references to objects in the underlying 3D API without freeing
141      * them. Used when the API context has been torn down before the GrContext.
142      */
143     void abandon();
144
145     /**
146      * Tests whether a object has been abandoned or released. All objects will
147      * be in this state after their creating GrContext is destroyed or has
148      * contextLost called. It's up to the client to test wasDestroyed() before
149      * attempting to use an object if it holds refs on objects across
150      * ~GrContext, freeResources with the force flag, or contextLost.
151      *
152      * @return true if the object has been released or abandoned,
153      *         false otherwise.
154      */
155     bool wasDestroyed() const { return NULL == fGpu; }
156
157     /**
158      * Retrieves the context that owns the object. Note that it is possible for
159      * this to return NULL. When objects have been release()ed or abandon()ed
160      * they no longer have an owning context. Destroying a GrContext
161      * automatically releases all its resources.
162      */
163     const GrContext* getContext() const;
164     GrContext* getContext();
165
166     /**
167      * Retrieves the amount of GPU memory used by this resource in bytes. It is
168      * approximate since we aren't aware of additional padding or copies made
169      * by the driver.
170      *
171      * @return the amount of GPU memory used in bytes
172      */
173     virtual size_t gpuMemorySize() const = 0;
174
175     void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; }
176     GrResourceCacheEntry* getCacheEntry() const { return fCacheEntry; }
177     bool isScratch() const;
178
179     /** 
180      * If this resource can be used as a scratch resource this returns a valid
181      * scratch key. Otherwise it returns a key for which isNullScratch is true.
182      */
183     const GrResourceKey& getScratchKey() const { return fScratchKey; }
184
185     /** 
186      * If this resource is currently cached by its contents then this will return
187      * the content key. Otherwise, NULL is returned.
188      */
189     const GrResourceKey* getContentKey() const;
190
191     /**
192      * Gets an id that is unique for this GrGpuResource object. It is static in that it does
193      * not change when the content of the GrGpuResource object changes. This will never return
194      * 0.
195      */
196     uint32_t getUniqueID() const { return fUniqueID; }
197
198 protected:
199     // This must be called by every GrGpuObject. It should be called once the object is fully
200     // initialized (i.e. not in a base class constructor).
201     void registerWithCache();
202
203     GrGpuResource(GrGpu*, bool isWrapped);
204     virtual ~GrGpuResource();
205
206     bool isInCache() const { return SkToBool(fCacheEntry); }
207
208     GrGpu* getGpu() const { return fGpu; }
209
210     // Derived classes should always call their parent class' onRelease
211     // and onAbandon methods in their overrides.
212     virtual void onRelease() {};
213     virtual void onAbandon() {};
214
215     bool isWrapped() const { return kWrapped_FlagBit & fFlags; }
216
217     /**
218      * This entry point should be called whenever gpuMemorySize() begins
219      * reporting a different size. If the object is in the cache, it will call
220      * gpuMemorySize() immediately and pass the new size on to the resource
221      * cache.
222      */
223     void didChangeGpuMemorySize() const;
224
225     /**
226      * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
227      * By default resources are not usable as scratch. This should only be called once.
228      **/
229     void setScratchKey(const GrResourceKey& scratchKey);
230
231 private:
232     void notifyIsPurgable() const;
233
234 #ifdef SK_DEBUG
235     friend class GrGpu; // for assert in GrGpu to access getGpu
236 #endif
237
238     static uint32_t CreateUniqueID();
239
240     // We're in an internal doubly linked list owned by GrResourceCache2
241     SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
242
243     // This is not ref'ed but abandon() or release() will be called before the GrGpu object
244     // is destroyed. Those calls set will this to NULL.
245     GrGpu* fGpu;
246
247     enum Flags {
248         /**
249          * This object wraps a GPU object given to us by the user.
250          * Lifetime management is left up to the user (i.e., we will not
251          * free it).
252          */
253         kWrapped_FlagBit         = 0x1,
254     };
255
256     uint32_t                fFlags;
257
258     GrResourceCacheEntry*   fCacheEntry;  // NULL if not in cache
259     const uint32_t          fUniqueID;
260
261     GrResourceKey           fScratchKey;
262
263     typedef GrIORef<GrGpuResource> INHERITED;
264     friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable.
265 };
266
267 #endif