GrGpuResource.h revision f320e04c50a1c8a861bc1d8f50bf732044ff9843
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * Copyright 2014 Google Inc. 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 68fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com */ 78fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 86d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon#ifndef GrGpuResource_DEFINED 96d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon#define GrGpuResource_DEFINED 108fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 11bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon#include "GrResourceKey.h" 12bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon#include "GrTypesPriv.h" 135756aff40921e700dc40f2a1757291a64acddeaajunov#include "SkData.h" 14c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#include "SkInstCnt.h" 159474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com 16f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.comclass GrContext; 1737dd331b20a92ce79cc26556e065dec98a66cb0bbsalomonclass GrGpu; 180ea80f43a1af05b8157a4ef387223bb5b0da35edbsalomonclass GrResourceCache; 198fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 2076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com/** 2100b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base 2200b76bd750e668a6989dd497313e715d1b476fdcbsalomon * class to isolate the ref-cnting behavior and provide friendship without exposing all of 2300b76bd750e668a6989dd497313e715d1b476fdcbsalomon * GrGpuResource. 246f07665768dc84453316e7b2bbd6049576764cb1mtklein * 2500b76bd750e668a6989dd497313e715d1b476fdcbsalomon * Gpu resources can have three types of refs: 2600b76bd750e668a6989dd497313e715d1b476fdcbsalomon * 1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls 2700b76bd750e668a6989dd497313e715d1b476fdcbsalomon * that read and write the resource via GrDrawTarget and by any object that must own a 2800b76bd750e668a6989dd497313e715d1b476fdcbsalomon * GrGpuResource and is itself owned (directly or indirectly) by Skia-client code. 29bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * 2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read 3000b76bd750e668a6989dd497313e715d1b476fdcbsalomon * of the resource by the GPU as a result of a skia API call but hasn't executed it yet. 31bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * 3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a 3200b76bd750e668a6989dd497313e715d1b476fdcbsalomon * write to the resource by the GPU as a result of a skia API call but hasn't executed it yet. 3300b76bd750e668a6989dd497313e715d1b476fdcbsalomon * 3400b76bd750e668a6989dd497313e715d1b476fdcbsalomon * The latter two ref types are private and intended only for Gr core code. 35bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * 3663c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon * When an item is purgeable DERIVED:notifyIsPurgeable() will be called (static poly morphism using 37bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * CRTP). GrIORef and GrGpuResource are separate classes for organizational reasons and to be 38bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon * able to give access via friendship to only the functions related to pending IO operations. 3976b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com */ 40bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomontemplate <typename DERIVED> class GrIORef : public SkNoncopyable { 418fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.compublic: 426f07665768dc84453316e7b2bbd6049576764cb1mtklein SK_DECLARE_INST_COUNT(GrIORef) 43c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon 4400b76bd750e668a6989dd497313e715d1b476fdcbsalomon // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with 4500b76bd750e668a6989dd497313e715d1b476fdcbsalomon // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of 4600b76bd750e668a6989dd497313e715d1b476fdcbsalomon // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are 4700b76bd750e668a6989dd497313e715d1b476fdcbsalomon // not intended to cross thread boundaries. 4800b76bd750e668a6989dd497313e715d1b476fdcbsalomon void ref() const { 499323b8b8e16df4adcd63ee8496a6382e8df535c9Brian Salomon this->validate(); 50bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon ++fRefCnt; 5100b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 5200b76bd750e668a6989dd497313e715d1b476fdcbsalomon 5300b76bd750e668a6989dd497313e715d1b476fdcbsalomon void unref() const { 5400b76bd750e668a6989dd497313e715d1b476fdcbsalomon this->validate(); 5500b76bd750e668a6989dd497313e715d1b476fdcbsalomon --fRefCnt; 56bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon this->didUnref(); 5700b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 5800b76bd750e668a6989dd497313e715d1b476fdcbsalomon 59c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon void validate() const { 6000b76bd750e668a6989dd497313e715d1b476fdcbsalomon#ifdef SK_DEBUG 6100b76bd750e668a6989dd497313e715d1b476fdcbsalomon SkASSERT(fRefCnt >= 0); 6200b76bd750e668a6989dd497313e715d1b476fdcbsalomon SkASSERT(fPendingReads >= 0); 6300b76bd750e668a6989dd497313e715d1b476fdcbsalomon SkASSERT(fPendingWrites >= 0); 6412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0); 65c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#endif 6600b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 6700b76bd750e668a6989dd497313e715d1b476fdcbsalomon 6800b76bd750e668a6989dd497313e715d1b476fdcbsalomonprotected: 691e2530babb65a883a01df5ee87147432f6707ce3bsalomon GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { } 7000b76bd750e668a6989dd497313e715d1b476fdcbsalomon 7163c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon bool isPurgeable() const { return !this->internalHasRef() && !this->internalHasPendingIO(); } 7212299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon 738d034a154fec81167ecb696c07da389b98cc02a7bsalomon bool internalHasPendingRead() const { return SkToBool(fPendingReads); } 748d034a154fec81167ecb696c07da389b98cc02a7bsalomon bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); } 758d034a154fec81167ecb696c07da389b98cc02a7bsalomon bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); } 768d034a154fec81167ecb696c07da389b98cc02a7bsalomon 776d4488c5e03010c94200b3706631d34ec3201411bsalomon bool internalHasRef() const { return SkToBool(fRefCnt); } 786d4488c5e03010c94200b3706631d34ec3201411bsalomon 7900b76bd750e668a6989dd497313e715d1b476fdcbsalomonprivate: 8000b76bd750e668a6989dd497313e715d1b476fdcbsalomon void addPendingRead() const { 8100b76bd750e668a6989dd497313e715d1b476fdcbsalomon this->validate(); 8200b76bd750e668a6989dd497313e715d1b476fdcbsalomon ++fPendingReads; 8300b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 8400b76bd750e668a6989dd497313e715d1b476fdcbsalomon 8500b76bd750e668a6989dd497313e715d1b476fdcbsalomon void completedRead() const { 8600b76bd750e668a6989dd497313e715d1b476fdcbsalomon this->validate(); 8700b76bd750e668a6989dd497313e715d1b476fdcbsalomon --fPendingReads; 88bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon this->didUnref(); 8900b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 9000b76bd750e668a6989dd497313e715d1b476fdcbsalomon 9100b76bd750e668a6989dd497313e715d1b476fdcbsalomon void addPendingWrite() const { 9200b76bd750e668a6989dd497313e715d1b476fdcbsalomon this->validate(); 9300b76bd750e668a6989dd497313e715d1b476fdcbsalomon ++fPendingWrites; 9400b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 9500b76bd750e668a6989dd497313e715d1b476fdcbsalomon 9600b76bd750e668a6989dd497313e715d1b476fdcbsalomon void completedWrite() const { 9700b76bd750e668a6989dd497313e715d1b476fdcbsalomon this->validate(); 9800b76bd750e668a6989dd497313e715d1b476fdcbsalomon --fPendingWrites; 99bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon this->didUnref(); 10000b76bd750e668a6989dd497313e715d1b476fdcbsalomon } 10100b76bd750e668a6989dd497313e715d1b476fdcbsalomon 10200b76bd750e668a6989dd497313e715d1b476fdcbsalomonprivate: 103bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon void didUnref() const { 10412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) { 10563c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon static_cast<const DERIVED*>(this)->notifyIsPurgeable(); 106bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon } 107bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon } 108bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon 10900b76bd750e668a6989dd497313e715d1b476fdcbsalomon mutable int32_t fRefCnt; 11000b76bd750e668a6989dd497313e715d1b476fdcbsalomon mutable int32_t fPendingReads; 11100b76bd750e668a6989dd497313e715d1b476fdcbsalomon mutable int32_t fPendingWrites; 11200b76bd750e668a6989dd497313e715d1b476fdcbsalomon 113ac8d6193eaa29e02d1786fe56efa98eefee74e50bsalomon // This class is used to manage conversion of refs to pending reads/writes. 114f96ba02513eadd9fa24d75396ec9f2d6682e464cbsalomon friend class GrGpuResourceRef; 1150ea80f43a1af05b8157a4ef387223bb5b0da35edbsalomon friend class GrResourceCache; // to check IO ref counts. 116bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon 117bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon template <typename, GrIOType> friend class GrPendingIOResource; 11800b76bd750e668a6989dd497313e715d1b476fdcbsalomon}; 11900b76bd750e668a6989dd497313e715d1b476fdcbsalomon 12000b76bd750e668a6989dd497313e715d1b476fdcbsalomon/** 1210ea80f43a1af05b8157a4ef387223bb5b0da35edbsalomon * Base class for objects that can be kept in the GrResourceCache. 12200b76bd750e668a6989dd497313e715d1b476fdcbsalomon */ 123544fe2338ff7459dbd887549aca31b8fc4cde7f4bsalomonclass SK_API GrGpuResource : public GrIORef<GrGpuResource> { 12400b76bd750e668a6989dd497313e715d1b476fdcbsalomonpublic: 12500b76bd750e668a6989dd497313e715d1b476fdcbsalomon SK_DECLARE_INST_COUNT(GrGpuResource) 126977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com 1275236cf480daf82b2f36e42795abdbbc915533a59bsalomon enum LifeCycle { 1285236cf480daf82b2f36e42795abdbbc915533a59bsalomon /** 1295236cf480daf82b2f36e42795abdbbc915533a59bsalomon * The resource is cached and owned by Skia. Resources with this status may be kept alive 1305236cf480daf82b2f36e42795abdbbc915533a59bsalomon * by the cache as either scratch or content resources even when there are no refs to them. 1315236cf480daf82b2f36e42795abdbbc915533a59bsalomon * The cache may release them whenever there are no refs. 1325236cf480daf82b2f36e42795abdbbc915533a59bsalomon */ 1335236cf480daf82b2f36e42795abdbbc915533a59bsalomon kCached_LifeCycle, 1345236cf480daf82b2f36e42795abdbbc915533a59bsalomon /** 1355236cf480daf82b2f36e42795abdbbc915533a59bsalomon * The resource is uncached. As soon as there are no more refs to it, it is released. Under 1365236cf480daf82b2f36e42795abdbbc915533a59bsalomon * the hood the cache may opaquely recycle it as a cached resource. 1375236cf480daf82b2f36e42795abdbbc915533a59bsalomon */ 1385236cf480daf82b2f36e42795abdbbc915533a59bsalomon kUncached_LifeCycle, 1395236cf480daf82b2f36e42795abdbbc915533a59bsalomon /** 1405236cf480daf82b2f36e42795abdbbc915533a59bsalomon * Similar to uncached, but Skia does not manage the lifetime of the underlying backend 1415236cf480daf82b2f36e42795abdbbc915533a59bsalomon * 3D API object(s). The client is responsible for freeing those. Used to inject client- 1425236cf480daf82b2f36e42795abdbbc915533a59bsalomon * created GPU resources into Skia (e.g. to render to a client-created texture). 1435236cf480daf82b2f36e42795abdbbc915533a59bsalomon */ 1445236cf480daf82b2f36e42795abdbbc915533a59bsalomon kWrapped_LifeCycle, 1455236cf480daf82b2f36e42795abdbbc915533a59bsalomon }; 1465236cf480daf82b2f36e42795abdbbc915533a59bsalomon 1478fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com /** 148089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * Tests whether a object has been abandoned or released. All objects will 149089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * be in this state after their creating GrContext is destroyed or has 150089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * contextLost called. It's up to the client to test wasDestroyed() before 151089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * attempting to use an object if it holds refs on objects across 1528fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com * ~GrContext, freeResources with the force flag, or contextLost. 1538fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com * 154089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * @return true if the object has been released or abandoned, 1558fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com * false otherwise. 1568fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com */ 157089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org bool wasDestroyed() const { return NULL == fGpu; } 1588fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 159cee661af926cc977addc6e039b7022975a448acebsalomon@google.com /** 160089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * Retrieves the context that owns the object. Note that it is possible for 161089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * this to return NULL. When objects have been release()ed or abandon()ed 162089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * they no longer have an owning context. Destroying a GrContext 163089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org * automatically releases all its resources. 164838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com */ 1651f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com const GrContext* getContext() const; 1661f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com GrContext* getContext(); 1671f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com 168c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon /** 169c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * Retrieves the amount of GPU memory used by this resource in bytes. It is 170c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * approximate since we aren't aware of additional padding or copies made 171c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * by the driver. 172c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * 173c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * @return the amount of GPU memory used in bytes 174c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon */ 17569ed47f42d4877c178fdc0031cb01af2966ae235bsalomon size_t gpuMemorySize() const { 17669ed47f42d4877c178fdc0031cb01af2966ae235bsalomon if (kInvalidGpuMemorySize == fGpuMemorySize) { 17769ed47f42d4877c178fdc0031cb01af2966ae235bsalomon fGpuMemorySize = this->onGpuMemorySize(); 17869ed47f42d4877c178fdc0031cb01af2966ae235bsalomon SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); 17969ed47f42d4877c178fdc0031cb01af2966ae235bsalomon } 18069ed47f42d4877c178fdc0031cb01af2966ae235bsalomon return fGpuMemorySize; 18169ed47f42d4877c178fdc0031cb01af2966ae235bsalomon } 182c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon 183728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com /** 18452e9d63f7110ac691609660342cdab32082a4235bsalomon * Gets an id that is unique for this GrGpuResource object. It is static in that it does 18552e9d63f7110ac691609660342cdab32082a4235bsalomon * not change when the content of the GrGpuResource object changes. This will never return 186c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon * 0. 187728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com */ 188c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon uint32_t getUniqueID() const { return fUniqueID; } 189c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon 190563ff60b33fa50b9c6a48dd048b8b36a88596668bsalomon /** Returns the current content key for the resource. It will be invalid if the resource has not 191563ff60b33fa50b9c6a48dd048b8b36a88596668bsalomon been cached by its contents. */ 192563ff60b33fa50b9c6a48dd048b8b36a88596668bsalomon const GrContentKey& getContentKey() const { return fContentKey; } 193563ff60b33fa50b9c6a48dd048b8b36a88596668bsalomon 194453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon /** 1955756aff40921e700dc40f2a1757291a64acddeaajunov * Attach a custom data object to this resource. The data will remain attached 1965756aff40921e700dc40f2a1757291a64acddeaajunov * for the lifetime of this resource (until it is abandoned or released). 1975756aff40921e700dc40f2a1757291a64acddeaajunov * Takes a ref on data. Previously attached data, if any, is unrefed. 1985756aff40921e700dc40f2a1757291a64acddeaajunov * Returns the data argument, for convenience. 1995756aff40921e700dc40f2a1757291a64acddeaajunov */ 2005756aff40921e700dc40f2a1757291a64acddeaajunov const SkData* setCustomData(const SkData* data); 2015756aff40921e700dc40f2a1757291a64acddeaajunov 2025756aff40921e700dc40f2a1757291a64acddeaajunov /** 2035756aff40921e700dc40f2a1757291a64acddeaajunov * Returns the custom data object that was attached to this resource by 2045756aff40921e700dc40f2a1757291a64acddeaajunov * calling setCustomData. 2055756aff40921e700dc40f2a1757291a64acddeaajunov */ 2065756aff40921e700dc40f2a1757291a64acddeaajunov const SkData* getCustomData() const { return fData.get(); } 2075756aff40921e700dc40f2a1757291a64acddeaajunov 2085756aff40921e700dc40f2a1757291a64acddeaajunov /** 2093582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon * Internal-only helper class used for manipulations of the resource by the cache. 210453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon */ 211453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon class CacheAccess; 212453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon inline CacheAccess cacheAccess(); 213453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon inline const CacheAccess cacheAccess() const; 214453cf40ac7702722695bb09ae2c6df44c19d008bbsalomon 215436293a3308d58ce494d9667bd13428dd6e35236junov /** 2163582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon * Internal-only helper class used for manipulations of the resource by internal code. 2173582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon */ 2183582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon class ResourcePriv; 2193582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon inline ResourcePriv resourcePriv(); 2203582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon inline const ResourcePriv resourcePriv() const; 2213582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon 2223582d3ee9fffdec715f5e4949a241ab08e6271ecbsalomon /** 223436293a3308d58ce494d9667bd13428dd6e35236junov * Removes references to objects in the underlying 3D API without freeing them. 224436293a3308d58ce494d9667bd13428dd6e35236junov * Called by CacheAccess. 225436293a3308d58ce494d9667bd13428dd6e35236junov * In general this method should not be called outside of skia. It was 226436293a3308d58ce494d9667bd13428dd6e35236junov * made by public for a special case where it needs to be called in Blink 227436293a3308d58ce494d9667bd13428dd6e35236junov * when a texture becomes unsafe to use after having been shared through 228436293a3308d58ce494d9667bd13428dd6e35236junov * a texture mailbox. 229436293a3308d58ce494d9667bd13428dd6e35236junov */ 230436293a3308d58ce494d9667bd13428dd6e35236junov void abandon(); 231436293a3308d58ce494d9667bd13428dd6e35236junov 232c44be0e9e4cee5402909c06370a630eee188a8f3bsalomonprotected: 233169612621f00b3fe9f71014079991287d311751absalomon // This must be called by every GrGpuObject. It should be called once the object is fully 234169612621f00b3fe9f71014079991287d311751absalomon // initialized (i.e. not in a base class constructor). 235169612621f00b3fe9f71014079991287d311751absalomon void registerWithCache(); 236169612621f00b3fe9f71014079991287d311751absalomon 2375236cf480daf82b2f36e42795abdbbc915533a59bsalomon GrGpuResource(GrGpu*, LifeCycle); 2386d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon virtual ~GrGpuResource(); 23976b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com 24076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com GrGpu* getGpu() const { return fGpu; } 2418fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 24212299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon /** Overridden to free GPU resources in the backend API. */ 24312299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon virtual void onRelease() { } 24412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon /** Overridden to abandon any internal handles, ptrs, etc to backend API resources. 24512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon This may be called when the underlying 3D context is no longer valid and so no 24612299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon backend API calls should be made. */ 24712299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon virtual void onAbandon() { } 2488fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 2495236cf480daf82b2f36e42795abdbbc915533a59bsalomon bool isWrapped() const { return kWrapped_LifeCycle == fLifeCycle; } 250a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com 251c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon /** 25269ed47f42d4877c178fdc0031cb01af2966ae235bsalomon * This entry point should be called whenever gpuMemorySize() should report a different size. 25369ed47f42d4877c178fdc0031cb01af2966ae235bsalomon * The cache will call gpuMemorySize() to update the current size of the resource. 254c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon */ 255c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon void didChangeGpuMemorySize() const; 256c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon 257744998e666073166307d2522847b2536000a7619bsalomon /** 258744998e666073166307d2522847b2536000a7619bsalomon * Optionally called by the GrGpuResource subclass if the resource can be used as scratch. 259744998e666073166307d2522847b2536000a7619bsalomon * By default resources are not usable as scratch. This should only be called once. 260744998e666073166307d2522847b2536000a7619bsalomon **/ 2617775c85611c734a2af709b3a9c127939a4296c48bsalomon void setScratchKey(const GrScratchKey& scratchKey); 262744998e666073166307d2522847b2536000a7619bsalomon 2638fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comprivate: 26412299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon /** 26512299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon * Frees the object in the underlying 3D API. Called by CacheAccess. 26612299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon */ 26712299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon void release(); 26812299ab7a1be5f4b99284ecf289d46107ef0a946bsalomon 26969ed47f42d4877c178fdc0031cb01af2966ae235bsalomon virtual size_t onGpuMemorySize() const = 0; 27069ed47f42d4877c178fdc0031cb01af2966ae235bsalomon 2719f2d1571ed1f0ed579e5d7779c46a90e20f30f22bsalomon // See comments in CacheAccess and ResourcePriv. 27224db3b1c35fb935660229da164fc5ad31977387fbsalomon bool setContentKey(const GrContentKey& contentKey); 27323e619cf462b2a8a500f3ca750e099f79601f508bsalomon void removeContentKey(); 27463c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon void notifyIsPurgeable() const; 27510e23caea3106be125acea10a637789e5a15c728bsalomon void removeScratchKey(); 276afe3005be3392e43bc51eb7eb2017eefaed85ad1bsalomon void makeBudgeted(); 277c2f35b750a57d7dc0b8053a98279631d1ccb9b56bsalomon void makeUnbudgeted(); 278bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon 279515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG 2809474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com friend class GrGpu; // for assert in GrGpu to access getGpu 2819474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com#endif 2828fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 283c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon static uint32_t CreateUniqueID(); 284c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon 285f320e04c50a1c8a861bc1d8f50bf732044ff9843bsalomon // An index into a heap when this resource is purgeable or an array when not. This is maintained 286f320e04c50a1c8a861bc1d8f50bf732044ff9843bsalomon // by the cache. 2879f2d1571ed1f0ed579e5d7779c46a90e20f30f22bsalomon int fCacheArrayIndex; 2889f2d1571ed1f0ed579e5d7779c46a90e20f30f22bsalomon // This value reflects how recently this resource was accessed in the cache. This is maintained 2899f2d1571ed1f0ed579e5d7779c46a90e20f30f22bsalomon // by the cache. 2909f2d1571ed1f0ed579e5d7779c46a90e20f30f22bsalomon uint32_t fTimestamp; 291c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon 29284c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); 29324db3b1c35fb935660229da164fc5ad31977387fbsalomon GrScratchKey fScratchKey; 29424db3b1c35fb935660229da164fc5ad31977387fbsalomon GrContentKey fContentKey; 29584c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon 29684c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon // This is not ref'ed but abandon() or release() will be called before the GrGpu object 29784c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon // is destroyed. Those calls set will this to NULL. 29824db3b1c35fb935660229da164fc5ad31977387fbsalomon GrGpu* fGpu; 29924db3b1c35fb935660229da164fc5ad31977387fbsalomon mutable size_t fGpuMemorySize; 30084c8e62fad59f0e19b40ac718467f5b7884b431dbsalomon 30124db3b1c35fb935660229da164fc5ad31977387fbsalomon LifeCycle fLifeCycle; 30224db3b1c35fb935660229da164fc5ad31977387fbsalomon const uint32_t fUniqueID; 303744998e666073166307d2522847b2536000a7619bsalomon 30424db3b1c35fb935660229da164fc5ad31977387fbsalomon SkAutoTUnref<const SkData> fData; 3055756aff40921e700dc40f2a1757291a64acddeaajunov 306bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon typedef GrIORef<GrGpuResource> INHERITED; 30763c992f6c05ea728b5386de61d279f10eb7e08d9bsalomon friend class GrIORef<GrGpuResource>; // to access notifyIsPurgeable. 3088fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com}; 3098fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com 3108fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com#endif 311