blob: d7d92c82b9476487ed009011cb02e8f973064e79 [file] [log] [blame]
sugoi@google.com781cc762013-01-15 15:40:19 +00001/*
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 "SkDisplacementMapEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00009#include "SkReadBuffer.h"
10#include "SkWriteBuffer.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000011#include "SkUnPreMultiply.h"
sugoi@google.com8cec63e2013-01-15 15:59:45 +000012#include "SkColorPriv.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000013#if SK_SUPPORT_GPU
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000014#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070015#include "GrDrawContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000016#include "GrCoordTransform.h"
egdaniel605dd0f2014-11-12 08:35:25 -080017#include "GrInvariantOutput.h"
senorblancod762ca22015-04-07 12:16:55 -070018#include "effects/GrTextureDomain.h"
joshualittb0a8a372014-09-23 09:50:21 -070019#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070020#include "gl/builders/GrGLProgramBuilder.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000021#endif
22
23namespace {
24
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000025#define kChannelSelectorKeyBits 3; // Max value is 4, so 3 bits are required at most
26
sugoi@google.com781cc762013-01-15 15:40:19 +000027template<SkDisplacementMapEffect::ChannelSelectorType type>
28uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) {
mtklein@google.com330313a2013-08-22 15:37:26 +000029 SkDEBUGFAIL("Unknown channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +000030 return 0;
31}
32
33template<> uint32_t getValue<SkDisplacementMapEffect::kR_ChannelSelectorType>(
34 SkColor l, const SkUnPreMultiply::Scale* table) {
35 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedR32(l));
36}
37
38template<> uint32_t getValue<SkDisplacementMapEffect::kG_ChannelSelectorType>(
39 SkColor l, const SkUnPreMultiply::Scale* table) {
40 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedG32(l));
41}
42
43template<> uint32_t getValue<SkDisplacementMapEffect::kB_ChannelSelectorType>(
44 SkColor l, const SkUnPreMultiply::Scale* table) {
45 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedB32(l));
46}
47
48template<> uint32_t getValue<SkDisplacementMapEffect::kA_ChannelSelectorType>(
49 SkColor l, const SkUnPreMultiply::Scale*) {
50 return SkGetPackedA32(l);
51}
52
53template<SkDisplacementMapEffect::ChannelSelectorType typeX,
54 SkDisplacementMapEffect::ChannelSelectorType typeY>
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000055void computeDisplacement(const SkVector& scale, SkBitmap* dst,
56 SkBitmap* displ, const SkIPoint& offset,
57 SkBitmap* src,
58 const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +000059{
reed80ea19c2015-05-12 10:37:34 -070060 static const SkScalar Inv8bit = SkScalarInvert(255);
sugoi@google.com781cc762013-01-15 15:40:19 +000061 const int srcW = src->width();
62 const int srcH = src->height();
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000063 const SkVector scaleForColor = SkVector::Make(SkScalarMul(scale.fX, Inv8bit),
64 SkScalarMul(scale.fY, Inv8bit));
65 const SkVector scaleAdj = SkVector::Make(SK_ScalarHalf - SkScalarMul(scale.fX, SK_ScalarHalf),
66 SK_ScalarHalf - SkScalarMul(scale.fY, SK_ScalarHalf));
sugoi@google.com781cc762013-01-15 15:40:19 +000067 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000068 SkPMColor* dstPtr = dst->getAddr32(0, 0);
69 for (int y = bounds.top(); y < bounds.bottom(); ++y) {
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000070 const SkPMColor* displPtr = displ->getAddr32(bounds.left() + offset.fX,
71 y + offset.fY);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000072 for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) {
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000073 const SkScalar displX = SkScalarMul(scaleForColor.fX,
74 SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj.fX;
75 const SkScalar displY = SkScalarMul(scaleForColor.fY,
76 SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj.fY;
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000077 // Truncate the displacement values
78 const int srcX = x + SkScalarTruncToInt(displX);
79 const int srcY = y + SkScalarTruncToInt(displY);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000080 *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ?
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000081 0 : *(src->getAddr32(srcX, srcY));
sugoi@google.com781cc762013-01-15 15:40:19 +000082 }
83 }
84}
85
86template<SkDisplacementMapEffect::ChannelSelectorType typeX>
87void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000088 const SkVector& scale, SkBitmap* dst,
89 SkBitmap* displ, const SkIPoint& offset,
90 SkBitmap* src,
91 const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +000092{
93 switch (yChannelSelector) {
94 case SkDisplacementMapEffect::kR_ChannelSelectorType:
95 computeDisplacement<typeX, SkDisplacementMapEffect::kR_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000096 scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +000097 break;
98 case SkDisplacementMapEffect::kG_ChannelSelectorType:
99 computeDisplacement<typeX, SkDisplacementMapEffect::kG_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000100 scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000101 break;
102 case SkDisplacementMapEffect::kB_ChannelSelectorType:
103 computeDisplacement<typeX, SkDisplacementMapEffect::kB_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000104 scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000105 break;
106 case SkDisplacementMapEffect::kA_ChannelSelectorType:
107 computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000108 scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000109 break;
110 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
111 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000112 SkDEBUGFAIL("Unknown Y channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000113 }
114}
115
116void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
117 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000118 const SkVector& scale, SkBitmap* dst,
119 SkBitmap* displ, const SkIPoint& offset,
120 SkBitmap* src,
121 const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +0000122{
123 switch (xChannelSelector) {
124 case SkDisplacementMapEffect::kR_ChannelSelectorType:
125 computeDisplacement<SkDisplacementMapEffect::kR_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000126 yChannelSelector, scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000127 break;
128 case SkDisplacementMapEffect::kG_ChannelSelectorType:
129 computeDisplacement<SkDisplacementMapEffect::kG_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000130 yChannelSelector, scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000131 break;
132 case SkDisplacementMapEffect::kB_ChannelSelectorType:
133 computeDisplacement<SkDisplacementMapEffect::kB_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000134 yChannelSelector, scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000135 break;
136 case SkDisplacementMapEffect::kA_ChannelSelectorType:
137 computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000138 yChannelSelector, scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000139 break;
140 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
141 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000142 SkDEBUGFAIL("Unknown X channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000143 }
144}
145
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000146bool channel_selector_type_is_valid(SkDisplacementMapEffect::ChannelSelectorType cst) {
147 switch (cst) {
148 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
149 case SkDisplacementMapEffect::kR_ChannelSelectorType:
150 case SkDisplacementMapEffect::kG_ChannelSelectorType:
151 case SkDisplacementMapEffect::kB_ChannelSelectorType:
152 case SkDisplacementMapEffect::kA_ChannelSelectorType:
153 return true;
154 default:
155 break;
156 }
157 return false;
158}
159
sugoi@google.com781cc762013-01-15 15:40:19 +0000160} // end namespace
161
162///////////////////////////////////////////////////////////////////////////////
163
reed9fa60da2014-08-21 07:59:51 -0700164SkDisplacementMapEffect* SkDisplacementMapEffect::Create(ChannelSelectorType xChannelSelector,
165 ChannelSelectorType yChannelSelector,
166 SkScalar scale,
167 SkImageFilter* displacement,
168 SkImageFilter* color,
senorblanco24e06d52015-03-18 12:11:33 -0700169 const CropRect* cropRect) {
reed9fa60da2014-08-21 07:59:51 -0700170 if (!channel_selector_type_is_valid(xChannelSelector) ||
171 !channel_selector_type_is_valid(yChannelSelector)) {
172 return NULL;
173 }
174
175 SkImageFilter* inputs[2] = { displacement, color };
176 return SkNEW_ARGS(SkDisplacementMapEffect, (xChannelSelector, yChannelSelector, scale,
senorblanco24e06d52015-03-18 12:11:33 -0700177 inputs, cropRect));
reed9fa60da2014-08-21 07:59:51 -0700178}
179
sugoi@google.com781cc762013-01-15 15:40:19 +0000180SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSelector,
181 ChannelSelectorType yChannelSelector,
182 SkScalar scale,
senorblanco9ea3d572014-07-08 09:16:22 -0700183 SkImageFilter* inputs[2],
senorblanco24e06d52015-03-18 12:11:33 -0700184 const CropRect* cropRect)
185 : INHERITED(2, inputs, cropRect)
sugoi@google.com781cc762013-01-15 15:40:19 +0000186 , fXChannelSelector(xChannelSelector)
187 , fYChannelSelector(yChannelSelector)
188 , fScale(scale)
189{
190}
191
192SkDisplacementMapEffect::~SkDisplacementMapEffect() {
193}
194
reed9fa60da2014-08-21 07:59:51 -0700195SkFlattenable* SkDisplacementMapEffect::CreateProc(SkReadBuffer& buffer) {
196 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2);
197 ChannelSelectorType xsel = (ChannelSelectorType)buffer.readInt();
198 ChannelSelectorType ysel = (ChannelSelectorType)buffer.readInt();
199 SkScalar scale = buffer.readScalar();
senorblanco24e06d52015-03-18 12:11:33 -0700200 return Create(xsel, ysel, scale, common.getInput(0), common.getInput(1), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -0700201}
sugoi@google.com781cc762013-01-15 15:40:19 +0000202
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000203void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const {
sugoi@google.com781cc762013-01-15 15:40:19 +0000204 this->INHERITED::flatten(buffer);
205 buffer.writeInt((int) fXChannelSelector);
206 buffer.writeInt((int) fYChannelSelector);
207 buffer.writeScalar(fScale);
208}
209
210bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy,
211 const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000212 const Context& ctx,
sugoi@google.com781cc762013-01-15 15:40:19 +0000213 SkBitmap* dst,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000214 SkIPoint* offset) const {
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000215 SkBitmap displ = src, color = src;
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000216 const SkImageFilter* colorInput = getColorInput();
217 const SkImageFilter* displInput = getDisplacementInput();
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000218 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0, 0);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000219 if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorOffset)) ||
220 (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displOffset))) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000221 return false;
222 }
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000223 if ((displ.colorType() != kN32_SkColorType) ||
224 (color.colorType() != kN32_SkColorType)) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000225 return false;
226 }
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000227 SkIRect bounds;
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000228 // Since computeDisplacement does bounds checking on color pixel access, we don't need to pad
229 // the color bitmap to bounds here.
230 if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) {
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000231 return false;
232 }
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000233 SkIRect displBounds;
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000234 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &displ)) {
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000235 return false;
236 }
237 if (!bounds.intersect(displBounds)) {
238 return false;
239 }
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000240 SkAutoLockPixels alp_displacement(displ), alp_color(color);
241 if (!displ.getPixels() || !color.getPixels()) {
242 return false;
243 }
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000244
reed84825042014-09-02 12:50:45 -0700245 if (!dst->tryAllocPixels(color.info().makeWH(bounds.width(), bounds.height()))) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000246 return false;
247 }
248
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000249 SkVector scale = SkVector::Make(fScale, fScale);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000250 ctx.ctm().mapVectors(&scale, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000251 SkIRect colorBounds = bounds;
252 colorBounds.offset(-colorOffset);
253
254 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst,
255 &displ, colorOffset - displOffset, &color, colorBounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000256
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000257 offset->fX = bounds.left();
258 offset->fY = bounds.top();
sugoi@google.com781cc762013-01-15 15:40:19 +0000259 return true;
260}
261
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000262void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const {
263 if (getColorInput()) {
264 getColorInput()->computeFastBounds(src, dst);
265 } else {
266 *dst = src;
267 }
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000268 dst->outset(fScale * SK_ScalarHalf, fScale * SK_ScalarHalf);
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000269}
270
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000271bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
272 SkIRect* dst) const {
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000273 SkIRect bounds = src;
senorblanco@chromium.orgd4db6572014-05-07 20:00:04 +0000274 SkVector scale = SkVector::Make(fScale, fScale);
275 ctm.mapVectors(&scale, 1);
276 bounds.outset(SkScalarCeilToInt(scale.fX * SK_ScalarHalf),
277 SkScalarCeilToInt(scale.fY * SK_ScalarHalf));
278 if (getColorInput()) {
279 return getColorInput()->filterBounds(bounds, ctm, dst);
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000280 }
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000281 *dst = bounds;
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000282 return true;
283}
284
robertphillipsf3f5bad2014-12-19 13:49:15 -0800285#ifndef SK_IGNORE_TO_STRING
286void SkDisplacementMapEffect::toString(SkString* str) const {
287 str->appendf("SkDisplacementMapEffect: (");
288 str->appendf("scale: %f ", fScale);
289 str->appendf("displacement: (");
290 if (this->getDisplacementInput()) {
291 this->getDisplacementInput()->toString(str);
292 }
293 str->appendf(") color: (");
294 if (this->getColorInput()) {
295 this->getColorInput()->toString(str);
296 }
297 str->appendf("))");
298}
299#endif
300
sugoi@google.com781cc762013-01-15 15:40:19 +0000301///////////////////////////////////////////////////////////////////////////////
302
303#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -0700304class GrGLDisplacementMapEffect : public GrGLFragmentProcessor {
sugoi@google.com781cc762013-01-15 15:40:19 +0000305public:
joshualitteb2a6762014-12-04 11:35:33 -0800306 GrGLDisplacementMapEffect(const GrProcessor&);
sugoi@google.com781cc762013-01-15 15:40:19 +0000307 virtual ~GrGLDisplacementMapEffect();
308
joshualitt15988992014-10-09 15:04:05 -0700309 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700310 const GrFragmentProcessor&,
sugoi@google.com781cc762013-01-15 15:40:19 +0000311 const char* outputColor,
312 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000313 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700314 const TextureSamplerArray&) override;
sugoi@google.com781cc762013-01-15 15:40:19 +0000315
jvanverthcfc18862015-04-28 08:48:20 -0700316 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
sugoi@google.com781cc762013-01-15 15:40:19 +0000317
mtklein36352bf2015-03-25 18:17:31 -0700318 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
senorblancod762ca22015-04-07 12:16:55 -0700319 const GrTextureDomain::GLDomain& glDomain() const { return fGLDomain; }
sugoi@google.com781cc762013-01-15 15:40:19 +0000320
321private:
322 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
323 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
kkinnunen7510b222014-07-30 00:04:16 -0700324 GrGLProgramDataManager::UniformHandle fScaleUni;
senorblancod762ca22015-04-07 12:16:55 -0700325 GrTextureDomain::GLDomain fGLDomain;
sugoi@google.com781cc762013-01-15 15:40:19 +0000326
joshualittb0a8a372014-09-23 09:50:21 -0700327 typedef GrGLFragmentProcessor INHERITED;
sugoi@google.com781cc762013-01-15 15:40:19 +0000328};
329
330///////////////////////////////////////////////////////////////////////////////
331
joshualittb0a8a372014-09-23 09:50:21 -0700332class GrDisplacementMapEffect : public GrFragmentProcessor {
sugoi@google.com781cc762013-01-15 15:40:19 +0000333public:
joshualittb0a8a372014-09-23 09:50:21 -0700334 static GrFragmentProcessor* Create(
335 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
336 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, SkVector scale,
senorblancod762ca22015-04-07 12:16:55 -0700337 GrTexture* displacement, const SkMatrix& offsetMatrix, GrTexture* color,
338 const SkISize& colorDimensions) {
bsalomon55fad7a2014-07-08 07:34:20 -0700339 return SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSelector,
340 yChannelSelector,
341 scale,
342 displacement,
343 offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700344 color,
345 colorDimensions));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000346 }
347
sugoi@google.com781cc762013-01-15 15:40:19 +0000348 virtual ~GrDisplacementMapEffect();
349
jvanverthcfc18862015-04-28 08:48:20 -0700350 virtual void getGLProcessorKey(const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700351 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800352 GrGLDisplacementMapEffect::GenKey(*this, caps, b);
353 }
354
mtklein36352bf2015-03-25 18:17:31 -0700355 GrGLFragmentProcessor* createGLInstance() const override {
joshualitteb2a6762014-12-04 11:35:33 -0800356 return SkNEW_ARGS(GrGLDisplacementMapEffect, (*this));
357 }
358
sugoi@google.com781cc762013-01-15 15:40:19 +0000359 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const
360 { return fXChannelSelector; }
361 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const
362 { return fYChannelSelector; }
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000363 const SkVector& scale() const { return fScale; }
sugoi@google.com781cc762013-01-15 15:40:19 +0000364
mtklein36352bf2015-03-25 18:17:31 -0700365 const char* name() const override { return "DisplacementMap"; }
senorblancod762ca22015-04-07 12:16:55 -0700366 const GrTextureDomain& domain() const { return fDomain; }
sugoi@google.com781cc762013-01-15 15:40:19 +0000367
sugoi@google.com781cc762013-01-15 15:40:19 +0000368private:
mtklein36352bf2015-03-25 18:17:31 -0700369 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000370
mtklein36352bf2015-03-25 18:17:31 -0700371 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700372
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000373 GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
374 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000375 const SkVector& scale,
376 GrTexture* displacement, const SkMatrix& offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700377 GrTexture* color,
378 const SkISize& colorDimensions);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000379
joshualittb0a8a372014-09-23 09:50:21 -0700380 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.com781cc762013-01-15 15:40:19 +0000381
bsalomon@google.com77af6802013-10-02 13:04:56 +0000382 GrCoordTransform fDisplacementTransform;
sugoi@google.com781cc762013-01-15 15:40:19 +0000383 GrTextureAccess fDisplacementAccess;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000384 GrCoordTransform fColorTransform;
senorblancod762ca22015-04-07 12:16:55 -0700385 GrTextureDomain fDomain;
sugoi@google.com781cc762013-01-15 15:40:19 +0000386 GrTextureAccess fColorAccess;
387 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
388 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000389 SkVector fScale;
sugoi@google.com781cc762013-01-15 15:40:19 +0000390
joshualittb0a8a372014-09-23 09:50:21 -0700391 typedef GrFragmentProcessor INHERITED;
sugoi@google.com781cc762013-01-15 15:40:19 +0000392};
393
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000394bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000395 SkBitmap* result, SkIPoint* offset) const {
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000396 SkBitmap colorBM = src;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000397 SkIPoint colorOffset = SkIPoint::Make(0, 0);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000398 if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx, &colorBM,
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000399 &colorOffset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000400 return false;
sugoi@google.com781cc762013-01-15 15:40:19 +0000401 }
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000402 SkBitmap displacementBM = src;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000403 SkIPoint displacementOffset = SkIPoint::Make(0, 0);
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000404 if (getDisplacementInput() &&
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000405 !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacementBM,
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000406 &displacementOffset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000407 return false;
408 }
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000409 SkIRect bounds;
410 // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to
411 // pad the color bitmap to bounds here.
412 if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) {
413 return false;
414 }
415 SkIRect displBounds;
416 if (!this->applyCropRect(ctx, proxy, displacementBM,
417 &displacementOffset, &displBounds, &displacementBM)) {
418 return false;
419 }
420 if (!bounds.intersect(displBounds)) {
421 return false;
422 }
423 GrTexture* color = colorBM.getTexture();
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000424 GrTexture* displacement = displacementBM.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000425 GrContext* context = color->getContext();
sugoi@google.com781cc762013-01-15 15:40:19 +0000426
bsalomonf2703d82014-10-28 14:33:06 -0700427 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800428 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.orgd9cdff62014-03-17 23:41:40 +0000429 desc.fWidth = bounds.width();
430 desc.fHeight = bounds.height();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000431 desc.fConfig = kSkia8888_GrPixelConfig;
sugoi@google.com781cc762013-01-15 15:40:19 +0000432
bsalomond309e7a2015-04-30 14:18:54 -0700433 SkAutoTUnref<GrTexture> dst(context->textureProvider()->refScratchTexture(desc,
434 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700435
436 if (!dst) {
senorblanco673d9732014-08-15 10:48:43 -0700437 return false;
438 }
sugoi@google.com781cc762013-01-15 15:40:19 +0000439
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000440 SkVector scale = SkVector::Make(fScale, fScale);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000441 ctx.ctm().mapVectors(&scale, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000442
443 GrPaint paint;
bsalomon6267f812014-08-29 15:05:53 -0700444 SkMatrix offsetMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(displacement);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000445 offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset.fX),
446 SkIntToScalar(colorOffset.fY - displacementOffset.fY));
447
joshualittb0a8a372014-09-23 09:50:21 -0700448 paint.addColorProcessor(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000449 GrDisplacementMapEffect::Create(fXChannelSelector,
450 fYChannelSelector,
451 scale,
452 displacement,
453 offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700454 color,
455 colorBM.dimensions()))->unref();
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000456 SkIRect colorBounds = bounds;
457 colorBounds.offset(-colorOffset);
458 SkMatrix matrix;
459 matrix.setTranslate(-SkIntToScalar(colorBounds.x()),
460 -SkIntToScalar(colorBounds.y()));
robertphillipsea461502015-05-26 11:38:03 -0700461
462 GrDrawContext* drawContext = context->drawContext();
463 if (!drawContext) {
464 return false;
465 }
466
467 drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, matrix,
468 SkRect::Make(colorBounds));
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000469 offset->fX = bounds.left();
470 offset->fY = bounds.top();
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000471 WrapTexture(dst, bounds.width(), bounds.height(), result);
472 return true;
sugoi@google.com781cc762013-01-15 15:40:19 +0000473}
474
475///////////////////////////////////////////////////////////////////////////////
476
477GrDisplacementMapEffect::GrDisplacementMapEffect(
478 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
479 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000480 const SkVector& scale,
sugoi@google.com781cc762013-01-15 15:40:19 +0000481 GrTexture* displacement,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000482 const SkMatrix& offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700483 GrTexture* color,
484 const SkISize& colorDimensions)
bsalomon9f876a32014-12-09 10:51:07 -0800485 : fDisplacementTransform(kLocal_GrCoordSet, offsetMatrix, displacement,
486 GrTextureParams::kNone_FilterMode)
bsalomon@google.com77af6802013-10-02 13:04:56 +0000487 , fDisplacementAccess(displacement)
bsalomon9f876a32014-12-09 10:51:07 -0800488 , fColorTransform(kLocal_GrCoordSet, color, GrTextureParams::kNone_FilterMode)
senorblancod762ca22015-04-07 12:16:55 -0700489 , fDomain(GrTextureDomain::MakeTexelDomain(color, SkIRect::MakeSize(colorDimensions)),
490 GrTextureDomain::kDecal_Mode)
sugoi@google.com781cc762013-01-15 15:40:19 +0000491 , fColorAccess(color)
492 , fXChannelSelector(xChannelSelector)
493 , fYChannelSelector(yChannelSelector)
494 , fScale(scale) {
joshualitteb2a6762014-12-04 11:35:33 -0800495 this->initClassID<GrDisplacementMapEffect>();
bsalomon@google.com77af6802013-10-02 13:04:56 +0000496 this->addCoordTransform(&fDisplacementTransform);
sugoi@google.com781cc762013-01-15 15:40:19 +0000497 this->addTextureAccess(&fDisplacementAccess);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000498 this->addCoordTransform(&fColorTransform);
sugoi@google.com781cc762013-01-15 15:40:19 +0000499 this->addTextureAccess(&fColorAccess);
500}
501
502GrDisplacementMapEffect::~GrDisplacementMapEffect() {
503}
504
bsalomon0e08fc12014-10-15 08:19:04 -0700505bool GrDisplacementMapEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700506 const GrDisplacementMapEffect& s = sBase.cast<GrDisplacementMapEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700507 return fXChannelSelector == s.fXChannelSelector &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000508 fYChannelSelector == s.fYChannelSelector &&
509 fScale == s.fScale;
sugoi@google.com781cc762013-01-15 15:40:19 +0000510}
511
egdaniel605dd0f2014-11-12 08:35:25 -0800512void GrDisplacementMapEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
sugoi@google.com781cc762013-01-15 15:40:19 +0000513 // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0),
514 // so the only way we'd get a constant alpha is if the input color image has a constant alpha
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000515 // and no displacement offset push any texture coordinates out of bounds OR if the constant
sugoi@google.com781cc762013-01-15 15:40:19 +0000516 // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is
517 // not of constant color when a displacement effect is applied.
egdaniel605dd0f2014-11-12 08:35:25 -0800518 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
sugoi@google.com781cc762013-01-15 15:40:19 +0000519}
520
521///////////////////////////////////////////////////////////////////////////////
522
joshualittb0a8a372014-09-23 09:50:21 -0700523GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDisplacementMapEffect);
sugoi@google.com781cc762013-01-15 15:40:19 +0000524
joshualittb0a8a372014-09-23 09:50:21 -0700525GrFragmentProcessor* GrDisplacementMapEffect::TestCreate(SkRandom* random,
bsalomon83d081a2014-07-08 09:56:10 -0700526 GrContext*,
bsalomon4b91f762015-05-19 09:29:46 -0700527 const GrCaps&,
bsalomon83d081a2014-07-08 09:56:10 -0700528 GrTexture* textures[]) {
joshualittb0a8a372014-09-23 09:50:21 -0700529 int texIdxDispl = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
530 GrProcessorUnitTest::kAlphaTextureIdx;
531 int texIdxColor = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
532 GrProcessorUnitTest::kAlphaTextureIdx;
sugoi@google.com781cc762013-01-15 15:40:19 +0000533 static const int kMaxComponent = 4;
534 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector =
535 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
536 random->nextRangeU(1, kMaxComponent));
537 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector =
538 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
539 random->nextRangeU(1, kMaxComponent));
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000540 SkVector scale = SkVector::Make(random->nextRangeScalar(0, 100.0f),
541 random->nextRangeScalar(0, 100.0f));
senorblancod762ca22015-04-07 12:16:55 -0700542 SkISize colorDimensions;
543 colorDimensions.fWidth = random->nextRangeU(0, textures[texIdxColor]->width());
544 colorDimensions.fHeight = random->nextRangeU(0, textures[texIdxColor]->height());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000545 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000546 textures[texIdxDispl], SkMatrix::I(),
senorblancod762ca22015-04-07 12:16:55 -0700547 textures[texIdxColor], colorDimensions);
sugoi@google.com781cc762013-01-15 15:40:19 +0000548}
549
550///////////////////////////////////////////////////////////////////////////////
551
joshualitteb2a6762014-12-04 11:35:33 -0800552GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrProcessor& proc)
553 : fXChannelSelector(proc.cast<GrDisplacementMapEffect>().xChannelSelector())
joshualittb0a8a372014-09-23 09:50:21 -0700554 , fYChannelSelector(proc.cast<GrDisplacementMapEffect>().yChannelSelector()) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000555}
556
557GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() {
558}
559
joshualitt15988992014-10-09 15:04:05 -0700560void GrGLDisplacementMapEffect::emitCode(GrGLFPBuilder* builder,
senorblancod762ca22015-04-07 12:16:55 -0700561 const GrFragmentProcessor& fp,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000562 const char* outputColor,
563 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000564 const TransformedCoordsArray& coords,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000565 const TextureSamplerArray& samplers) {
senorblancod762ca22015-04-07 12:16:55 -0700566 const GrTextureDomain& domain = fp.cast<GrDisplacementMapEffect>().domain();
sugoi@google.com4d94e1b2013-03-21 18:38:03 +0000567 sk_ignore_unused_variable(inputColor);
568
joshualitt30ba4362014-08-21 20:18:45 -0700569 fScaleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800570 kVec2f_GrSLType, kDefault_GrSLPrecision, "Scale");
sugoi@google.com781cc762013-01-15 15:40:19 +0000571 const char* scaleUni = builder->getUniformCStr(fScaleUni);
sugoi@google.com781cc762013-01-15 15:40:19 +0000572 const char* dColor = "dColor";
sugoi@google.com781cc762013-01-15 15:40:19 +0000573 const char* cCoords = "cCoords";
574 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use
575 // a number smaller than that to approximate 0, but
576 // leave room for 32-bit float GPU rounding errors.
577
egdaniel29bee0f2015-04-29 11:54:42 -0700578 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700579 fsBuilder->codeAppendf("\t\tvec4 %s = ", dColor);
joshualitt23e280d2014-09-18 12:26:38 -0700580 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -0700581 fsBuilder->codeAppend(";\n");
sugoi@google.com781cc762013-01-15 15:40:19 +0000582
583 // Unpremultiply the displacement
joshualitt30ba4362014-08-21 20:18:45 -0700584 fsBuilder->codeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);",
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000585 dColor, dColor, nearZero, dColor, dColor);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800586 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 1);
joshualitt30ba4362014-08-21 20:18:45 -0700587 fsBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.",
joshualitt8fc6c2d2014-12-22 15:27:05 -0800588 cCoords, coords2D.c_str(), scaleUni, dColor);
sugoi@google.com781cc762013-01-15 15:40:19 +0000589
590 switch (fXChannelSelector) {
591 case SkDisplacementMapEffect::kR_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700592 fsBuilder->codeAppend("r");
sugoi@google.com781cc762013-01-15 15:40:19 +0000593 break;
594 case SkDisplacementMapEffect::kG_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700595 fsBuilder->codeAppend("g");
sugoi@google.com781cc762013-01-15 15:40:19 +0000596 break;
597 case SkDisplacementMapEffect::kB_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700598 fsBuilder->codeAppend("b");
sugoi@google.com781cc762013-01-15 15:40:19 +0000599 break;
600 case SkDisplacementMapEffect::kA_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700601 fsBuilder->codeAppend("a");
sugoi@google.com781cc762013-01-15 15:40:19 +0000602 break;
603 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
604 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000605 SkDEBUGFAIL("Unknown X channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000606 }
607
608 switch (fYChannelSelector) {
609 case SkDisplacementMapEffect::kR_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700610 fsBuilder->codeAppend("r");
sugoi@google.com781cc762013-01-15 15:40:19 +0000611 break;
612 case SkDisplacementMapEffect::kG_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700613 fsBuilder->codeAppend("g");
sugoi@google.com781cc762013-01-15 15:40:19 +0000614 break;
615 case SkDisplacementMapEffect::kB_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700616 fsBuilder->codeAppend("b");
sugoi@google.com781cc762013-01-15 15:40:19 +0000617 break;
618 case SkDisplacementMapEffect::kA_ChannelSelectorType:
joshualitt30ba4362014-08-21 20:18:45 -0700619 fsBuilder->codeAppend("a");
sugoi@google.com781cc762013-01-15 15:40:19 +0000620 break;
621 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
622 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000623 SkDEBUGFAIL("Unknown Y channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000624 }
joshualitt30ba4362014-08-21 20:18:45 -0700625 fsBuilder->codeAppend("-vec2(0.5));\t\t");
sugoi@google.com781cc762013-01-15 15:40:19 +0000626
senorblancod762ca22015-04-07 12:16:55 -0700627 fGLDomain.sampleTexture(fsBuilder, domain, outputColor, SkString(cCoords), samplers[1]);
joshualitt30ba4362014-08-21 20:18:45 -0700628 fsBuilder->codeAppend(";\n");
sugoi@google.com781cc762013-01-15 15:40:19 +0000629}
630
kkinnunen7510b222014-07-30 00:04:16 -0700631void GrGLDisplacementMapEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700632 const GrProcessor& proc) {
633 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>();
sugoi@google.com781cc762013-01-15 15:40:19 +0000634 GrTexture* colorTex = displacementMap.texture(1);
reed80ea19c2015-05-12 10:37:34 -0700635 SkScalar scaleX = displacementMap.scale().fX / colorTex->width();
636 SkScalar scaleY = displacementMap.scale().fY / colorTex->height();
kkinnunen7510b222014-07-30 00:04:16 -0700637 pdman.set2f(fScaleUni, SkScalarToFloat(scaleX),
638 colorTex->origin() == kTopLeft_GrSurfaceOrigin ?
639 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
senorblancod762ca22015-04-07 12:16:55 -0700640 fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin());
sugoi@google.com781cc762013-01-15 15:40:19 +0000641}
642
joshualittb0a8a372014-09-23 09:50:21 -0700643void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700644 const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualittb0a8a372014-09-23 09:50:21 -0700645 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>();
sugoi@google.com781cc762013-01-15 15:40:19 +0000646
bsalomon63e99f72014-07-21 08:03:14 -0700647 uint32_t xKey = displacementMap.xChannelSelector();
648 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits;
sugoi@google.com781cc762013-01-15 15:40:19 +0000649
bsalomon63e99f72014-07-21 08:03:14 -0700650 b->add32(xKey | yKey);
sugoi@google.com781cc762013-01-15 15:40:19 +0000651}
652#endif
robertphillipsf3f5bad2014-12-19 13:49:15 -0800653