Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: src/gpu/effects/GrBicubicEffect.cpp

Issue 99203011: Use GrTextureDomain in GrBicubicEffect to perform non-bleeding HQ filter drawBitmap. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: add initializer list for dummy rectangle to fix warnings Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/gpu/effects/GrBicubicEffect.h ('k') | src/gpu/effects/GrTextureDomain.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "GrBicubicEffect.h" 1 #include "GrBicubicEffect.h"
2 2
3 #define DS(x) SkDoubleToScalar(x) 3 #define DS(x) SkDoubleToScalar(x)
4 4
5 const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = { 5 const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = {
6 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), 6 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0),
7 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), 7 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0),
8 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), 8 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0),
9 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), 9 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0),
10 }; 10 };
11 11
12 12
13 class GrGLBicubicEffect : public GrGLEffect { 13 class GrGLBicubicEffect : public GrGLEffect {
14 public: 14 public:
15 GrGLBicubicEffect(const GrBackendEffectFactory& factory, 15 GrGLBicubicEffect(const GrBackendEffectFactory& factory,
16 const GrDrawEffect&); 16 const GrDrawEffect&);
17
17 virtual void emitCode(GrGLShaderBuilder*, 18 virtual void emitCode(GrGLShaderBuilder*,
18 const GrDrawEffect&, 19 const GrDrawEffect&,
19 EffectKey, 20 EffectKey,
20 const char* outputColor, 21 const char* outputColor,
21 const char* inputColor, 22 const char* inputColor,
22 const TransformedCoordsArray&, 23 const TransformedCoordsArray&,
23 const TextureSamplerArray&) SK_OVERRIDE; 24 const TextureSamplerArray&) SK_OVERRIDE;
24 25
25 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE; 26 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
26 27
28 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap s&) {
29 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>() .domain();
30 return GrTextureDomain::GLDomain::DomainKey(domain);
31 }
32
27 private: 33 private:
28 typedef GrGLUniformManager::UniformHandle UniformHandle; 34 typedef GrGLUniformManager::UniformHandle UniformHandle;
29 35
30 UniformHandle fCoefficientsUni; 36 UniformHandle fCoefficientsUni;
31 UniformHandle fImageIncrementUni; 37 UniformHandle fImageIncrementUni;
38 GrTextureDomain::GLDomain fDomain;
32 39
33 typedef GrGLEffect INHERITED; 40 typedef GrGLEffect INHERITED;
34 }; 41 };
35 42
36 GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, cons t GrDrawEffect&) 43 GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, cons t GrDrawEffect&)
37 : INHERITED(factory) { 44 : INHERITED(factory) {
38 } 45 }
39 46
40 void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, 47 void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
41 const GrDrawEffect&, 48 const GrDrawEffect& drawEffect,
42 EffectKey key, 49 EffectKey key,
43 const char* outputColor, 50 const char* outputColor,
44 const char* inputColor, 51 const char* inputColor,
45 const TransformedCoordsArray& coords, 52 const TransformedCoordsArray& coords,
46 const TextureSamplerArray& samplers) { 53 const TextureSamplerArray& samplers) {
47 sk_ignore_unused_variable(inputColor); 54 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().dom ain();
48 55
49 SkString coords2D = builder->ensureFSCoords2D(coords, 0); 56 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
50 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty, 57 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty,
51 kMat44f_GrSLType, "Coefficients"); 58 kMat44f_GrSLType, "Coefficients");
52 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity, 59 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity,
53 kVec2f_GrSLType, "ImageIncrement"); 60 kVec2f_GrSLType, "ImageIncrement");
54 61
55 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); 62 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
56 const char* coeff = builder->getUniformCStr(fCoefficientsUni); 63 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
57 64
(...skipping 16 matching lines...) Expand all
74 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3; \n", 81 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3; \n",
75 &cubicBlendName); 82 &cubicBlendName);
76 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_s tr(), imgInc); 83 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_s tr(), imgInc);
77 // We unnormalize the coord in order to determine our fractional offset (f) within the texel 84 // We unnormalize the coord in order to determine our fractional offset (f) within the texel
78 // We then snap coord to a texel center and renormalize. The snap prevents c ases where the 85 // We then snap coord to a texel center and renormalize. The snap prevents c ases where the
79 // starting coords are near a texel boundary and accumulations of imgInc wou ld cause us to skip/ 86 // starting coords are near a texel boundary and accumulations of imgInc wou ld cause us to skip/
80 // double hit a texel. 87 // double hit a texel.
81 builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc); 88 builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc);
82 builder->fsCodeAppend("\tvec2 f = fract(coord);\n"); 89 builder->fsCodeAppend("\tvec2 f = fract(coord);\n");
83 builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc); 90 builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc);
91 builder->fsCodeAppend("\tvec4 rowColors[4];\n");
84 for (int y = 0; y < 4; ++y) { 92 for (int y = 0; y < 4; ++y) {
85 for (int x = 0; x < 4; ++x) { 93 for (int x = 0; x < 4; ++x) {
86 SkString coord; 94 SkString coord;
87 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); 95 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
88 builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y); 96 SkString sampleVar;
89 builder->fsAppendTextureLookup(samplers[0], coord.c_str()); 97 sampleVar.printf("rowColors[%d]", x);
90 builder->fsCodeAppend(";\n"); 98 fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, sam plers[0]);
91 } 99 }
92 builder->fsCodeAppendf("\tvec4 s%d = %s(%s, f.x, s0%d, s1%d, s2%d, s3%d) ;\n", y, cubicBlendName.c_str(), coeff, y, y, y, y); 100 builder->fsCodeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors [1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff);
93 } 101 }
94 builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff); 102 SkString bicubicColor;
103 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), c oeff);
104 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColo r.c_str()) * GrGLSLExpr4(inputColor)).c_str());
95 } 105 }
96 106
97 void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, 107 void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
98 const GrDrawEffect& drawEffect) { 108 const GrDrawEffect& drawEffect) {
99 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); 109 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
100 GrTexture& texture = *effect.texture(0); 110 const GrTexture& texture = *effect.texture(0);
101 float imageIncrement[2]; 111 float imageIncrement[2];
102 imageIncrement[0] = 1.0f / texture.width(); 112 imageIncrement[0] = 1.0f / texture.width();
103 imageIncrement[1] = 1.0f / texture.height(); 113 imageIncrement[1] = 1.0f / texture.height();
104 uman.set2fv(fImageIncrementUni, 1, imageIncrement); 114 uman.set2fv(fImageIncrementUni, 1, imageIncrement);
105 uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); 115 uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
116 fDomain.setData(uman, effect.domain(), texture.origin());
117 }
118
119 static inline void convert_row_major_scalar_coeffs_to_column_major_floats(float dst[16],
120 const SkScalar src[16]) {
121 for (int y = 0; y < 4; y++) {
122 for (int x = 0; x < 4; x++) {
123 dst[x * 4 + y] = SkScalarToFloat(src[y * 4 + x]);
124 }
125 }
106 } 126 }
107 127
108 GrBicubicEffect::GrBicubicEffect(GrTexture* texture, 128 GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
109 const SkScalar coefficients[16], 129 const SkScalar coefficients[16],
110 const SkMatrix &matrix, 130 const SkMatrix &matrix,
111 const SkShader::TileMode tileModes[2]) 131 const SkShader::TileMode tileModes[2])
112 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone _FilterMode)) { 132 : INHERITED(texture, matrix, GrTextureParams(tileModes, GrTextureParams::kNone _FilterMode))
113 for (int y = 0; y < 4; y++) { 133 , fDomain(GrTextureDomain::IgnoredDomain()) {
114 for (int x = 0; x < 4; x++) { 134 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coeffi cients);
115 // Convert from row-major scalars to column-major floats. 135 }
116 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]); 136
117 } 137 GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
118 } 138 const SkScalar coefficients[16],
119 this->setWillNotUseInputColor(); 139 const SkMatrix &matrix,
140 const SkRect& domain)
141 : INHERITED(texture, matrix, GrTextureParams(SkShader::kClamp_TileMode,
142 GrTextureParams::kNone_FilterMode ))
143 , fDomain(domain, GrTextureDomain::kClamp_Mode) {
144 convert_row_major_scalar_coeffs_to_column_major_floats(fCoefficients, coeffi cients);
120 } 145 }
121 146
122 GrBicubicEffect::~GrBicubicEffect() { 147 GrBicubicEffect::~GrBicubicEffect() {
123 } 148 }
124 149
125 const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { 150 const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
126 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance(); 151 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
127 } 152 }
128 153
129 bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { 154 bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
130 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase); 155 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
131 return this->textureAccess(0) == s.textureAccess(0) && 156 return this->textureAccess(0) == s.textureAccess(0) &&
132 !memcmp(fCoefficients, s.coefficients(), 16); 157 !memcmp(fCoefficients, s.coefficients(), 16);
133 } 158 }
134 159
135 void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* valid Flags) const { 160 void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* valid Flags) const {
136 // FIXME: Perhaps we can do better. 161 // FIXME: Perhaps we can do better.
137 *validFlags = 0; 162 *validFlags = 0;
138 return; 163 return;
139 } 164 }
140 165
141 GR_DEFINE_EFFECT_TEST(GrBicubicEffect); 166 GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
142 167
143 GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random, 168 GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random,
144 GrContext* context, 169 GrContext* context,
145 const GrDrawTargetCaps&, 170 const GrDrawTargetCaps&,
146 GrTexture* textures[]) { 171 GrTexture* textures[]) {
147 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 172 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
148 GrEffectUnitTest::kAlphaTextureIdx; 173 GrEffectUnitTest::kAlphaTextureIdx;
149 SkScalar coefficients[16]; 174 SkScalar coefficients[16];
150 for (int i = 0; i < 16; i++) { 175 for (int i = 0; i < 16; i++) {
151 coefficients[i] = random->nextSScalar1(); 176 coefficients[i] = random->nextSScalar1();
152 } 177 }
153 return GrBicubicEffect::Create(textures[texIdx], coefficients); 178 return GrBicubicEffect::Create(textures[texIdx], coefficients);
154 } 179 }
OLDNEW
« no previous file with comments | « src/gpu/effects/GrBicubicEffect.h ('k') | src/gpu/effects/GrTextureDomain.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698