Add morphology, convolution, single texture, texture domain effects to new unit test system
Review URL: http://codereview.appspot.com/6442085/
git-svn-id: http://skia.googlecode.com/svn/trunk@4951 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkRandom.h b/include/core/SkRandom.h
index b1b7564..c033f40 100644
--- a/include/core/SkRandom.h
+++ b/include/core/SkRandom.h
@@ -81,6 +81,13 @@
SkScalar nextUScalar1() { return SkFixedToScalar(this->nextUFixed1()); }
/** Return the next pseudo random number expressed as a SkScalar
+ in the range [min..max).
+ */
+ SkScalar nextRangeScalar(SkScalar min, SkScalar max) {
+ return SkScalarMul(this->nextSScalar1(), (max - min)) + min;
+ }
+
+ /** Return the next pseudo random number expressed as a SkScalar
in the range (-SK_Scalar1..SK_Scalar1).
*/
SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
diff --git a/src/gpu/effects/GrColorTableEffect.cpp b/src/gpu/effects/GrColorTableEffect.cpp
index 94eb8fd..c6ce559 100644
--- a/src/gpu/effects/GrColorTableEffect.cpp
+++ b/src/gpu/effects/GrColorTableEffect.cpp
@@ -124,3 +124,13 @@
return NULL;
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GR_DEFINE_CUSTOM_STAGE_TEST(GrColorTableEffect);
+
+GrCustomStage* GrColorTableEffect::TestCreate(SkRandom* random,
+ GrContext* context,
+ GrTexture* textures[]) {
+ return SkNEW_ARGS(GrColorTableEffect, (textures[GrCustomStageTestFactory::kAlphaTextureIdx]));
+}
diff --git a/src/gpu/effects/GrColorTableEffect.h b/src/gpu/effects/GrColorTableEffect.h
index 16d76ad..de86768 100644
--- a/src/gpu/effects/GrColorTableEffect.h
+++ b/src/gpu/effects/GrColorTableEffect.h
@@ -34,6 +34,7 @@
typedef GrGLColorTableEffect GLProgramStage;
private:
+ GR_DECLARE_CUSTOM_STAGE_TEST;
GrTextureAccess fTextureAccess;
diff --git a/src/gpu/effects/GrConvolutionEffect.cpp b/src/gpu/effects/GrConvolutionEffect.cpp
index 5cce800..a28d44d 100644
--- a/src/gpu/effects/GrConvolutionEffect.cpp
+++ b/src/gpu/effects/GrConvolutionEffect.cpp
@@ -22,7 +22,7 @@
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* builder,
- const char* vertexCoords) SK_OVERRIDE;
+ const char* vertexCoords) SK_OVERRIDE {};
virtual void emitFS(GrGLShaderBuilder* builder,
const char* outputColor,
const char* inputColor,
@@ -56,20 +56,12 @@
}
void GrGLConvolutionEffect::setupVariables(GrGLShaderBuilder* builder) {
- fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
- GrGLShaderBuilder::kVertex_ShaderType,
+ fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec2f_GrSLType, "ImageIncrement");
fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType,
kFloat_GrSLType, "Kernel", this->width());
}
-void GrGLConvolutionEffect::emitVS(GrGLShaderBuilder* builder,
- const char* vertexCoords) {
- SkString* code = &builder->fVSCode;
- const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
- code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n", vertexCoords, fRadius, fRadius, imgInc);
-}
-
void GrGLConvolutionEffect::emitFS(GrGLShaderBuilder* builder,
const char* outputColor,
const char* inputColor,
@@ -77,12 +69,14 @@
SkString* code = &builder->fFSCode;
code->appendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor);
-
- code->appendf("\t\tvec2 coord = %s;\n", builder->fSampleCoords.c_str());
int width = this ->width();
const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni);
const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
+
+ code->appendf("\t\tvec2 coord = %s - %d * %s;\n",
+ builder->fSampleCoords.c_str(), fRadius, imgInc);
+
// Manually unroll loop because some drivers don't; yields 20-30% speedup.
for (int i = 0; i < width; i++) {
SkString index;
@@ -185,3 +179,23 @@
this->direction() == s.direction() &&
0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GR_DEFINE_CUSTOM_STAGE_TEST(GrConvolutionEffect);
+
+GrCustomStage* GrConvolutionEffect::TestCreate(SkRandom* random,
+ GrContext* context,
+ GrTexture* textures[]) {
+ int texIdx = random->nextBool() ? GrCustomStageTestFactory::kSkiaPMTextureIdx :
+ GrCustomStageTestFactory::kAlphaTextureIdx;
+ Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
+ int radius = random->nextRangeU(1, kMaxKernelRadius);
+ float kernel[kMaxKernelRadius];
+ for (int i = 0; i < kMaxKernelRadius; ++i) {
+ kernel[i] = random->nextSScalar1();
+ }
+
+ return SkNEW_ARGS(GrConvolutionEffect, (textures[texIdx], dir, radius, kernel));
+}
+
diff --git a/src/gpu/effects/GrConvolutionEffect.h b/src/gpu/effects/GrConvolutionEffect.h
index e04e802..21c022d 100644
--- a/src/gpu/effects/GrConvolutionEffect.h
+++ b/src/gpu/effects/GrConvolutionEffect.h
@@ -55,6 +55,7 @@
float fKernel[kMaxKernelWidth];
private:
+ GR_DECLARE_CUSTOM_STAGE_TEST;
typedef Gr1DKernelEffect INHERITED;
};
diff --git a/src/gpu/effects/GrMorphologyEffect.cpp b/src/gpu/effects/GrMorphologyEffect.cpp
index be8485e..a4c5ef9 100644
--- a/src/gpu/effects/GrMorphologyEffect.cpp
+++ b/src/gpu/effects/GrMorphologyEffect.cpp
@@ -20,7 +20,7 @@
virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
virtual void emitVS(GrGLShaderBuilder* state,
- const char* vertexCoords) SK_OVERRIDE;
+ const char* vertexCoords) SK_OVERRIDE {};
virtual void emitFS(GrGLShaderBuilder* state,
const char* outputColor,
const char* inputColor,
@@ -53,18 +53,10 @@
}
void GrGLMorphologyEffect::setupVariables(GrGLShaderBuilder* builder) {
- fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType |
- GrGLShaderBuilder::kVertex_ShaderType,
+ fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec2f_GrSLType, "ImageIncrement");
}
-void GrGLMorphologyEffect::emitVS(GrGLShaderBuilder* builder,
- const char* vertexCoords) {
- SkString* code = &builder->fVSCode;
- const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
- code->appendf("\t\t%s -= vec2(%d, %d) * %s;\n", vertexCoords, fRadius, fRadius, imgInc);
-}
-
void GrGLMorphologyEffect ::emitFS(GrGLShaderBuilder* builder,
const char* outputColor,
const char* inputColor,
@@ -88,7 +80,8 @@
}
const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
- code->appendf("\t\tvec2 coord = %s;\n", builder->fSampleCoords.c_str());
+ code->appendf("\t\tvec2 coord = %s - %d * %s;\n",
+ builder->fSampleCoords.c_str(), fRadius, imgInc);
code->appendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width());
code->appendf("\t\t\tvalue = %s(value, ", func);
builder->emitTextureLookup(samplerName, "coord");
@@ -155,3 +148,21 @@
this->direction() == s.direction() &&
this->type() == s.type());
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GR_DEFINE_CUSTOM_STAGE_TEST(GrMorphologyEffect);
+
+GrCustomStage* GrMorphologyEffect::TestCreate(SkRandom* random,
+ GrContext* context,
+ GrTexture* textures[]) {
+ int texIdx = random->nextBool() ? GrCustomStageTestFactory::kSkiaPMTextureIdx :
+ GrCustomStageTestFactory::kAlphaTextureIdx;
+ Direction dir = random->nextBool() ? kX_Direction : kY_Direction;
+ static const int kMaxRadius = 10;
+ int radius = random->nextRangeU(1, kMaxRadius);
+ MorphologyType type = random->nextBool() ? GrContext::kErode_MorphologyType :
+ GrContext::kDilate_MorphologyType;
+
+ return SkNEW_ARGS(GrMorphologyEffect, (textures[texIdx], dir, radius, type));
+}
diff --git a/src/gpu/effects/GrMorphologyEffect.h b/src/gpu/effects/GrMorphologyEffect.h
index c8bd2d5..bb65de7 100644
--- a/src/gpu/effects/GrMorphologyEffect.h
+++ b/src/gpu/effects/GrMorphologyEffect.h
@@ -42,6 +42,7 @@
MorphologyType fType;
private:
+ GR_DECLARE_CUSTOM_STAGE_TEST;
typedef Gr1DKernelEffect INHERITED;
};
diff --git a/src/gpu/effects/GrSingleTextureEffect.cpp b/src/gpu/effects/GrSingleTextureEffect.cpp
index 5f66e09..ebd0913 100644
--- a/src/gpu/effects/GrSingleTextureEffect.cpp
+++ b/src/gpu/effects/GrSingleTextureEffect.cpp
@@ -56,3 +56,15 @@
const GrProgramStageFactory& GrSingleTextureEffect::getFactory() const {
return GrTProgramStageFactory<GrSingleTextureEffect>::getInstance();
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GR_DEFINE_CUSTOM_STAGE_TEST(GrSingleTextureEffect);
+
+GrCustomStage* GrSingleTextureEffect::TestCreate(SkRandom* random,
+ GrContext* context,
+ GrTexture* textures[]) {
+ int texIdx = random->nextBool() ? GrCustomStageTestFactory::kSkiaPMTextureIdx :
+ GrCustomStageTestFactory::kAlphaTextureIdx;
+ return SkNEW_ARGS(GrSingleTextureEffect, (textures[texIdx]));
+}
diff --git a/src/gpu/effects/GrSingleTextureEffect.h b/src/gpu/effects/GrSingleTextureEffect.h
index c49dac8..211319c 100644
--- a/src/gpu/effects/GrSingleTextureEffect.h
+++ b/src/gpu/effects/GrSingleTextureEffect.h
@@ -31,6 +31,8 @@
virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
private:
+ GR_DECLARE_CUSTOM_STAGE_TEST;
+
GrTexture* fTexture;
typedef GrCustomStage INHERITED;
diff --git a/src/gpu/effects/GrTextureDomainEffect.cpp b/src/gpu/effects/GrTextureDomainEffect.cpp
index e76d0c9..6b22690 100644
--- a/src/gpu/effects/GrTextureDomainEffect.cpp
+++ b/src/gpu/effects/GrTextureDomainEffect.cpp
@@ -107,3 +107,20 @@
const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+GR_DEFINE_CUSTOM_STAGE_TEST(GrTextureDomainEffect);
+
+GrCustomStage* GrTextureDomainEffect::TestCreate(SkRandom* random,
+ GrContext* context,
+ GrTexture* textures[]) {
+ int texIdx = random->nextBool() ? GrCustomStageTestFactory::kSkiaPMTextureIdx :
+ GrCustomStageTestFactory::kAlphaTextureIdx;
+ GrRect domain;
+ domain.fLeft = random->nextUScalar1();
+ domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
+ domain.fTop = random->nextUScalar1();
+ domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
+ return SkNEW_ARGS(GrTextureDomainEffect, (textures[texIdx], domain));
+}
diff --git a/src/gpu/effects/GrTextureDomainEffect.h b/src/gpu/effects/GrTextureDomainEffect.h
index 6157175..559398e 100644
--- a/src/gpu/effects/GrTextureDomainEffect.h
+++ b/src/gpu/effects/GrTextureDomainEffect.h
@@ -38,6 +38,7 @@
GrRect fTextureDomain;
private:
+ GR_DECLARE_CUSTOM_STAGE_TEST;
typedef GrSingleTextureEffect INHERITED;
};
diff --git a/tests/GLProgramsTest.cpp b/tests/GLProgramsTest.cpp
index 199cc49..1228b4e 100644
--- a/tests/GLProgramsTest.cpp
+++ b/tests/GLProgramsTest.cpp
@@ -13,9 +13,6 @@
#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
#include "gl/GrGpuGL.h"
-#include "effects/GrColorTableEffect.h"
-#include "effects/GrConvolutionEffect.h"
-#include "effects/GrMorphologyEffect.h"
#include "SkLightingImageFilter.h"
#include "GrProgramStageFactory.h"
#include "GrRandom.h"
@@ -51,9 +48,6 @@
GrContext* context,
GrTexture* dummyTextures[]) {
enum EffectType {
- kConvolution_EffectType,
- kErode_EffectType,
- kDilate_EffectType,
/**
* Lighting effects don't work in unit test because they assume they insert functions and
* assume the names are unique. This breaks when there are two light effects in the same
@@ -68,8 +62,6 @@
kSpecularSpot_EffectType,
*/
- kColorTable_EffectType,
-
kEffectCount
};
@@ -79,11 +71,6 @@
StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
- static const Gr1DKernelEffect::Direction gKernelDirections[] = {
- Gr1DKernelEffect::kX_Direction,
- Gr1DKernelEffect::kY_Direction
- };
-
// The new code uses SkRandom not GrRandom.
// TODO: Remove GrRandom.
SkRandom sk_random;
@@ -102,48 +89,7 @@
// TODO: When matrices are property of the custom-stage then remove the
// no-persp flag code below.
int effect = random_int(random, kEffectCount);
- switch (effect) {
- case kConvolution_EffectType: {
- int direction = random_int(random, 2);
- int kernelRadius = random_int(random, 1, 4);
- float kernel[GrConvolutionEffect::kMaxKernelWidth];
- for (int i = 0; i < GrConvolutionEffect::kMaxKernelWidth; i++) {
- kernel[i] = random->nextF();
- }
- // does not work with perspective or mul-by-alpha-mask
- stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
- stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
- return SkNEW_ARGS(GrConvolutionEffect,
- (NULL,
- gKernelDirections[direction],
- kernelRadius,
- kernel));
- }
- case kErode_EffectType: {
- int direction = random_int(random, 2);
- int kernelRadius = random_int(random, 1, 4);
- // does not work with perspective or mul-by-alpha-mask
- stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
- stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
- return SkNEW_ARGS(GrMorphologyEffect,
- (NULL,
- gKernelDirections[direction],
- kernelRadius,
- GrContext::kErode_MorphologyType));
- }
- case kDilate_EffectType: {
- int direction = random_int(random, 2);
- int kernelRadius = random_int(random, 1, 4);
- // does not work with perspective or mul-by-alpha-mask
- stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
- stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
- return SkNEW_ARGS(GrMorphologyEffect,
- (NULL,
- gKernelDirections[direction],
- kernelRadius,
- GrContext::kDilate_MorphologyType));
- }
- /*
+/* switch (effect) {
case kDiffuseDistant_EffectType: {
SkPoint3 direction = random_point3(random);
direction.normalize();
@@ -230,14 +176,10 @@
SkASSERT(ok);
return stage;
}
- */
- case kColorTable_EffectType: {
- GrTexture* alphaTexture = dummyTextures[GrCustomStageTestFactory::kAlphaTextureIdx];
- return SkNEW_ARGS(GrColorTableEffect, (alphaTexture));
- }
default:
GrCrash("Unexpected custom effect type");
}
+ */
return NULL;
}
}