Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/core/SkComposeShader.cpp

Issue 1292353005: Added SkComposeShader GPU implementation (Closed) Base URL: https://skia.googlesource.com/skia@cs3_glinstances2
Patch Set: removed children's names from name of GrComposeEffect Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkComposeShader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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("ComposeShader (Mode: %s)", SkXfermode::ModeName(fMode));
bsalomon 2015/08/27 20:12:24 Maybe we should init this in name()? I believe it
wangyix 2015/08/31 14:10:05 I'm not sure what you mean by this. On another no
bsalomon 2015/08/31 14:48:35 Let's go with that. I just don't want the construc
wangyix 2015/08/31 18:22:09 Done.
231 }
232
233 GrGLFragmentProcessor* onCreateGLInstance() const override;
234
235 SkString fName;
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;
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 bool SkComposeShader::asFragmentProcessor(GrContext* context, const SkPaint& pai nt,
316 const SkMatrix& viewM, const SkMatrix* localMatrix,
317 GrColor* paintColor,
318 GrProcessorDataManager* procDataManage r,
319 GrFragmentProcessor** fp) const {
320 // NULL fMode implies src-over
321 SkXfermode::Mode mode = SkXfermode::Mode::kSrcOver_Mode;
322 if (fMode) {
323 // Fragment processor will only support coefficient modes that aren't kC lear_Mode.
324 // This is because GrGLBlend::AppendPorterDuffBlend(), which emits the b lend code in the
325 // shader, only supports those xfer modes.
326 SkXfermode::Coeff srcCoeff, dstCoeff;
327 if (!fMode->asMode(&mode) || !SkXfermode::ModeAsCoeff(mode, &srcCoeff, & dstCoeff)
328 || mode == SkXfermode::Mode::kClear_Mode) {
wangyix 2015/08/31 14:10:05 What should the behavior be for kClear_Mode under
egdaniel 2015/08/31 14:43:05 Did AppendPorterDuffBlend() ever get fixed to supp
bsalomon 2015/08/31 14:48:35 Hm... I think making kSrc, kDst, kClear FPs is may
wangyix 2015/08/31 15:59:24 If the clear optimization happens in GrComposeEffe
329 return false;
330 }
331 }
332 GrFragmentProcessor* fpA = NULL;
333 if (!fShaderA->asFragmentProcessor(context, paint, viewM, localMatrix, paint Color,
334 procDataManager, &fpA) || !fpA) {
335 return false;
336 }
337 GrFragmentProcessor* fpB = NULL;
338 if (!fShaderB->asFragmentProcessor(context, paint, viewM, localMatrix, paint Color,
339 procDataManager, &fpB) || !fpB) {
340 fpA->unref();
341 return false;
342 }
343
344 *fp = GrComposeEffect::Create(fpA, fpB, mode);
345 fpA->unref();
346 fpB->unref();
347
348 return true;
349 }
350
351
352
353 #else
bsalomon 2015/08/28 16:15:30 Let's just put the function decl in the header beh
wangyix 2015/08/31 18:22:09 Done.
354 bool SkComposeShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa trix&,
355 const SkMatrix*, GrColor*, GrProcessor DataManager*,
356 GrFragmentProcessor**) const {
357 SkDEBUGFAIL("Should not call in GPU-less build");
358 return false;
359 }
360 #endif
361
197 #ifndef SK_IGNORE_TO_STRING 362 #ifndef SK_IGNORE_TO_STRING
198 void SkComposeShader::toString(SkString* str) const { 363 void SkComposeShader::toString(SkString* str) const {
199 str->append("SkComposeShader: ("); 364 str->append("SkComposeShader: (");
200 365
201 str->append("ShaderA: "); 366 str->append("ShaderA: ");
202 fShaderA->toString(str); 367 fShaderA->toString(str);
203 str->append(" ShaderB: "); 368 str->append(" ShaderB: ");
204 fShaderB->toString(str); 369 fShaderB->toString(str);
205 if (fMode) { 370 if (fMode) {
206 str->append(" Xfermode: "); 371 str->append(" Xfermode: ");
207 fMode->toString(str); 372 fMode->toString(str);
208 } 373 }
209 374
210 this->INHERITED::toString(str); 375 this->INHERITED::toString(str);
211 376
212 str->append(")"); 377 str->append(")");
213 } 378 }
214 #endif 379 #endif
OLDNEW
« no previous file with comments | « include/core/SkComposeShader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698