1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/gpu/GrResource.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 1.4 +/* 1.5 + * Copyright 2011 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrResource_DEFINED 1.12 +#define GrResource_DEFINED 1.13 + 1.14 +#include "SkRefCnt.h" 1.15 +#include "SkTInternalLList.h" 1.16 + 1.17 +class GrGpu; 1.18 +class GrContext; 1.19 +class GrResourceEntry; 1.20 + 1.21 +/** 1.22 + * Base class for the GPU resources created by a GrContext. 1.23 + */ 1.24 +class GrResource : public SkRefCnt { 1.25 +public: 1.26 + SK_DECLARE_INST_COUNT(GrResource) 1.27 + 1.28 + /** 1.29 + * Frees the resource in the underlying 3D API. It must be safe to call this 1.30 + * when the resource has been previously abandoned. 1.31 + */ 1.32 + void release(); 1.33 + 1.34 + /** 1.35 + * Removes references to objects in the underlying 3D API without freeing 1.36 + * them. Used when the API context has been torn down before the GrContext. 1.37 + */ 1.38 + void abandon(); 1.39 + 1.40 + /** 1.41 + * Tests whether a resource has been abandoned or released. All resources 1.42 + * will be in this state after their creating GrContext is destroyed or has 1.43 + * contextLost called. It's up to the client to test isValid() before 1.44 + * attempting to use a resource if it holds refs on resources across 1.45 + * ~GrContext, freeResources with the force flag, or contextLost. 1.46 + * 1.47 + * @return true if the resource has been released or abandoned, 1.48 + * false otherwise. 1.49 + */ 1.50 + bool isValid() const { return NULL != fGpu; } 1.51 + 1.52 + /** 1.53 + * Retrieves the size of the object in GPU memory. This is approximate since 1.54 + * we aren't aware of additional padding or copies made by the driver. 1.55 + * 1.56 + * @return the size of the buffer in bytes 1.57 + */ 1.58 + virtual size_t sizeInBytes() const = 0; 1.59 + 1.60 + /** 1.61 + * Retrieves the context that owns the resource. Note that it is possible 1.62 + * for this to return NULL. When resources have been release()ed or 1.63 + * abandon()ed they no longer have an owning context. Destroying a 1.64 + * GrContext automatically releases all its resources. 1.65 + */ 1.66 + const GrContext* getContext() const; 1.67 + GrContext* getContext(); 1.68 + 1.69 + void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; } 1.70 + GrResourceEntry* getCacheEntry() { return fCacheEntry; } 1.71 + 1.72 + void incDeferredRefCount() const { 1.73 + SkASSERT(fDeferredRefCount >= 0); 1.74 + ++fDeferredRefCount; 1.75 + } 1.76 + 1.77 + void decDeferredRefCount() const { 1.78 + SkASSERT(fDeferredRefCount > 0); 1.79 + --fDeferredRefCount; 1.80 + if (0 == fDeferredRefCount && this->needsDeferredUnref()) { 1.81 + SkASSERT(this->getRefCnt() > 1); 1.82 + this->unref(); 1.83 + } 1.84 + } 1.85 + 1.86 + int getDeferredRefCount() const { return fDeferredRefCount; } 1.87 + 1.88 + void setNeedsDeferredUnref() { fFlags |= kDeferredUnref_FlagBit; } 1.89 + 1.90 +protected: 1.91 + /** 1.92 + * isWrapped indicates we have wrapped a client-created backend resource in a GrResource. If it 1.93 + * is true then the client is responsible for the lifetime of the underlying backend resource. 1.94 + * Otherwise, our onRelease() should free the resource. 1.95 + */ 1.96 + GrResource(GrGpu* gpu, bool isWrapped); 1.97 + virtual ~GrResource(); 1.98 + 1.99 + GrGpu* getGpu() const { return fGpu; } 1.100 + 1.101 + // Derived classes should always call their parent class' onRelease 1.102 + // and onAbandon methods in their overrides. 1.103 + virtual void onRelease() {}; 1.104 + virtual void onAbandon() {}; 1.105 + 1.106 + bool isInCache() const { return NULL != fCacheEntry; } 1.107 + bool isWrapped() const { return kWrapped_FlagBit & fFlags; } 1.108 + bool needsDeferredUnref() const { return SkToBool(kDeferredUnref_FlagBit & fFlags); } 1.109 + 1.110 +private: 1.111 +#ifdef SK_DEBUG 1.112 + friend class GrGpu; // for assert in GrGpu to access getGpu 1.113 +#endif 1.114 + 1.115 + // We're in an internal doubly linked list 1.116 + SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); 1.117 + 1.118 + GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there 1.119 + // are still live GrResources. It will call 1.120 + // release() on all such resources in its 1.121 + // destructor. 1.122 + GrResourceEntry* fCacheEntry; // NULL if not in cache 1.123 + mutable int fDeferredRefCount; // How many references in deferred drawing buffers. 1.124 + 1.125 + enum Flags { 1.126 + /** 1.127 + * This resource wraps a GPU resource given to us by the user. 1.128 + * Lifetime management is left up to the user (i.e., we will not 1.129 + * free it). 1.130 + */ 1.131 + kWrapped_FlagBit = 0x1, 1.132 + 1.133 + /** 1.134 + * This texture should be de-refed when the deferred ref count goes 1.135 + * to zero. A resource gets into this state when the resource cache 1.136 + * is holding a ref-of-obligation (i.e., someone needs to own it but 1.137 + * no one else wants to) but doesn't really want to keep it around. 1.138 + */ 1.139 + kDeferredUnref_FlagBit = 0x2, 1.140 + }; 1.141 + uint32_t fFlags; 1.142 + 1.143 + typedef SkRefCnt INHERITED; 1.144 +}; 1.145 + 1.146 +#endif