SkColorFilterImageFilter.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/effects/SkColorFilterImageFilter.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColorFilter.h"
  10. #include "src/core/SkImageFilterPriv.h"
  11. #include "src/core/SkReadBuffer.h"
  12. #include "src/core/SkSpecialImage.h"
  13. #include "src/core/SkSpecialSurface.h"
  14. #include "src/core/SkWriteBuffer.h"
  15. sk_sp<SkImageFilter> SkColorFilterImageFilter::Make(sk_sp<SkColorFilter> cf,
  16. sk_sp<SkImageFilter> input,
  17. const CropRect* cropRect) {
  18. if (!cf) {
  19. return nullptr;
  20. }
  21. SkColorFilter* inputCF;
  22. if (input && input->isColorFilterNode(&inputCF)) {
  23. // This is an optimization, as it collapses the hierarchy by just combining the two
  24. // colorfilters into a single one, which the new imagefilter will wrap.
  25. sk_sp<SkColorFilter> newCF = cf->makeComposed(sk_sp<SkColorFilter>(inputCF));
  26. if (newCF) {
  27. return sk_sp<SkImageFilter>(new SkColorFilterImageFilter(std::move(newCF),
  28. sk_ref_sp(input->getInput(0)),
  29. cropRect));
  30. }
  31. }
  32. return sk_sp<SkImageFilter>(new SkColorFilterImageFilter(std::move(cf),
  33. std::move(input),
  34. cropRect));
  35. }
  36. SkColorFilterImageFilter::SkColorFilterImageFilter(sk_sp<SkColorFilter> cf,
  37. sk_sp<SkImageFilter> input,
  38. const CropRect* cropRect)
  39. : INHERITED(&input, 1, cropRect)
  40. , fColorFilter(std::move(cf)) {
  41. }
  42. sk_sp<SkFlattenable> SkColorFilterImageFilter::CreateProc(SkReadBuffer& buffer) {
  43. SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
  44. sk_sp<SkColorFilter> cf(buffer.readColorFilter());
  45. return Make(std::move(cf), common.getInput(0), &common.cropRect());
  46. }
  47. void SkColorFilterImageFilter::flatten(SkWriteBuffer& buffer) const {
  48. this->INHERITED::flatten(buffer);
  49. buffer.writeFlattenable(fColorFilter.get());
  50. }
  51. sk_sp<SkSpecialImage> SkColorFilterImageFilter::onFilterImage(SkSpecialImage* source,
  52. const Context& ctx,
  53. SkIPoint* offset) const {
  54. SkIPoint inputOffset = SkIPoint::Make(0, 0);
  55. sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
  56. SkIRect inputBounds;
  57. if (fColorFilter->affectsTransparentBlack()) {
  58. // If the color filter affects transparent black, the bounds are the entire clip.
  59. inputBounds = ctx.clipBounds();
  60. } else if (!input) {
  61. return nullptr;
  62. } else {
  63. inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
  64. input->width(), input->height());
  65. }
  66. SkIRect bounds;
  67. if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
  68. return nullptr;
  69. }
  70. sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size()));
  71. if (!surf) {
  72. return nullptr;
  73. }
  74. SkCanvas* canvas = surf->getCanvas();
  75. SkASSERT(canvas);
  76. SkPaint paint;
  77. paint.setBlendMode(SkBlendMode::kSrc);
  78. paint.setColorFilter(fColorFilter);
  79. // TODO: it may not be necessary to clear or drawPaint inside the input bounds
  80. // (see skbug.com/5075)
  81. if (fColorFilter->affectsTransparentBlack()) {
  82. // The subsequent input->draw() call may not fill the entire canvas. For filters which
  83. // affect transparent black, ensure that the filter is applied everywhere.
  84. paint.setColor(SK_ColorTRANSPARENT);
  85. canvas->drawPaint(paint);
  86. paint.setColor(SK_ColorBLACK);
  87. } else {
  88. canvas->clear(0x0);
  89. }
  90. if (input) {
  91. input->draw(canvas,
  92. SkIntToScalar(inputOffset.fX - bounds.fLeft),
  93. SkIntToScalar(inputOffset.fY - bounds.fTop),
  94. &paint);
  95. }
  96. offset->fX = bounds.fLeft;
  97. offset->fY = bounds.fTop;
  98. return surf->makeImageSnapshot();
  99. }
  100. bool SkColorFilterImageFilter::onIsColorFilterNode(SkColorFilter** filter) const {
  101. SkASSERT(1 == this->countInputs());
  102. if (!this->cropRectIsSet()) {
  103. if (filter) {
  104. *filter = SkRef(fColorFilter.get());
  105. }
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool SkColorFilterImageFilter::affectsTransparentBlack() const {
  111. return fColorFilter->affectsTransparentBlack();
  112. }