| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #ifndef GrGpuResourceCacheAccess_DEFINED | 9 #ifndef GrGpuResourceCacheAccess_DEFINED |
| 10 #define GrGpuResourceCacheAccess_DEFINED | 10 #define GrGpuResourceCacheAccess_DEFINED |
| 11 | 11 |
| 12 #include "GrGpuResource.h" | 12 #include "GrGpuResource.h" |
| 13 #include "GrGpuResourcePriv.h" |
| 14 |
| 15 namespace skiatest { |
| 16 class Reporter; |
| 17 } |
| 13 | 18 |
| 14 /** | 19 /** |
| 15 * This class allows code internal to Skia privileged access to manage the cache
keys of a | 20 * This class allows GrResourceCache increased privileged access to GrGpuResourc
e objects. |
| 16 * GrGpuResource object. | |
| 17 */ | 21 */ |
| 18 class GrGpuResource::CacheAccess { | 22 class GrGpuResource::CacheAccess { |
| 19 public: | 23 private: |
| 20 /** | |
| 21 * Sets a content key for the resource. If the resource was previously cache
d as scratch it will | |
| 22 * be converted to a content resource. Currently this may only be called onc
e per resource. It | |
| 23 * fails if there is already a resource with the same content key. TODO: mak
e this supplant the | |
| 24 * resource that currently is using the content key, allow resources' conten
t keys to change, | |
| 25 * and allow removal of a content key to convert a resource back to scratch. | |
| 26 */ | |
| 27 bool setContentKey(const GrContentKey& contentKey) { | |
| 28 return fResource->setContentKey(contentKey); | |
| 29 } | |
| 30 | |
| 31 void removeContentKey() { return fResource->removeContentKey(); } | |
| 32 | |
| 33 /** | 24 /** |
| 34 * Is the resource currently cached as scratch? This means it is cached, has
a valid scratch | 25 * Is the resource currently cached as scratch? This means it is cached, has
a valid scratch |
| 35 * key, and does not have a content key. | 26 * key, and does not have a content key. |
| 36 */ | 27 */ |
| 37 bool isScratch() const { | 28 bool isScratch() const { |
| 38 return !fResource->getContentKey().isValid() && fResource->fScratchKey.i
sValid() && | 29 return !fResource->getContentKey().isValid() && fResource->fScratchKey.i
sValid() && |
| 39 this->isBudgeted(); | 30 fResource->resourcePriv().isBudgeted(); |
| 40 } | 31 } |
| 41 | 32 |
| 42 /** | |
| 43 * If this resource can be used as a scratch resource this returns a valid s
cratch key. | |
| 44 * Otherwise it returns a key for which isNullScratch is true. The resource
may currently be | |
| 45 * used as a content resource rather than scratch. Check isScratch(). | |
| 46 */ | |
| 47 const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; } | |
| 48 | |
| 49 /** | |
| 50 * If the resource has a scratch key, the key will be removed. Since scratch
keys are installed | |
| 51 * at resource creation time, this means the resource will never again be us
ed as scratch. | |
| 52 */ | |
| 53 void removeScratchKey() const { fResource->removeScratchKey(); } | |
| 54 | |
| 55 /** | 33 /** |
| 56 * Is the resource object wrapping an externally allocated GPU resource? | 34 * Is the resource object wrapping an externally allocated GPU resource? |
| 57 */ | 35 */ |
| 58 bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResour
ce->fLifeCycle; } | 36 bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResour
ce->fLifeCycle; } |
| 59 | 37 |
| 60 /** | |
| 61 * Does the resource count against the resource budget? | |
| 62 */ | |
| 63 bool isBudgeted() const { | |
| 64 bool ret = GrGpuResource::kCached_LifeCycle == fResource->fLifeCycle; | |
| 65 SkASSERT(ret || !fResource->getContentKey().isValid()); | |
| 66 return ret; | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * If the resource is uncached make it cached. Has no effect on resources th
at are wrapped or | |
| 71 * already cached. | |
| 72 */ | |
| 73 void makeBudgeted() { fResource->makeBudgeted(); } | |
| 74 | |
| 75 /** | |
| 76 * If the resource is cached make it uncached. Has no effect on resources th
at are wrapped or | |
| 77 * already uncached. Furthermore, resources with content keys cannot be made
unbudgeted. | |
| 78 */ | |
| 79 void makeUnbudgeted() { fResource->makeUnbudgeted(); } | |
| 80 | |
| 81 /** | 38 /** |
| 82 * Called by the cache to delete the resource under normal circumstances. | 39 * Called by the cache to delete the resource under normal circumstances. |
| 83 */ | 40 */ |
| 84 void release() { | 41 void release() { |
| 85 fResource->release(); | 42 fResource->release(); |
| 86 if (fResource->isPurgeable()) { | 43 if (fResource->isPurgeable()) { |
| 87 SkDELETE(fResource); | 44 SkDELETE(fResource); |
| 88 } | 45 } |
| 89 } | 46 } |
| 90 | 47 |
| 91 /** | 48 /** |
| 92 * Called by the cache to delete the resource when the backend 3D context is
no longer valid. | 49 * Called by the cache to delete the resource when the backend 3D context is
no longer valid. |
| 93 */ | 50 */ |
| 94 void abandon() { | 51 void abandon() { |
| 95 fResource->abandon(); | 52 fResource->abandon(); |
| 96 if (fResource->isPurgeable()) { | 53 if (fResource->isPurgeable()) { |
| 97 SkDELETE(fResource); | 54 SkDELETE(fResource); |
| 98 } | 55 } |
| 99 } | 56 } |
| 100 | 57 |
| 101 private: | 58 CacheAccess(GrGpuResource* resource) : fResource(resource) {} |
| 102 CacheAccess(GrGpuResource* resource) : fResource(resource) { } | 59 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {} |
| 103 CacheAccess(const CacheAccess& that) : fResource(that.fResource) { } | |
| 104 CacheAccess& operator=(const CacheAccess&); // unimpl | 60 CacheAccess& operator=(const CacheAccess&); // unimpl |
| 105 | 61 |
| 106 // No taking addresses of this type. | 62 // No taking addresses of this type. |
| 107 const CacheAccess* operator&() const; | 63 const CacheAccess* operator&() const; |
| 108 CacheAccess* operator&(); | 64 CacheAccess* operator&(); |
| 109 | 65 |
| 110 GrGpuResource* fResource; | 66 GrGpuResource* fResource; |
| 111 | 67 |
| 112 friend class GrGpuResource; // to construct/copy this type. | 68 friend class GrGpuResource; // to construct/copy this type. |
| 69 friend class GrResourceCache; // to use this type |
| 70 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for
unit testing |
| 113 }; | 71 }; |
| 114 | 72 |
| 115 inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAcc
ess(this); } | 73 inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAcc
ess(this); } |
| 116 | 74 |
| 117 inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { | 75 inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 118 return CacheAccess(const_cast<GrGpuResource*>(this)); | 76 return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 119 } | 77 } |
| 120 | 78 |
| 121 #endif | 79 #endif |
| OLD | NEW |