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 |
| 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 typedef GrFragmentProcessor INHERITED; |
| 239 }; |
| 240 |
| 241 ///////////////////////////////////////////////////////////////////// |
| 242 |
| 243 class GrGLComposeEffect : public GrGLFragmentProcessor { |
| 244 public: |
| 245 GrGLComposeEffect(const GrProcessor& processor) {} |
| 246 |
| 247 void emitCode(EmitArgs&) override; |
| 248 |
| 249 private: |
| 250 typedef GrGLFragmentProcessor INHERITED; |
| 251 }; |
| 252 |
| 253 bool GrComposeEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 254 const GrComposeEffect& cs = other.cast<GrComposeEffect>(); |
| 255 return fMode == cs.fMode; |
| 256 } |
| 257 |
| 258 void GrComposeEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
| 259 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
| 260 } |
| 261 |
| 262 void GrComposeEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKey
Builder* b) const { |
| 263 b->add32(fMode); |
| 264 } |
| 265 |
| 266 GrGLFragmentProcessor* GrComposeEffect::onCreateGLInstance() const{ |
| 267 return SkNEW_ARGS(GrGLComposeEffect, (*this)); |
| 268 } |
| 269 |
| 270 ///////////////////////////////////////////////////////////////////// |
| 271 |
| 272 void GrGLComposeEffect::emitCode(EmitArgs& args) { |
| 273 |
| 274 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
| 275 const GrComposeEffect& cs = args.fFp.cast<GrComposeEffect>(); |
| 276 |
| 277 // Store alpha of input color and un-premultiply the input color by its alph
a. We will |
| 278 // re-multiply by this alpha after blending the output colors of the two chi
ld procs. |
| 279 // This is because we don't want the paint's alpha to affect either child pr
oc's output |
| 280 // before the blend; we want to apply the paint's alpha AFTER the blend. Thi
s mirrors the |
| 281 // software implementation of SkComposeShader. |
| 282 SkString inputAlpha("inputAlpha"); |
| 283 fsBuilder->codeAppendf("float %s = %s.a;", inputAlpha.c_str(), args.fInputCo
lor); |
| 284 fsBuilder->codeAppendf("%s /= %s.a;", args.fInputColor, args.fInputColor); |
| 285 |
| 286 // emit the code of the two child shaders |
| 287 SkString mangledOutputColorA; |
| 288 this->emitChild(0, args.fInputColor, &mangledOutputColorA, args); |
| 289 SkString mangledOutputColorB; |
| 290 this->emitChild(1, args.fInputColor, &mangledOutputColorB, args); |
| 291 |
| 292 // emit blend code |
| 293 SkXfermode::Mode mode = cs.getMode(); |
| 294 fsBuilder->codeAppend("{"); |
| 295 fsBuilder->codeAppendf("// Compose Xfer Mode: %s\n", SkXfermode::ModeName(mo
de)); |
| 296 GrGLBlend::AppendPorterDuffBlend(fsBuilder, mangledOutputColorB.c_str(), |
| 297 mangledOutputColorA.c_str(), args.fOutputCo
lor, mode); |
| 298 fsBuilder->codeAppend("}"); |
| 299 |
| 300 // re-multiply the output color by the input color's alpha |
| 301 fsBuilder->codeAppendf("%s *= %s;", args.fOutputColor, inputAlpha.c_str()); |
| 302 } |
| 303 |
| 304 ///////////////////////////////////////////////////////////////////// |
| 305 |
| 306 const GrFragmentProcessor* SkComposeShader::asFragmentProcessor(GrContext* conte
xt, |
| 307 const SkMatrix& view
M, |
| 308 const SkMatrix* loca
lMatrix, |
| 309 SkFilterQuality fq, |
| 310 GrProcessorDataManag
er* procDataManager |
| 311 ) const { |
| 312 // Fragment processor will only support coefficient modes. This is because |
| 313 // GrGLBlend::AppendPorterDuffBlend(), which emits the blend code in the sha
der, |
| 314 // only supports those modes. |
| 315 SkXfermode::Mode mode; |
| 316 if (!(SkXfermode::AsMode(fMode, &mode) && mode <= SkXfermode::kLastCoeffMode
)) { |
| 317 return nullptr; |
| 318 } |
| 319 |
| 320 switch (mode) { |
| 321 case SkXfermode::kClear_Mode: |
| 322 return GrConstColorProcessor::Create(GrColor_TRANS_BLACK, |
| 323 GrConstColorProcessor::kIgnore_
InputMode); |
| 324 break; |
| 325 case SkXfermode::kSrc_Mode: |
| 326 return fShaderB->asFragmentProcessor(context, viewM, localMatrix, fq
, procDataManager); |
| 327 break; |
| 328 case SkXfermode::kDst_Mode: |
| 329 return fShaderA->asFragmentProcessor(context, viewM, localMatrix, fq
, procDataManager); |
| 330 break; |
| 331 default: |
| 332 SkAutoTUnref<const GrFragmentProcessor> fpA(fShaderA->asFragmentProc
essor(context, |
| 333 viewM, localMatrix, fq,
procDataManager)); |
| 334 if (!fpA.get()) { |
| 335 return nullptr; |
| 336 } |
| 337 SkAutoTUnref<const GrFragmentProcessor> fpB(fShaderB->asFragmentProc
essor(context, |
| 338 viewM, localMatrix, fq,
procDataManager)); |
| 339 if (!fpB.get()) { |
| 340 return nullptr; |
| 341 } |
| 342 return GrComposeEffect::Create(fpA, fpB, mode); |
| 343 } |
| 344 } |
| 345 #endif |
| 346 |
197 #ifndef SK_IGNORE_TO_STRING | 347 #ifndef SK_IGNORE_TO_STRING |
198 void SkComposeShader::toString(SkString* str) const { | 348 void SkComposeShader::toString(SkString* str) const { |
199 str->append("SkComposeShader: ("); | 349 str->append("SkComposeShader: ("); |
200 | 350 |
201 str->append("ShaderA: "); | 351 str->append("ShaderA: "); |
202 fShaderA->toString(str); | 352 fShaderA->toString(str); |
203 str->append(" ShaderB: "); | 353 str->append(" ShaderB: "); |
204 fShaderB->toString(str); | 354 fShaderB->toString(str); |
205 if (fMode) { | 355 if (fMode) { |
206 str->append(" Xfermode: "); | 356 str->append(" Xfermode: "); |
207 fMode->toString(str); | 357 fMode->toString(str); |
208 } | 358 } |
209 | 359 |
210 this->INHERITED::toString(str); | 360 this->INHERITED::toString(str); |
211 | 361 |
212 str->append(")"); | 362 str->append(")"); |
213 } | 363 } |
214 #endif | 364 #endif |
OLD | NEW |