1876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 2876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com/* 3876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * Copyright 2011 Google Inc. 4876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * 5876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be 6876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * found in the LICENSE file. 7876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com */ 8876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 9876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 10876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com#ifndef GrResource_DEFINED 11876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com#define GrResource_DEFINED 12876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 13876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com#include "GrRefCnt.h" 14876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 15811cb71d61137d85122f8e530c201d4bf396f7babsalomon@google.com#include "SkTInternalLList.h" 1638501c53326bca147ee786524212a6ccd80a13bbrobertphillips@google.com 17876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.comclass GrGpu; 185e18b9086487e3b04681e1ab5e3846e63a94e533bsalomon@google.comclass GrContext; 19eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.comclass GrResourceEntry; 20876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 2156f8dd6a34326cec9f2682ce01b2b591c3898af1bsalomon@google.com/** 2256f8dd6a34326cec9f2682ce01b2b591c3898af1bsalomon@google.com * Base class for the GPU resources created by a GrContext. 2356f8dd6a34326cec9f2682ce01b2b591c3898af1bsalomon@google.com */ 24876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.comclass GrResource : public GrRefCnt { 25876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.compublic: 266dc32d0d4ad0b6ac04ec50159af3543ece3ed49drobertphillips@google.com SK_DECLARE_INST_COUNT(GrResource) 27346886136698d7a7f46ebfbdb3ba32a9c54b1597robertphillips@google.com 28876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com /** 29876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * Frees the resource in the underlying 3D API. It must be safe to call this 30876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * when the resource has been previously abandoned. 31876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com */ 32876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com void release(); 33876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 34876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com /** 35876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * Removes references to objects in the underlying 3D API without freeing 36876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * them. Used when the API context has been torn down before the GrContext. 37876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com */ 38876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com void abandon(); 39876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 40876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com /** 41876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * Tests whether a resource has been abandoned or released. All resources 42876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * will be in this state after their creating GrContext is destroyed or has 43876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * contextLost called. It's up to the client to test isValid() before 44876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * attempting to use a resource if it holds refs on resources across 45876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * ~GrContext, freeResources with the force flag, or contextLost. 46876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * 47876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * @return true if the resource has been released or abandoned, 48876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * false otherwise. 49876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com */ 50876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com bool isValid() const { return NULL != fGpu; } 51876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 52876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com /** 53876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * Retrieves the size of the object in GPU memory. This is approximate since 54876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * we aren't aware of additional padding or copies made by the driver. 55876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * 56876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com * @return the size of the buffer in bytes 57876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com */ 58eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com virtual size_t sizeInBytes() const = 0; 59876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 60ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com /** 61ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com * Retrieves the context that owns the resource. Note that it is possible 62ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com * for this to return NULL. When resources have been release()ed or 63ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com * abandon()ed they no longer have an owning context. Destroying a 64ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com * GrContext automatically releases all its resources. 65ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com */ 66eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com const GrContext* getContext() const; 67eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com GrContext* getContext(); 68eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com 69eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; } 70eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com GrResourceEntry* getCacheEntry() { return fCacheEntry; } 715e18b9086487e3b04681e1ab5e3846e63a94e533bsalomon@google.com 72ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com void incDeferredRefCount() const { GrAssert(fDeferredRefCount >= 0); ++fDeferredRefCount; } 73ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com void decDeferredRefCount() const { GrAssert(fDeferredRefCount > 0); --fDeferredRefCount; } 74ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com 75876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.comprotected: 763bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com /** 773bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com * isWrapped indicates we have wrapped a client-created backend resource in a GrResource. If it 783bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com * is true then the client is responsible for the lifetime of the underlying backend resource. 793bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com * Otherwise, our onRelease() should free the resource. 803bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com */ 813bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com GrResource(GrGpu* gpu, bool isWrapped); 8256f8dd6a34326cec9f2682ce01b2b591c3898af1bsalomon@google.com virtual ~GrResource(); 8356f8dd6a34326cec9f2682ce01b2b591c3898af1bsalomon@google.com 8456f8dd6a34326cec9f2682ce01b2b591c3898af1bsalomon@google.com GrGpu* getGpu() const { return fGpu; } 85876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 8668a39fc6f1b342f1f2bac30ab4929bd086e4720erobertphillips@google.com // Derived classes should always call their parent class' onRelease 8768a39fc6f1b342f1f2bac30ab4929bd086e4720erobertphillips@google.com // and onAbandon methods in their overrides. 8868a39fc6f1b342f1f2bac30ab4929bd086e4720erobertphillips@google.com virtual void onRelease() {}; 8968a39fc6f1b342f1f2bac30ab4929bd086e4720erobertphillips@google.com virtual void onAbandon() {}; 90876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 91956835b7d6e06f621d3650cce8ce22350967fe1ebsalomon@google.com bool isInCache() const { return NULL != fCacheEntry; } 923bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com bool isWrapped() const { return kWrapped_Flag & fFlags; } 93956835b7d6e06f621d3650cce8ce22350967fe1ebsalomon@google.com 94876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.comprivate: 9538501c53326bca147ee786524212a6ccd80a13bbrobertphillips@google.com#if GR_DEBUG 9638501c53326bca147ee786524212a6ccd80a13bbrobertphillips@google.com friend class GrGpu; // for assert in GrGpu to access getGpu 9738501c53326bca147ee786524212a6ccd80a13bbrobertphillips@google.com#endif 98876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 99811cb71d61137d85122f8e530c201d4bf396f7babsalomon@google.com // We're in an internal doubly linked list 100811cb71d61137d85122f8e530c201d4bf396f7babsalomon@google.com SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); 101876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 102ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there 103ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com // are still live GrResources. It will call 104ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com // release() on all such resources in its 105ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com // destructor. 106ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com GrResourceEntry* fCacheEntry; // NULL if not in cache 107ce3472f232b725fb71cc58751d26a0d0240bd3c0bsalomon@google.com mutable int fDeferredRefCount; // How many references in deferred drawing buffers. 108eabaa3a28c9f73c9e590bc6f6193c50c437143d7robertphillips@google.com 1093bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com enum Flags { 110798c06a19a572b82c1c926acb280e2dda54c74e3bsalomon@google.com kWrapped_Flag = 0x1, 1113bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com }; 1123bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com uint32_t fFlags; 1133bd66feeebf650263cc207d6c2f76ca129f2fa66bsalomon@google.com 114876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com typedef GrRefCnt INHERITED; 115876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com}; 116876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com 117876688bbdef378c43ef01f50e52fa8539f28ec33bsalomon@google.com#endif 118