SkMagnifierImageFilter.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/SkMagnifierImageFilter.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/private/SkColorData.h"
  10. #include "src/core/SkImageFilterPriv.h"
  11. #include "src/core/SkReadBuffer.h"
  12. #include "src/core/SkSpecialImage.h"
  13. #include "src/core/SkValidationUtils.h"
  14. #include "src/core/SkWriteBuffer.h"
  15. ////////////////////////////////////////////////////////////////////////////////
  16. #if SK_SUPPORT_GPU
  17. #include "include/gpu/GrContext.h"
  18. #include "include/gpu/GrTexture.h"
  19. #include "src/gpu/GrColorSpaceXform.h"
  20. #include "src/gpu/GrCoordTransform.h"
  21. #include "src/gpu/effects/generated/GrMagnifierEffect.h"
  22. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  23. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  24. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  25. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  26. #endif
  27. sk_sp<SkImageFilter> SkMagnifierImageFilter::Make(const SkRect& srcRect, SkScalar inset,
  28. sk_sp<SkImageFilter> input,
  29. const CropRect* cropRect) {
  30. if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) {
  31. return nullptr;
  32. }
  33. if (inset < 0) {
  34. return nullptr;
  35. }
  36. // Negative numbers in src rect are not supported
  37. if (srcRect.fLeft < 0 || srcRect.fTop < 0) {
  38. return nullptr;
  39. }
  40. return sk_sp<SkImageFilter>(new SkMagnifierImageFilter(srcRect, inset,
  41. std::move(input),
  42. cropRect));
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. SkMagnifierImageFilter::SkMagnifierImageFilter(const SkRect& srcRect,
  46. SkScalar inset,
  47. sk_sp<SkImageFilter> input,
  48. const CropRect* cropRect)
  49. : INHERITED(&input, 1, cropRect)
  50. , fSrcRect(srcRect)
  51. , fInset(inset) {
  52. SkASSERT(srcRect.left() >= 0 && srcRect.top() >= 0 && inset >= 0);
  53. }
  54. sk_sp<SkFlattenable> SkMagnifierImageFilter::CreateProc(SkReadBuffer& buffer) {
  55. SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
  56. SkRect src;
  57. buffer.readRect(&src);
  58. return Make(src, buffer.readScalar(), common.getInput(0), &common.cropRect());
  59. }
  60. void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const {
  61. this->INHERITED::flatten(buffer);
  62. buffer.writeRect(fSrcRect);
  63. buffer.writeScalar(fInset);
  64. }
  65. sk_sp<SkSpecialImage> SkMagnifierImageFilter::onFilterImage(SkSpecialImage* source,
  66. const Context& ctx,
  67. SkIPoint* offset) const {
  68. SkIPoint inputOffset = SkIPoint::Make(0, 0);
  69. sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
  70. if (!input) {
  71. return nullptr;
  72. }
  73. const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
  74. input->width(), input->height());
  75. SkIRect bounds;
  76. if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
  77. return nullptr;
  78. }
  79. SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
  80. SkScalar invXZoom = fSrcRect.width() / bounds.width();
  81. SkScalar invYZoom = fSrcRect.height() / bounds.height();
  82. #if SK_SUPPORT_GPU
  83. if (source->isTextureBacked()) {
  84. auto context = source->getContext();
  85. sk_sp<GrTextureProxy> inputProxy(input->asTextureProxyRef(context));
  86. SkASSERT(inputProxy);
  87. const auto isProtected = inputProxy->isProtected();
  88. offset->fX = bounds.left();
  89. offset->fY = bounds.top();
  90. bounds.offset(-inputOffset);
  91. auto fp = GrMagnifierEffect::Make(std::move(inputProxy),
  92. bounds,
  93. fSrcRect,
  94. invXZoom,
  95. invYZoom,
  96. bounds.width() * invInset,
  97. bounds.height() * invInset);
  98. fp = GrColorSpaceXformEffect::Make(std::move(fp), input->getColorSpace(),
  99. input->alphaType(), ctx.outputProperties().colorSpace());
  100. if (!fp) {
  101. return nullptr;
  102. }
  103. return DrawWithFP(context, std::move(fp), bounds, ctx.outputProperties(),
  104. isProtected ? GrProtected::kYes : GrProtected::kNo);
  105. }
  106. #endif
  107. SkBitmap inputBM;
  108. if (!input->getROPixels(&inputBM)) {
  109. return nullptr;
  110. }
  111. if ((inputBM.colorType() != kN32_SkColorType) ||
  112. (fSrcRect.width() >= inputBM.width()) || (fSrcRect.height() >= inputBM.height())) {
  113. return nullptr;
  114. }
  115. SkASSERT(inputBM.getPixels());
  116. if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
  117. return nullptr;
  118. }
  119. const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
  120. SkBitmap dst;
  121. if (!dst.tryAllocPixels(info)) {
  122. return nullptr;
  123. }
  124. SkColor* dptr = dst.getAddr32(0, 0);
  125. int dstWidth = dst.width(), dstHeight = dst.height();
  126. for (int y = 0; y < dstHeight; ++y) {
  127. for (int x = 0; x < dstWidth; ++x) {
  128. SkScalar x_dist = SkMin32(x, dstWidth - x - 1) * invInset;
  129. SkScalar y_dist = SkMin32(y, dstHeight - y - 1) * invInset;
  130. SkScalar weight = 0;
  131. static const SkScalar kScalar2 = SkScalar(2);
  132. // To create a smooth curve at the corners, we need to work on
  133. // a square twice the size of the inset.
  134. if (x_dist < kScalar2 && y_dist < kScalar2) {
  135. x_dist = kScalar2 - x_dist;
  136. y_dist = kScalar2 - y_dist;
  137. SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
  138. SkScalarSquare(y_dist));
  139. dist = SkMaxScalar(kScalar2 - dist, 0);
  140. weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1);
  141. } else {
  142. SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
  143. SkScalarSquare(y_dist));
  144. weight = SkMinScalar(sqDist, SK_Scalar1);
  145. }
  146. SkScalar x_interp = weight * (fSrcRect.x() + x * invXZoom) + (1 - weight) * x;
  147. SkScalar y_interp = weight * (fSrcRect.y() + y * invYZoom) + (1 - weight) * y;
  148. int x_val = SkTPin(bounds.x() + SkScalarFloorToInt(x_interp), 0, inputBM.width() - 1);
  149. int y_val = SkTPin(bounds.y() + SkScalarFloorToInt(y_interp), 0, inputBM.height() - 1);
  150. *dptr = *inputBM.getAddr32(x_val, y_val);
  151. dptr++;
  152. }
  153. }
  154. offset->fX = bounds.left();
  155. offset->fY = bounds.top();
  156. return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
  157. dst);
  158. }