Sk2DPathEffect.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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/core/SkPath.h"
  8. #include "include/core/SkRegion.h"
  9. #include "include/core/SkStrokeRec.h"
  10. #include "include/effects/Sk2DPathEffect.h"
  11. #include "src/core/SkReadBuffer.h"
  12. #include "src/core/SkWriteBuffer.h"
  13. Sk2DPathEffect::Sk2DPathEffect(const SkMatrix& mat) : fMatrix(mat) {
  14. // Calling invert will set the type mask on both matrices, making them thread safe.
  15. fMatrixIsInvertible = fMatrix.invert(&fInverse);
  16. }
  17. bool Sk2DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
  18. SkStrokeRec*, const SkRect*) const {
  19. if (!fMatrixIsInvertible) {
  20. return false;
  21. }
  22. SkPath tmp;
  23. SkIRect ir;
  24. src.transform(fInverse, &tmp);
  25. tmp.getBounds().round(&ir);
  26. if (!ir.isEmpty()) {
  27. this->begin(ir, dst);
  28. SkRegion rgn;
  29. rgn.setPath(tmp, SkRegion(ir));
  30. SkRegion::Iterator iter(rgn);
  31. for (; !iter.done(); iter.next()) {
  32. const SkIRect& rect = iter.rect();
  33. for (int y = rect.fTop; y < rect.fBottom; ++y) {
  34. this->nextSpan(rect.fLeft, y, rect.width(), dst);
  35. }
  36. }
  37. this->end(dst);
  38. }
  39. return true;
  40. }
  41. void Sk2DPathEffect::nextSpan(int x, int y, int count, SkPath* path) const {
  42. if (!fMatrixIsInvertible) {
  43. return;
  44. }
  45. const SkMatrix& mat = this->getMatrix();
  46. SkPoint src, dst;
  47. src.set(SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf);
  48. do {
  49. mat.mapPoints(&dst, &src, 1);
  50. this->next(dst, x++, y, path);
  51. src.fX += SK_Scalar1;
  52. } while (--count > 0);
  53. }
  54. void Sk2DPathEffect::begin(const SkIRect& uvBounds, SkPath* dst) const {}
  55. void Sk2DPathEffect::next(const SkPoint& loc, int u, int v, SkPath* dst) const {}
  56. void Sk2DPathEffect::end(SkPath* dst) const {}
  57. ///////////////////////////////////////////////////////////////////////////////
  58. void Sk2DPathEffect::flatten(SkWriteBuffer& buffer) const {
  59. this->INHERITED::flatten(buffer);
  60. buffer.writeMatrix(fMatrix);
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////
  63. bool SkLine2DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
  64. SkStrokeRec* rec, const SkRect* cullRect) const {
  65. if (this->INHERITED::onFilterPath(dst, src, rec, cullRect)) {
  66. rec->setStrokeStyle(fWidth);
  67. return true;
  68. }
  69. return false;
  70. }
  71. void SkLine2DPathEffect::nextSpan(int u, int v, int ucount, SkPath* dst) const {
  72. if (ucount > 1) {
  73. SkPoint src[2], dstP[2];
  74. src[0].set(SkIntToScalar(u) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
  75. src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, SkIntToScalar(v) + SK_ScalarHalf);
  76. this->getMatrix().mapPoints(dstP, src, 2);
  77. dst->moveTo(dstP[0]);
  78. dst->lineTo(dstP[1]);
  79. }
  80. }
  81. sk_sp<SkFlattenable> SkLine2DPathEffect::CreateProc(SkReadBuffer& buffer) {
  82. SkMatrix matrix;
  83. buffer.readMatrix(&matrix);
  84. SkScalar width = buffer.readScalar();
  85. return SkLine2DPathEffect::Make(width, matrix);
  86. }
  87. void SkLine2DPathEffect::flatten(SkWriteBuffer &buffer) const {
  88. buffer.writeMatrix(this->getMatrix());
  89. buffer.writeScalar(fWidth);
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////
  92. SkPath2DPathEffect::SkPath2DPathEffect(const SkMatrix& m, const SkPath& p)
  93. : INHERITED(m), fPath(p) {
  94. }
  95. sk_sp<SkFlattenable> SkPath2DPathEffect::CreateProc(SkReadBuffer& buffer) {
  96. SkMatrix matrix;
  97. buffer.readMatrix(&matrix);
  98. SkPath path;
  99. buffer.readPath(&path);
  100. return SkPath2DPathEffect::Make(matrix, path);
  101. }
  102. void SkPath2DPathEffect::flatten(SkWriteBuffer& buffer) const {
  103. buffer.writeMatrix(this->getMatrix());
  104. buffer.writePath(fPath);
  105. }
  106. void SkPath2DPathEffect::next(const SkPoint& loc, int u, int v,
  107. SkPath* dst) const {
  108. dst->addPath(fPath, loc.fX, loc.fY);
  109. }