blob: 6e5c910edf4f6f04a670841d2906587e95115675 [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"
9#include "SkFlattenableBuffers.h"
10#include "SkUnPreMultiply.h"
sugoi@google.com8cec63e2013-01-15 15:59:45 +000011#include "SkColorPriv.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000012#if SK_SUPPORT_GPU
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000013#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000014#include "GrCoordTransform.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000015#include "gl/GrGLEffect.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000016#include "GrTBackendEffectFactory.h"
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +000017#include "SkImageFilterUtils.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000018#endif
19
20namespace {
21
22template<SkDisplacementMapEffect::ChannelSelectorType type>
23uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) {
mtklein@google.com330313a2013-08-22 15:37:26 +000024 SkDEBUGFAIL("Unknown channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +000025 return 0;
26}
27
28template<> uint32_t getValue<SkDisplacementMapEffect::kR_ChannelSelectorType>(
29 SkColor l, const SkUnPreMultiply::Scale* table) {
30 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedR32(l));
31}
32
33template<> uint32_t getValue<SkDisplacementMapEffect::kG_ChannelSelectorType>(
34 SkColor l, const SkUnPreMultiply::Scale* table) {
35 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedG32(l));
36}
37
38template<> uint32_t getValue<SkDisplacementMapEffect::kB_ChannelSelectorType>(
39 SkColor l, const SkUnPreMultiply::Scale* table) {
40 return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedB32(l));
41}
42
43template<> uint32_t getValue<SkDisplacementMapEffect::kA_ChannelSelectorType>(
44 SkColor l, const SkUnPreMultiply::Scale*) {
45 return SkGetPackedA32(l);
46}
47
48template<SkDisplacementMapEffect::ChannelSelectorType typeX,
49 SkDisplacementMapEffect::ChannelSelectorType typeY>
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000050void computeDisplacement(SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitmap* src, const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +000051{
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000052 static const SkScalar Inv8bit = SkScalarDiv(SK_Scalar1, 255.0f);
sugoi@google.com781cc762013-01-15 15:40:19 +000053 const int srcW = src->width();
54 const int srcH = src->height();
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000055 const SkScalar scaleForColor = SkScalarMul(scale, Inv8bit);
56 const SkScalar scaleAdj = SK_ScalarHalf - SkScalarMul(scale, SK_ScalarHalf);
sugoi@google.com781cc762013-01-15 15:40:19 +000057 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000058 SkPMColor* dstPtr = dst->getAddr32(0, 0);
59 for (int y = bounds.top(); y < bounds.bottom(); ++y) {
60 const SkPMColor* displPtr = displ->getAddr32(bounds.left(), y);
61 for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) {
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000062 const SkScalar displX = SkScalarMul(scaleForColor,
63 SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj;
64 const SkScalar displY = SkScalarMul(scaleForColor,
65 SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj;
66 // Truncate the displacement values
67 const int srcX = x + SkScalarTruncToInt(displX);
68 const int srcY = y + SkScalarTruncToInt(displY);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000069 *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ?
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000070 0 : *(src->getAddr32(srcX, srcY));
sugoi@google.com781cc762013-01-15 15:40:19 +000071 }
72 }
73}
74
75template<SkDisplacementMapEffect::ChannelSelectorType typeX>
76void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000077 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitmap* src, const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +000078{
79 switch (yChannelSelector) {
80 case SkDisplacementMapEffect::kR_ChannelSelectorType:
81 computeDisplacement<typeX, SkDisplacementMapEffect::kR_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000082 scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +000083 break;
84 case SkDisplacementMapEffect::kG_ChannelSelectorType:
85 computeDisplacement<typeX, SkDisplacementMapEffect::kG_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000086 scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +000087 break;
88 case SkDisplacementMapEffect::kB_ChannelSelectorType:
89 computeDisplacement<typeX, SkDisplacementMapEffect::kB_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000090 scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +000091 break;
92 case SkDisplacementMapEffect::kA_ChannelSelectorType:
93 computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000094 scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +000095 break;
96 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
97 default:
mtklein@google.com330313a2013-08-22 15:37:26 +000098 SkDEBUGFAIL("Unknown Y channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +000099 }
100}
101
102void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
103 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000104 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitmap* src, const SkIRect& bounds)
sugoi@google.com781cc762013-01-15 15:40:19 +0000105{
106 switch (xChannelSelector) {
107 case SkDisplacementMapEffect::kR_ChannelSelectorType:
108 computeDisplacement<SkDisplacementMapEffect::kR_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000109 yChannelSelector, scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000110 break;
111 case SkDisplacementMapEffect::kG_ChannelSelectorType:
112 computeDisplacement<SkDisplacementMapEffect::kG_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000113 yChannelSelector, scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000114 break;
115 case SkDisplacementMapEffect::kB_ChannelSelectorType:
116 computeDisplacement<SkDisplacementMapEffect::kB_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000117 yChannelSelector, scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000118 break;
119 case SkDisplacementMapEffect::kA_ChannelSelectorType:
120 computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>(
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000121 yChannelSelector, scale, dst, displ, src, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000122 break;
123 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
124 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000125 SkDEBUGFAIL("Unknown X channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000126 }
127}
128
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000129bool channel_selector_type_is_valid(SkDisplacementMapEffect::ChannelSelectorType cst) {
130 switch (cst) {
131 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
132 case SkDisplacementMapEffect::kR_ChannelSelectorType:
133 case SkDisplacementMapEffect::kG_ChannelSelectorType:
134 case SkDisplacementMapEffect::kB_ChannelSelectorType:
135 case SkDisplacementMapEffect::kA_ChannelSelectorType:
136 return true;
137 default:
138 break;
139 }
140 return false;
141}
142
sugoi@google.com781cc762013-01-15 15:40:19 +0000143} // end namespace
144
145///////////////////////////////////////////////////////////////////////////////
146
147SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSelector,
148 ChannelSelectorType yChannelSelector,
149 SkScalar scale,
150 SkImageFilter* displacement,
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000151 SkImageFilter* color,
152 const CropRect* cropRect)
153 : INHERITED(displacement, color, cropRect)
sugoi@google.com781cc762013-01-15 15:40:19 +0000154 , fXChannelSelector(xChannelSelector)
155 , fYChannelSelector(yChannelSelector)
156 , fScale(scale)
157{
158}
159
160SkDisplacementMapEffect::~SkDisplacementMapEffect() {
161}
162
163SkDisplacementMapEffect::SkDisplacementMapEffect(SkFlattenableReadBuffer& buffer)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000164 : INHERITED(2, buffer)
sugoi@google.com781cc762013-01-15 15:40:19 +0000165{
166 fXChannelSelector = (SkDisplacementMapEffect::ChannelSelectorType) buffer.readInt();
167 fYChannelSelector = (SkDisplacementMapEffect::ChannelSelectorType) buffer.readInt();
168 fScale = buffer.readScalar();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000169 buffer.validate(channel_selector_type_is_valid(fXChannelSelector) &&
170 channel_selector_type_is_valid(fYChannelSelector) &&
171 SkScalarIsFinite(fScale));
sugoi@google.com781cc762013-01-15 15:40:19 +0000172}
173
174void SkDisplacementMapEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
175 this->INHERITED::flatten(buffer);
176 buffer.writeInt((int) fXChannelSelector);
177 buffer.writeInt((int) fYChannelSelector);
178 buffer.writeScalar(fScale);
179}
180
181bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy,
182 const SkBitmap& src,
183 const SkMatrix& ctm,
184 SkBitmap* dst,
185 SkIPoint* offset) {
186 SkBitmap displ, color = src;
187 SkImageFilter* colorInput = getColorInput();
188 SkImageFilter* displacementInput = getDisplacementInput();
189 SkASSERT(NULL != displacementInput);
190 if ((colorInput && !colorInput->filterImage(proxy, src, ctm, &color, offset)) ||
191 !displacementInput->filterImage(proxy, src, ctm, &displ, offset)) {
192 return false;
193 }
194 if ((displ.config() != SkBitmap::kARGB_8888_Config) ||
195 (color.config() != SkBitmap::kARGB_8888_Config)) {
196 return false;
197 }
198
199 SkAutoLockPixels alp_displacement(displ), alp_color(color);
200 if (!displ.getPixels() || !color.getPixels()) {
201 return false;
202 }
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000203 SkIRect bounds;
204 color.getBounds(&bounds);
205 if (!this->applyCropRect(&bounds, ctm)) {
206 return false;
207 }
208
209 dst->setConfig(color.config(), bounds.width(), bounds.height());
sugoi@google.com781cc762013-01-15 15:40:19 +0000210 dst->allocPixels();
211 if (!dst->getPixels()) {
212 return false;
213 }
214
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000215 computeDisplacement(fXChannelSelector, fYChannelSelector, fScale, dst, &displ, &color, bounds);
sugoi@google.com781cc762013-01-15 15:40:19 +0000216
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000217 offset->fX += bounds.left();
218 offset->fY += bounds.top();
sugoi@google.com781cc762013-01-15 15:40:19 +0000219 return true;
220}
221
222///////////////////////////////////////////////////////////////////////////////
223
224#if SK_SUPPORT_GPU
225class GrGLDisplacementMapEffect : public GrGLEffect {
226public:
227 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000228 const GrDrawEffect& drawEffect);
sugoi@google.com781cc762013-01-15 15:40:19 +0000229 virtual ~GrGLDisplacementMapEffect();
230
231 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000232 const GrDrawEffect&,
sugoi@google.com781cc762013-01-15 15:40:19 +0000233 EffectKey,
sugoi@google.com781cc762013-01-15 15:40:19 +0000234 const char* outputColor,
235 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000236 const TransformedCoordsArray&,
sugoi@google.com781cc762013-01-15 15:40:19 +0000237 const TextureSamplerArray&) SK_OVERRIDE;
238
bsalomon@google.comc7818882013-03-20 19:19:53 +0000239 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
sugoi@google.com781cc762013-01-15 15:40:19 +0000240
bsalomon@google.comc7818882013-03-20 19:19:53 +0000241 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
sugoi@google.com781cc762013-01-15 15:40:19 +0000242
243private:
244 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
245 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
sugoi@google.com781cc762013-01-15 15:40:19 +0000246 GrGLUniformManager::UniformHandle fScaleUni;
sugoi@google.com781cc762013-01-15 15:40:19 +0000247
248 typedef GrGLEffect INHERITED;
249};
250
251///////////////////////////////////////////////////////////////////////////////
252
253class GrDisplacementMapEffect : public GrEffect {
254public:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000255 static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
256 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
257 SkScalar scale, GrTexture* displacement, GrTexture* color) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000258 AutoEffectUnref effect(SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSelector,
259 yChannelSelector,
260 scale,
261 displacement,
262 color)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000263 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000264 }
265
sugoi@google.com781cc762013-01-15 15:40:19 +0000266 virtual ~GrDisplacementMapEffect();
267
bsalomon@google.comc7818882013-03-20 19:19:53 +0000268 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
sugoi@google.com781cc762013-01-15 15:40:19 +0000269 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const
270 { return fXChannelSelector; }
271 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const
272 { return fYChannelSelector; }
273 SkScalar scale() const { return fScale; }
274
275 typedef GrGLDisplacementMapEffect GLEffect;
276 static const char* Name() { return "DisplacementMap"; }
277
bsalomon@google.comc7818882013-03-20 19:19:53 +0000278 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
sugoi@google.com781cc762013-01-15 15:40:19 +0000279
280private:
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000281 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000282
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000283 GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
284 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
285 SkScalar scale, GrTexture* displacement, GrTexture* color);
286
sugoi@google.com781cc762013-01-15 15:40:19 +0000287 GR_DECLARE_EFFECT_TEST;
288
bsalomon@google.com77af6802013-10-02 13:04:56 +0000289 GrCoordTransform fDisplacementTransform;
sugoi@google.com781cc762013-01-15 15:40:19 +0000290 GrTextureAccess fDisplacementAccess;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000291 GrCoordTransform fColorTransform;
sugoi@google.com781cc762013-01-15 15:40:19 +0000292 GrTextureAccess fColorAccess;
293 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
294 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
295 SkScalar fScale;
296
297 typedef GrEffect INHERITED;
298};
299
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +0000300bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
301 SkBitmap* result, SkIPoint* offset) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000302 SkBitmap colorBM;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000303 SkIPoint colorOffset = SkIPoint::Make(0, 0);
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +0000304 if (!SkImageFilterUtils::GetInputResultGPU(getColorInput(), proxy, src, ctm, &colorBM,
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000305 &colorOffset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000306 return false;
sugoi@google.com781cc762013-01-15 15:40:19 +0000307 }
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000308 GrTexture* color = colorBM.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000309 SkBitmap displacementBM;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000310 SkIPoint displacementOffset = SkIPoint::Make(0, 0);
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +0000311 if (!SkImageFilterUtils::GetInputResultGPU(getDisplacementInput(), proxy, src, ctm,
312 &displacementBM, &displacementOffset)) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000313 return false;
314 }
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000315 GrTexture* displacement = displacementBM.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000316 GrContext* context = color->getContext();
sugoi@google.com781cc762013-01-15 15:40:19 +0000317
318 GrTextureDesc desc;
319 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000320 desc.fWidth = src.width();
321 desc.fHeight = src.height();
322 desc.fConfig = kSkia8888_GrPixelConfig;
sugoi@google.com781cc762013-01-15 15:40:19 +0000323
324 GrAutoScratchTexture ast(context, desc);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000325 SkAutoTUnref<GrTexture> dst(ast.detach());
sugoi@google.com781cc762013-01-15 15:40:19 +0000326
327 GrContext::AutoRenderTarget art(context, dst->asRenderTarget());
sugoi@google.com781cc762013-01-15 15:40:19 +0000328
329 GrPaint paint;
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000330 paint.addColorEffect(
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000331 GrDisplacementMapEffect::Create(fXChannelSelector,
332 fYChannelSelector,
333 fScale,
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000334 displacement,
335 color))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000336 SkIRect bounds;
337 src.getBounds(&bounds);
338 if (!this->applyCropRect(&bounds, ctm)) {
339 return false;
340 }
341 SkRect srcRect = SkRect::Make(bounds);
342 SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
343 context->drawRectToRect(paint, dstRect, srcRect);
344 offset->fX += bounds.left();
345 offset->fY += bounds.top();
346 return SkImageFilterUtils::WrapTexture(dst, bounds.width(), bounds.height(), result);
sugoi@google.com781cc762013-01-15 15:40:19 +0000347}
348
349///////////////////////////////////////////////////////////////////////////////
350
351GrDisplacementMapEffect::GrDisplacementMapEffect(
352 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
353 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
354 SkScalar scale,
355 GrTexture* displacement,
356 GrTexture* color)
bsalomon@google.com77af6802013-10-02 13:04:56 +0000357 : fDisplacementTransform(kLocal_GrCoordSet, displacement)
358 , fDisplacementAccess(displacement)
359 , fColorTransform(kLocal_GrCoordSet, color)
sugoi@google.com781cc762013-01-15 15:40:19 +0000360 , fColorAccess(color)
361 , fXChannelSelector(xChannelSelector)
362 , fYChannelSelector(yChannelSelector)
363 , fScale(scale) {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000364 this->addCoordTransform(&fDisplacementTransform);
sugoi@google.com781cc762013-01-15 15:40:19 +0000365 this->addTextureAccess(&fDisplacementAccess);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000366 this->addCoordTransform(&fColorTransform);
sugoi@google.com781cc762013-01-15 15:40:19 +0000367 this->addTextureAccess(&fColorAccess);
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000368 this->setWillNotUseInputColor();
sugoi@google.com781cc762013-01-15 15:40:19 +0000369}
370
371GrDisplacementMapEffect::~GrDisplacementMapEffect() {
372}
373
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000374bool GrDisplacementMapEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000375 const GrDisplacementMapEffect& s = CastEffect<GrDisplacementMapEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000376 return fDisplacementAccess.getTexture() == s.fDisplacementAccess.getTexture() &&
377 fColorAccess.getTexture() == s.fColorAccess.getTexture() &&
378 fXChannelSelector == s.fXChannelSelector &&
379 fYChannelSelector == s.fYChannelSelector &&
380 fScale == s.fScale;
sugoi@google.com781cc762013-01-15 15:40:19 +0000381}
382
383const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const {
384 return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance();
385}
386
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000387void GrDisplacementMapEffect::getConstantColorComponents(GrColor*,
sugoi@google.com781cc762013-01-15 15:40:19 +0000388 uint32_t* validFlags) const {
389 // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0),
390 // 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 +0000391 // and no displacement offset push any texture coordinates out of bounds OR if the constant
sugoi@google.com781cc762013-01-15 15:40:19 +0000392 // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is
393 // not of constant color when a displacement effect is applied.
394 *validFlags = 0;
395}
396
397///////////////////////////////////////////////////////////////////////////////
398
399GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect);
400
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000401GrEffectRef* GrDisplacementMapEffect::TestCreate(SkRandom* random,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000402 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000403 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000404 GrTexture* textures[]) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000405 int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
406 GrEffectUnitTest::kAlphaTextureIdx;
407 int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
408 GrEffectUnitTest::kAlphaTextureIdx;
409 static const int kMaxComponent = 4;
410 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector =
411 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
412 random->nextRangeU(1, kMaxComponent));
413 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector =
414 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
415 random->nextRangeU(1, kMaxComponent));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000416 SkScalar scale = random->nextRangeScalar(0, 100.0f);
sugoi@google.com781cc762013-01-15 15:40:19 +0000417
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000418 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale,
419 textures[texIdxDispl], textures[texIdxColor]);
sugoi@google.com781cc762013-01-15 15:40:19 +0000420}
421
422///////////////////////////////////////////////////////////////////////////////
423
bsalomon@google.com6340a412013-01-22 19:55:59 +0000424GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000425 const GrDrawEffect& drawEffect)
sugoi@google.com781cc762013-01-15 15:40:19 +0000426 : INHERITED(factory)
bsalomon@google.comc7818882013-03-20 19:19:53 +0000427 , fXChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().xChannelSelector())
bsalomon@google.com77af6802013-10-02 13:04:56 +0000428 , fYChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().yChannelSelector()) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000429}
430
431GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() {
432}
433
434void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000435 const GrDrawEffect&,
436 EffectKey key,
437 const char* outputColor,
438 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000439 const TransformedCoordsArray& coords,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000440 const TextureSamplerArray& samplers) {
sugoi@google.com4d94e1b2013-03-21 18:38:03 +0000441 sk_ignore_unused_variable(inputColor);
442
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000443 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
sugoi@google.com781cc762013-01-15 15:40:19 +0000444 kVec2f_GrSLType, "Scale");
445 const char* scaleUni = builder->getUniformCStr(fScaleUni);
sugoi@google.com781cc762013-01-15 15:40:19 +0000446 const char* dColor = "dColor";
sugoi@google.com781cc762013-01-15 15:40:19 +0000447 const char* cCoords = "cCoords";
sugoi@google.com4d94e1b2013-03-21 18:38:03 +0000448 const char* outOfBounds = "outOfBounds";
sugoi@google.com781cc762013-01-15 15:40:19 +0000449 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use
450 // a number smaller than that to approximate 0, but
451 // leave room for 32-bit float GPU rounding errors.
452
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000453 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000454 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000455 builder->fsCodeAppend(";\n");
sugoi@google.com781cc762013-01-15 15:40:19 +0000456
457 // Unpremultiply the displacement
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000458 builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);",
459 dColor, dColor, nearZero, dColor, dColor);
sugoi@google.com781cc762013-01-15 15:40:19 +0000460
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000461 builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.",
bsalomon@google.com77af6802013-10-02 13:04:56 +0000462 cCoords, coords[1].c_str(), scaleUni, dColor);
sugoi@google.com781cc762013-01-15 15:40:19 +0000463
464 switch (fXChannelSelector) {
465 case SkDisplacementMapEffect::kR_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000466 builder->fsCodeAppend("r");
sugoi@google.com781cc762013-01-15 15:40:19 +0000467 break;
468 case SkDisplacementMapEffect::kG_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000469 builder->fsCodeAppend("g");
sugoi@google.com781cc762013-01-15 15:40:19 +0000470 break;
471 case SkDisplacementMapEffect::kB_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000472 builder->fsCodeAppend("b");
sugoi@google.com781cc762013-01-15 15:40:19 +0000473 break;
474 case SkDisplacementMapEffect::kA_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000475 builder->fsCodeAppend("a");
sugoi@google.com781cc762013-01-15 15:40:19 +0000476 break;
477 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
478 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000479 SkDEBUGFAIL("Unknown X channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000480 }
481
482 switch (fYChannelSelector) {
483 case SkDisplacementMapEffect::kR_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000484 builder->fsCodeAppend("r");
sugoi@google.com781cc762013-01-15 15:40:19 +0000485 break;
486 case SkDisplacementMapEffect::kG_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000487 builder->fsCodeAppend("g");
sugoi@google.com781cc762013-01-15 15:40:19 +0000488 break;
489 case SkDisplacementMapEffect::kB_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000490 builder->fsCodeAppend("b");
sugoi@google.com781cc762013-01-15 15:40:19 +0000491 break;
492 case SkDisplacementMapEffect::kA_ChannelSelectorType:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000493 builder->fsCodeAppend("a");
sugoi@google.com781cc762013-01-15 15:40:19 +0000494 break;
495 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
496 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000497 SkDEBUGFAIL("Unknown Y channel selector");
sugoi@google.com781cc762013-01-15 15:40:19 +0000498 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000499 builder->fsCodeAppend("-vec2(0.5));\t\t");
sugoi@google.com781cc762013-01-15 15:40:19 +0000500
501 // FIXME : This can be achieved with a "clamp to border" texture repeat mode and
502 // a 0 border color instead of computing if cCoords is out of bounds here.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000503 builder->fsCodeAppendf(
sugoi@google.com4d94e1b2013-03-21 18:38:03 +0000504 "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);\t\t",
505 outOfBounds, cCoords, cCoords, cCoords, cCoords);
506 builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000507 builder->fsAppendTextureLookup(samplers[1], cCoords, coords[1].type());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000508 builder->fsCodeAppend(";\n");
sugoi@google.com781cc762013-01-15 15:40:19 +0000509}
510
bsalomon@google.comc7818882013-03-20 19:19:53 +0000511void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman,
512 const GrDrawEffect& drawEffect) {
513 const GrDisplacementMapEffect& displacementMap =
514 drawEffect.castEffect<GrDisplacementMapEffect>();
sugoi@google.com781cc762013-01-15 15:40:19 +0000515 GrTexture* colorTex = displacementMap.texture(1);
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000516 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTex->width()));
517 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTex->height()));
518 uman.set2f(fScaleUni, SkScalarToFloat(scaleX),
519 colorTex->origin() == kTopLeft_GrSurfaceOrigin ?
520 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
sugoi@google.com781cc762013-01-15 15:40:19 +0000521}
522
bsalomon@google.comc7818882013-03-20 19:19:53 +0000523GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrDrawEffect& drawEffect,
sugoi@google.com781cc762013-01-15 15:40:19 +0000524 const GrGLCaps&) {
525 const GrDisplacementMapEffect& displacementMap =
bsalomon@google.comc7818882013-03-20 19:19:53 +0000526 drawEffect.castEffect<GrDisplacementMapEffect>();
sugoi@google.com781cc762013-01-15 15:40:19 +0000527
bsalomon@google.com77af6802013-10-02 13:04:56 +0000528 EffectKey xKey = displacementMap.xChannelSelector();
529 EffectKey yKey = displacementMap.yChannelSelector() << SkDisplacementMapEffect::kKeyBits;
sugoi@google.com781cc762013-01-15 15:40:19 +0000530
bsalomon@google.com77af6802013-10-02 13:04:56 +0000531 return xKey | yKey;
sugoi@google.com781cc762013-01-15 15:40:19 +0000532}
533#endif