blob: 17abe28b185389c0ec76a6ed069d7d0e62548c58 [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
13#include "SkGr.h"
14#include "SkGrPixelRef.h"
15#include "gl/GrGLEffect.h"
16#include "gl/GrGLEffectMatrix.h"
17#include "GrTBackendEffectFactory.h"
18#endif
19
20namespace {
21
22template<SkDisplacementMapEffect::ChannelSelectorType type>
23uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) {
24 SkASSERT(!"Unknown channel selector");
25 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>
50void computeDisplacement(SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitmap* src)
51{
52 static const SkScalar Inv8bit = SkScalarDiv(SK_Scalar1, SkFloatToScalar(255.0f));
53 static const SkScalar Half8bit = SkFloatToScalar(255.0f * 0.5f);
54 const int dstW = displ->width();
55 const int dstH = displ->height();
56 const int srcW = src->width();
57 const int srcH = src->height();
58 const SkScalar scaleX = SkScalarMul(SkScalarMul(scale, SkIntToScalar(dstW)), Inv8bit);
59 const SkScalar scaleY = SkScalarMul(SkScalarMul(scale, SkIntToScalar(dstH)), Inv8bit);
60 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable();
61 for (int y = 0; y < dstH; ++y) {
62 const SkPMColor* displPtr = displ->getAddr32(0, y);
63 SkPMColor* dstPtr = dst->getAddr32(0, y);
64 for (int x = 0; x < dstW; ++x, ++displPtr, ++dstPtr) {
65 const SkScalar displX =
66 SkScalarMul(scaleX, SkIntToScalar(getValue<typeX>(*displPtr, table))-Half8bit);
67 const SkScalar displY =
68 SkScalarMul(scaleY, SkIntToScalar(getValue<typeY>(*displPtr, table))-Half8bit);
69 const int coordX = x + SkScalarRoundToInt(displX);
70 const int coordY = y + SkScalarRoundToInt(displY);
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000071 *dstPtr = ((coordX < 0) || (coordX >= srcW) || (coordY < 0) || (coordY >= srcH)) ?
sugoi@google.com781cc762013-01-15 15:40:19 +000072 0 : *(src->getAddr32(coordX, coordY));
73 }
74 }
75}
76
77template<SkDisplacementMapEffect::ChannelSelectorType typeX>
78void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
79 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitmap* src)
80{
81 switch (yChannelSelector) {
82 case SkDisplacementMapEffect::kR_ChannelSelectorType:
83 computeDisplacement<typeX, SkDisplacementMapEffect::kR_ChannelSelectorType>(
84 scale, dst, displ, src);
85 break;
86 case SkDisplacementMapEffect::kG_ChannelSelectorType:
87 computeDisplacement<typeX, SkDisplacementMapEffect::kG_ChannelSelectorType>(
88 scale, dst, displ, src);
89 break;
90 case SkDisplacementMapEffect::kB_ChannelSelectorType:
91 computeDisplacement<typeX, SkDisplacementMapEffect::kB_ChannelSelectorType>(
92 scale, dst, displ, src);
93 break;
94 case SkDisplacementMapEffect::kA_ChannelSelectorType:
95 computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorType>(
96 scale, dst, displ, src);
97 break;
98 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
99 default:
100 SkASSERT(!"Unknown Y channel selector");
101 }
102}
103
104void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
105 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
106 SkScalar scale, SkBitmap* dst, SkBitmap* displ, SkBitmap* src)
107{
108 switch (xChannelSelector) {
109 case SkDisplacementMapEffect::kR_ChannelSelectorType:
110 computeDisplacement<SkDisplacementMapEffect::kR_ChannelSelectorType>(
111 yChannelSelector, scale, dst, displ, src);
112 break;
113 case SkDisplacementMapEffect::kG_ChannelSelectorType:
114 computeDisplacement<SkDisplacementMapEffect::kG_ChannelSelectorType>(
115 yChannelSelector, scale, dst, displ, src);
116 break;
117 case SkDisplacementMapEffect::kB_ChannelSelectorType:
118 computeDisplacement<SkDisplacementMapEffect::kB_ChannelSelectorType>(
119 yChannelSelector, scale, dst, displ, src);
120 break;
121 case SkDisplacementMapEffect::kA_ChannelSelectorType:
122 computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>(
123 yChannelSelector, scale, dst, displ, src);
124 break;
125 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
126 default:
127 SkASSERT(!"Unknown X channel selector");
128 }
129}
130
131} // end namespace
132
133///////////////////////////////////////////////////////////////////////////////
134
135SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSelector,
136 ChannelSelectorType yChannelSelector,
137 SkScalar scale,
138 SkImageFilter* displacement,
139 SkImageFilter* color)
140 : INHERITED(displacement, color)
141 , fXChannelSelector(xChannelSelector)
142 , fYChannelSelector(yChannelSelector)
143 , fScale(scale)
144{
145}
146
147SkDisplacementMapEffect::~SkDisplacementMapEffect() {
148}
149
150SkDisplacementMapEffect::SkDisplacementMapEffect(SkFlattenableReadBuffer& buffer)
151 : INHERITED(buffer)
152{
153 fXChannelSelector = (SkDisplacementMapEffect::ChannelSelectorType) buffer.readInt();
154 fYChannelSelector = (SkDisplacementMapEffect::ChannelSelectorType) buffer.readInt();
155 fScale = buffer.readScalar();
156}
157
158void SkDisplacementMapEffect::flatten(SkFlattenableWriteBuffer& buffer) const {
159 this->INHERITED::flatten(buffer);
160 buffer.writeInt((int) fXChannelSelector);
161 buffer.writeInt((int) fYChannelSelector);
162 buffer.writeScalar(fScale);
163}
164
165bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy,
166 const SkBitmap& src,
167 const SkMatrix& ctm,
168 SkBitmap* dst,
169 SkIPoint* offset) {
170 SkBitmap displ, color = src;
171 SkImageFilter* colorInput = getColorInput();
172 SkImageFilter* displacementInput = getDisplacementInput();
173 SkASSERT(NULL != displacementInput);
174 if ((colorInput && !colorInput->filterImage(proxy, src, ctm, &color, offset)) ||
175 !displacementInput->filterImage(proxy, src, ctm, &displ, offset)) {
176 return false;
177 }
178 if ((displ.config() != SkBitmap::kARGB_8888_Config) ||
179 (color.config() != SkBitmap::kARGB_8888_Config)) {
180 return false;
181 }
182
183 SkAutoLockPixels alp_displacement(displ), alp_color(color);
184 if (!displ.getPixels() || !color.getPixels()) {
185 return false;
186 }
187 dst->setConfig(displ.config(), displ.width(), displ.height());
188 dst->allocPixels();
189 if (!dst->getPixels()) {
190 return false;
191 }
192
193 computeDisplacement(fXChannelSelector, fYChannelSelector, fScale, dst, &displ, &color);
194
195 return true;
196}
197
198///////////////////////////////////////////////////////////////////////////////
199
200#if SK_SUPPORT_GPU
201class GrGLDisplacementMapEffect : public GrGLEffect {
202public:
203 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory,
204 const GrEffect& effect);
205 virtual ~GrGLDisplacementMapEffect();
206
207 virtual void emitCode(GrGLShaderBuilder*,
208 const GrEffectStage&,
209 EffectKey,
210 const char* vertexCoords,
211 const char* outputColor,
212 const char* inputColor,
213 const TextureSamplerArray&) SK_OVERRIDE;
214
215 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
216
217 virtual void setData(const GrGLUniformManager&, const GrEffectStage&);
218
219private:
220 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
221 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
222 GrGLEffectMatrix fDisplacementEffectMatrix;
223 GrGLEffectMatrix fColorEffectMatrix;
224 GrGLUniformManager::UniformHandle fScaleUni;
225 GrGLUniformManager::UniformHandle fYSignColor;
226 GrGLUniformManager::UniformHandle fYSignDispl;
227
228 typedef GrGLEffect INHERITED;
229};
230
231///////////////////////////////////////////////////////////////////////////////
232
233class GrDisplacementMapEffect : public GrEffect {
234public:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000235 static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
236 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
237 SkScalar scale, GrTexture* displacement, GrTexture* color) {
238 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSelector,
239 yChannelSelector,
240 scale,
241 displacement,
242 color)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000243 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000244 }
245
sugoi@google.com781cc762013-01-15 15:40:19 +0000246 virtual ~GrDisplacementMapEffect();
247
sugoi@google.com781cc762013-01-15 15:40:19 +0000248 const GrBackendEffectFactory& getFactory() const;
249 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const
250 { return fXChannelSelector; }
251 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const
252 { return fYChannelSelector; }
253 SkScalar scale() const { return fScale; }
254
255 typedef GrGLDisplacementMapEffect GLEffect;
256 static const char* Name() { return "DisplacementMap"; }
257
258 void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
259
260private:
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000261 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
262
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000263 GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
264 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
265 SkScalar scale, GrTexture* displacement, GrTexture* color);
266
sugoi@google.com781cc762013-01-15 15:40:19 +0000267 GR_DECLARE_EFFECT_TEST;
268
269 GrTextureAccess fDisplacementAccess;
270 GrTextureAccess fColorAccess;
271 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector;
272 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector;
273 SkScalar fScale;
274
275 typedef GrEffect INHERITED;
276};
277
278// FIXME: This should be refactored with SkSingleInputImageFilter's version.
279static GrTexture* getInputResultAsTexture(SkImageFilter::Proxy* proxy,
280 SkImageFilter* input,
281 GrTexture* src,
282 const SkRect& rect) {
283 GrTexture* resultTex = NULL;
284 if (!input) {
285 resultTex = src;
286 } else if (input->canFilterImageGPU()) {
287 // filterImageGPU() already refs the result, so just return it here.
288 return input->filterImageGPU(proxy, src, rect);
289 } else {
290 SkBitmap srcBitmap, result;
291 srcBitmap.setConfig(SkBitmap::kARGB_8888_Config, src->width(), src->height());
292 srcBitmap.setPixelRef(new SkGrPixelRef(src))->unref();
293 SkIPoint offset;
294 if (input->filterImage(proxy, srcBitmap, SkMatrix(), &result, &offset)) {
295 if (result.getTexture()) {
296 resultTex = (GrTexture*) result.getTexture();
297 } else {
298 resultTex = GrLockCachedBitmapTexture(src->getContext(), result, NULL);
299 SkSafeRef(resultTex);
300 GrUnlockCachedBitmapTexture(resultTex);
301 return resultTex;
302 }
303 } else {
304 resultTex = src;
305 }
306 }
307 SkSafeRef(resultTex);
308 return resultTex;
309}
310
311GrTexture* SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, GrTexture* src,
312 const SkRect& rect) {
313 SkAutoTUnref<GrTexture> color(getInputResultAsTexture(proxy, getColorInput(), src, rect));
314 SkAutoTUnref<GrTexture> displacement(getInputResultAsTexture(proxy, getDisplacementInput(),
315 src, rect));
316 GrContext* context = src->getContext();
317
318 GrTextureDesc desc;
319 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
320 desc.fWidth = SkScalarCeilToInt(rect.width());
321 desc.fHeight = SkScalarCeilToInt(rect.height());
322 desc.fConfig = kRGBA_8888_GrPixelConfig;
323
324 GrAutoScratchTexture ast(context, desc);
325 GrTexture* dst = ast.detach();
326
327 GrContext::AutoMatrix am;
328 am.setIdentity(context);
329
330 GrContext::AutoRenderTarget art(context, dst->asRenderTarget());
331 GrContext::AutoClip ac(context, rect);
332
333 GrPaint paint;
334 paint.colorStage(0)->setEffect(
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000335 GrDisplacementMapEffect::Create(fXChannelSelector,
336 fYChannelSelector,
337 fScale,
338 displacement.get(),
339 color.get()))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000340 context->drawRect(paint, rect);
341 return dst;
342}
343
344///////////////////////////////////////////////////////////////////////////////
345
346GrDisplacementMapEffect::GrDisplacementMapEffect(
347 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector,
348 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector,
349 SkScalar scale,
350 GrTexture* displacement,
351 GrTexture* color)
352 : fDisplacementAccess(displacement)
353 , fColorAccess(color)
354 , fXChannelSelector(xChannelSelector)
355 , fYChannelSelector(yChannelSelector)
356 , fScale(scale) {
357 this->addTextureAccess(&fDisplacementAccess);
358 this->addTextureAccess(&fColorAccess);
359}
360
361GrDisplacementMapEffect::~GrDisplacementMapEffect() {
362}
363
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000364bool GrDisplacementMapEffect::onIsEqual(const GrEffect& sBase) const {
sugoi@google.com781cc762013-01-15 15:40:19 +0000365 const GrDisplacementMapEffect& s = static_cast<const GrDisplacementMapEffect&>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000366 return fDisplacementAccess.getTexture() == s.fDisplacementAccess.getTexture() &&
367 fColorAccess.getTexture() == s.fColorAccess.getTexture() &&
368 fXChannelSelector == s.fXChannelSelector &&
369 fYChannelSelector == s.fYChannelSelector &&
370 fScale == s.fScale;
sugoi@google.com781cc762013-01-15 15:40:19 +0000371}
372
373const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const {
374 return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance();
375}
376
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000377void GrDisplacementMapEffect::getConstantColorComponents(GrColor* color,
sugoi@google.com781cc762013-01-15 15:40:19 +0000378 uint32_t* validFlags) const {
379 // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0),
380 // 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 +0000381 // and no displacement offset push any texture coordinates out of bounds OR if the constant
sugoi@google.com781cc762013-01-15 15:40:19 +0000382 // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is
383 // not of constant color when a displacement effect is applied.
384 *validFlags = 0;
385}
386
387///////////////////////////////////////////////////////////////////////////////
388
389GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect);
390
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000391GrEffectRef* GrDisplacementMapEffect::TestCreate(SkRandom* random,
392 GrContext* context,
393 GrTexture* textures[]) {
sugoi@google.com781cc762013-01-15 15:40:19 +0000394 int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
395 GrEffectUnitTest::kAlphaTextureIdx;
396 int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
397 GrEffectUnitTest::kAlphaTextureIdx;
398 static const int kMaxComponent = 4;
399 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector =
400 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
401 random->nextRangeU(1, kMaxComponent));
402 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector =
403 static_cast<SkDisplacementMapEffect::ChannelSelectorType>(
404 random->nextRangeU(1, kMaxComponent));
405 SkScalar scale = random->nextUScalar1();
406
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000407 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale,
408 textures[texIdxDispl], textures[texIdxColor]);
sugoi@google.com781cc762013-01-15 15:40:19 +0000409}
410
411///////////////////////////////////////////////////////////////////////////////
412
413GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, const GrEffect& effect)
414 : INHERITED(factory)
415 , fXChannelSelector(static_cast<const GrDisplacementMapEffect&>(effect).xChannelSelector())
416 , fYChannelSelector(static_cast<const GrDisplacementMapEffect&>(effect).yChannelSelector()) {
417}
418
419GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() {
420}
421
422void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder,
423 const GrEffectStage&,
424 EffectKey key,
425 const char* vertexCoords,
426 const char* outputColor,
427 const char* inputColor,
428 const TextureSamplerArray& samplers) {
429 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
430 kVec2f_GrSLType, "Scale");
431 const char* scaleUni = builder->getUniformCStr(fScaleUni);
432
433 const char* dCoordsIn;
434 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode(
435 builder, key, vertexCoords, &dCoordsIn, NULL, "DISPL");
436 const char* cCoordsIn;
437 GrSLType cCoordsType = fColorEffectMatrix.emitCode(
438 builder, key, vertexCoords, &cCoordsIn, NULL, "COLOR");
439
440 SkString* code = &builder->fFSCode;
441 const char* dColor = "dColor";
sugoi@google.com781cc762013-01-15 15:40:19 +0000442 const char* cCoords = "cCoords";
443 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use
444 // a number smaller than that to approximate 0, but
445 // leave room for 32-bit float GPU rounding errors.
446
447 code->appendf("\t\tvec4 %s = ", dColor);
448 builder->appendTextureLookup(code, samplers[0], dCoordsIn, dCoordsType);
449 code->append(";\n");
450
451 // Unpremultiply the displacement
452 code->appendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);",
453 dColor, dColor, nearZero, dColor, dColor);
454
455 code->appendf("\t\tvec2 %s = %s + %s*(%s.",
456 cCoords, cCoordsIn, scaleUni, dColor);
457
458 switch (fXChannelSelector) {
459 case SkDisplacementMapEffect::kR_ChannelSelectorType:
460 code->append("r");
461 break;
462 case SkDisplacementMapEffect::kG_ChannelSelectorType:
463 code->append("g");
464 break;
465 case SkDisplacementMapEffect::kB_ChannelSelectorType:
466 code->append("b");
467 break;
468 case SkDisplacementMapEffect::kA_ChannelSelectorType:
469 code->append("a");
470 break;
471 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
472 default:
473 SkASSERT(!"Unknown X channel selector");
474 }
475
476 switch (fYChannelSelector) {
477 case SkDisplacementMapEffect::kR_ChannelSelectorType:
478 code->append("r");
479 break;
480 case SkDisplacementMapEffect::kG_ChannelSelectorType:
481 code->append("g");
482 break;
483 case SkDisplacementMapEffect::kB_ChannelSelectorType:
484 code->append("b");
485 break;
486 case SkDisplacementMapEffect::kA_ChannelSelectorType:
487 code->append("a");
488 break;
489 case SkDisplacementMapEffect::kUnknown_ChannelSelectorType:
490 default:
491 SkASSERT(!"Unknown Y channel selector");
492 }
493 code->append("-vec2(0.5));\t\t");
494
495 // FIXME : This can be achieved with a "clamp to border" texture repeat mode and
496 // a 0 border color instead of computing if cCoords is out of bounds here.
497 code->appendf(
498 "%s = any(greaterThan(vec4(vec2(0.0), %s), vec4(%s, vec2(1.0)))) ? vec4(0.0) : ",
499 outputColor, cCoords, cCoords);
500 builder->appendTextureLookup(code, samplers[1], cCoords, cCoordsType);
501 code->append(";\n");
502}
503
504void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
505 const GrDisplacementMapEffect& displacementMap = static_cast<const GrDisplacementMapEffect&>(*stage.getEffect());
506 GrTexture* displTex = displacementMap.texture(0);
507 GrTexture* colorTex = displacementMap.texture(1);
508 fDisplacementEffectMatrix.setData(uman,
509 GrEffect::MakeDivByTextureWHMatrix(displTex),
510 stage.getCoordChangeMatrix(),
511 displTex);
512 fColorEffectMatrix.setData(uman,
513 GrEffect::MakeDivByTextureWHMatrix(colorTex),
514 stage.getCoordChangeMatrix(),
515 colorTex);
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000516
sugoi@google.com781cc762013-01-15 15:40:19 +0000517 uman.set2f(fScaleUni, SkScalarToFloat(displacementMap.scale()),
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000518 colorTex->origin() == GrSurface::kTopLeft_Origin ?
sugoi@google.com781cc762013-01-15 15:40:19 +0000519 SkScalarToFloat(displacementMap.scale()) :
520 SkScalarToFloat(-displacementMap.scale()));
521}
522
523GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrEffectStage& stage,
524 const GrGLCaps&) {
525 const GrDisplacementMapEffect& displacementMap =
526 static_cast<const GrDisplacementMapEffect&>(*stage.getEffect());
527
528 GrTexture* displTex = displacementMap.texture(0);
529 GrTexture* colorTex = displacementMap.texture(1);
530
531 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMatrix(displTex),
532 stage.getCoordChangeMatrix(),
533 displTex);
534
535 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMatrix(colorTex),
536 stage.getCoordChangeMatrix(),
537 colorTex);
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000538
sugoi@google.com781cc762013-01-15 15:40:19 +0000539 colorKey <<= GrGLEffectMatrix::kKeyBits;
540 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix::kKeyBits);
541 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix::kKeyBits +
542 SkDisplacementMapEffect::kKeyBits);
543
544 return xKey | yKey | displKey | colorKey;
545}
546#endif