1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #include "GrGpuResource.h" 11 #include "GrResourceCache2.h" 12 #include "GrGpu.h" 13 14 GrIORef::~GrIORef() { 15 SkASSERT(0 == fRefCnt); 16 SkASSERT(0 == fPendingReads); 17 SkASSERT(0 == fPendingWrites); 18 // Set to invalid values. 19 SkDEBUGCODE(fRefCnt = fPendingReads = fPendingWrites = -10;) 20 } 21 22 /////////////////////////////////////////////////////////////////////////////// 23 24 static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { 25 SkASSERT(gpu); 26 SkASSERT(gpu->getContext()); 27 SkASSERT(gpu->getContext()->getResourceCache2()); 28 return gpu->getContext()->getResourceCache2(); 29 } 30 31 GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) 32 : fGpu(gpu) 33 , fCacheEntry(NULL) 34 , fUniqueID(CreateUniqueID()) 35 , fScratchKey(GrResourceKey::NullScratchKey()) { 36 if (isWrapped) { 37 fFlags = kWrapped_FlagBit; 38 } else { 39 fFlags = 0; 40 } 41 } 42 43 void GrGpuResource::registerWithCache() { 44 get_resource_cache2(fGpu)->insertResource(this); 45 } 46 47 GrGpuResource::~GrGpuResource() { 48 // subclass should have released this. 49 SkASSERT(this->wasDestroyed()); 50 } 51 52 void GrGpuResource::release() { 53 if (fGpu) { 54 this->onRelease(); 55 get_resource_cache2(fGpu)->removeResource(this); 56 fGpu = NULL; 57 } 58 } 59 60 void GrGpuResource::abandon() { 61 if (fGpu) { 62 this->onAbandon(); 63 get_resource_cache2(fGpu)->removeResource(this); 64 fGpu = NULL; 65 } 66 } 67 68 const GrContext* GrGpuResource::getContext() const { 69 if (fGpu) { 70 return fGpu->getContext(); 71 } else { 72 return NULL; 73 } 74 } 75 76 GrContext* GrGpuResource::getContext() { 77 if (fGpu) { 78 return fGpu->getContext(); 79 } else { 80 return NULL; 81 } 82 } 83 84 void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) { 85 SkASSERT(fScratchKey.isNullScratch()); 86 SkASSERT(scratchKey.isScratch()); 87 SkASSERT(!scratchKey.isNullScratch()); 88 fScratchKey = scratchKey; 89 } 90 91 uint32_t GrGpuResource::CreateUniqueID() { 92 static int32_t gUniqueID = SK_InvalidUniqueID; 93 uint32_t id; 94 do { 95 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); 96 } while (id == SK_InvalidUniqueID); 97 return id; 98 } 99