Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2013 The Android Open Source Project | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "SkXfermodeImageFilter.h" | |
| 9 #include "SkCanvas.h" | |
| 10 #include "SkColorPriv.h" | |
| 11 #include "SkFlattenableBuffers.h" | |
| 12 #include "SkXfermode.h" | |
| 13 #if SK_SUPPORT_GPU | |
| 14 #include "GrContext.h" | |
| 15 #include "effects/GrSimpleTextureEffect.h" | |
| 16 #include "SkGr.h" | |
| 17 #include "SkImageFilterUtils.h" | |
| 18 #endif | |
| 19 | |
| 20 /////////////////////////////////////////////////////////////////////////////// | |
| 21 | |
| 22 SkXfermodeImageFilter::SkXfermodeImageFilter(SkXfermode* mode, SkImageFilter* ba ckground, SkImageFilter* foreground) | |
| 23 : INHERITED(background, foreground), fMode(mode) | |
| 24 { | |
| 25 SkSafeRef(fMode); | |
|
reed1
2013/05/30 16:56:32
What happens if mode is NULL? Is that ever useful?
Stephen White
2013/05/30 17:24:20
I don't think it's particularly useful, but it's n
| |
| 26 } | |
| 27 | |
| 28 SkXfermodeImageFilter::~SkXfermodeImageFilter() { | |
| 29 SkSafeUnref(fMode); | |
| 30 } | |
| 31 | |
| 32 SkXfermodeImageFilter::SkXfermodeImageFilter(SkFlattenableReadBuffer& buffer) | |
| 33 : INHERITED(buffer) | |
| 34 { | |
| 35 fMode = buffer.readFlattenableT<SkXfermode>(); | |
| 36 } | |
| 37 | |
| 38 void SkXfermodeImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { | |
| 39 this->INHERITED::flatten(buffer); | |
| 40 buffer.writeFlattenable(fMode); | |
| 41 } | |
| 42 | |
| 43 bool SkXfermodeImageFilter::onFilterImage(Proxy* proxy, | |
| 44 const SkBitmap& src, | |
| 45 const SkMatrix& ctm, | |
| 46 SkBitmap* dst, | |
| 47 SkIPoint* offset) { | |
| 48 SkBitmap background = src, foreground = src; | |
| 49 SkImageFilter* backgroundInput = getInput(0); | |
| 50 SkImageFilter* foregroundInput = getInput(1); | |
| 51 if (backgroundInput && !backgroundInput->filterImage(proxy, src, ctm, &backg round, offset)) { | |
| 52 return false; | |
| 53 } | |
| 54 if (foregroundInput && !foregroundInput->filterImage(proxy, src, ctm, &foreg round, offset)) { | |
| 55 return false; | |
| 56 } | |
| 57 dst->setConfig(background.config(), background.width(), background.height()) ; | |
| 58 dst->allocPixels(); | |
| 59 SkCanvas canvas(*dst); | |
| 60 SkPaint paint; | |
| 61 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
| 62 canvas.drawBitmap(background, 0, 0, &paint); | |
| 63 paint.setXfermode(fMode); | |
| 64 canvas.drawBitmap(foreground, 0, 0, &paint); | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 #if SK_SUPPORT_GPU | |
| 69 | |
| 70 bool SkXfermodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, Sk Bitmap* result) { | |
| 71 SkBitmap background; | |
| 72 if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, &backgro und)) { | |
| 73 return false; | |
| 74 } | |
| 75 GrTexture* backgroundTex = (GrTexture*) background.getTexture(); | |
| 76 SkBitmap foreground; | |
| 77 if (!SkImageFilterUtils::GetInputResultGPU(getInput(1), proxy, src, &foregro und)) { | |
| 78 return false; | |
| 79 } | |
| 80 GrTexture* foregroundTex = (GrTexture*) foreground.getTexture(); | |
| 81 GrContext* context = foregroundTex->getContext(); | |
| 82 | |
| 83 GrEffectRef* xferEffect = NULL; | |
| 84 | |
| 85 GrTextureDesc desc; | |
| 86 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; | |
| 87 desc.fWidth = src.width(); | |
| 88 desc.fHeight = src.height(); | |
| 89 desc.fConfig = kSkia8888_GrPixelConfig; | |
| 90 | |
| 91 GrAutoScratchTexture ast(context, desc); | |
| 92 SkAutoTUnref<GrTexture> dst(ast.detach()); | |
| 93 | |
| 94 GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); | |
| 95 | |
| 96 SkXfermode::Coeff sm, dm; | |
| 97 if (!SkXfermode::AsNewEffectOrCoeff(fMode, context, &xferEffect, &sm, &dm, b ackgroundTex)) { | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 GrPaint paint; | |
| 102 SkRect srcRect; | |
| 103 src.getBounds(&srcRect); | |
| 104 if (NULL != xferEffect) { | |
| 105 paint.colorStage(0)->setEffect( | |
| 106 GrSimpleTextureEffect::Create(foregroundTex, GrEffect::MakeDivByText ureWHMatrix(foregroundTex)))->unref(); | |
| 107 paint.colorStage(1)->setEffect(xferEffect); | |
| 108 context->drawRect(paint, srcRect); | |
| 109 } else { | |
| 110 paint.colorStage(0)->setEffect( | |
| 111 GrSimpleTextureEffect::Create(backgroundTex, GrEffect::MakeDivByText ureWHMatrix(backgroundTex)))->unref(); | |
| 112 context->drawRect(paint, srcRect); | |
| 113 paint.setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm)); | |
| 114 paint.colorStage(0)->setEffect( | |
| 115 GrSimpleTextureEffect::Create(foregroundTex, GrEffect::MakeDivByText ureWHMatrix(foregroundTex)))->unref(); | |
| 116 context->drawRect(paint, srcRect); | |
| 117 } | |
| 118 return SkImageFilterUtils::WrapTexture(dst, src.width(), src.height(), resul t); | |
| 119 } | |
| 120 | |
| 121 #endif | |
| OLD | NEW |