SkLocalMatrixImageFilter.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2015 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/core/SkString.h"
  8. #include "src/core/SkImageFilterPriv.h"
  9. #include "src/core/SkLocalMatrixImageFilter.h"
  10. #include "src/core/SkReadBuffer.h"
  11. #include "src/core/SkSpecialImage.h"
  12. sk_sp<SkImageFilter> SkLocalMatrixImageFilter::Make(const SkMatrix& localM,
  13. sk_sp<SkImageFilter> input) {
  14. if (!input) {
  15. return nullptr;
  16. }
  17. if (localM.isIdentity()) {
  18. return input;
  19. }
  20. if (!input->canHandleComplexCTM() && !localM.isScaleTranslate()) {
  21. // Nothing we can do at this point
  22. return nullptr;
  23. }
  24. return sk_sp<SkImageFilter>(new SkLocalMatrixImageFilter(localM, input));
  25. }
  26. SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM,
  27. sk_sp<SkImageFilter> input)
  28. : INHERITED(&input, 1, nullptr)
  29. , fLocalM(localM) {
  30. }
  31. sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) {
  32. SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
  33. SkMatrix lm;
  34. buffer.readMatrix(&lm);
  35. return SkLocalMatrixImageFilter::Make(lm, common.getInput(0));
  36. }
  37. void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const {
  38. this->INHERITED::flatten(buffer);
  39. buffer.writeMatrix(fLocalM);
  40. }
  41. sk_sp<SkSpecialImage> SkLocalMatrixImageFilter::onFilterImage(SkSpecialImage* source,
  42. const Context& ctx,
  43. SkIPoint* offset) const {
  44. Context localCtx(SkMatrix::Concat(ctx.ctm(), fLocalM), ctx.clipBounds(), ctx.cache(),
  45. ctx.outputProperties());
  46. return this->filterInput(0, source, localCtx, offset);
  47. }
  48. SkIRect SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
  49. MapDirection dir, const SkIRect* inputRect) const {
  50. return this->getInput(0)->filterBounds(src, SkMatrix::Concat(ctm, fLocalM), dir, inputRect);
  51. }