blob: 2f95474a76b2d0a70f097432b3c65d223ffdf5fc [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"
senorblanco1d3ff432015-10-20 10:17:34 -07009#include "SkDevice.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000012#include "SkUnPreMultiply.h"
sugoi@google.com8cec63e2013-01-15 15:59:45 +000013#include "SkColorPriv.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000014#if SK_SUPPORT_GPU
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000015#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070016#include "GrDrawContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000017#include "GrCoordTransform.h"
egdaniel605dd0f2014-11-12 08:35:25 -080018#include "GrInvariantOutput.h"
robertphillips1de87df2016-01-14 06:03:29 -080019#include "SkGr.h"
senorblancod762ca22015-04-07 12:16:55 -070020#include "effects/GrTextureDomain.h"
egdaniel64c47282015-11-13 06:54:19 -080021#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080022#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070023#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080024#include "glsl/GrGLSLUniformHandler.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000025#endif
26
27namespace {
28
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000029#define kChannelSelectorKeyBits 3; // Max value is 4, so 3 bits are required at most
30
sugoi@google.com781cc762013-01-15 15:40:19 +000031template<SkDisplacementMapEffect::ChannelSelectorType type>
32uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) {
mtklein@google.com330313a2013-08-22 15:37:26 +000033 SkDEBUGFAIL("Unknown channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +000034 return 0;
35}
36
37template<> uint32_t getValue<SkDisplacementMapEffect::kR_ChannelSelectorType>(
38 SkColor l, const SkUnPreMultiply::Scale* table) {
39 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedR32(l));
40}
41
42template<> uint32_t getValue<SkDisplacementMapEffect::kG_ChannelSelectorType>(
43 SkColor l, const SkUnPreMultiply::Scale* table) {
44 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedG32(l));
45}
46
47template<> uint32_t getValue<SkDisplacementMapEffect::kB_ChannelSelectorType>(
48 SkColor l, const SkUnPreMultiply::Scale* table) {
49 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedB32(l));
50}
51
52template<> uint32_t getValue<SkDisplacementMapEffect::kA_ChannelSelectorType>(
53 SkColor l, const SkUnPreMultiply::Scale*) {
54 return SkGetPackedA32(l);
55}
56
57template<SkDisplacementMapEffect::ChannelSelectorType typeX,
58 SkDisplacementMapEffect::ChannelSelectorType typeY>
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000059void computeDisplacement(const SkVector& scale, SkBitmap* dst,
60 SkBitmap* displ, const SkIPoint& offset,
61 SkBitmap* src,
62 const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +000063{
reed80ea19c2015-05-12 10:37:34 -070064 static const SkScalar Inv8bit = SkScalarInvert(255);
sugoi@google.com781cc762013-01-15 15:40:19 +000065 const int srcW = src->width();
66 const int srcH = src->height();
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000067 const SkVector scaleForColor = SkVector::Make(SkScalarMul(scale.fX, Inv8bit),
68 SkScalarMul(scale.fY, Inv8bit));
69 const SkVector scaleAdj = SkVector::Make(SK_ScalarHalf - SkScalarMul(scale.fX, SK_ScalarHalf),
70 SK_ScalarHalf - SkScalarMul(scale.fY, SK_ScalarHalf));
sugoi@google.com781cc762013-01-15 15:40:19 +000071 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000072 SkPMColor* dstPtr = dst->getAddr32(0, 0);
73 for (int y = bounds.top(); y < bounds.bottom(); ++y) {
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000074 const SkPMColor* displPtr = displ->getAddr32(bounds.left() + offset.fX,
75 y + offset.fY);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000076 for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) {
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000077 const SkScalar displX = SkScalarMul(scaleForColor.fX,
78 SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj.fX;
79 const SkScalar displY = SkScalarMul(scaleForColor.fY,
80 SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj.fY;
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000081 // Truncate the displacement values
82 const int srcX = x + SkScalarTruncToInt(displX);
83 const int srcY = y + SkScalarTruncToInt(displY);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000084 *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ?
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000085 0 : *(src->getAddr32(srcX, srcY));
sugoi@google.com781cc762013-01-15 15:40:19 +000086 }
87 }
88}
89
90template<SkDisplacementMapEffect::ChannelSelectorType typeX>
91void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000092 const SkVector& scale, SkBitmap* dst,
93 SkBitmap* displ, const SkIPoint& offset,
94 SkBitmap* src,
95 const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +000096{
97 switch (yChannelSelector) {
98 case SkDisplacementMapEffect::kR_ChannelSelectorType:
99 computeDisplacement<typeX, SkDisplacementMapEffect::kR_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::kG_ChannelSelectorType:
103 computeDisplacement<typeX, SkDisplacementMapEffect::kG_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::kB_ChannelSelectorType:
107 computeDisplacement<typeX, SkDisplacementMapEffect::kB_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::kA_ChannelSelectorType:
111 computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000112 scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000113 break;
114 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
115 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000116 SkDEBUGFAIL("Unknown Y channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000117 }
118}
119
120void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
121 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000122 const SkVector& scale, SkBitmap* dst,
123 SkBitmap* displ, const SkIPoint& offset,
124 SkBitmap* src,
125 const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +0000126{
127 switch (xChannelSelector) {
128 case SkDisplacementMapEffect::kR_ChannelSelectorType:
129 computeDisplacement<SkDisplacementMapEffect::kR_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::kG_ChannelSelectorType:
133 computeDisplacement<SkDisplacementMapEffect::kG_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::kB_ChannelSelectorType:
137 computeDisplacement<SkDisplacementMapEffect::kB_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::kA_ChannelSelectorType:
141 computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>(
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000142 yChannelSelector, scale, dst, displ, offset, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000143 break;
144 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
145 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000146 SkDEBUGFAIL("Unknown X channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000147 }
148}
149
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000150bool channel_selector_type_is_valid(SkDisplacementMapEffect::ChannelSelectorType cst) {
151 switch (cst) {
152 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
153 case SkDisplacementMapEffect::kR_ChannelSelectorType:
154 case SkDisplacementMapEffect::kG_ChannelSelectorType:
155 case SkDisplacementMapEffect::kB_ChannelSelectorType:
156 case SkDisplacementMapEffect::kA_ChannelSelectorType:
157 return true;
158 default:
159 break;
160 }
161 return false;
162}
163
sugoi@google.com781cc762013-01-15 15:40:19 +0000164} // end namespace
165
166///////////////////////////////////////////////////////////////////////////////
167
reed5e1ddb12015-12-21 08:52:45 -0800168SkImageFilter* SkDisplacementMapEffect::Create(ChannelSelectorType xChannelSelector,
169 ChannelSelectorType yChannelSelector,
170 SkScalar scale,
171 SkImageFilter* displacement,
172 SkImageFilter* color,
173 const CropRect* cropRect) {
reed9fa60da2014-08-21 07:59:51 -0700174 if (!channel_selector_type_is_valid(xChannelSelector) ||
175 !channel_selector_type_is_valid(yChannelSelector)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700177 }
178
179 SkImageFilter* inputs[2] = { displacement, color };
halcanary385fe4d2015-08-26 13:07:48 -0700180 return new SkDisplacementMapEffect(xChannelSelector, yChannelSelector, scale, inputs, cropRect);
reed9fa60da2014-08-21 07:59:51 -0700181}
182
sugoi@google.com781cc762013-01-15 15:40:19 +0000183SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSelector,
184 ChannelSelectorType yChannelSelector,
185 SkScalar scale,
senorblanco9ea3d572014-07-08 09:16:22 -0700186 SkImageFilter* inputs[2],
senorblanco24e06d52015-03-18 12:11:33 -0700187 const CropRect* cropRect)
188 : INHERITED(2, inputs, cropRect)
sugoi@google.com781cc762013-01-15 15:40:19 +0000189 , fXChannelSelector(xChannelSelector)
190 , fYChannelSelector(yChannelSelector)
191 , fScale(scale)
192{
193}
194
195SkDisplacementMapEffect::~SkDisplacementMapEffect() {
196}
197
reed9fa60da2014-08-21 07:59:51 -0700198SkFlattenable* SkDisplacementMapEffect::CreateProc(SkReadBuffer& buffer) {
199 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2);
200 ChannelSelectorType xsel = (ChannelSelectorType)buffer.readInt();
201 ChannelSelectorType ysel = (ChannelSelectorType)buffer.readInt();
202 SkScalar scale = buffer.readScalar();
senorblanco24e06d52015-03-18 12:11:33 -0700203 return Create(xsel, ysel, scale, common.getInput(0), common.getInput(1), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -0700204}
sugoi@google.com781cc762013-01-15 15:40:19 +0000205
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000206void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const {
sugoi@google.com781cc762013-01-15 15:40:19 +0000207 this->INHERITED::flatten(buffer);
208 buffer.writeInt((int) fXChannelSelector);
209 buffer.writeInt((int) fYChannelSelector);
210 buffer.writeScalar(fScale);
211}
212
213bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy,
214 const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000215 const Context& ctx,
sugoi@google.com781cc762013-01-15 15:40:19 +0000216 SkBitmap* dst,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000217 SkIPoint* offset) const {
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000218 SkBitmap displ = src, color = src;
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000219 SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0, 0);
senorblancob9519f82015-10-15 12:15:13 -0700220 if (!this->filterInput(1, proxy, src, ctx, &color, &colorOffset) ||
221 !this->filterInput(0, proxy, src, ctx, &displ, &displOffset)) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000222 return false;
223 }
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000224 if ((displ.colorType() != kN32_SkColorType) ||
225 (color.colorType() != kN32_SkColorType)) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000226 return false;
227 }
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000228 SkIRect bounds;
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000229 // Since computeDisplacement does bounds checking on color pixel access, we don't need to pad
230 // the color bitmap to bounds here.
senorblancoafec27f2016-02-16 09:11:18 -0800231 SkIRect srcBounds = color.bounds();
232 srcBounds.offset(colorOffset);
233 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000234 return false;
235 }
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000236 SkIRect displBounds;
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000237 if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &displ)) {
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000238 return false;
239 }
240 if (!bounds.intersect(displBounds)) {
241 return false;
242 }
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000243 SkAutoLockPixels alp_displacement(displ), alp_color(color);
244 if (!displ.getPixels() || !color.getPixels()) {
245 return false;
246 }
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000247
senorblanco1d3ff432015-10-20 10:17:34 -0700248 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
249 if (!device) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000250 return false;
251 }
senorblanco1d3ff432015-10-20 10:17:34 -0700252 *dst = device->accessBitmap(false);
253 SkAutoLockPixels alp_dst(*dst);
sugoi@google.com781cc762013-01-15 15:40:19 +0000254
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000255 SkVector scale = SkVector::Make(fScale, fScale);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000256 ctx.ctm().mapVectors(&scale, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000257 SkIRect colorBounds = bounds;
258 colorBounds.offset(-colorOffset);
259
260 computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst,
261 &displ, colorOffset - displOffset, &color, colorBounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000262
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000263 offset->fX = bounds.left();
264 offset->fY = bounds.top();
sugoi@google.com781cc762013-01-15 15:40:19 +0000265 return true;
266}
267
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000268void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const {
robertphillipsff0ca5e2015-07-22 11:54:44 -0700269 if (this->getColorInput()) {
270 this->getColorInput()->computeFastBounds(src, dst);
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000271 } else {
272 *dst = src;
273 }
senorblanco00502372016-01-21 09:55:47 -0800274 dst->outset(SkScalarAbs(fScale) * SK_ScalarHalf, SkScalarAbs(fScale) * SK_ScalarHalf);
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000275}
276
senorblancodb64af32015-12-09 10:11:43 -0800277void SkDisplacementMapEffect::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
278 SkIRect* dst, MapDirection) const {
279 *dst = src;
senorblanco@chromium.orgd4db6572014-05-07 20:00:04 +0000280 SkVector scale = SkVector::Make(fScale, fScale);
281 ctm.mapVectors(&scale, 1);
senorblanco00502372016-01-21 09:55:47 -0800282 dst->outset(SkScalarCeilToInt(SkScalarAbs(scale.fX) * SK_ScalarHalf),
283 SkScalarCeilToInt(SkScalarAbs(scale.fY) * SK_ScalarHalf));
senorblancodb64af32015-12-09 10:11:43 -0800284}
285
286bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
senorblancod8ff5b32016-01-28 08:23:02 -0800287 SkIRect* dst, MapDirection direction) const {
288 // Recurse only into color input.
robertphillipsff0ca5e2015-07-22 11:54:44 -0700289 if (this->getColorInput()) {
senorblancod8ff5b32016-01-28 08:23:02 -0800290 return this->getColorInput()->filterBounds(src, ctm, dst, direction);
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000291 }
senorblancod8ff5b32016-01-28 08:23:02 -0800292 *dst = src;
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000293 return true;
294}
295
robertphillipsf3f5bad2014-12-19 13:49:15 -0800296#ifndef SK_IGNORE_TO_STRING
297void SkDisplacementMapEffect::toString(SkString* str) const {
298 str->appendf("SkDisplacementMapEffect: (");
299 str->appendf("scale: %f ", fScale);
300 str->appendf("displacement: (");
301 if (this->getDisplacementInput()) {
302 this->getDisplacementInput()->toString(str);
303 }
304 str->appendf(") color: (");
305 if (this->getColorInput()) {
306 this->getColorInput()->toString(str);
307 }
308 str->appendf("))");
309}
310#endif
311
sugoi@google.com781cc762013-01-15 15:40:19 +0000312///////////////////////////////////////////////////////////////////////////////
313
314#if SK_SUPPORT_GPU
egdaniel64c47282015-11-13 06:54:19 -0800315class GrGLDisplacementMapEffect : public GrGLSLFragmentProcessor {
sugoi@google.com781cc762013-01-15 15:40:19 +0000316public:
robertphillips9cdb9922016-02-03 12:25:40 -0800317 void emitCode(EmitArgs&) override;
sugoi@google.com781cc762013-01-15 15:40:19 +0000318
jvanverthcfc18862015-04-28 08:48:20 -0700319 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
sugoi@google.com781cc762013-01-15 15:40:19 +0000320
wangyixb1daa862015-08-18 11:29:31 -0700321protected:
egdaniel018fb622015-10-28 07:26:40 -0700322 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700323
sugoi@google.com781cc762013-01-15 15:40:19 +0000324private:
egdaniel018fb622015-10-28 07:26:40 -0700325 GrGLSLProgramDataManager::UniformHandle fScaleUni;
senorblancod762ca22015-04-07 12:16:55 -0700326 GrTextureDomain::GLDomain fGLDomain;
sugoi@google.com781cc762013-01-15 15:40:19 +0000327
egdaniel64c47282015-11-13 06:54:19 -0800328 typedef GrGLSLFragmentProcessor INHERITED;
sugoi@google.com781cc762013-01-15 15:40:19 +0000329};
330
331///////////////////////////////////////////////////////////////////////////////
332
joshualittb0a8a372014-09-23 09:50:21 -0700333class GrDisplacementMapEffect : public GrFragmentProcessor {
sugoi@google.com781cc762013-01-15 15:40:19 +0000334public:
joshualittb0a8a372014-09-23 09:50:21 -0700335 static GrFragmentProcessor* Create(
336 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
337 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, SkVector scale,
senorblancod762ca22015-04-07 12:16:55 -0700338 GrTexture* displacement, const SkMatrix& offsetMatrix, GrTexture* color,
339 const SkISize& colorDimensions) {
bsalomon4a339522015-10-06 08:40:50 -0700340 return new GrDisplacementMapEffect(xChannelSelector, yChannelSelector, scale, displacement,
341 offsetMatrix, color, colorDimensions);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000342 }
343
sugoi@google.com781cc762013-01-15 15:40:19 +0000344 virtual ~GrDisplacementMapEffect();
345
sugoi@google.com781cc762013-01-15 15:40:19 +0000346 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const
347 { return fXChannelSelector; }
348 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const
349 { return fYChannelSelector; }
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000350 const SkVector& scale() const { return fScale; }
sugoi@google.com781cc762013-01-15 15:40:19 +0000351
mtklein36352bf2015-03-25 18:17:31 -0700352 const char* name() const override { return "DisplacementMap"; }
senorblancod762ca22015-04-07 12:16:55 -0700353 const GrTextureDomain& domain() const { return fDomain; }
sugoi@google.com781cc762013-01-15 15:40:19 +0000354
sugoi@google.com781cc762013-01-15 15:40:19 +0000355private:
egdaniel57d3b032015-11-13 11:57:27 -0800356 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillipsbf536af2016-02-04 06:11:53 -0800357 return new GrGLDisplacementMapEffect;
wangyixb1daa862015-08-18 11:29:31 -0700358 }
359
robertphillipsbf536af2016-02-04 06:11:53 -0800360 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -0700361 GrGLDisplacementMapEffect::GenKey(*this, caps, b);
362 }
363
mtklein36352bf2015-03-25 18:17:31 -0700364 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000365
mtklein36352bf2015-03-25 18:17:31 -0700366 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700367
bsalomon4a339522015-10-06 08:40:50 -0700368 GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000369 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000370 const SkVector& scale,
371 GrTexture* displacement, const SkMatrix& offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700372 GrTexture* color,
373 const SkISize& colorDimensions);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000374
joshualittb0a8a372014-09-23 09:50:21 -0700375 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.com781cc762013-01-15 15:40:19 +0000376
bsalomon@google.com77af6802013-10-02 13:04:56 +0000377 GrCoordTransform fDisplacementTransform;
sugoi@google.com781cc762013-01-15 15:40:19 +0000378 GrTextureAccess fDisplacementAccess;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000379 GrCoordTransform fColorTransform;
senorblancod762ca22015-04-07 12:16:55 -0700380 GrTextureDomain fDomain;
sugoi@google.com781cc762013-01-15 15:40:19 +0000381 GrTextureAccess fColorAccess;
382 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
383 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000384 SkVector fScale;
sugoi@google.com781cc762013-01-15 15:40:19 +0000385
joshualittb0a8a372014-09-23 09:50:21 -0700386 typedef GrFragmentProcessor INHERITED;
sugoi@google.com781cc762013-01-15 15:40:19 +0000387};
388
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000389bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000390 SkBitmap* result, SkIPoint* offset) const {
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000391 SkBitmap colorBM = src;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000392 SkIPoint colorOffset = SkIPoint::Make(0, 0);
senorblanco9a70b6e2015-10-16 11:35:14 -0700393 if (!this->filterInputGPU(1, proxy, src, ctx, &colorBM, &colorOffset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000394 return false;
sugoi@google.com781cc762013-01-15 15:40:19 +0000395 }
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000396 SkBitmap displacementBM = src;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000397 SkIPoint displacementOffset = SkIPoint::Make(0, 0);
senorblanco9a70b6e2015-10-16 11:35:14 -0700398 if (!this->filterInputGPU(0, proxy, src, ctx, &displacementBM, &displacementOffset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000399 return false;
400 }
senorblancoafec27f2016-02-16 09:11:18 -0800401 SkIRect srcBounds = colorBM.bounds();
402 srcBounds.offset(colorOffset);
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000403 SkIRect bounds;
404 // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to
405 // pad the color bitmap to bounds here.
senorblancoafec27f2016-02-16 09:11:18 -0800406 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000407 return false;
408 }
409 SkIRect displBounds;
410 if (!this->applyCropRect(ctx, proxy, displacementBM,
411 &displacementOffset, &displBounds, &displacementBM)) {
412 return false;
413 }
414 if (!bounds.intersect(displBounds)) {
415 return false;
416 }
417 GrTexture* color = colorBM.getTexture();
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000418 GrTexture* displacement = displacementBM.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000419 GrContext* context = color->getContext();
sugoi@google.com781cc762013-01-15 15:40:19 +0000420
bsalomonf2703d82014-10-28 14:33:06 -0700421 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800422 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.orgd9cdff62014-03-17 23:41:40 +0000423 desc.fWidth = bounds.width();
424 desc.fHeight = bounds.height();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000425 desc.fConfig = kSkia8888_GrPixelConfig;
sugoi@google.com781cc762013-01-15 15:40:19 +0000426
reed4e23cda2016-01-11 10:56:59 -0800427 SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture(desc));
bsalomone3059732014-10-14 11:47:22 -0700428
429 if (!dst) {
senorblanco673d9732014-08-15 10:48:43 -0700430 return false;
431 }
sugoi@google.com781cc762013-01-15 15:40:19 +0000432
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000433 SkVector scale = SkVector::Make(fScale, fScale);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000434 ctx.ctm().mapVectors(&scale, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000435
436 GrPaint paint;
bsalomon6267f812014-08-29 15:05:53 -0700437 SkMatrix offsetMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(displacement);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000438 offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset.fX),
439 SkIntToScalar(colorOffset.fY - displacementOffset.fY));
440
bsalomonac856c92015-08-27 06:30:17 -0700441 paint.addColorFragmentProcessor(
bsalomon4a339522015-10-06 08:40:50 -0700442 GrDisplacementMapEffect::Create(fXChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000443 fYChannelSelector,
444 scale,
445 displacement,
446 offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700447 color,
448 colorBM.dimensions()))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800449 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000450 SkIRect colorBounds = bounds;
451 colorBounds.offset(-colorOffset);
452 SkMatrix matrix;
453 matrix.setTranslate(-SkIntToScalar(colorBounds.x()),
454 -SkIntToScalar(colorBounds.y()));
robertphillipsea461502015-05-26 11:38:03 -0700455
robertphillips2e1e51f2015-10-15 08:01:48 -0700456 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700457 if (!drawContext) {
458 return false;
459 }
460
robertphillips2e1e51f2015-10-15 08:01:48 -0700461 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, SkRect::Make(colorBounds));
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000462 offset->fX = bounds.left();
463 offset->fY = bounds.top();
robertphillips1de87df2016-01-14 06:03:29 -0800464 GrWrapTextureInBitmap(dst, bounds.width(), bounds.height(), false, result);
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000465 return true;
sugoi@google.com781cc762013-01-15 15:40:19 +0000466}
467
468///////////////////////////////////////////////////////////////////////////////
469
470GrDisplacementMapEffect::GrDisplacementMapEffect(
471 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
472 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000473 const SkVector& scale,
sugoi@google.com781cc762013-01-15 15:40:19 +0000474 GrTexture* displacement,
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000475 const SkMatrix& offsetMatrix,
senorblancod762ca22015-04-07 12:16:55 -0700476 GrTexture* color,
477 const SkISize& colorDimensions)
bsalomon9f876a32014-12-09 10:51:07 -0800478 : fDisplacementTransform(kLocal_GrCoordSet, offsetMatrix, displacement,
479 GrTextureParams::kNone_FilterMode)
bsalomon@google.com77af6802013-10-02 13:04:56 +0000480 , fDisplacementAccess(displacement)
bsalomon9f876a32014-12-09 10:51:07 -0800481 , fColorTransform(kLocal_GrCoordSet, color, GrTextureParams::kNone_FilterMode)
senorblancod762ca22015-04-07 12:16:55 -0700482 , fDomain(GrTextureDomain::MakeTexelDomain(color, SkIRect::MakeSize(colorDimensions)),
483 GrTextureDomain::kDecal_Mode)
sugoi@google.com781cc762013-01-15 15:40:19 +0000484 , fColorAccess(color)
485 , fXChannelSelector(xChannelSelector)
486 , fYChannelSelector(yChannelSelector)
487 , fScale(scale) {
joshualitteb2a6762014-12-04 11:35:33 -0800488 this->initClassID<GrDisplacementMapEffect>();
bsalomon@google.com77af6802013-10-02 13:04:56 +0000489 this->addCoordTransform(&fDisplacementTransform);
sugoi@google.com781cc762013-01-15 15:40:19 +0000490 this->addTextureAccess(&fDisplacementAccess);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000491 this->addCoordTransform(&fColorTransform);
sugoi@google.com781cc762013-01-15 15:40:19 +0000492 this->addTextureAccess(&fColorAccess);
493}
494
495GrDisplacementMapEffect::~GrDisplacementMapEffect() {
496}
497
bsalomon0e08fc12014-10-15 08:19:04 -0700498bool GrDisplacementMapEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700499 const GrDisplacementMapEffect& s = sBase.cast<GrDisplacementMapEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700500 return fXChannelSelector == s.fXChannelSelector &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000501 fYChannelSelector == s.fYChannelSelector &&
502 fScale == s.fScale;
sugoi@google.com781cc762013-01-15 15:40:19 +0000503}
504
egdaniel605dd0f2014-11-12 08:35:25 -0800505void GrDisplacementMapEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
sugoi@google.com781cc762013-01-15 15:40:19 +0000506 // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0),
507 // 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 +0000508 // and no displacement offset push any texture coordinates out of bounds OR if the constant
sugoi@google.com781cc762013-01-15 15:40:19 +0000509 // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is
510 // not of constant color when a displacement effect is applied.
egdaniel605dd0f2014-11-12 08:35:25 -0800511 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
sugoi@google.com781cc762013-01-15 15:40:19 +0000512}
513
514///////////////////////////////////////////////////////////////////////////////
515
joshualittb0a8a372014-09-23 09:50:21 -0700516GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDisplacementMapEffect);
sugoi@google.com781cc762013-01-15 15:40:19 +0000517
bsalomonc21b09e2015-08-28 18:46:56 -0700518const GrFragmentProcessor* GrDisplacementMapEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700519 int texIdxDispl = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
520 GrProcessorUnitTest::kAlphaTextureIdx;
521 int texIdxColor = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
522 GrProcessorUnitTest::kAlphaTextureIdx;
sugoi@google.com781cc762013-01-15 15:40:19 +0000523 static const int kMaxComponent = 4;
524 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector =
525 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
joshualitt0067ff52015-07-08 14:26:19 -0700526 d->fRandom->nextRangeU(1, kMaxComponent));
sugoi@google.com781cc762013-01-15 15:40:19 +0000527 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector =
528 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
joshualitt0067ff52015-07-08 14:26:19 -0700529 d->fRandom->nextRangeU(1, kMaxComponent));
530 SkVector scale = SkVector::Make(d->fRandom->nextRangeScalar(0, 100.0f),
531 d->fRandom->nextRangeScalar(0, 100.0f));
senorblancod762ca22015-04-07 12:16:55 -0700532 SkISize colorDimensions;
joshualitt0067ff52015-07-08 14:26:19 -0700533 colorDimensions.fWidth = d->fRandom->nextRangeU(0, d->fTextures[texIdxColor]->width());
534 colorDimensions.fHeight = d->fRandom->nextRangeU(0, d->fTextures[texIdxColor]->height());
bsalomon4a339522015-10-06 08:40:50 -0700535 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale,
joshualitt0067ff52015-07-08 14:26:19 -0700536 d->fTextures[texIdxDispl], SkMatrix::I(),
537 d->fTextures[texIdxColor], colorDimensions);
sugoi@google.com781cc762013-01-15 15:40:19 +0000538}
539
540///////////////////////////////////////////////////////////////////////////////
541
wangyix7c157a92015-07-22 15:08:53 -0700542void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800543 const GrDisplacementMapEffect& displacementMap = args.fFp.cast<GrDisplacementMapEffect>();
544 const GrTextureDomain& domain = displacementMap.domain();
sugoi@google.com4d94e1b2013-03-21 18:38:03 +0000545
cdalton5e58cee2016-02-11 12:49:47 -0800546 fScaleUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800547 kVec2f_GrSLType, kDefault_GrSLPrecision, "Scale");
548 const char* scaleUni = args.fUniformHandler->getUniformCStr(fScaleUni);
sugoi@google.com781cc762013-01-15 15:40:19 +0000549 const char* dColor = "dColor";
sugoi@google.com781cc762013-01-15 15:40:19 +0000550 const char* cCoords = "cCoords";
551 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use
552 // a number smaller than that to approximate 0, but
553 // leave room for 32-bit float GPU rounding errors.
554
egdaniel4ca2e602015-11-18 08:01:26 -0800555 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
556 fragBuilder->codeAppendf("\t\tvec4 %s = ", dColor);
557 fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(),
wangyix7c157a92015-07-22 15:08:53 -0700558 args.fCoords[0].getType());
egdaniel4ca2e602015-11-18 08:01:26 -0800559 fragBuilder->codeAppend(";\n");
sugoi@google.com781cc762013-01-15 15:40:19 +0000560
561 // Unpremultiply the displacement
egdaniel4ca2e602015-11-18 08:01:26 -0800562 fragBuilder->codeAppendf(
563 "\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);",
564 dColor, dColor, nearZero, dColor, dColor);
565 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 1);
566 fragBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.",
567 cCoords, coords2D.c_str(), scaleUni, dColor);
sugoi@google.com781cc762013-01-15 15:40:19 +0000568
robertphillipsbf536af2016-02-04 06:11:53 -0800569 switch (displacementMap.xChannelSelector()) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000570 case SkDisplacementMapEffect::kR_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800571 fragBuilder->codeAppend("r");
sugoi@google.com781cc762013-01-15 15:40:19 +0000572 break;
573 case SkDisplacementMapEffect::kG_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800574 fragBuilder->codeAppend("g");
sugoi@google.com781cc762013-01-15 15:40:19 +0000575 break;
576 case SkDisplacementMapEffect::kB_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800577 fragBuilder->codeAppend("b");
sugoi@google.com781cc762013-01-15 15:40:19 +0000578 break;
579 case SkDisplacementMapEffect::kA_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800580 fragBuilder->codeAppend("a");
sugoi@google.com781cc762013-01-15 15:40:19 +0000581 break;
582 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
583 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000584 SkDEBUGFAIL("Unknown X channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000585 }
586
robertphillipsbf536af2016-02-04 06:11:53 -0800587 switch (displacementMap.yChannelSelector()) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000588 case SkDisplacementMapEffect::kR_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800589 fragBuilder->codeAppend("r");
sugoi@google.com781cc762013-01-15 15:40:19 +0000590 break;
591 case SkDisplacementMapEffect::kG_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800592 fragBuilder->codeAppend("g");
sugoi@google.com781cc762013-01-15 15:40:19 +0000593 break;
594 case SkDisplacementMapEffect::kB_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800595 fragBuilder->codeAppend("b");
sugoi@google.com781cc762013-01-15 15:40:19 +0000596 break;
597 case SkDisplacementMapEffect::kA_ChannelSelectorType:
egdaniel4ca2e602015-11-18 08:01:26 -0800598 fragBuilder->codeAppend("a");
sugoi@google.com781cc762013-01-15 15:40:19 +0000599 break;
600 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
601 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000602 SkDEBUGFAIL("Unknown Y channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000603 }
egdaniel4ca2e602015-11-18 08:01:26 -0800604 fragBuilder->codeAppend("-vec2(0.5));\t\t");
sugoi@google.com781cc762013-01-15 15:40:19 +0000605
egdaniela2e3e0f2015-11-19 07:23:45 -0800606 fGLDomain.sampleTexture(fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -0800607 args.fUniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -0800608 args.fGLSLCaps,
609 domain,
610 args.fOutputColor,
611 SkString(cCoords),
wangyix7c157a92015-07-22 15:08:53 -0700612 args.fSamplers[1]);
egdaniel4ca2e602015-11-18 08:01:26 -0800613 fragBuilder->codeAppend(";\n");
sugoi@google.com781cc762013-01-15 15:40:19 +0000614}
615
egdaniel018fb622015-10-28 07:26:40 -0700616void GrGLDisplacementMapEffect::onSetData(const GrGLSLProgramDataManager& pdman,
617 const GrProcessor& proc) {
joshualittb0a8a372014-09-23 09:50:21 -0700618 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>();
sugoi@google.com781cc762013-01-15 15:40:19 +0000619 GrTexture* colorTex = displacementMap.texture(1);
reed80ea19c2015-05-12 10:37:34 -0700620 SkScalar scaleX = displacementMap.scale().fX / colorTex->width();
621 SkScalar scaleY = displacementMap.scale().fY / colorTex->height();
kkinnunen7510b222014-07-30 00:04:16 -0700622 pdman.set2f(fScaleUni, SkScalarToFloat(scaleX),
623 colorTex->origin() == kTopLeft_GrSurfaceOrigin ?
624 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
senorblancod762ca22015-04-07 12:16:55 -0700625 fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin());
sugoi@google.com781cc762013-01-15 15:40:19 +0000626}
627
joshualittb0a8a372014-09-23 09:50:21 -0700628void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700629 const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualittb0a8a372014-09-23 09:50:21 -0700630 const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>();
sugoi@google.com781cc762013-01-15 15:40:19 +0000631
bsalomon63e99f72014-07-21 08:03:14 -0700632 uint32_t xKey = displacementMap.xChannelSelector();
633 uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits;
sugoi@google.com781cc762013-01-15 15:40:19 +0000634
bsalomon63e99f72014-07-21 08:03:14 -0700635 b->add32(xKey | yKey);
sugoi@google.com781cc762013-01-15 15:40:19 +0000636}
637#endif
robertphillipsf3f5bad2014-12-19 13:49:15 -0800638