GrTextureProxy.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "include/gpu/GrSamplerState.h"
  10. #include "src/gpu/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. GrTextureType textureType() const { return this->backendFormat().textureType(); }
  35. /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
  36. bool hasRestrictedSampling() const {
  37. return GrTextureTypeHasRestrictedSampling(this->textureType());
  38. }
  39. // Returns true if the passed in proxies can be used as dynamic state together when flushing
  40. // draws to the gpu.
  41. static bool ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
  42. const GrTextureProxy* second);
  43. /**
  44. * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
  45. */
  46. const GrUniqueKey& getUniqueKey() const {
  47. #ifdef SK_DEBUG
  48. if (this->isInstantiated() && fUniqueKey.isValid() && fSyncTargetKey) {
  49. GrSurface* surface = this->peekSurface();
  50. SkASSERT(surface);
  51. SkASSERT(surface->getUniqueKey().isValid());
  52. // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to
  53. // it. This just means that a future user of the resource will be filling it with unique
  54. // data. However, if the proxy has a unique key its attached resource should also
  55. // have that key.
  56. SkASSERT(fUniqueKey == surface->getUniqueKey());
  57. }
  58. #endif
  59. return fUniqueKey;
  60. }
  61. /**
  62. * Internal-only helper class used for manipulations of the resource by the cache.
  63. */
  64. class CacheAccess;
  65. inline CacheAccess cacheAccess();
  66. inline const CacheAccess cacheAccess() const;
  67. // Provides access to special purpose functions.
  68. GrTextureProxyPriv texPriv();
  69. const GrTextureProxyPriv texPriv() const;
  70. protected:
  71. // DDL TODO: rm the GrSurfaceProxy friending
  72. friend class GrSurfaceProxy; // for ctors
  73. friend class GrProxyProvider; // for ctors
  74. friend class GrTextureProxyPriv;
  75. friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation.
  76. // Deferred version - no data.
  77. GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin,
  78. GrMipMapped, const GrSwizzle& textureSwizzle, SkBackingFit, SkBudgeted,
  79. GrProtected, GrInternalSurfaceFlags);
  80. // Lazy-callback version
  81. // There are two main use cases for lazily-instantiated proxies:
  82. // basic knowledge - width, height, config, origin are known
  83. // minimal knowledge - only config is known.
  84. //
  85. // The basic knowledge version is used for DDL where we know the type of proxy we are going to
  86. // use, but we don't have access to the GPU yet to instantiate it.
  87. //
  88. // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
  89. // know the final size until flush time.
  90. GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&,
  91. const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, const GrSwizzle&,
  92. SkBackingFit, SkBudgeted, GrProtected, GrInternalSurfaceFlags);
  93. // Wrapped version
  94. GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle&);
  95. ~GrTextureProxy() override;
  96. sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
  97. void setTargetKeySync(bool sync) { fSyncTargetKey = sync; }
  98. private:
  99. // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
  100. // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
  101. // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
  102. // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
  103. // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
  104. // that particular class don't require it. Changing the size of this object can move the start
  105. // address of other types, leading to this problem.
  106. GrMipMapped fMipMapped;
  107. bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours.
  108. GrUniqueKey fUniqueKey;
  109. GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
  110. // Only used for proxies whose contents are being prepared on a worker thread. This object
  111. // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
  112. // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
  113. std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
  114. size_t onUninstantiatedGpuMemorySize() const override;
  115. // Methods made available via GrTextureProxy::CacheAccess
  116. void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
  117. void clearUniqueKey();
  118. SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
  119. // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy.
  120. // For deferred proxies that pointer will be filled in when we need to instantiate
  121. // the deferred resource
  122. typedef GrSurfaceProxy INHERITED;
  123. };
  124. #endif