SkLocalMatrixShader.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2014 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/core/SkTLazy.h"
  8. #include "src/shaders/SkLocalMatrixShader.h"
  9. #if SK_SUPPORT_GPU
  10. #include "src/gpu/GrFragmentProcessor.h"
  11. #endif
  12. #if SK_SUPPORT_GPU
  13. std::unique_ptr<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(
  14. const GrFPArgs& args) const {
  15. return as_SB(fProxyShader)->asFragmentProcessor(
  16. GrFPArgs::WithPreLocalMatrix(args, this->getLocalMatrix()));
  17. }
  18. #endif
  19. sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
  20. SkMatrix lm;
  21. buffer.readMatrix(&lm);
  22. auto baseShader(buffer.readShader());
  23. if (!baseShader) {
  24. return nullptr;
  25. }
  26. return baseShader->makeWithLocalMatrix(lm);
  27. }
  28. void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
  29. buffer.writeMatrix(this->getLocalMatrix());
  30. buffer.writeFlattenable(fProxyShader.get());
  31. }
  32. #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
  33. SkShaderBase::Context* SkLocalMatrixShader::onMakeContext(
  34. const ContextRec& rec, SkArenaAlloc* alloc) const
  35. {
  36. SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
  37. if (rec.fLocalMatrix) {
  38. lm.writable()->preConcat(*rec.fLocalMatrix);
  39. }
  40. ContextRec newRec(rec);
  41. newRec.fLocalMatrix = lm;
  42. return as_SB(fProxyShader)->makeContext(newRec, alloc);
  43. }
  44. #endif
  45. SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, SkTileMode* mode) const {
  46. SkMatrix imageMatrix;
  47. SkImage* image = fProxyShader->isAImage(&imageMatrix, mode);
  48. if (image && outMatrix) {
  49. // Local matrix must be applied first so it is on the right side of the concat.
  50. *outMatrix = SkMatrix::Concat(imageMatrix, this->getLocalMatrix());
  51. }
  52. return image;
  53. }
  54. SkPicture* SkLocalMatrixShader::isAPicture(SkMatrix* matrix,
  55. SkTileMode tileModes[2],
  56. SkRect* tile) const {
  57. SkMatrix proxyMatrix;
  58. SkPicture* picture = as_SB(fProxyShader)->isAPicture(&proxyMatrix, tileModes, tile);
  59. if (picture && matrix) {
  60. *matrix = SkMatrix::Concat(proxyMatrix, this->getLocalMatrix());
  61. }
  62. return picture;
  63. }
  64. bool SkLocalMatrixShader::onAppendStages(const SkStageRec& rec) const {
  65. SkTCopyOnFirstWrite<SkMatrix> lm(this->getLocalMatrix());
  66. if (rec.fLocalM) {
  67. lm.writable()->preConcat(*rec.fLocalM);
  68. }
  69. SkStageRec newRec = rec;
  70. newRec.fLocalM = lm;
  71. return as_SB(fProxyShader)->appendStages(newRec);
  72. }
  73. sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
  74. if (localMatrix.isIdentity()) {
  75. return sk_ref_sp(const_cast<SkShader*>(this));
  76. }
  77. const SkMatrix* lm = &localMatrix;
  78. sk_sp<SkShader> baseShader;
  79. SkMatrix otherLocalMatrix;
  80. sk_sp<SkShader> proxy(as_SB(this)->makeAsALocalMatrixShader(&otherLocalMatrix));
  81. if (proxy) {
  82. otherLocalMatrix.preConcat(localMatrix);
  83. lm = &otherLocalMatrix;
  84. baseShader = proxy;
  85. } else {
  86. baseShader = sk_ref_sp(const_cast<SkShader*>(this));
  87. }
  88. return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
  89. }