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