SkOffsetImageFilter.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2012 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/SkCanvas.h"
  8. #include "include/core/SkMatrix.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/effects/SkOffsetImageFilter.h"
  11. #include "src/core/SkImageFilterPriv.h"
  12. #include "src/core/SkPointPriv.h"
  13. #include "src/core/SkReadBuffer.h"
  14. #include "src/core/SkSpecialImage.h"
  15. #include "src/core/SkSpecialSurface.h"
  16. #include "src/core/SkWriteBuffer.h"
  17. static SkIPoint map_offset_vector(const SkMatrix& ctm, const SkVector& offset) {
  18. SkVector vec = ctm.mapVector(offset.fX, offset.fY);
  19. return SkIPoint::Make(SkScalarRoundToInt(vec.fX), SkScalarRoundToInt(vec.fY));
  20. }
  21. sk_sp<SkImageFilter> SkOffsetImageFilter::Make(SkScalar dx, SkScalar dy,
  22. sk_sp<SkImageFilter> input,
  23. const CropRect* cropRect) {
  24. if (!SkScalarIsFinite(dx) || !SkScalarIsFinite(dy)) {
  25. return nullptr;
  26. }
  27. return sk_sp<SkImageFilter>(new SkOffsetImageFilter(dx, dy, std::move(input), cropRect));
  28. }
  29. sk_sp<SkSpecialImage> SkOffsetImageFilter::onFilterImage(SkSpecialImage* source,
  30. const Context& ctx,
  31. SkIPoint* offset) const {
  32. SkIPoint srcOffset = SkIPoint::Make(0, 0);
  33. sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &srcOffset));
  34. if (!input) {
  35. return nullptr;
  36. }
  37. SkIPoint vec = map_offset_vector(ctx.ctm(), fOffset);
  38. if (!this->cropRectIsSet()) {
  39. offset->fX = Sk32_sat_add(srcOffset.fX, vec.fX);
  40. offset->fY = Sk32_sat_add(srcOffset.fY, vec.fY);
  41. return input;
  42. } else {
  43. SkIRect bounds;
  44. const SkIRect srcBounds = SkIRect::MakeXYWH(srcOffset.fX, srcOffset.fY,
  45. input->width(), input->height());
  46. if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
  47. return nullptr;
  48. }
  49. sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size()));
  50. if (!surf) {
  51. return nullptr;
  52. }
  53. SkCanvas* canvas = surf->getCanvas();
  54. SkASSERT(canvas);
  55. // TODO: it seems like this clear shouldn't be necessary (see skbug.com/5075)
  56. canvas->clear(0x0);
  57. SkPaint paint;
  58. paint.setBlendMode(SkBlendMode::kSrc);
  59. canvas->translate(SkIntToScalar(srcOffset.fX - bounds.fLeft),
  60. SkIntToScalar(srcOffset.fY - bounds.fTop));
  61. input->draw(canvas, vec.fX, vec.fY, &paint);
  62. offset->fX = bounds.fLeft;
  63. offset->fY = bounds.fTop;
  64. return surf->makeImageSnapshot();
  65. }
  66. }
  67. SkRect SkOffsetImageFilter::computeFastBounds(const SkRect& src) const {
  68. SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
  69. bounds.offset(fOffset.fX, fOffset.fY);
  70. return bounds;
  71. }
  72. SkIRect SkOffsetImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
  73. MapDirection dir, const SkIRect* inputRect) const {
  74. SkIPoint vec = map_offset_vector(ctm, fOffset);
  75. if (kReverse_MapDirection == dir) {
  76. SkPointPriv::Negate(vec);
  77. }
  78. return src.makeOffset(vec.fX, vec.fY);
  79. }
  80. sk_sp<SkFlattenable> SkOffsetImageFilter::CreateProc(SkReadBuffer& buffer) {
  81. SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
  82. SkPoint offset;
  83. buffer.readPoint(&offset);
  84. return Make(offset.x(), offset.y(), common.getInput(0), &common.cropRect());
  85. }
  86. void SkOffsetImageFilter::flatten(SkWriteBuffer& buffer) const {
  87. this->INHERITED::flatten(buffer);
  88. buffer.writePoint(fOffset);
  89. }
  90. SkOffsetImageFilter::SkOffsetImageFilter(SkScalar dx, SkScalar dy,
  91. sk_sp<SkImageFilter> input,
  92. const CropRect* cropRect)
  93. : INHERITED(&input, 1, cropRect) {
  94. fOffset.set(dx, dy);
  95. }