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

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

Issue 23779003: first cut at HQ GPU scaling; refactored existing bicubic scaler (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 3 months 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
OLDNEW
(Empty)
1 #include "GrBicubicEffect.h"
2
3 GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory,
4 const GrDrawEffect& drawEffect)
5 : INHERITED(factory)
6 , fEffectMatrix(drawEffect.castEffect<GrBicubicEffect>().coordsType()) {
7 }
8
9 void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder,
10 const GrDrawEffect&,
11 EffectKey key,
12 const char* outputColor,
13 const char* inputColor,
14 const TextureSamplerArray& samplers) {
15 SkString coords;
16 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords);
17 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty,
18 kMat44f_GrSLType, "Coefficients");
19 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity,
20 kVec2f_GrSLType, "ImageIncrement");
21
22 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
23 const char* coeff = builder->getUniformCStr(fCoefficientsUni);
24
25 SkString cubicBlendName;
26
27 static const GrGLShaderVar gCubicBlendArgs[] = {
28 GrGLShaderVar("coefficients", kMat44f_GrSLType),
29 GrGLShaderVar("t", kFloat_GrSLType),
30 GrGLShaderVar("c0", kVec4f_GrSLType),
31 GrGLShaderVar("c1", kVec4f_GrSLType),
32 GrGLShaderVar("c2", kVec4f_GrSLType),
33 GrGLShaderVar("c3", kVec4f_GrSLType),
34 };
35 builder->fsEmitFunction(kVec4f_GrSLType,
36 "cubicBlend",
37 SK_ARRAY_COUNT(gCubicBlendArgs),
38 gCubicBlendArgs,
39 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n"
40 "\tvec4 c = coefficients * ts;\n"
41 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3; \n",
42 &cubicBlendName);
43 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5, 0.5);\n", coords. c_str(), imgInc);
44 builder->fsCodeAppendf("\tvec2 f = fract(coord / %s);\n", imgInc);
45 for (int y = 0; y < 4; ++y) {
46 for (int x = 0; x < 4; ++x) {
47 SkString coord;
48 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1);
49 builder->fsCodeAppendf("\tvec4 s%d%d = ", x, y);
50 builder->fsAppendTextureLookup(samplers[0], coord.c_str());
51 builder->fsCodeAppend(";\n");
52 }
53 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);
54 }
55 builder->fsCodeAppendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff);
56 }
57
58 GrGLEffect::EffectKey GrGLBicubicEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
59 const GrBicubicEffect& bicubic = drawEffect.castEffect<GrBicubicEffect>();
60 EffectKey matrixKey = GrGLEffectMatrix::GenKey(bicubic.getMatrix(),
61 drawEffect,
62 bicubic.coordsType(),
63 bicubic.texture(0));
64 return matrixKey;
65 }
66
67 void GrGLBicubicEffect::setData(const GrGLUniformManager& uman,
68 const GrDrawEffect& drawEffect) {
69 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>();
70 GrTexture& texture = *effect.texture(0);
71 float imageIncrement[2];
72 imageIncrement[0] = 1.0f / texture.width();
73 imageIncrement[1] = 1.0f / texture.height();
74 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement);
75 uman.setMatrix4f(fCoefficientsUni, effect.coefficients());
76 fEffectMatrix.setData(uman,
77 effect.getMatrix(),
78 drawEffect,
79 effect.texture(0));
80 }
81
82 GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
83 const SkScalar coefficients[16])
84 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) {
85 for (int y = 0; y < 4; y++) {
86 for (int x = 0; x < 4; x++) {
87 // Convert from row-major scalars to column-major floats.
88 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
89 }
90 }
91 }
92
93 GrBicubicEffect::GrBicubicEffect(GrTexture* texture,
94 const SkScalar coefficients[16],
95 const SkMatrix &matrix,
96 const GrTextureParams &params,
97 CoordsType coordsType)
98 : INHERITED(texture, MakeDivByTextureWHMatrix(texture), params, coordsType) {
99 for (int y = 0; y < 4; y++) {
100 for (int x = 0; x < 4; x++) {
101 // Convert from row-major scalars to column-major floats.
102 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]);
103 }
104 }
105 }
106
107 GrBicubicEffect::~GrBicubicEffect() {
108 }
109
110 const GrBackendEffectFactory& GrBicubicEffect::getFactory() const {
111 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance();
112 }
113
114 bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const {
115 const GrBicubicEffect& s = CastEffect<GrBicubicEffect>(sBase);
116 return this->texture(0) == s.texture(0) &&
117 !memcmp(fCoefficients, s.coefficients(), 16);
118 }
119
120 void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* valid Flags) const {
121 // FIXME: Perhaps we can do better.
122 *validFlags = 0;
123 return;
124 }
125
126 GR_DEFINE_EFFECT_TEST(GrBicubicEffect);
127
128 GrEffectRef* GrBicubicEffect::TestCreate(SkMWCRandom* random,
129 GrContext* context,
130 const GrDrawTargetCaps&,
131 GrTexture* textures[]) {
132 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
133 GrEffectUnitTest::kAlphaTextureIdx;
134 SkScalar coefficients[16];
135 for (int i = 0; i < 16; i++) {
136 coefficients[i] = random->nextSScalar1();
137 }
138 return GrBicubicEffect::Create(textures[texIdx], coefficients);
139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698