1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
* Copyright 2012 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkMagnifierImageFilter.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
#include "SkReadBuffer.h"
#include "SkSpecialImage.h"
#include "SkWriteBuffer.h"
#include "SkValidationUtils.h"
////////////////////////////////////////////////////////////////////////////////
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrDrawContext.h"
#include "GrInvariantOutput.h"
#include "effects/GrSingleTextureEffect.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h"
#include "glsl/GrGLSLUniformHandler.h"
class GrMagnifierEffect : public GrSingleTextureEffect {
public:
static GrFragmentProcessor* Create(GrTexture* texture,
const SkRect& bounds,
float xOffset,
float yOffset,
float xInvZoom,
float yInvZoom,
float xInvInset,
float yInvInset) {
return new GrMagnifierEffect(texture, bounds,
xOffset, yOffset,
xInvZoom, yInvZoom,
xInvInset, yInvInset);
}
~GrMagnifierEffect() override {};
const char* name() const override { return "Magnifier"; }
const SkRect& bounds() const { return fBounds; } // Bounds of source image.
// Offset to apply to zoomed pixels, (srcRect position / texture size).
float xOffset() const { return fXOffset; }
float yOffset() const { return fYOffset; }
// Scale to apply to zoomed pixels (srcRect size / bounds size).
float xInvZoom() const { return fXInvZoom; }
float yInvZoom() const { return fYInvZoom; }
// 1/radius over which to transition from unzoomed to zoomed pixels (bounds size / inset).
float xInvInset() const { return fXInvInset; }
float yInvInset() const { return fYInvInset; }
private:
GrMagnifierEffect(GrTexture* texture,
const SkRect& bounds,
float xOffset,
float yOffset,
float xInvZoom,
float yInvZoom,
float xInvInset,
float yInvInset)
: INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture))
, fBounds(bounds)
, fXOffset(xOffset)
, fYOffset(yOffset)
, fXInvZoom(xInvZoom)
, fYInvZoom(yInvZoom)
, fXInvInset(xInvInset)
, fYInvInset(yInvInset) {
this->initClassID<GrMagnifierEffect>();
}
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
bool onIsEqual(const GrFragmentProcessor&) const override;
void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
SkRect fBounds;
float fXOffset;
float fYOffset;
float fXInvZoom;
float fYInvZoom;
float fXInvInset;
float fYInvInset;
typedef GrSingleTextureEffect INHERITED;
};
// For brevity
typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
class GrGLMagnifierEffect : public GrGLSLFragmentProcessor {
public:
void emitCode(EmitArgs&) override;
protected:
void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
private:
UniformHandle fOffsetVar;
UniformHandle fInvZoomVar;
UniformHandle fInvInsetVar;
UniformHandle fBoundsVar;
typedef GrGLSLFragmentProcessor INHERITED;
};
void GrGLMagnifierEffect::emitCode(EmitArgs& args) {
GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
fOffsetVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec2f_GrSLType, kDefault_GrSLPrecision,
"Offset");
fInvZoomVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec2f_GrSLType, kDefault_GrSLPrecision,
"InvZoom");
fInvInsetVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec2f_GrSLType, kDefault_GrSLPrecision,
"InvInset");
fBoundsVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
kVec4f_GrSLType, kDefault_GrSLPrecision,
"Bounds");
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
fragBuilder->codeAppendf("\t\tvec2 zoom_coord = %s + %s * %s;\n",
uniformHandler->getUniformCStr(fOffsetVar),
coords2D.c_str(),
uniformHandler->getUniformCStr(fInvZoomVar));
const char* bounds = uniformHandler->getUniformCStr(fBoundsVar);
fragBuilder->codeAppendf("\t\tvec2 delta = (coord - %s.xy) * %s.zw;\n", bounds, bounds);
fragBuilder->codeAppendf("\t\tdelta = min(delta, vec2(1.0, 1.0) - delta);\n");
fragBuilder->codeAppendf("\t\tdelta = delta * %s;\n",
uniformHandler->getUniformCStr(fInvInsetVar));
fragBuilder->codeAppend("\t\tfloat weight = 0.0;\n");
fragBuilder->codeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n");
fragBuilder->codeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n");
fragBuilder->codeAppend("\t\t\tfloat dist = length(delta);\n");
fragBuilder->codeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n");
fragBuilder->codeAppend("\t\t\tweight = min(dist * dist, 1.0);\n");
fragBuilder->codeAppend("\t\t} else {\n");
fragBuilder->codeAppend("\t\t\tvec2 delta_squared = delta * delta;\n");
fragBuilder->codeAppend("\t\t\tweight = min(min(delta_squared.x, delta_squared.y), 1.0);\n");
fragBuilder->codeAppend("\t\t}\n");
fragBuilder->codeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n");
fragBuilder->codeAppend("\t\tvec4 output_color = ");
fragBuilder->appendTextureLookup(args.fTexSamplers[0], "mix_coord");
fragBuilder->codeAppend(";\n");
fragBuilder->codeAppendf("\t\t%s = output_color;", args.fOutputColor);
SkString modulate;
GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
fragBuilder->codeAppend(modulate.c_str());
}
void GrGLMagnifierEffect::onSetData(const GrGLSLProgramDataManager& pdman,
const GrProcessor& effect) {
const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>();
pdman.set2f(fOffsetVar, zoom.xOffset(), zoom.yOffset());
pdman.set2f(fInvZoomVar, zoom.xInvZoom(), zoom.yInvZoom());
pdman.set2f(fInvInsetVar, zoom.xInvInset(), zoom.yInvInset());
pdman.set4f(fBoundsVar, zoom.bounds().x(), zoom.bounds().y(),
zoom.bounds().width(), zoom.bounds().height());
}
/////////////////////////////////////////////////////////////////////
void GrMagnifierEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
GrProcessorKeyBuilder* b) const {
GrGLMagnifierEffect::GenKey(*this, caps, b);
}
GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
return new GrGLMagnifierEffect;
}
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
const GrFragmentProcessor* GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
GrTexture* texture = d->fTextures[0];
const int kMaxWidth = 200;
const int kMaxHeight = 200;
const int kMaxInset = 20;
uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
uint32_t x = d->fRandom->nextULessThan(kMaxWidth - width);
uint32_t y = d->fRandom->nextULessThan(kMaxHeight - height);
uint32_t inset = d->fRandom->nextULessThan(kMaxInset);
GrFragmentProcessor* effect = GrMagnifierEffect::Create(
texture,
SkRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)),
(float) width / texture->width(),
(float) height / texture->height(),
texture->width() / (float) x,
texture->height() / (float) y,
(float) inset / texture->width(),
(float) inset / texture->height());
SkASSERT(effect);
return effect;
}
///////////////////////////////////////////////////////////////////////////////
bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
const GrMagnifierEffect& s = sBase.cast<GrMagnifierEffect>();
return (this->fBounds == s.fBounds &&
this->fXOffset == s.fXOffset &&
this->fYOffset == s.fYOffset &&
this->fXInvZoom == s.fXInvZoom &&
this->fYInvZoom == s.fYInvZoom &&
this->fXInvInset == s.fXInvInset &&
this->fYInvInset == s.fYInvInset);
}
void GrMagnifierEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
this->updateInvariantOutputForModulation(inout);
}
#endif
////////////////////////////////////////////////////////////////////////////////
sk_sp<SkImageFilter> SkMagnifierImageFilter::Make(const SkRect& srcRect, SkScalar inset,
sk_sp<SkImageFilter> input,
const CropRect* cropRect) {
if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) {
return nullptr;
}
// Negative numbers in src rect are not supported
if (srcRect.fLeft < 0 || srcRect.fTop < 0) {
return nullptr;
}
return sk_sp<SkImageFilter>(new SkMagnifierImageFilter(srcRect, inset,
std::move(input),
cropRect));
}
SkMagnifierImageFilter::SkMagnifierImageFilter(const SkRect& srcRect,
SkScalar inset,
sk_sp<SkImageFilter> input,
const CropRect* cropRect)
: INHERITED(&input, 1, cropRect)
, fSrcRect(srcRect)
, fInset(inset) {
SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0);
}
sk_sp<SkFlattenable> SkMagnifierImageFilter::CreateProc(SkReadBuffer& buffer) {
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
SkRect src;
buffer.readRect(&src);
return Make(src, buffer.readScalar(), common.getInput(0), &common.cropRect());
}
void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.writeRect(fSrcRect);
buffer.writeScalar(fInset);
}
sk_sp<SkSpecialImage> SkMagnifierImageFilter::onFilterImage(SkSpecialImage* source,
const Context& ctx,
SkIPoint* offset) const {
SkIPoint inputOffset = SkIPoint::Make(0, 0);
sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
if (!input) {
return nullptr;
}
const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
input->width(), input->height());
SkIRect bounds;
if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
return nullptr;
}
SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
SkScalar invXZoom = fSrcRect.width() / bounds.width();
SkScalar invYZoom = fSrcRect.height() / bounds.height();
#if SK_SUPPORT_GPU
if (source->isTextureBacked()) {
GrContext* context = source->getContext();
sk_sp<GrTexture> inputTexture(input->asTextureRef(context));
SkASSERT(inputTexture);
offset->fX = bounds.left();
offset->fY = bounds.top();
bounds.offset(-inputOffset);
SkScalar yOffset = inputTexture->origin() == kTopLeft_GrSurfaceOrigin
? fSrcRect.y()
: inputTexture->height() -
fSrcRect.height() * inputTexture->height() / bounds.height() - fSrcRect.y();
int boundsY = inputTexture->origin() == kTopLeft_GrSurfaceOrigin
? bounds.y()
: inputTexture->height() - bounds.height();
SkRect effectBounds = SkRect::MakeXYWH(
SkIntToScalar(bounds.x()) / inputTexture->width(),
SkIntToScalar(boundsY) / inputTexture->height(),
SkIntToScalar(inputTexture->width()) / bounds.width(),
SkIntToScalar(inputTexture->height()) / bounds.height());
// SRGBTODO: Handle sRGB here
sk_sp<GrFragmentProcessor> fp(GrMagnifierEffect::Create(
inputTexture.get(),
effectBounds,
fSrcRect.x() / inputTexture->width(),
yOffset / inputTexture->height(),
invXZoom,
invYZoom,
bounds.width() * invInset,
bounds.height() * invInset));
if (!fp) {
return nullptr;
}
return DrawWithFP(context, std::move(fp), bounds, source->internal_getProxy());
}
#endif
SkBitmap inputBM;
if (!input->getROPixels(&inputBM)) {
return nullptr;
}
if ((inputBM.colorType() != kN32_SkColorType) ||
(fSrcRect.width() >= inputBM.width()) || (fSrcRect.height() >= inputBM.height())) {
return nullptr;
}
SkAutoLockPixels alp(inputBM);
SkASSERT(inputBM.getPixels());
if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
return nullptr;
}
const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
SkBitmap dst;
if (!dst.tryAllocPixels(info)) {
return nullptr;
}
SkAutoLockPixels dstLock(dst);
SkColor* dptr = dst.getAddr32(0, 0);
int dstWidth = dst.width(), dstHeight = dst.height();
for (int y = 0; y < dstHeight; ++y) {
for (int x = 0; x < dstWidth; ++x) {
SkScalar x_dist = SkMin32(x, dstWidth - x - 1) * invInset;
SkScalar y_dist = SkMin32(y, dstHeight - y - 1) * invInset;
SkScalar weight = 0;
static const SkScalar kScalar2 = SkScalar(2);
// To create a smooth curve at the corners, we need to work on
// a square twice the size of the inset.
if (x_dist < kScalar2 && y_dist < kScalar2) {
x_dist = kScalar2 - x_dist;
y_dist = kScalar2 - y_dist;
SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
SkScalarSquare(y_dist));
dist = SkMaxScalar(kScalar2 - dist, 0);
weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1);
} else {
SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
SkScalarSquare(y_dist));
weight = SkMinScalar(sqDist, SK_Scalar1);
}
SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * invXZoom)) +
(SK_Scalar1 - weight) * x;
SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * invYZoom)) +
(SK_Scalar1 - weight) * y;
int x_val = SkTPin(bounds.x() + SkScalarFloorToInt(x_interp), 0, inputBM.width() - 1);
int y_val = SkTPin(bounds.y() + SkScalarFloorToInt(y_interp), 0, inputBM.height() - 1);
*dptr = *inputBM.getAddr32(x_val, y_val);
dptr++;
}
}
offset->fX = bounds.left();
offset->fY = bounds.top();
return SkSpecialImage::MakeFromRaster(source->internal_getProxy(),
SkIRect::MakeWH(bounds.width(), bounds.height()),
dst);
}
#ifndef SK_IGNORE_TO_STRING
void SkMagnifierImageFilter::toString(SkString* str) const {
str->appendf("SkMagnifierImageFilter: (");
str->appendf("src: (%f,%f,%f,%f) ",
fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBottom);
str->appendf("inset: %f", fInset);
str->append(")");
}
#endif
|