| 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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 | 197 #if SK_SUPPORT_GPU |
| 198 | 198 |
| 199 #include "SkGr.h" | |
| 200 #include "GrProcessor.h" | |
| 201 #include "effects/GrConstColorProcessor.h" | 199 #include "effects/GrConstColorProcessor.h" |
| 202 #include "gl/GrGLBlend.h" | 200 #include "effects/GrXfermodeFragmentProcessor.h" |
| 203 #include "gl/builders/GrGLProgramBuilder.h" | |
| 204 | 201 |
| 205 ///////////////////////////////////////////////////////////////////// | 202 ///////////////////////////////////////////////////////////////////// |
| 206 | 203 |
| 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 | |
| 219 protected: | |
| 220 bool onIsEqual(const GrFragmentProcessor&) const override; | |
| 221 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; | |
| 222 | |
| 223 private: | |
| 224 GrComposeEffect(const GrFragmentProcessor* fpA, const GrFragmentProcessor* f
pB, | |
| 225 SkXfermode::Mode mode) | |
| 226 : fMode(mode) { | |
| 227 this->initClassID<GrComposeEffect>(); | |
| 228 SkDEBUGCODE(int shaderAChildIndex = )this->registerChildProcessor(fpA); | |
| 229 SkDEBUGCODE(int shaderBChildIndex = )this->registerChildProcessor(fpB); | |
| 230 SkASSERT(0 == shaderAChildIndex); | |
| 231 SkASSERT(1 == shaderBChildIndex); | |
| 232 } | |
| 233 | |
| 234 GrGLFragmentProcessor* onCreateGLInstance() const override; | |
| 235 | |
| 236 SkXfermode::Mode fMode; | |
| 237 | |
| 238 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | |
| 239 | |
| 240 typedef GrFragmentProcessor INHERITED; | |
| 241 }; | |
| 242 | |
| 243 ///////////////////////////////////////////////////////////////////// | |
| 244 | |
| 245 class GrGLComposeEffect : public GrGLFragmentProcessor { | |
| 246 public: | |
| 247 GrGLComposeEffect(const GrProcessor& processor) {} | |
| 248 | |
| 249 void emitCode(EmitArgs&) override; | |
| 250 | |
| 251 private: | |
| 252 typedef GrGLFragmentProcessor INHERITED; | |
| 253 }; | |
| 254 | |
| 255 ///////////////////////////////////////////////////////////////////// | |
| 256 | |
| 257 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrComposeEffect); | |
| 258 | |
| 259 const GrFragmentProcessor* GrComposeEffect::TestCreate(GrProcessorTestData* d) { | |
| 260 #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS | |
| 261 // Create two random frag procs. | |
| 262 // For now, we'll prevent either children from being a shader with children
to prevent the | |
| 263 // possibility of an arbitrarily large tree of procs. | |
| 264 SkAutoTUnref<const GrFragmentProcessor> fpA; | |
| 265 do { | |
| 266 fpA.reset(GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(d)); | |
| 267 SkASSERT(fpA); | |
| 268 } while (fpA->numChildProcessors() != 0); | |
| 269 SkAutoTUnref<const GrFragmentProcessor> fpB; | |
| 270 do { | |
| 271 fpB.reset(GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(d)); | |
| 272 SkASSERT(fpB); | |
| 273 } while (fpB->numChildProcessors() != 0); | |
| 274 | |
| 275 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>( | |
| 276 d->fRandom->nextRangeU(0, SkXfermode::kLastCoeffMode)); | |
| 277 return SkNEW_ARGS(GrComposeEffect, (fpA, fpB, mode)); | |
| 278 #else | |
| 279 SkFAIL("Should not be called if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS"); | |
| 280 return nullptr; | |
| 281 #endif | |
| 282 } | |
| 283 | |
| 284 bool GrComposeEffect::onIsEqual(const GrFragmentProcessor& other) const { | |
| 285 const GrComposeEffect& cs = other.cast<GrComposeEffect>(); | |
| 286 return fMode == cs.fMode; | |
| 287 } | |
| 288 | |
| 289 void GrComposeEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { | |
| 290 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | |
| 291 } | |
| 292 | |
| 293 void GrComposeEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKey
Builder* b) const { | |
| 294 b->add32(fMode); | |
| 295 } | |
| 296 | |
| 297 GrGLFragmentProcessor* GrComposeEffect::onCreateGLInstance() const{ | |
| 298 return SkNEW_ARGS(GrGLComposeEffect, (*this)); | |
| 299 } | |
| 300 | |
| 301 ///////////////////////////////////////////////////////////////////// | |
| 302 | |
| 303 void GrGLComposeEffect::emitCode(EmitArgs& args) { | |
| 304 | |
| 305 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | |
| 306 const GrComposeEffect& cs = args.fFp.cast<GrComposeEffect>(); | |
| 307 | |
| 308 // Store alpha of input color and un-premultiply the input color by its alph
a. We will | |
| 309 // re-multiply by this alpha after blending the output colors of the two chi
ld procs. | |
| 310 // This is because we don't want the paint's alpha to affect either child pr
oc's output | |
| 311 // before the blend; we want to apply the paint's alpha AFTER the blend. Thi
s mirrors the | |
| 312 // software implementation of SkComposeShader. | |
| 313 SkString inputAlpha("inputAlpha"); | |
| 314 fsBuilder->codeAppendf("float %s = %s.a;", inputAlpha.c_str(), args.fInputCo
lor); | |
| 315 fsBuilder->codeAppendf("%s /= %s.a;", args.fInputColor, args.fInputColor); | |
| 316 | |
| 317 // declare outputColor and emit the code for each of the two children | |
| 318 SkString outputColorA(args.fOutputColor); | |
| 319 outputColorA.append("_dst"); | |
| 320 fsBuilder->codeAppendf("vec4 %s;\n", outputColorA.c_str()); | |
| 321 this->emitChild(0, args.fInputColor, outputColorA.c_str(), args); | |
| 322 | |
| 323 SkString outputColorB(args.fOutputColor); | |
| 324 outputColorB.append("_src"); | |
| 325 fsBuilder->codeAppendf("vec4 %s;\n", outputColorB.c_str()); | |
| 326 this->emitChild(1, args.fInputColor, outputColorB.c_str(), args); | |
| 327 | |
| 328 // emit blend code | |
| 329 SkXfermode::Mode mode = cs.getMode(); | |
| 330 fsBuilder->codeAppend("{"); | |
| 331 fsBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(mo
de)); | |
| 332 GrGLBlend::AppendPorterDuffBlend(fsBuilder, outputColorB.c_str(), | |
| 333 outputColorA.c_str(), args.fOutputColor, mo
de); | |
| 334 fsBuilder->codeAppend("}"); | |
| 335 | |
| 336 // re-multiply the output color by the input color's alpha | |
| 337 fsBuilder->codeAppendf("%s *= %s;", args.fOutputColor, inputAlpha.c_str()); | |
| 338 } | |
| 339 | |
| 340 ///////////////////////////////////////////////////////////////////// | |
| 341 | |
| 342 const GrFragmentProcessor* SkComposeShader::asFragmentProcessor(GrContext* conte
xt, | 204 const GrFragmentProcessor* SkComposeShader::asFragmentProcessor(GrContext* conte
xt, |
| 343 const SkMatrix& view
M, | 205 const SkMatrix& view
M, |
| 344 const SkMatrix* loca
lMatrix, | 206 const SkMatrix* loca
lMatrix, |
| 345 SkFilterQuality fq, | 207 SkFilterQuality fq, |
| 346 GrProcessorDataManag
er* procDataManager | 208 GrProcessorDataManag
er* procDataManager |
| 347 ) const { | 209 ) const { |
| 348 // Fragment processor will only support coefficient modes. This is because | 210 // Fragment processor will only support coefficient modes. This is because |
| 349 // GrGLBlend::AppendPorterDuffBlend(), which emits the blend code in the sha
der, | 211 // GrGLBlend::AppendPorterDuffBlend(), which emits the blend code in the sha
der, |
| 350 // only supports those modes. | 212 // only supports those modes. |
| 351 SkXfermode::Mode mode; | 213 SkXfermode::Mode mode; |
| 352 if (!(SkXfermode::AsMode(fMode, &mode) && mode <= SkXfermode::kLastCoeffMode
)) { | 214 if (!(SkXfermode::AsMode(fMode, &mode) && SkXfermode::kLastCoeffMode >= mode
)) { |
| 353 return nullptr; | 215 return nullptr; |
| 354 } | 216 } |
| 355 | 217 |
| 356 switch (mode) { | 218 switch (mode) { |
| 357 case SkXfermode::kClear_Mode: | 219 case SkXfermode::kClear_Mode: |
| 358 return GrConstColorProcessor::Create(GrColor_TRANS_BLACK, | 220 return GrConstColorProcessor::Create(GrColor_TRANS_BLACK, |
| 359 GrConstColorProcessor::kIgnore_
InputMode); | 221 GrConstColorProcessor::kIgnore_
InputMode); |
| 360 break; | 222 break; |
| 361 case SkXfermode::kSrc_Mode: | 223 case SkXfermode::kSrc_Mode: |
| 362 return fShaderB->asFragmentProcessor(context, viewM, localMatrix, fq
, procDataManager); | 224 return fShaderB->asFragmentProcessor(context, viewM, localMatrix, fq
, procDataManager); |
| 363 break; | 225 break; |
| 364 case SkXfermode::kDst_Mode: | 226 case SkXfermode::kDst_Mode: |
| 365 return fShaderA->asFragmentProcessor(context, viewM, localMatrix, fq
, procDataManager); | 227 return fShaderA->asFragmentProcessor(context, viewM, localMatrix, fq
, procDataManager); |
| 366 break; | 228 break; |
| 367 default: | 229 default: |
| 368 SkAutoTUnref<const GrFragmentProcessor> fpA(fShaderA->asFragmentProc
essor(context, | 230 SkAutoTUnref<const GrFragmentProcessor> fpA(fShaderA->asFragmentProc
essor(context, |
| 369 viewM, localMatrix, fq,
procDataManager)); | 231 viewM, localMatrix, fq,
procDataManager)); |
| 370 if (!fpA.get()) { | 232 if (!fpA.get()) { |
| 371 return nullptr; | 233 return nullptr; |
| 372 } | 234 } |
| 373 SkAutoTUnref<const GrFragmentProcessor> fpB(fShaderB->asFragmentProc
essor(context, | 235 SkAutoTUnref<const GrFragmentProcessor> fpB(fShaderB->asFragmentProc
essor(context, |
| 374 viewM, localMatrix, fq,
procDataManager)); | 236 viewM, localMatrix, fq,
procDataManager)); |
| 375 if (!fpB.get()) { | 237 if (!fpB.get()) { |
| 376 return nullptr; | 238 return nullptr; |
| 377 } | 239 } |
| 378 return GrComposeEffect::Create(fpA, fpB, mode); | 240 return GrXfermodeFragmentProcessor::CreateFromTwoProcessors(fpB, fpA
, mode); |
| 379 } | 241 } |
| 380 } | 242 } |
| 381 #endif | 243 #endif |
| 382 | 244 |
| 383 #ifndef SK_IGNORE_TO_STRING | 245 #ifndef SK_IGNORE_TO_STRING |
| 384 void SkComposeShader::toString(SkString* str) const { | 246 void SkComposeShader::toString(SkString* str) const { |
| 385 str->append("SkComposeShader: ("); | 247 str->append("SkComposeShader: ("); |
| 386 | 248 |
| 387 str->append("ShaderA: "); | 249 str->append("ShaderA: "); |
| 388 fShaderA->toString(str); | 250 fShaderA->toString(str); |
| 389 str->append(" ShaderB: "); | 251 str->append(" ShaderB: "); |
| 390 fShaderB->toString(str); | 252 fShaderB->toString(str); |
| 391 if (fMode) { | 253 if (fMode) { |
| 392 str->append(" Xfermode: "); | 254 str->append(" Xfermode: "); |
| 393 fMode->toString(str); | 255 fMode->toString(str); |
| 394 } | 256 } |
| 395 | 257 |
| 396 this->INHERITED::toString(str); | 258 this->INHERITED::toString(str); |
| 397 | 259 |
| 398 str->append(")"); | 260 str->append(")"); |
| 399 } | 261 } |
| 400 #endif | 262 #endif |
| OLD | NEW |