GrTextureProxy.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "src/gpu/GrTextureProxy.h"
  8. #include "src/gpu/GrTextureProxyPriv.h"
  9. #include "include/gpu/GrContext.h"
  10. #include "src/gpu/GrContextPriv.h"
  11. #include "src/gpu/GrDeferredProxyUploader.h"
  12. #include "src/gpu/GrProxyProvider.h"
  13. #include "src/gpu/GrSurfacePriv.h"
  14. #include "src/gpu/GrTexturePriv.h"
  15. // Deferred version - no data
  16. GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc,
  17. GrSurfaceOrigin origin, GrMipMapped mipMapped,
  18. const GrSwizzle& textureSwizzle, SkBackingFit fit,
  19. SkBudgeted budgeted, GrProtected isProtected,
  20. GrInternalSurfaceFlags surfaceFlags)
  21. : INHERITED(format, srcDesc, GrRenderable::kNo, origin, textureSwizzle, fit, budgeted,
  22. isProtected, surfaceFlags)
  23. , fMipMapped(mipMapped)
  24. , fProxyProvider(nullptr)
  25. , fDeferredUploader(nullptr) {}
  26. // Lazy-callback version
  27. GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
  28. const GrBackendFormat& format, const GrSurfaceDesc& desc,
  29. GrSurfaceOrigin origin, GrMipMapped mipMapped,
  30. const GrSwizzle& texSwizzle, SkBackingFit fit, SkBudgeted budgeted,
  31. GrProtected isProtected, GrInternalSurfaceFlags surfaceFlags)
  32. : INHERITED(std::move(callback), lazyType, format, desc, GrRenderable::kNo, origin,
  33. texSwizzle, fit, budgeted, isProtected, surfaceFlags)
  34. , fMipMapped(mipMapped)
  35. , fProxyProvider(nullptr)
  36. , fDeferredUploader(nullptr) {}
  37. // Wrapped version
  38. GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
  39. const GrSwizzle& textureSwizzle)
  40. : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
  41. , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
  42. , fProxyProvider(nullptr)
  43. , fDeferredUploader(nullptr) {
  44. if (fTarget->getUniqueKey().isValid()) {
  45. fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
  46. fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
  47. }
  48. }
  49. GrTextureProxy::~GrTextureProxy() {
  50. // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
  51. // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
  52. fTarget = nullptr;
  53. // In DDL-mode, uniquely keyed proxies keep their key even after their originating
  54. // proxy provider has gone away. In that case there is noone to send the invalid key
  55. // message to (Note: in this case we don't want to remove its cached resource).
  56. if (fUniqueKey.isValid() && fProxyProvider) {
  57. fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
  58. GrProxyProvider::InvalidateGPUResource::kNo);
  59. } else {
  60. SkASSERT(!fProxyProvider);
  61. }
  62. }
  63. bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
  64. if (LazyState::kNot != this->lazyInstantiationState()) {
  65. return false;
  66. }
  67. if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, GrRenderable::kNo,
  68. fMipMapped, fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
  69. return false;
  70. }
  71. SkASSERT(!this->peekRenderTarget());
  72. SkASSERT(this->peekTexture());
  73. return true;
  74. }
  75. sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
  76. sk_sp<GrSurface> surface =
  77. this->createSurfaceImpl(resourceProvider, 1,
  78. /* needsStencil = */ false, GrRenderable::kNo, fMipMapped);
  79. if (!surface) {
  80. return nullptr;
  81. }
  82. SkASSERT(!surface->asRenderTarget());
  83. SkASSERT(surface->asTexture());
  84. return surface;
  85. }
  86. void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
  87. SkASSERT(!fTextureProxy->fDeferredUploader);
  88. fTextureProxy->fDeferredUploader = std::move(uploader);
  89. }
  90. void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
  91. // The texture proxy's contents may already have been uploaded or instantiation may have failed
  92. if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
  93. fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
  94. }
  95. }
  96. void GrTextureProxyPriv::resetDeferredUploader() {
  97. SkASSERT(fTextureProxy->fDeferredUploader);
  98. fTextureProxy->fDeferredUploader.reset();
  99. }
  100. GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
  101. return this->hasRestrictedSampling() ? GrSamplerState::Filter::kBilerp
  102. : GrSamplerState::Filter::kMipMap;
  103. }
  104. GrMipMapped GrTextureProxy::mipMapped() const {
  105. if (this->isInstantiated()) {
  106. return this->peekTexture()->texturePriv().mipMapped();
  107. }
  108. return fMipMapped;
  109. }
  110. size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
  111. return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
  112. this->proxyMipMapped(), !this->priv().isExact());
  113. }
  114. bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
  115. const GrTextureProxy* second) {
  116. return first->config() == second->config() &&
  117. first->textureType() == second->textureType() &&
  118. first->backendFormat() == second->backendFormat();
  119. }
  120. void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
  121. SkASSERT(key.isValid());
  122. SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
  123. if (fTarget && fSyncTargetKey) {
  124. if (!fTarget->getUniqueKey().isValid()) {
  125. fTarget->resourcePriv().setUniqueKey(key);
  126. }
  127. SkASSERT(fTarget->getUniqueKey() == key);
  128. }
  129. fUniqueKey = key;
  130. fProxyProvider = proxyProvider;
  131. }
  132. void GrTextureProxy::clearUniqueKey() {
  133. fUniqueKey.reset();
  134. fProxyProvider = nullptr;
  135. }
  136. #ifdef SK_DEBUG
  137. void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
  138. SkASSERT(!surface->asRenderTarget());
  139. // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
  140. SkASSERT(surface->asTexture());
  141. SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
  142. GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
  143. SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
  144. GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
  145. GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
  146. SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
  147. (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
  148. }
  149. #endif