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 | |
204 ///////////////////////////////////////////////////////////////////// | |
205 | |
206 class GrComposeEffect : public GrFragmentProcessor { | |
207 public: | |
208 | |
209 static GrFragmentProcessor* Create(GrFragmentProcessor* fpA, GrFragmentProce ssor* fpB, | |
210 SkXfermode::Mode mode) { | |
211 return SkNEW_ARGS(GrComposeEffect, (fpA, fpB, mode)); | |
212 } | |
213 const char* name() const override {return fName.c_str(); } | |
214 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c onst override; | |
215 | |
216 SkXfermode::Mode getMode() const { return fMode; } | |
217 int getShaderAChildIndex() const { return fShaderAChildIndex; } | |
218 int getShaderBChildIndex() const { return fShaderBChildIndex; } | |
219 | |
220 protected: | |
221 bool onIsEqual(const GrFragmentProcessor&) const override; | |
222 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; | |
223 | |
224 private: | |
225 GrComposeEffect(GrFragmentProcessor* fpA, GrFragmentProcessor* fpB, SkXfermo de::Mode mode) | |
226 : fMode(mode) { | |
227 this->initClassID<GrComposeEffect>(); | |
228 fShaderAChildIndex = this->registerChildProcessor(fpA); | |
229 fShaderBChildIndex = this->registerChildProcessor(fpB); | |
230 fName.printf("Compose Shader <%s, %s> (Mode: %s)", fpA->name(), fpB->nam e(), | |
231 SkXfermode::ModeName(fMode)); | |
232 } | |
233 | |
234 GrGLFragmentProcessor* onCreateGLInstance() const override; | |
235 | |
236 SkString fName; | |
237 int fShaderAChildIndex; | |
238 int fShaderBChildIndex; | |
239 SkXfermode::Mode fMode; | |
240 | |
241 typedef GrFragmentProcessor INHERITED; | |
242 }; | |
243 | |
244 ///////////////////////////////////////////////////////////////////// | |
245 | |
246 class GrGLComposeEffect : public GrGLFragmentProcessor { | |
247 public: | |
248 GrGLComposeEffect(const GrProcessor& processor) { | |
249 const GrComposeEffect& cs = processor.cast<GrComposeEffect>(); | |
250 fShaderAChildIndex = cs.getShaderAChildIndex(); | |
251 fShaderBChildIndex = cs.getShaderBChildIndex(); | |
252 fMode = cs.getMode(); | |
253 } | |
254 | |
255 void emitCode(EmitArgs&) override; | |
256 | |
257 private: | |
258 int fShaderAChildIndex; | |
259 int fShaderBChildIndex; | |
260 SkXfermode::Mode fMode; | |
261 | |
262 typedef GrGLFragmentProcessor INHERITED; | |
263 }; | |
264 | |
265 bool GrComposeEffect::onIsEqual(const GrFragmentProcessor& other) const { | |
266 const GrComposeEffect& cs = other.cast<GrComposeEffect>(); | |
267 return fMode == cs.fMode; | |
268 } | |
269 | |
270 void GrComposeEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { | |
271 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | |
272 } | |
273 | |
274 void GrComposeEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKey Builder* b) const { | |
275 b->add32(fMode); | |
276 } | |
277 | |
278 GrGLFragmentProcessor* GrComposeEffect::onCreateGLInstance() const{ | |
279 return SkNEW_ARGS(GrGLComposeEffect, (*this)); | |
280 } | |
281 | |
282 ///////////////////////////////////////////////////////////////////// | |
283 | |
284 void GrGLComposeEffect::emitCode(EmitArgs& args) { | |
285 | |
286 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | |
287 | |
288 // emit the code of the two child shaders | |
289 SkString mangledOutputColorA; | |
290 this->emitChild(fShaderAChildIndex, args.fInputColor, &mangledOutputColorA, args); | |
291 SkString mangledOutputColorB; | |
292 this->emitChild(fShaderBChildIndex, args.fInputColor, &mangledOutputColorB, args); | |
293 | |
294 // emit blend code | |
295 fsBuilder->codeAppend("\t{\n"); | |
296 fsBuilder->codeAppendf("\t\t// Compose Xfer Mode: %s\n", SkXfermode::ModeNam e(fMode)); | |
297 GrGLBlend::AppendPorterDuffBlend(fsBuilder, mangledOutputColorB.c_str(), | |
298 mangledOutputColorA.c_str(), args.fOutputCo lor, fMode); | |
299 fsBuilder->codeAppend("\t}\n"); | |
300 } | |
301 | |
302 ///////////////////////////////////////////////////////////////////// | |
303 | |
304 bool SkComposeShader::asFragmentProcessor(GrContext* context, const SkPaint& pai nt, | |
305 const SkMatrix& viewM, const SkMatrix* localMatrix, | |
306 GrColor* paintColor, | |
307 GrProcessorDataManager* procDataManage r, | |
308 GrFragmentProcessor** fp) const { | |
309 // NULL fMode implies src-over | |
310 SkXfermode::Mode mode = SkXfermode::Mode::kSrcOver_Mode; | |
311 if (fMode) { | |
312 // Fragment processor will only support coefficient modes that aren't kC lear_Mode. | |
313 // This is because GrGLBlend::AppendPorterDuffBlend(), which emits the b lend code in the | |
314 // shader, only supports those xfer modes. | |
315 SkXfermode::Coeff srcCoeff, dstCoeff; | |
316 if (!fMode->asMode(&mode) || !SkXfermode::ModeAsCoeff(mode, &srcCoeff, & dstCoeff) | |
317 || mode == SkXfermode::Mode::kClear_Mode) { | |
egdaniel
2015/08/21 16:21:39
nit: we put the variable on the right side of ==
| |
318 return false; | |
319 } | |
320 } | |
321 GrFragmentProcessor* fpA = NULL; | |
322 if (!fShaderA->asFragmentProcessor(context, paint, viewM, localMatrix, paint Color, | |
323 procDataManager, &fpA) || !fpA) { | |
egdaniel
2015/08/21 16:21:39
Can you line this up so that procDataMangeger, &fp
| |
324 return false; | |
325 } | |
326 GrFragmentProcessor* fpB = NULL; | |
327 if (!fShaderB->asFragmentProcessor(context, paint, viewM, localMatrix, paint Color, | |
egdaniel
2015/08/21 16:21:39
same as above
| |
328 procDataManager, &fpB) || !fpB) { | |
329 fpA->unref(); | |
330 return false; | |
331 } | |
332 | |
333 *fp = GrComposeEffect::Create(fpA, fpB, mode); | |
egdaniel
2015/08/21 16:21:39
Is there any chance that this Create can fail and
| |
334 fpA->unref(); | |
335 fpB->unref(); | |
336 | |
337 return true; | |
338 } | |
339 | |
340 | |
341 | |
342 #else | |
343 bool SkComposeShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa trix&, | |
344 const SkMatrix*, GrColor*, GrProcessor DataManager*, | |
345 GrFragmentProcessor**) const { | |
346 SkDEBUGFAIL("Should not call in GPU-less build"); | |
347 return false; | |
348 } | |
349 #endif | |
350 | |
197 #ifndef SK_IGNORE_TO_STRING | 351 #ifndef SK_IGNORE_TO_STRING |
198 void SkComposeShader::toString(SkString* str) const { | 352 void SkComposeShader::toString(SkString* str) const { |
199 str->append("SkComposeShader: ("); | 353 str->append("SkComposeShader: ("); |
200 | 354 |
201 str->append("ShaderA: "); | 355 str->append("ShaderA: "); |
202 fShaderA->toString(str); | 356 fShaderA->toString(str); |
203 str->append(" ShaderB: "); | 357 str->append(" ShaderB: "); |
204 fShaderB->toString(str); | 358 fShaderB->toString(str); |
205 if (fMode) { | 359 if (fMode) { |
206 str->append(" Xfermode: "); | 360 str->append(" Xfermode: "); |
207 fMode->toString(str); | 361 fMode->toString(str); |
208 } | 362 } |
209 | 363 |
210 this->INHERITED::toString(str); | 364 this->INHERITED::toString(str); |
211 | 365 |
212 str->append(")"); | 366 str->append(")"); |
213 } | 367 } |
214 #endif | 368 #endif |
OLD | NEW |