OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkMagnifierImageFilter.h" | 9 #include "SkMagnifierImageFilter.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 typedef GrSingleTextureEffect INHERITED; | 102 typedef GrSingleTextureEffect INHERITED; |
103 }; | 103 }; |
104 | 104 |
105 // For brevity | 105 // For brevity |
106 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 106 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
107 | 107 |
108 class GrGLMagnifierEffect : public GrGLFragmentProcessor { | 108 class GrGLMagnifierEffect : public GrGLFragmentProcessor { |
109 public: | 109 public: |
110 GrGLMagnifierEffect(const GrProcessor&); | 110 GrGLMagnifierEffect(const GrProcessor&); |
111 | 111 |
112 virtual void emitCode(GrGLFPBuilder*, | 112 virtual void emitCode(EmitArgs&) override; |
113 const GrFragmentProcessor&, | |
114 const char* outputColor, | |
115 const char* inputColor, | |
116 const TransformedCoordsArray&, | |
117 const TextureSamplerArray&) override; | |
118 | 113 |
119 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; | 114 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; |
120 | 115 |
121 private: | 116 private: |
122 UniformHandle fOffsetVar; | 117 UniformHandle fOffsetVar; |
123 UniformHandle fInvZoomVar; | 118 UniformHandle fInvZoomVar; |
124 UniformHandle fInvInsetVar; | 119 UniformHandle fInvInsetVar; |
125 UniformHandle fBoundsVar; | 120 UniformHandle fBoundsVar; |
126 | 121 |
127 typedef GrGLFragmentProcessor INHERITED; | 122 typedef GrGLFragmentProcessor INHERITED; |
128 }; | 123 }; |
129 | 124 |
130 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrProcessor&) { | 125 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrProcessor&) { |
131 } | 126 } |
132 | 127 |
133 void GrGLMagnifierEffect::emitCode(GrGLFPBuilder* builder, | 128 void GrGLMagnifierEffect::emitCode(EmitArgs& args) { |
134 const GrFragmentProcessor&, | 129 fOffsetVar = args.fBuilder->addUniform( |
135 const char* outputColor, | |
136 const char* inputColor, | |
137 const TransformedCoordsArray& coords, | |
138 const TextureSamplerArray& samplers) { | |
139 fOffsetVar = builder->addUniform( | |
140 GrGLProgramBuilder::kFragment_Visibility | | 130 GrGLProgramBuilder::kFragment_Visibility | |
141 GrGLProgramBuilder::kVertex_Visibility, | 131 GrGLProgramBuilder::kVertex_Visibility, |
142 kVec2f_GrSLType, kDefault_GrSLPrecision, "Offset"); | 132 kVec2f_GrSLType, kDefault_GrSLPrecision, "Offset"); |
143 fInvZoomVar = builder->addUniform( | 133 fInvZoomVar = args.fBuilder->addUniform( |
144 GrGLProgramBuilder::kFragment_Visibility | | 134 GrGLProgramBuilder::kFragment_Visibility | |
145 GrGLProgramBuilder::kVertex_Visibility, | 135 GrGLProgramBuilder::kVertex_Visibility, |
146 kVec2f_GrSLType, kDefault_GrSLPrecision, "InvZoom"); | 136 kVec2f_GrSLType, kDefault_GrSLPrecision, "InvZoom"); |
147 fInvInsetVar = builder->addUniform( | 137 fInvInsetVar = args.fBuilder->addUniform( |
148 GrGLProgramBuilder::kFragment_Visibility | | 138 GrGLProgramBuilder::kFragment_Visibility | |
149 GrGLProgramBuilder::kVertex_Visibility, | 139 GrGLProgramBuilder::kVertex_Visibility, |
150 kVec2f_GrSLType, kDefault_GrSLPrecision, "InvInset"); | 140 kVec2f_GrSLType, kDefault_GrSLPrecision, "InvInset"); |
151 fBoundsVar = builder->addUniform( | 141 fBoundsVar = args.fBuilder->addUniform( |
152 GrGLProgramBuilder::kFragment_Visibility | | 142 GrGLProgramBuilder::kFragment_Visibility | |
153 GrGLProgramBuilder::kVertex_Visibility, | 143 GrGLProgramBuilder::kVertex_Visibility, |
154 kVec4f_GrSLType, kDefault_GrSLPrecision, "Bounds"); | 144 kVec4f_GrSLType, kDefault_GrSLPrecision, "Bounds"); |
155 | 145 |
156 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 146 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
157 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); | 147 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0); |
158 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); | 148 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
159 fsBuilder->codeAppendf("\t\tvec2 zoom_coord = %s + %s * %s;\n", | 149 fsBuilder->codeAppendf("\t\tvec2 zoom_coord = %s + %s * %s;\n", |
160 builder->getUniformCStr(fOffsetVar), | 150 args.fBuilder->getUniformCStr(fOffsetVar), |
161 coords2D.c_str(), | 151 coords2D.c_str(), |
162 builder->getUniformCStr(fInvZoomVar)); | 152 args.fBuilder->getUniformCStr(fInvZoomVar)); |
163 const char* bounds = builder->getUniformCStr(fBoundsVar); | 153 const char* bounds = args.fBuilder->getUniformCStr(fBoundsVar); |
164 fsBuilder->codeAppendf("\t\tvec2 delta = (coord - %s.xy) * %s.zw;\n", bounds
, bounds); | 154 fsBuilder->codeAppendf("\t\tvec2 delta = (coord - %s.xy) * %s.zw;\n", bounds
, bounds); |
165 fsBuilder->codeAppendf("\t\tdelta = min(delta, vec2(1.0, 1.0) - delta);\n"); | 155 fsBuilder->codeAppendf("\t\tdelta = min(delta, vec2(1.0, 1.0) - delta);\n"); |
166 fsBuilder->codeAppendf("\t\tdelta = delta * %s;\n", builder->getUniformCStr(
fInvInsetVar)); | 156 fsBuilder->codeAppendf("\t\tdelta = delta * %s;\n", |
| 157 args.fBuilder->getUniformCStr(fInvInsetVar)); |
167 | 158 |
168 fsBuilder->codeAppend("\t\tfloat weight = 0.0;\n"); | 159 fsBuilder->codeAppend("\t\tfloat weight = 0.0;\n"); |
169 fsBuilder->codeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n"); | 160 fsBuilder->codeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n"); |
170 fsBuilder->codeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n"); | 161 fsBuilder->codeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n"); |
171 fsBuilder->codeAppend("\t\t\tfloat dist = length(delta);\n"); | 162 fsBuilder->codeAppend("\t\t\tfloat dist = length(delta);\n"); |
172 fsBuilder->codeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n"); | 163 fsBuilder->codeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n"); |
173 fsBuilder->codeAppend("\t\t\tweight = min(dist * dist, 1.0);\n"); | 164 fsBuilder->codeAppend("\t\t\tweight = min(dist * dist, 1.0);\n"); |
174 fsBuilder->codeAppend("\t\t} else {\n"); | 165 fsBuilder->codeAppend("\t\t} else {\n"); |
175 fsBuilder->codeAppend("\t\t\tvec2 delta_squared = delta * delta;\n"); | 166 fsBuilder->codeAppend("\t\t\tvec2 delta_squared = delta * delta;\n"); |
176 fsBuilder->codeAppend("\t\t\tweight = min(min(delta_squared.x, delta_squared
.y), 1.0);\n"); | 167 fsBuilder->codeAppend("\t\t\tweight = min(min(delta_squared.x, delta_squared
.y), 1.0);\n"); |
177 fsBuilder->codeAppend("\t\t}\n"); | 168 fsBuilder->codeAppend("\t\t}\n"); |
178 | 169 |
179 fsBuilder->codeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\
n"); | 170 fsBuilder->codeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\
n"); |
180 fsBuilder->codeAppend("\t\tvec4 output_color = "); | 171 fsBuilder->codeAppend("\t\tvec4 output_color = "); |
181 fsBuilder->appendTextureLookup(samplers[0], "mix_coord"); | 172 fsBuilder->appendTextureLookup(args.fSamplers[0], "mix_coord"); |
182 fsBuilder->codeAppend(";\n"); | 173 fsBuilder->codeAppend(";\n"); |
183 | 174 |
184 fsBuilder->codeAppendf("\t\t%s = output_color;", outputColor); | 175 fsBuilder->codeAppendf("\t\t%s = output_color;", args.fOutputColor); |
185 SkString modulate; | 176 SkString modulate; |
186 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); | 177 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
187 fsBuilder->codeAppend(modulate.c_str()); | 178 fsBuilder->codeAppend(modulate.c_str()); |
188 } | 179 } |
189 | 180 |
190 void GrGLMagnifierEffect::setData(const GrGLProgramDataManager& pdman, | 181 void GrGLMagnifierEffect::setData(const GrGLProgramDataManager& pdman, |
191 const GrProcessor& effect) { | 182 const GrProcessor& effect) { |
192 const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>(); | 183 const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>(); |
193 pdman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); | 184 pdman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); |
194 pdman.set2f(fInvZoomVar, zoom.x_inv_zoom(), zoom.y_inv_zoom()); | 185 pdman.set2f(fInvZoomVar, zoom.x_inv_zoom(), zoom.y_inv_zoom()); |
195 pdman.set2f(fInvInsetVar, zoom.x_inv_inset(), zoom.y_inv_inset()); | 186 pdman.set2f(fInvInsetVar, zoom.x_inv_inset(), zoom.y_inv_inset()); |
196 pdman.set4f(fBoundsVar, zoom.bounds().x(), zoom.bounds().y(), | 187 pdman.set4f(fBoundsVar, zoom.bounds().x(), zoom.bounds().y(), |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 | 379 |
389 #ifndef SK_IGNORE_TO_STRING | 380 #ifndef SK_IGNORE_TO_STRING |
390 void SkMagnifierImageFilter::toString(SkString* str) const { | 381 void SkMagnifierImageFilter::toString(SkString* str) const { |
391 str->appendf("SkMagnifierImageFilter: ("); | 382 str->appendf("SkMagnifierImageFilter: ("); |
392 str->appendf("src: (%f,%f,%f,%f) ", | 383 str->appendf("src: (%f,%f,%f,%f) ", |
393 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m); | 384 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m); |
394 str->appendf("inset: %f", fInset); | 385 str->appendf("inset: %f", fInset); |
395 str->append(")"); | 386 str->append(")"); |
396 } | 387 } |
397 #endif | 388 #endif |
OLD | NEW |