GrTextureRenderTargetProxy.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/GrTextureRenderTargetProxy.h"
  8. #include "include/gpu/GrRenderTarget.h"
  9. #include "include/gpu/GrTexture.h"
  10. #include "src/gpu/GrCaps.h"
  11. #include "src/gpu/GrSurfacePriv.h"
  12. #include "src/gpu/GrSurfaceProxyPriv.h"
  13. #include "src/gpu/GrTexturePriv.h"
  14. #include "src/gpu/GrTextureProxyPriv.h"
  15. // Deferred version
  16. // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
  17. // GrRenderTargetProxy) so its constructor must be explicitly called.
  18. GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(const GrCaps& caps,
  19. const GrBackendFormat& format,
  20. const GrSurfaceDesc& desc,
  21. int sampleCnt,
  22. GrSurfaceOrigin origin,
  23. GrMipMapped mipMapped,
  24. const GrSwizzle& texSwizzle,
  25. const GrSwizzle& outSwizzle,
  26. SkBackingFit fit,
  27. SkBudgeted budgeted,
  28. GrProtected isProtected,
  29. GrInternalSurfaceFlags surfaceFlags)
  30. : GrSurfaceProxy(format, desc, GrRenderable::kYes, origin, texSwizzle, fit, budgeted,
  31. isProtected, surfaceFlags)
  32. // for now textures w/ data are always wrapped
  33. , GrRenderTargetProxy(caps, format, desc, sampleCnt, origin, texSwizzle, outSwizzle, fit,
  34. budgeted, isProtected, surfaceFlags)
  35. , GrTextureProxy(format, desc, origin, mipMapped, texSwizzle, fit, budgeted, isProtected,
  36. surfaceFlags) {}
  37. // Lazy-callback version
  38. GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
  39. LazyInstantiationType lazyType,
  40. const GrBackendFormat& format,
  41. const GrSurfaceDesc& desc,
  42. int sampleCnt,
  43. GrSurfaceOrigin origin,
  44. GrMipMapped mipMapped,
  45. const GrSwizzle& texSwizzle,
  46. const GrSwizzle& outSwizzle,
  47. SkBackingFit fit,
  48. SkBudgeted budgeted,
  49. GrProtected isProtected,
  50. GrInternalSurfaceFlags surfaceFlags)
  51. : GrSurfaceProxy(std::move(callback), lazyType, format, desc, GrRenderable::kYes, origin,
  52. texSwizzle, fit, budgeted, isProtected, surfaceFlags)
  53. // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
  54. // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
  55. , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, sampleCnt, origin,
  56. texSwizzle, outSwizzle, fit, budgeted, isProtected, surfaceFlags,
  57. WrapsVkSecondaryCB::kNo)
  58. , GrTextureProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, mipMapped,
  59. texSwizzle, fit, budgeted, isProtected, surfaceFlags) {}
  60. // Wrapped version
  61. // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
  62. // GrRenderTargetProxy) so its constructor must be explicitly called.
  63. GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
  64. GrSurfaceOrigin origin,
  65. const GrSwizzle& texSwizzle,
  66. const GrSwizzle& outSwizzle)
  67. : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact)
  68. , GrRenderTargetProxy(surf, origin, texSwizzle, outSwizzle)
  69. , GrTextureProxy(surf, origin, texSwizzle) {
  70. SkASSERT(surf->asTexture());
  71. SkASSERT(surf->asRenderTarget());
  72. }
  73. size_t GrTextureRenderTargetProxy::onUninstantiatedGpuMemorySize() const {
  74. int colorSamplesPerPixel = this->numSamples();
  75. if (colorSamplesPerPixel > 1) {
  76. // Add one to account for the resolve buffer.
  77. ++colorSamplesPerPixel;
  78. }
  79. // TODO: do we have enough information to improve this worst case estimate?
  80. return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
  81. colorSamplesPerPixel, this->proxyMipMapped(),
  82. !this->priv().isExact());
  83. }
  84. bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
  85. if (LazyState::kNot != this->lazyInstantiationState()) {
  86. return false;
  87. }
  88. const GrUniqueKey& key = this->getUniqueKey();
  89. if (!this->instantiateImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
  90. GrRenderable::kYes, this->mipMapped(),
  91. key.isValid() ? &key : nullptr)) {
  92. return false;
  93. }
  94. if (key.isValid()) {
  95. SkASSERT(key == this->getUniqueKey());
  96. }
  97. SkASSERT(this->peekRenderTarget());
  98. SkASSERT(this->peekTexture());
  99. return true;
  100. }
  101. sk_sp<GrSurface> GrTextureRenderTargetProxy::createSurface(
  102. GrResourceProvider* resourceProvider) const {
  103. sk_sp<GrSurface> surface =
  104. this->createSurfaceImpl(resourceProvider, this->numSamples(), this->numStencilSamples(),
  105. GrRenderable::kYes, this->mipMapped());
  106. if (!surface) {
  107. return nullptr;
  108. }
  109. SkASSERT(surface->asRenderTarget());
  110. SkASSERT(surface->asTexture());
  111. return surface;
  112. }
  113. #ifdef SK_DEBUG
  114. void GrTextureRenderTargetProxy::onValidateSurface(const GrSurface* surface) {
  115. // Anything checked here should also be checking the GrTextureProxy version
  116. SkASSERT(surface->asTexture());
  117. SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
  118. GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
  119. // Anything checked here should also be checking the GrRenderTargetProxy version
  120. SkASSERT(surface->asRenderTarget());
  121. SkASSERT(surface->asRenderTarget()->numSamples() == this->numSamples());
  122. SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
  123. GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
  124. GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
  125. // Only non-RT textures can be read only.
  126. SkASSERT(!(proxyFlags & GrInternalSurfaceFlags::kReadOnly));
  127. SkASSERT(!(surfaceFlags & GrInternalSurfaceFlags::kReadOnly));
  128. SkASSERT((proxyFlags & GrInternalSurfaceFlags::kRenderTargetMask) ==
  129. (surfaceFlags & GrInternalSurfaceFlags::kRenderTargetMask));
  130. SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
  131. (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
  132. }
  133. #endif