GrTextureProxy.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2016 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 GrTextureProxy_DEFINED
  8. #define GrTextureProxy_DEFINED
  9. #include "GrSamplerState.h"
  10. #include "GrSurfaceProxy.h"
  11. class GrCaps;
  12. class GrDeferredProxyUploader;
  13. class GrProxyProvider;
  14. class GrResourceProvider;
  15. class GrTextureOpList;
  16. class GrTextureProxyPriv;
  17. // This class delays the acquisition of textures until they are actually required
  18. class GrTextureProxy : virtual public GrSurfaceProxy {
  19. public:
  20. GrTextureProxy* asTextureProxy() override { return this; }
  21. const GrTextureProxy* asTextureProxy() const override { return this; }
  22. // Actually instantiate the backing texture, if necessary
  23. bool instantiate(GrResourceProvider*) override;
  24. GrSamplerState::Filter highestFilterMode() const;
  25. // If we are instantiated and have a target, return the mip state of that target. Otherwise
  26. // returns the proxy's mip state from creation time. This is useful for lazy proxies which may
  27. // claim to not need mips at creation time, but the instantiation happens to give us a mipped
  28. // target. In that case we should use that for our benefit to avoid possible copies/mip
  29. // generation later.
  30. GrMipMapped mipMapped() const;
  31. // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has
  32. // been instantiated or not.
  33. GrMipMapped proxyMipMapped() const { return fMipMapped; }
  34. /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
  35. bool hasRestrictedSampling() const {
  36. return GrTextureTypeHasRestrictedSampling(this->textureType());
  37. }
  38. /**
  39. * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
  40. */
  41. const GrUniqueKey& getUniqueKey() const {
  42. #ifdef SK_DEBUG
  43. if (fTarget && fUniqueKey.isValid()) {
  44. SkASSERT(fTarget->getUniqueKey().isValid());
  45. // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to
  46. // it. This just means that a future user of the resource will be filling it with unique
  47. // data. However, if the proxy has a unique key its attached resource should also
  48. // have that key.
  49. SkASSERT(fUniqueKey == fTarget->getUniqueKey());
  50. }
  51. #endif
  52. return fUniqueKey;
  53. }
  54. /**
  55. * Internal-only helper class used for manipulations of the resource by the cache.
  56. */
  57. class CacheAccess;
  58. inline CacheAccess cacheAccess();
  59. inline const CacheAccess cacheAccess() const;
  60. // Provides access to special purpose functions.
  61. GrTextureProxyPriv texPriv();
  62. const GrTextureProxyPriv texPriv() const;
  63. protected:
  64. // DDL TODO: rm the GrSurfaceProxy friending
  65. friend class GrSurfaceProxy; // for ctors
  66. friend class GrProxyProvider; // for ctors
  67. friend class GrTextureProxyPriv;
  68. // Deferred version - when constructed with data the origin is always kTopLeft.
  69. GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrMipMapped, SkBackingFit,
  70. SkBudgeted, const void* srcData, size_t srcRowBytes, GrInternalSurfaceFlags);
  71. // Deferred version - no data.
  72. GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin,
  73. GrMipMapped, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
  74. // Lazy-callback version
  75. // There are two main use cases for lazily-instantiated proxies:
  76. // basic knowledge - width, height, config, origin are known
  77. // minimal knowledge - only config is known.
  78. //
  79. // The basic knowledge version is used for DDL where we know the type of proxy we are going to
  80. // use, but we don't have access to the GPU yet to instantiate it.
  81. //
  82. // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
  83. // know the final size until flush time.
  84. GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&,
  85. const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, SkBackingFit,
  86. SkBudgeted, GrInternalSurfaceFlags);
  87. // Wrapped version
  88. GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin);
  89. ~GrTextureProxy() override;
  90. sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
  91. private:
  92. // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
  93. // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
  94. // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
  95. // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
  96. // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
  97. // that particular class don't require it. Changing the size of this object can move the start
  98. // address of other types, leading to this problem.
  99. GrMipMapped fMipMapped;
  100. GrUniqueKey fUniqueKey;
  101. GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
  102. // Only used for proxies whose contents are being prepared on a worker thread. This object
  103. // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
  104. // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
  105. std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
  106. size_t onUninstantiatedGpuMemorySize() const override;
  107. // Methods made available via GrTextureProxy::CacheAccess
  108. void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
  109. void clearUniqueKey();
  110. SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
  111. // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy.
  112. // For deferred proxies that pointer will be filled in when we need to instantiate
  113. // the deferred resource
  114. typedef GrSurfaceProxy INHERITED;
  115. };
  116. #endif