GrGpuResourceCacheAccess.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrGpuResourceCacheAccess_DEFINED
  8. #define GrGpuResourceCacheAccess_DEFINED
  9. #include "include/gpu/GrGpuResource.h"
  10. #include "src/gpu/GrGpuResourcePriv.h"
  11. namespace skiatest {
  12. class Reporter;
  13. }
  14. /**
  15. * This class allows GrResourceCache increased privileged access to GrGpuResource objects.
  16. */
  17. class GrGpuResource::CacheAccess {
  18. private:
  19. /** The cache is allowed to go from no refs to 1 ref. */
  20. void ref() { fResource->addInitialRef(); }
  21. /**
  22. * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
  23. * key, and does not have a unique key.
  24. */
  25. bool isScratch() const {
  26. return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
  27. GrBudgetedType::kBudgeted == fResource->resourcePriv().budgetedType();
  28. }
  29. /**
  30. * Called by the cache to delete the resource under normal circumstances.
  31. */
  32. void release() {
  33. fResource->release();
  34. if (!fResource->hasRefOrPendingIO()) {
  35. delete fResource;
  36. }
  37. }
  38. /**
  39. * Called by the cache to delete the resource when the backend 3D context is no longer valid.
  40. */
  41. void abandon() {
  42. fResource->abandon();
  43. if (!fResource->hasRefOrPendingIO()) {
  44. delete fResource;
  45. }
  46. }
  47. /** Called by the cache to assign a new unique key. */
  48. void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
  49. /** Is the resource ref'ed (not counting pending IOs). */
  50. bool hasRef() const { return fResource->hasRef(); }
  51. /** Called by the cache to make the unique key invalid. */
  52. void removeUniqueKey() { fResource->fUniqueKey.reset(); }
  53. uint32_t timestamp() const { return fResource->fTimestamp; }
  54. void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
  55. void setTimeWhenResourceBecomePurgeable() {
  56. SkASSERT(fResource->isPurgeable());
  57. fResource->fTimeWhenBecamePurgeable = GrStdSteadyClock::now();
  58. }
  59. /**
  60. * Called by the cache to determine whether this resource should be purged based on the length
  61. * of time it has been available for purging.
  62. */
  63. GrStdSteadyClock::time_point timeWhenResourceBecamePurgeable() {
  64. SkASSERT(fResource->isPurgeable());
  65. return fResource->fTimeWhenBecamePurgeable;
  66. }
  67. int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
  68. CacheAccess(GrGpuResource* resource) : fResource(resource) {}
  69. CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
  70. CacheAccess& operator=(const CacheAccess&); // unimpl
  71. // No taking addresses of this type.
  72. const CacheAccess* operator&() const = delete;
  73. CacheAccess* operator&() = delete;
  74. GrGpuResource* fResource;
  75. friend class GrGpuResource; // to construct/copy this type.
  76. friend class GrResourceCache; // to use this type
  77. friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
  78. };
  79. inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
  80. inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
  81. return CacheAccess(const_cast<GrGpuResource*>(this));
  82. }
  83. #endif