GrRenderTarget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2011 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 "include/gpu/GrRenderTarget.h"
  8. #include "include/gpu/GrContext.h"
  9. #include "src/core/SkRectPriv.h"
  10. #include "src/gpu/GrContextPriv.h"
  11. #include "src/gpu/GrGpu.h"
  12. #include "src/gpu/GrRenderTargetContext.h"
  13. #include "src/gpu/GrRenderTargetOpList.h"
  14. #include "src/gpu/GrRenderTargetPriv.h"
  15. #include "src/gpu/GrSamplePatternDictionary.h"
  16. #include "src/gpu/GrStencilAttachment.h"
  17. #include "src/gpu/GrStencilSettings.h"
  18. GrRenderTarget::GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc, int sampleCount,
  19. GrProtected isProtected, GrStencilAttachment* stencil)
  20. : INHERITED(gpu, desc, isProtected)
  21. , fSampleCnt(sampleCount)
  22. , fSamplePatternKey(GrSamplePatternDictionary::kInvalidSamplePatternKey)
  23. , fStencilAttachment(stencil) {
  24. fResolveRect = SkRectPriv::MakeILargestInverted();
  25. }
  26. GrRenderTarget::~GrRenderTarget() = default;
  27. void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
  28. if (kCanResolve_ResolveType == getResolveType()) {
  29. if (rect) {
  30. fResolveRect.join(*rect);
  31. if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
  32. fResolveRect.setEmpty();
  33. }
  34. } else {
  35. fResolveRect.setLTRB(0, 0, this->width(), this->height());
  36. }
  37. }
  38. }
  39. void GrRenderTarget::flagAsResolved() {
  40. fResolveRect = SkRectPriv::MakeILargestInverted();
  41. }
  42. void GrRenderTarget::onRelease() {
  43. fStencilAttachment = nullptr;
  44. INHERITED::onRelease();
  45. }
  46. void GrRenderTarget::onAbandon() {
  47. fStencilAttachment = nullptr;
  48. INHERITED::onAbandon();
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////
  51. void GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
  52. #ifdef SK_DEBUG
  53. if (1 == fRenderTarget->fSampleCnt) {
  54. // TODO: We don't expect a mixed sampled render target to ever change its stencil buffer
  55. // right now. But if it does swap in a stencil buffer with a different number of samples,
  56. // and if we have a valid fSamplePatternKey, we will need to invalidate fSamplePatternKey
  57. // here and add tests to make sure we it properly.
  58. SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey ==
  59. fRenderTarget->fSamplePatternKey);
  60. } else {
  61. // Render targets with >1 color sample should never use mixed samples. (This would lead to
  62. // different sample patterns, depending on stencil state.)
  63. SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
  64. }
  65. #endif
  66. if (!stencil && !fRenderTarget->fStencilAttachment) {
  67. // No need to do any work since we currently don't have a stencil attachment and
  68. // we're not actually adding one.
  69. return;
  70. }
  71. fRenderTarget->fStencilAttachment = std::move(stencil);
  72. if (!fRenderTarget->completeStencilAttachment()) {
  73. fRenderTarget->fStencilAttachment = nullptr;
  74. }
  75. }
  76. int GrRenderTargetPriv::numStencilBits() const {
  77. SkASSERT(this->getStencilAttachment());
  78. return this->getStencilAttachment()->bits();
  79. }
  80. int GrRenderTargetPriv::getSamplePatternKey() const {
  81. #ifdef SK_DEBUG
  82. GrStencilAttachment* stencil = fRenderTarget->fStencilAttachment.get();
  83. if (fRenderTarget->fSampleCnt <= 1) {
  84. // If the color buffer is not multisampled, the sample pattern better come from the stencil
  85. // buffer (mixed samples).
  86. SkASSERT(stencil && stencil->numSamples() > 1);
  87. } else {
  88. // The color sample count and stencil count cannot both be unequal and both greater than
  89. // one. If this were the case, there would be more than one sample pattern associated with
  90. // the render target.
  91. SkASSERT(!stencil || stencil->numSamples() == fRenderTarget->fSampleCnt);
  92. }
  93. #endif
  94. if (GrSamplePatternDictionary::kInvalidSamplePatternKey == fRenderTarget->fSamplePatternKey) {
  95. fRenderTarget->fSamplePatternKey =
  96. fRenderTarget->getGpu()->findOrAssignSamplePatternKey(fRenderTarget);
  97. }
  98. SkASSERT(GrSamplePatternDictionary::kInvalidSamplePatternKey
  99. != fRenderTarget->fSamplePatternKey);
  100. return fRenderTarget->fSamplePatternKey;
  101. }