GrCoordTransform.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2013 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 GrCoordTransform_DEFINED
  8. #define GrCoordTransform_DEFINED
  9. #include "include/core/SkMatrix.h"
  10. #include "src/gpu/GrSurfaceProxyPriv.h"
  11. #include "src/gpu/GrTextureProxy.h"
  12. class GrTexture;
  13. /**
  14. * A class representing a linear transformation of local coordinates. GrFragnentProcessors
  15. * these transformations, and the GrGeometryProcessor implements the transformation.
  16. */
  17. class GrCoordTransform {
  18. public:
  19. GrCoordTransform()
  20. : fProxy(nullptr)
  21. , fNormalize(false)
  22. , fReverseY(false) {
  23. SkDEBUGCODE(fInProcessor = false);
  24. }
  25. GrCoordTransform(const GrCoordTransform&) = default;
  26. /**
  27. * Create a transformation that maps [0, 1] to a proxy's boundaries. The proxy origin also
  28. * implies whether a y-reversal should be performed.
  29. */
  30. GrCoordTransform(GrTextureProxy* proxy) {
  31. SkASSERT(proxy);
  32. SkDEBUGCODE(fInProcessor = false);
  33. this->reset(SkMatrix::I(), proxy);
  34. }
  35. /**
  36. * Create a transformation from a matrix. The proxy origin also implies whether a y-reversal
  37. * should be performed.
  38. */
  39. GrCoordTransform(const SkMatrix& m, GrTextureProxy* proxy) {
  40. SkASSERT(proxy);
  41. SkDEBUGCODE(fInProcessor = false);
  42. this->reset(m, proxy);
  43. }
  44. /**
  45. * Create a transformation that applies the matrix to a coord set.
  46. */
  47. GrCoordTransform(const SkMatrix& m) {
  48. SkDEBUGCODE(fInProcessor = false);
  49. this->reset(m);
  50. }
  51. GrCoordTransform& operator= (const GrCoordTransform& that) {
  52. SkASSERT(!fInProcessor);
  53. fMatrix = that.fMatrix;
  54. fProxy = that.fProxy;
  55. fNormalize = that.fNormalize;
  56. fReverseY = that.fReverseY;
  57. return *this;
  58. }
  59. /**
  60. * Access the matrix for editing. Note, this must be done before adding the transform to an
  61. * effect, since effects are immutable.
  62. */
  63. SkMatrix* accessMatrix() {
  64. SkASSERT(!fInProcessor);
  65. return &fMatrix;
  66. }
  67. bool hasSameEffectAs(const GrCoordTransform& that) const {
  68. if (fNormalize != that.fNormalize ||
  69. fReverseY != that.fReverseY ||
  70. !fMatrix.cheapEqualTo(that.fMatrix)) {
  71. return false;
  72. }
  73. if (fNormalize) {
  74. if (fProxy->underlyingUniqueID() != that.fProxy->underlyingUniqueID()) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. const SkMatrix& getMatrix() const { return fMatrix; }
  81. const GrTextureProxy* proxy() const { return fProxy; }
  82. bool normalize() const { return fNormalize; }
  83. bool reverseY() const { return fReverseY; }
  84. // This should only ever be called at flush time after the backing texture has been
  85. // successfully instantiated
  86. GrTexture* peekTexture() const { return fProxy->peekTexture(); }
  87. private:
  88. void reset(const SkMatrix& m, GrTextureProxy* proxy = nullptr) {
  89. SkASSERT(!fInProcessor);
  90. fMatrix = m;
  91. fProxy = proxy;
  92. fNormalize = proxy && proxy->textureType() != GrTextureType::kRectangle;
  93. fReverseY = proxy && kBottomLeft_GrSurfaceOrigin == proxy->origin();
  94. }
  95. // The textures' effect is to optionally normalize the final matrix, so a blind
  96. // equality check could be misleading
  97. bool operator==(const GrCoordTransform& that) const;
  98. bool operator!=(const GrCoordTransform& that) const;
  99. SkMatrix fMatrix;
  100. const GrTextureProxy* fProxy;
  101. bool fNormalize;
  102. bool fReverseY;
  103. #ifdef SK_DEBUG
  104. public:
  105. void setInProcessor() const { fInProcessor = true; }
  106. private:
  107. mutable bool fInProcessor;
  108. #endif
  109. };
  110. #endif