OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkComposeShader.h" | 10 #include "SkComposeShader.h" |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 } | 187 } |
188 } | 188 } |
189 | 189 |
190 result += n; | 190 result += n; |
191 x += n; | 191 x += n; |
192 count -= n; | 192 count -= n; |
193 } while (count > 0); | 193 } while (count > 0); |
194 } | 194 } |
195 } | 195 } |
196 | 196 |
197 #if SK_SUPPORT_GPU | |
198 | |
199 #include "SkGr.h" | |
200 #include "GrProcessor.h" | |
201 #include "gl/GrGLBlend.h" | |
202 #include "gl/builders/GrGLProgramBuilder.h" | |
203 #include "effects/GrConstColorProcessor.h" | |
204 | |
205 ///////////////////////////////////////////////////////////////////// | |
206 | |
207 class GrComposeEffect : public GrFragmentProcessor { | |
208 public: | |
209 | |
210 static GrFragmentProcessor* Create(const GrFragmentProcessor* fpA, | |
211 const GrFragmentProcessor* fpB, SkXfermod e::Mode mode) { | |
212 return SkNEW_ARGS(GrComposeEffect, (fpA, fpB, mode)); | |
213 } | |
214 const char* name() const override { return "ComposeShader"; } | |
215 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c onst override; | |
216 | |
217 SkXfermode::Mode getMode() const { return fMode; } | |
218 int getShaderAChildIndex() const { return fShaderAChildIndex; } | |
219 int getShaderBChildIndex() const { return fShaderBChildIndex; } | |
220 | |
221 protected: | |
222 bool onIsEqual(const GrFragmentProcessor&) const override; | |
223 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; | |
224 | |
225 private: | |
226 GrComposeEffect(const GrFragmentProcessor* fpA, const GrFragmentProcessor* f pB, | |
227 SkXfermode::Mode mode) | |
228 : fMode(mode) { | |
229 this->initClassID<GrComposeEffect>(); | |
230 fShaderAChildIndex = this->registerChildProcessor(fpA); | |
bsalomon
2015/08/31 18:51:37
Maybe just assert that indices are 0 and 1 rather
wangyix
2015/08/31 20:12:31
Done.
| |
231 fShaderBChildIndex = this->registerChildProcessor(fpB); | |
232 } | |
233 | |
234 GrGLFragmentProcessor* onCreateGLInstance() const override; | |
235 | |
236 int fShaderAChildIndex; | |
237 int fShaderBChildIndex; | |
238 SkXfermode::Mode fMode; | |
239 | |
240 typedef GrFragmentProcessor INHERITED; | |
241 }; | |
242 | |
243 ///////////////////////////////////////////////////////////////////// | |
244 | |
245 class GrGLComposeEffect : public GrGLFragmentProcessor { | |
246 public: | |
247 GrGLComposeEffect(const GrProcessor& processor) { | |
248 const GrComposeEffect& cs = processor.cast<GrComposeEffect>(); | |
249 fShaderAChildIndex = cs.getShaderAChildIndex(); | |
250 fShaderBChildIndex = cs.getShaderBChildIndex(); | |
251 fMode = cs.getMode(); | |
252 } | |
253 | |
254 void emitCode(EmitArgs&) override; | |
255 | |
256 private: | |
257 int fShaderAChildIndex; | |
bsalomon
2015/08/31 18:51:37
Does this guy really need any members? emitCode ha
wangyix
2015/08/31 20:12:31
Done.
| |
258 int fShaderBChildIndex; | |
259 SkXfermode::Mode fMode; | |
260 | |
261 typedef GrGLFragmentProcessor INHERITED; | |
262 }; | |
263 | |
264 bool GrComposeEffect::onIsEqual(const GrFragmentProcessor& other) const { | |
265 const GrComposeEffect& cs = other.cast<GrComposeEffect>(); | |
266 return fMode == cs.fMode; | |
267 } | |
268 | |
269 void GrComposeEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { | |
270 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | |
271 } | |
272 | |
273 void GrComposeEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKey Builder* b) const { | |
274 b->add32(fMode); | |
275 } | |
276 | |
277 GrGLFragmentProcessor* GrComposeEffect::onCreateGLInstance() const{ | |
278 return SkNEW_ARGS(GrGLComposeEffect, (*this)); | |
279 } | |
280 | |
281 ///////////////////////////////////////////////////////////////////// | |
282 | |
283 void GrGLComposeEffect::emitCode(EmitArgs& args) { | |
284 | |
285 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | |
286 | |
287 // Store alpha of input color and un-premultiply the input color by its alph a. We will | |
288 // re-multiply by this alpha after blending the output colors of the two chi ld procs. | |
289 // This is because we don't want the paint's alpha to affect either child pr oc's output | |
290 // before the blend; we want to apply the paint's alpha AFTER the blend. Thi s mirrors the | |
291 // software implementation of SkComposeShader. | |
292 SkString inputAlpha("inputAlpha"); | |
293 fsBuilder->codeAppendf("float %s = %s.a;\n", inputAlpha.c_str(), args.fInput Color); | |
294 fsBuilder->codeAppendf("%s /= %s.a;\n", args.fInputColor, args.fInputColor); | |
295 | |
296 // emit the code of the two child shaders | |
297 SkString mangledOutputColorA; | |
298 this->emitChild(fShaderAChildIndex, args.fInputColor, &mangledOutputColorA, args); | |
299 SkString mangledOutputColorB; | |
300 this->emitChild(fShaderBChildIndex, args.fInputColor, &mangledOutputColorB, args); | |
301 | |
302 // emit blend code | |
303 fsBuilder->codeAppend("{\n"); | |
304 fsBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(fM ode)); | |
305 GrGLBlend::AppendPorterDuffBlend(fsBuilder, mangledOutputColorB.c_str(), | |
306 mangledOutputColorA.c_str(), args.fOutputCo lor, fMode); | |
307 fsBuilder->codeAppend("}\n"); | |
308 | |
309 // re-multiply the output color by the input color's alpha | |
310 fsBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, inputAlpha.c_str()) ; | |
311 } | |
312 | |
313 ///////////////////////////////////////////////////////////////////// | |
314 | |
315 const GrFragmentProcessor* SkComposeShader::asFragmentProcessor(GrContext* conte xt, | |
316 const SkMatrix& view M, | |
317 const SkMatrix* loca lMatrix, | |
318 SkFilterQuality fq, | |
319 GrProcessorDataManag er* procDataManager | |
320 ) const { | |
321 // Fragment processor will only support coefficient modes. This is because | |
322 // GrGLBlend::AppendPorterDuffBlend(), which emits the blend code in the sha der, | |
323 // only supports those modes. | |
324 SkXfermode::Mode mode; | |
325 if (!(SkXfermode::AsMode(fMode, &mode) && mode <= SkXfermode::kLastCoeffMode )) { | |
326 return nullptr; | |
327 } | |
328 | |
329 switch (mode) { | |
330 case SkXfermode::kClear_Mode: | |
331 return GrConstColorProcessor::Create(GrColor_TRANS_BLACK, | |
332 GrConstColorProcessor::kIgnore_ InputMode); | |
333 break; | |
334 case SkXfermode::kSrc_Mode: | |
335 return fShaderB->asFragmentProcessor(context, viewM, localMatrix, fq , procDataManager); | |
336 break; | |
337 case SkXfermode::kDst_Mode: | |
338 return fShaderA->asFragmentProcessor(context, viewM, localMatrix, fq , procDataManager); | |
339 break; | |
340 default: | |
341 SkAutoTUnref<const GrFragmentProcessor> fpA(fShaderA->asFragmentProc essor(context, | |
342 viewM, localMatrix, fq , procDataManager)); | |
343 if (!fpA.get()) { | |
344 return nullptr; | |
345 } | |
346 SkAutoTUnref<const GrFragmentProcessor> fpB(fShaderB->asFragmentProc essor(context, | |
347 viewM, localMatrix, fq , procDataManager)); | |
348 if (!fpB.get()) { | |
349 return nullptr; | |
350 } | |
351 return GrComposeEffect::Create(fpA, fpB, mode); | |
352 } | |
353 } | |
354 #endif | |
355 | |
197 #ifndef SK_IGNORE_TO_STRING | 356 #ifndef SK_IGNORE_TO_STRING |
198 void SkComposeShader::toString(SkString* str) const { | 357 void SkComposeShader::toString(SkString* str) const { |
199 str->append("SkComposeShader: ("); | 358 str->append("SkComposeShader: ("); |
200 | 359 |
201 str->append("ShaderA: "); | 360 str->append("ShaderA: "); |
202 fShaderA->toString(str); | 361 fShaderA->toString(str); |
203 str->append(" ShaderB: "); | 362 str->append(" ShaderB: "); |
204 fShaderB->toString(str); | 363 fShaderB->toString(str); |
205 if (fMode) { | 364 if (fMode) { |
206 str->append(" Xfermode: "); | 365 str->append(" Xfermode: "); |
207 fMode->toString(str); | 366 fMode->toString(str); |
208 } | 367 } |
209 | 368 |
210 this->INHERITED::toString(str); | 369 this->INHERITED::toString(str); |
211 | 370 |
212 str->append(")"); | 371 str->append(")"); |
213 } | 372 } |
214 #endif | 373 #endif |
OLD | NEW |