1 /*
2 * Copyright 2013 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
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkUnPreMultiply.h"
10 #include "include/effects/SkImageFilters.h"
11 #include "include/private/SkColorData.h"
12 #include "src/core/SkImageFilter_Base.h"
13 #include "src/core/SkReadBuffer.h"
14 #include "src/core/SkSpecialImage.h"
15 #include "src/core/SkWriteBuffer.h"
16
17 #if SK_SUPPORT_GPU
18 #include "include/gpu/GrRecordingContext.h"
19 #include "src/gpu/GrCaps.h"
20 #include "src/gpu/GrColorSpaceXform.h"
21 #include "src/gpu/GrFragmentProcessor.h"
22 #include "src/gpu/GrRecordingContextPriv.h"
23 #include "src/gpu/GrTexture.h"
24 #include "src/gpu/GrTextureProxy.h"
25 #include "src/gpu/SkGr.h"
26 #include "src/gpu/SurfaceFillContext.h"
27 #include "src/gpu/effects/GrTextureEffect.h"
28 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
29 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
30 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
31 #endif
32
33 namespace {
34
35 class SkDisplacementMapImageFilter final : public SkImageFilter_Base {
36 public:
SkDisplacementMapImageFilter(SkColorChannel xChannelSelector,SkColorChannel yChannelSelector,SkScalar scale,sk_sp<SkImageFilter> inputs[2],const SkRect * cropRect)37 SkDisplacementMapImageFilter(SkColorChannel xChannelSelector, SkColorChannel yChannelSelector,
38 SkScalar scale, sk_sp<SkImageFilter> inputs[2],
39 const SkRect* cropRect)
40 : INHERITED(inputs, 2, cropRect)
41 , fXChannelSelector(xChannelSelector)
42 , fYChannelSelector(yChannelSelector)
43 , fScale(scale) {}
44
45 SkRect computeFastBounds(const SkRect& src) const override;
46
47 SkIRect onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
48 MapDirection, const SkIRect* inputRect) const override;
49 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix& ctm,
50 MapDirection, const SkIRect* inputRect) const override;
51
52 protected:
53 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
54
55 void flatten(SkWriteBuffer&) const override;
56
57 private:
58 friend void ::SkRegisterDisplacementMapImageFilterFlattenable();
59 SK_FLATTENABLE_HOOKS(SkDisplacementMapImageFilter)
60
61 SkColorChannel fXChannelSelector;
62 SkColorChannel fYChannelSelector;
63 SkScalar fScale;
64
getDisplacementInput() const65 const SkImageFilter* getDisplacementInput() const { return getInput(0); }
getColorInput() const66 const SkImageFilter* getColorInput() const { return getInput(1); }
67
68 using INHERITED = SkImageFilter_Base;
69 };
70
71 // Shift values to extract channels from an SkColor (SkColorGetR, SkColorGetG, etc)
72 const uint8_t gChannelTypeToShift[] = {
73 16, // R
74 8, // G
75 0, // B
76 24, // A
77 };
78 struct Extractor {
Extractor__anon9a1073f90111::Extractor79 Extractor(SkColorChannel typeX,
80 SkColorChannel typeY)
81 : fShiftX(gChannelTypeToShift[static_cast<int>(typeX)])
82 , fShiftY(gChannelTypeToShift[static_cast<int>(typeY)])
83 {}
84
85 unsigned fShiftX, fShiftY;
86
getX__anon9a1073f90111::Extractor87 unsigned getX(SkColor c) const { return (c >> fShiftX) & 0xFF; }
getY__anon9a1073f90111::Extractor88 unsigned getY(SkColor c) const { return (c >> fShiftY) & 0xFF; }
89 };
90
channel_selector_type_is_valid(SkColorChannel cst)91 static bool channel_selector_type_is_valid(SkColorChannel cst) {
92 switch (cst) {
93 case SkColorChannel::kR:
94 case SkColorChannel::kG:
95 case SkColorChannel::kB:
96 case SkColorChannel::kA:
97 return true;
98 default:
99 break;
100 }
101 return false;
102 }
103
104 } // anonymous namespace
105
106 ///////////////////////////////////////////////////////////////////////////////
107
DisplacementMap(SkColorChannel xChannelSelector,SkColorChannel yChannelSelector,SkScalar scale,sk_sp<SkImageFilter> displacement,sk_sp<SkImageFilter> color,const CropRect & cropRect)108 sk_sp<SkImageFilter> SkImageFilters::DisplacementMap(
109 SkColorChannel xChannelSelector, SkColorChannel yChannelSelector, SkScalar scale,
110 sk_sp<SkImageFilter> displacement, sk_sp<SkImageFilter> color, const CropRect& cropRect) {
111 if (!channel_selector_type_is_valid(xChannelSelector) ||
112 !channel_selector_type_is_valid(yChannelSelector)) {
113 return nullptr;
114 }
115
116 sk_sp<SkImageFilter> inputs[2] = { std::move(displacement), std::move(color) };
117 return sk_sp<SkImageFilter>(new SkDisplacementMapImageFilter(xChannelSelector, yChannelSelector,
118 scale, inputs, cropRect));
119 }
120
SkRegisterDisplacementMapImageFilterFlattenable()121 void SkRegisterDisplacementMapImageFilterFlattenable() {
122 SK_REGISTER_FLATTENABLE(SkDisplacementMapImageFilter);
123 // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name
124 SkFlattenable::Register("SkDisplacementMapEffect", SkDisplacementMapImageFilter::CreateProc);
125 SkFlattenable::Register("SkDisplacementMapEffectImpl",
126 SkDisplacementMapImageFilter::CreateProc);
127 }
128
CreateProc(SkReadBuffer & buffer)129 sk_sp<SkFlattenable> SkDisplacementMapImageFilter::CreateProc(SkReadBuffer& buffer) {
130 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2);
131
132 SkColorChannel xsel = buffer.read32LE(SkColorChannel::kLastEnum);
133 SkColorChannel ysel = buffer.read32LE(SkColorChannel::kLastEnum);
134 SkScalar scale = buffer.readScalar();
135
136 return SkImageFilters::DisplacementMap(xsel, ysel, scale, common.getInput(0),
137 common.getInput(1), common.cropRect());
138 }
139
flatten(SkWriteBuffer & buffer) const140 void SkDisplacementMapImageFilter::flatten(SkWriteBuffer& buffer) const {
141 this->INHERITED::flatten(buffer);
142 buffer.writeInt((int) fXChannelSelector);
143 buffer.writeInt((int) fYChannelSelector);
144 buffer.writeScalar(fScale);
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////
148
149 #if SK_SUPPORT_GPU
150
151 namespace {
152
153 class GrDisplacementMapEffect : public GrFragmentProcessor {
154 public:
155 static std::unique_ptr<GrFragmentProcessor> Make(SkColorChannel xChannelSelector,
156 SkColorChannel yChannelSelector,
157 SkVector scale,
158 GrSurfaceProxyView displacement,
159 const SkIRect& displSubset,
160 const SkMatrix& offsetMatrix,
161 GrSurfaceProxyView color,
162 const SkIRect& colorSubset,
163 const GrCaps&);
164
165 ~GrDisplacementMapEffect() override;
166
name() const167 const char* name() const override { return "DisplacementMap"; }
168
169 SkString getShaderDfxInfo() const override;
170
171 std::unique_ptr<GrFragmentProcessor> clone() const override;
172
173 private:
174 class Impl;
175
176 explicit GrDisplacementMapEffect(const GrDisplacementMapEffect&);
177
178 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
179
180 void onAddToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
181
182 bool onIsEqual(const GrFragmentProcessor&) const override;
183
184 GrDisplacementMapEffect(SkColorChannel xChannelSelector,
185 SkColorChannel yChannelSelector,
186 const SkVector& scale,
187 std::unique_ptr<GrFragmentProcessor> displacement,
188 std::unique_ptr<GrFragmentProcessor> color);
189
190 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
191
192 SkColorChannel fXChannelSelector;
193 SkColorChannel fYChannelSelector;
194 SkVector fScale;
195
196 using INHERITED = GrFragmentProcessor;
197 };
198
199 } // anonymous namespace
200 #endif
201
compute_displacement(Extractor ex,const SkVector & scale,SkBitmap * dst,const SkBitmap & displ,const SkIPoint & offset,const SkBitmap & src,const SkIRect & bounds)202 static void compute_displacement(Extractor ex, const SkVector& scale, SkBitmap* dst,
203 const SkBitmap& displ, const SkIPoint& offset,
204 const SkBitmap& src,
205 const SkIRect& bounds) {
206 static const SkScalar Inv8bit = SkScalarInvert(255);
207 const int srcW = src.width();
208 const int srcH = src.height();
209 const SkVector scaleForColor = SkVector::Make(scale.fX * Inv8bit, scale.fY * Inv8bit);
210 const SkVector scaleAdj = SkVector::Make(SK_ScalarHalf - scale.fX * SK_ScalarHalf,
211 SK_ScalarHalf - scale.fY * SK_ScalarHalf);
212 SkPMColor* dstPtr = dst->getAddr32(0, 0);
213 for (int y = bounds.top(); y < bounds.bottom(); ++y) {
214 const SkPMColor* displPtr = displ.getAddr32(bounds.left() + offset.fX, y + offset.fY);
215 for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) {
216 SkColor c = SkUnPreMultiply::PMColorToColor(*displPtr);
217
218 SkScalar displX = scaleForColor.fX * ex.getX(c) + scaleAdj.fX;
219 SkScalar displY = scaleForColor.fY * ex.getY(c) + scaleAdj.fY;
220 // Truncate the displacement values
221 const int32_t srcX = Sk32_sat_add(x, SkScalarTruncToInt(displX));
222 const int32_t srcY = Sk32_sat_add(y, SkScalarTruncToInt(displY));
223 *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ?
224 0 : *(src.getAddr32(srcX, srcY));
225 }
226 }
227 }
228
onFilterImage(const Context & ctx,SkIPoint * offset) const229 sk_sp<SkSpecialImage> SkDisplacementMapImageFilter::onFilterImage(const Context& ctx,
230 SkIPoint* offset) const {
231 SkIPoint colorOffset = SkIPoint::Make(0, 0);
232 sk_sp<SkSpecialImage> color(this->filterInput(1, ctx, &colorOffset));
233 if (!color) {
234 return nullptr;
235 }
236
237 SkIPoint displOffset = SkIPoint::Make(0, 0);
238 // Creation of the displacement map should happen in a non-colorspace aware context. This
239 // texture is a purely mathematical construct, so we want to just operate on the stored
240 // values. Consider:
241 // User supplies an sRGB displacement map. If we're rendering to a wider gamut, then we could
242 // end up filtering the displacement map into that gamut, which has the effect of reducing
243 // the amount of displacement that it represents (as encoded values move away from the
244 // primaries).
245 // With a more complex DAG attached to this input, it's not clear that working in ANY specific
246 // color space makes sense, so we ignore color spaces (and gamma) entirely. This may not be
247 // ideal, but it's at least consistent and predictable.
248 Context displContext(ctx.mapping(), ctx.desiredOutput(), ctx.cache(),
249 kN32_SkColorType, nullptr, ctx.source());
250 sk_sp<SkSpecialImage> displ(this->filterInput(0, displContext, &displOffset));
251 if (!displ) {
252 return nullptr;
253 }
254
255 const SkIRect srcBounds = SkIRect::MakeXYWH(colorOffset.x(), colorOffset.y(),
256 color->width(), color->height());
257
258 // Both paths do bounds checking on color pixel access, we don't need to
259 // pad the color bitmap to bounds here.
260 SkIRect bounds;
261 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
262 return nullptr;
263 }
264
265 SkIRect displBounds;
266 displ = this->applyCropRectAndPad(ctx, displ.get(), &displOffset, &displBounds);
267 if (!displ) {
268 return nullptr;
269 }
270
271 if (!bounds.intersect(displBounds)) {
272 return nullptr;
273 }
274
275 const SkIRect colorBounds = bounds.makeOffset(-colorOffset);
276 // If the offset overflowed (saturated) then we have to abort, as we need their
277 // dimensions to be equal. See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=7209
278 if (colorBounds.size() != bounds.size()) {
279 return nullptr;
280 }
281
282 SkVector scale = SkVector::Make(fScale, fScale);
283 ctx.ctm().mapVectors(&scale, 1);
284
285 #if SK_SUPPORT_GPU
286 if (ctx.gpuBacked()) {
287 auto rContext = ctx.getContext();
288
289 GrSurfaceProxyView colorView = color->view(rContext);
290 GrSurfaceProxyView displView = displ->view(rContext);
291 if (!colorView.proxy() || !displView.proxy()) {
292 return nullptr;
293 }
294 const auto isProtected = colorView.proxy()->isProtected();
295
296 SkMatrix offsetMatrix = SkMatrix::Translate(SkIntToScalar(colorOffset.fX - displOffset.fX),
297 SkIntToScalar(colorOffset.fY - displOffset.fY));
298
299 std::unique_ptr<GrFragmentProcessor> fp =
300 GrDisplacementMapEffect::Make(fXChannelSelector,
301 fYChannelSelector,
302 scale,
303 std::move(displView),
304 displ->subset(),
305 offsetMatrix,
306 std::move(colorView),
307 color->subset(),
308 *rContext->priv().caps());
309 fp = GrColorSpaceXformEffect::Make(std::move(fp),
310 color->getColorSpace(), color->alphaType(),
311 ctx.colorSpace(), kPremul_SkAlphaType);
312 GrImageInfo info(ctx.grColorType(),
313 kPremul_SkAlphaType,
314 ctx.refColorSpace(),
315 bounds.size());
316 auto sfc = rContext->priv().makeSFC(info,
317 SkBackingFit::kApprox,
318 1,
319 GrMipmapped::kNo,
320 isProtected,
321 kBottomLeft_GrSurfaceOrigin);
322 if (!sfc) {
323 return nullptr;
324 }
325
326 sfc->fillRectToRectWithFP(colorBounds,
327 SkIRect::MakeSize(colorBounds.size()),
328 std::move(fp));
329
330 offset->fX = bounds.left();
331 offset->fY = bounds.top();
332 return SkSpecialImage::MakeDeferredFromGpu(rContext,
333 SkIRect::MakeWH(bounds.width(), bounds.height()),
334 kNeedNewImageUniqueID_SpecialImage,
335 sfc->readSurfaceView(),
336 sfc->colorInfo().colorType(),
337 sfc->colorInfo().refColorSpace(),
338 ctx.surfaceProps());
339 }
340 #endif
341
342 SkBitmap colorBM, displBM;
343
344 if (!color->getROPixels(&colorBM) || !displ->getROPixels(&displBM)) {
345 return nullptr;
346 }
347
348 if ((colorBM.colorType() != kN32_SkColorType) ||
349 (displBM.colorType() != kN32_SkColorType)) {
350 return nullptr;
351 }
352
353 if (!colorBM.getPixels() || !displBM.getPixels()) {
354 return nullptr;
355 }
356
357 SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(),
358 colorBM.alphaType());
359
360 SkBitmap dst;
361 if (!dst.tryAllocPixels(info)) {
362 return nullptr;
363 }
364
365 compute_displacement(Extractor(fXChannelSelector, fYChannelSelector), scale, &dst,
366 displBM, colorOffset - displOffset, colorBM, colorBounds);
367
368 offset->fX = bounds.left();
369 offset->fY = bounds.top();
370 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
371 dst, ctx.surfaceProps());
372 }
373
computeFastBounds(const SkRect & src) const374 SkRect SkDisplacementMapImageFilter::computeFastBounds(const SkRect& src) const {
375 SkRect bounds = this->getColorInput() ? this->getColorInput()->computeFastBounds(src) : src;
376 bounds.outset(SkScalarAbs(fScale) * SK_ScalarHalf, SkScalarAbs(fScale) * SK_ScalarHalf);
377 return bounds;
378 }
379
onFilterNodeBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection,const SkIRect * inputRect) const380 SkIRect SkDisplacementMapImageFilter::onFilterNodeBounds(
381 const SkIRect& src, const SkMatrix& ctm, MapDirection, const SkIRect* inputRect) const {
382 SkVector scale = SkVector::Make(fScale, fScale);
383 ctm.mapVectors(&scale, 1);
384 return src.makeOutset(SkScalarCeilToInt(SkScalarAbs(scale.fX) * SK_ScalarHalf),
385 SkScalarCeilToInt(SkScalarAbs(scale.fY) * SK_ScalarHalf));
386 }
387
onFilterBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection dir,const SkIRect * inputRect) const388 SkIRect SkDisplacementMapImageFilter::onFilterBounds(
389 const SkIRect& src, const SkMatrix& ctm, MapDirection dir, const SkIRect* inputRect) const {
390 if (kReverse_MapDirection == dir) {
391 return INHERITED::onFilterBounds(src, ctm, dir, inputRect);
392 }
393 // Recurse only into color input.
394 if (this->getColorInput()) {
395 return this->getColorInput()->filterBounds(src, ctm, dir, inputRect);
396 }
397 return src;
398 }
399
400 ///////////////////////////////////////////////////////////////////////////////
401
402 #if SK_SUPPORT_GPU
403 class GrDisplacementMapEffect::Impl : public ProgramImpl {
404 public:
405 void emitCode(EmitArgs&) override;
406
407 private:
408 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
409
410 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
411
412 UniformHandle fScaleUni;
413 };
414
415 ///////////////////////////////////////////////////////////////////////////////
416
Make(SkColorChannel xChannelSelector,SkColorChannel yChannelSelector,SkVector scale,GrSurfaceProxyView displacement,const SkIRect & displSubset,const SkMatrix & offsetMatrix,GrSurfaceProxyView color,const SkIRect & colorSubset,const GrCaps & caps)417 std::unique_ptr<GrFragmentProcessor> GrDisplacementMapEffect::Make(SkColorChannel xChannelSelector,
418 SkColorChannel yChannelSelector,
419 SkVector scale,
420 GrSurfaceProxyView displacement,
421 const SkIRect& displSubset,
422 const SkMatrix& offsetMatrix,
423 GrSurfaceProxyView color,
424 const SkIRect& colorSubset,
425 const GrCaps& caps) {
426 static constexpr GrSamplerState kColorSampler(GrSamplerState::WrapMode::kClampToBorder,
427 GrSamplerState::Filter::kNearest);
428 auto colorEffect = GrTextureEffect::MakeSubset(std::move(color),
429 kPremul_SkAlphaType,
430 SkMatrix::Translate(colorSubset.topLeft()),
431 kColorSampler,
432 SkRect::Make(colorSubset),
433 caps);
434
435 auto dispM = SkMatrix::Concat(SkMatrix::Translate(displSubset.topLeft()), offsetMatrix);
436 auto dispEffect = GrTextureEffect::Make(std::move(displacement),
437 kPremul_SkAlphaType,
438 dispM,
439 GrSamplerState::Filter::kNearest);
440
441 return std::unique_ptr<GrFragmentProcessor>(
442 new GrDisplacementMapEffect(xChannelSelector,
443 yChannelSelector,
444 scale,
445 std::move(dispEffect),
446 std::move(colorEffect)));
447 }
448
449 std::unique_ptr<GrFragmentProcessor::ProgramImpl>
onMakeProgramImpl() const450 GrDisplacementMapEffect::onMakeProgramImpl() const {
451 return std::make_unique<Impl>();
452 }
453
getShaderDfxInfo() const454 SkString GrDisplacementMapEffect::getShaderDfxInfo() const
455 {
456 SkString format;
457 format.printf("ShaderDfx_GrDisplacementMapEffect_%d_%d", fXChannelSelector, fYChannelSelector);
458 return format;
459 }
460
onAddToKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const461 void GrDisplacementMapEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
462 static constexpr int kChannelSelectorKeyBits = 2; // Max value is 3, so 2 bits are required
463
464 uint32_t xKey = static_cast<uint32_t>(fXChannelSelector);
465 uint32_t yKey = static_cast<uint32_t>(fYChannelSelector) << kChannelSelectorKeyBits;
466
467 b->add32(xKey | yKey);
468 }
469
GrDisplacementMapEffect(SkColorChannel xChannelSelector,SkColorChannel yChannelSelector,const SkVector & scale,std::unique_ptr<GrFragmentProcessor> displacement,std::unique_ptr<GrFragmentProcessor> color)470 GrDisplacementMapEffect::GrDisplacementMapEffect(SkColorChannel xChannelSelector,
471 SkColorChannel yChannelSelector,
472 const SkVector& scale,
473 std::unique_ptr<GrFragmentProcessor> displacement,
474 std::unique_ptr<GrFragmentProcessor> color)
475 : INHERITED(kGrDisplacementMapEffect_ClassID, GrFragmentProcessor::kNone_OptimizationFlags)
476 , fXChannelSelector(xChannelSelector)
477 , fYChannelSelector(yChannelSelector)
478 , fScale(scale) {
479 this->registerChild(std::move(displacement));
480 this->registerChild(std::move(color), SkSL::SampleUsage::Explicit());
481 this->setUsesSampleCoordsDirectly();
482 }
483
GrDisplacementMapEffect(const GrDisplacementMapEffect & that)484 GrDisplacementMapEffect::GrDisplacementMapEffect(const GrDisplacementMapEffect& that)
485 : INHERITED(that)
486 , fXChannelSelector(that.fXChannelSelector)
487 , fYChannelSelector(that.fYChannelSelector)
488 , fScale(that.fScale) {}
489
~GrDisplacementMapEffect()490 GrDisplacementMapEffect::~GrDisplacementMapEffect() {}
491
clone() const492 std::unique_ptr<GrFragmentProcessor> GrDisplacementMapEffect::clone() const {
493 return std::unique_ptr<GrFragmentProcessor>(new GrDisplacementMapEffect(*this));
494 }
495
onIsEqual(const GrFragmentProcessor & sBase) const496 bool GrDisplacementMapEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
497 const GrDisplacementMapEffect& s = sBase.cast<GrDisplacementMapEffect>();
498 return fXChannelSelector == s.fXChannelSelector &&
499 fYChannelSelector == s.fYChannelSelector &&
500 fScale == s.fScale;
501 }
502
503 ///////////////////////////////////////////////////////////////////////////////
504
505 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDisplacementMapEffect);
506
507 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)508 std::unique_ptr<GrFragmentProcessor> GrDisplacementMapEffect::TestCreate(GrProcessorTestData* d) {
509 auto [dispView, ct1, at1] = d->randomView();
510 auto [colorView, ct2, at2] = d->randomView();
511 static const int kMaxComponent = static_cast<int>(SkColorChannel::kLastEnum);
512 SkColorChannel xChannelSelector =
513 static_cast<SkColorChannel>(d->fRandom->nextRangeU(1, kMaxComponent));
514 SkColorChannel yChannelSelector =
515 static_cast<SkColorChannel>(d->fRandom->nextRangeU(1, kMaxComponent));
516 SkVector scale;
517 scale.fX = d->fRandom->nextRangeScalar(0, 100.0f);
518 scale.fY = d->fRandom->nextRangeScalar(0, 100.0f);
519 SkISize colorDimensions;
520 colorDimensions.fWidth = d->fRandom->nextRangeU(0, colorView.width());
521 colorDimensions.fHeight = d->fRandom->nextRangeU(0, colorView.height());
522 SkIRect dispRect = SkIRect::MakeSize(dispView.dimensions());
523
524 return GrDisplacementMapEffect::Make(xChannelSelector,
525 yChannelSelector,
526 scale,
527 std::move(dispView),
528 dispRect,
529 SkMatrix::I(),
530 std::move(colorView),
531 SkIRect::MakeSize(colorDimensions),
532 *d->caps());
533 }
534
535 #endif
536
537 ///////////////////////////////////////////////////////////////////////////////
538
emitCode(EmitArgs & args)539 void GrDisplacementMapEffect::Impl::emitCode(EmitArgs& args) {
540 const GrDisplacementMapEffect& displacementMap = args.fFp.cast<GrDisplacementMapEffect>();
541
542 fScaleUni = args.fUniformHandler->addUniform(&displacementMap, kFragment_GrShaderFlag,
543 kHalf2_GrSLType, "Scale");
544 const char* scaleUni = args.fUniformHandler->getUniformCStr(fScaleUni);
545
546 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
547 SkString displacementSample = this->invokeChild(/*childIndex=*/0, args);
548 fragBuilder->codeAppendf("half4 dColor = unpremul(%s);", displacementSample.c_str());
549
550 auto chanChar = [](SkColorChannel c) {
551 switch(c) {
552 case SkColorChannel::kR: return 'r';
553 case SkColorChannel::kG: return 'g';
554 case SkColorChannel::kB: return 'b';
555 case SkColorChannel::kA: return 'a';
556 default: SkUNREACHABLE;
557 }
558 };
559 fragBuilder->codeAppendf("float2 cCoords = %s + %s * (dColor.%c%c - half2(0.5));",
560 args.fSampleCoord,
561 scaleUni,
562 chanChar(displacementMap.fXChannelSelector),
563 chanChar(displacementMap.fYChannelSelector));
564
565 SkString colorSample = this->invokeChild(/*childIndex=*/1, args, "cCoords");
566
567 fragBuilder->codeAppendf("return %s;", colorSample.c_str());
568 }
569
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & proc)570 void GrDisplacementMapEffect::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
571 const GrFragmentProcessor& proc) {
572 const auto& displacementMap = proc.cast<GrDisplacementMapEffect>();
573 pdman.set2f(fScaleUni, displacementMap.fScale.x(), displacementMap.fScale.y());
574 }
575 #endif
576