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