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 "SkDevice.h" | 10 #include "SkDevice.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 const SkBitmap& src, | 46 const SkBitmap& src, |
47 const Context& ctx, | 47 const Context& ctx, |
48 SkBitmap* dst, | 48 SkBitmap* dst, |
49 SkIPoint* offset) const { | 49 SkIPoint* offset) const { |
50 SkBitmap background = src, foreground = src; | 50 SkBitmap background = src, foreground = src; |
51 SkImageFilter* backgroundInput = getInput(0); | 51 SkImageFilter* backgroundInput = getInput(0); |
52 SkImageFilter* foregroundInput = getInput(1); | 52 SkImageFilter* foregroundInput = getInput(1); |
53 SkIPoint backgroundOffset = SkIPoint::Make(0, 0); | 53 SkIPoint backgroundOffset = SkIPoint::Make(0, 0); |
54 if (backgroundInput && | 54 if (backgroundInput && |
55 !backgroundInput->filterImage(proxy, src, ctx, &background, &backgroundO
ffset)) { | 55 !backgroundInput->filterImage(proxy, src, ctx, &background, &backgroundO
ffset)) { |
56 return false; | 56 background.reset(); |
57 } | 57 } |
58 SkIPoint foregroundOffset = SkIPoint::Make(0, 0); | 58 SkIPoint foregroundOffset = SkIPoint::Make(0, 0); |
59 if (foregroundInput && | 59 if (foregroundInput && |
60 !foregroundInput->filterImage(proxy, src, ctx, &foreground, &foregroundO
ffset)) { | 60 !foregroundInput->filterImage(proxy, src, ctx, &foreground, &foregroundO
ffset)) { |
61 return false; | 61 foreground.reset(); |
62 } | 62 } |
63 | 63 |
64 SkIRect bounds, foregroundBounds; | 64 SkIRect bounds, foregroundBounds; |
65 if (!applyCropRect(ctx, foreground, foregroundOffset, &foregroundBounds)) { | 65 if (!applyCropRect(ctx, foreground, foregroundOffset, &foregroundBounds)) { |
| 66 foregroundBounds.setEmpty(); |
| 67 foreground.reset(); |
| 68 } |
| 69 if (!applyCropRect(ctx, background, backgroundOffset, &bounds)) { |
| 70 bounds.setEmpty(); |
| 71 background.reset(); |
| 72 } |
| 73 bounds.join(foregroundBounds); |
| 74 if (bounds.isEmpty()) { |
66 return false; | 75 return false; |
67 } | 76 } |
68 if (!applyCropRect(ctx, background, backgroundOffset, &bounds)) { | |
69 return false; | |
70 } | |
71 bounds.join(foregroundBounds); | |
72 | 77 |
73 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds
.height())); | 78 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds
.height())); |
74 if (NULL == device.get()) { | 79 if (NULL == device.get()) { |
75 return false; | 80 return false; |
76 } | 81 } |
77 SkCanvas canvas(device); | 82 SkCanvas canvas(device); |
78 canvas.translate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())
); | 83 canvas.translate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())
); |
79 SkPaint paint; | 84 SkPaint paint; |
80 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 85 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
81 canvas.drawBitmap(background, SkIntToScalar(backgroundOffset.fX), | 86 canvas.drawBitmap(background, SkIntToScalar(backgroundOffset.fX), |
82 SkIntToScalar(backgroundOffset.fY), &paint); | 87 SkIntToScalar(backgroundOffset.fY), &paint); |
83 paint.setXfermode(fMode); | 88 paint.setXfermode(fMode); |
84 canvas.drawBitmap(foreground, SkIntToScalar(foregroundOffset.fX), | 89 canvas.drawBitmap(foreground, SkIntToScalar(foregroundOffset.fX), |
85 SkIntToScalar(foregroundOffset.fY), &paint); | 90 SkIntToScalar(foregroundOffset.fY), &paint); |
86 canvas.clipRect(SkRect::Make(foregroundBounds), SkRegion::kDifference_Op); | 91 canvas.clipRect(SkRect::Make(foregroundBounds), SkRegion::kDifference_Op); |
87 paint.setColor(SK_ColorTRANSPARENT); | 92 paint.setColor(SK_ColorTRANSPARENT); |
88 canvas.drawPaint(paint); | 93 canvas.drawPaint(paint); |
89 *dst = device->accessBitmap(false); | 94 *dst = device->accessBitmap(false); |
90 offset->fX = bounds.left(); | 95 offset->fX = bounds.left(); |
91 offset->fY = bounds.top(); | 96 offset->fY = bounds.top(); |
92 return true; | 97 return true; |
93 } | 98 } |
94 | 99 |
95 #if SK_SUPPORT_GPU | 100 #if SK_SUPPORT_GPU |
96 | 101 |
| 102 bool SkXfermodeImageFilter::canFilterImageGPU() const { |
| 103 return fMode && fMode->asNewEffect(NULL, NULL) && !cropRectIsSet(); |
| 104 } |
| 105 |
97 bool SkXfermodeImageFilter::filterImageGPU(Proxy* proxy, | 106 bool SkXfermodeImageFilter::filterImageGPU(Proxy* proxy, |
98 const SkBitmap& src, | 107 const SkBitmap& src, |
99 const Context& ctx, | 108 const Context& ctx, |
100 SkBitmap* result, | 109 SkBitmap* result, |
101 SkIPoint* offset) const { | 110 SkIPoint* offset) const { |
102 SkBitmap background = src; | 111 SkBitmap background = src; |
103 SkIPoint backgroundOffset = SkIPoint::Make(0, 0); | 112 SkIPoint backgroundOffset = SkIPoint::Make(0, 0); |
104 if (getInput(0) && !getInput(0)->getInputResultGPU(proxy, src, ctx, &backgro
und, | 113 if (getInput(0) && !getInput(0)->getInputResultGPU(proxy, src, ctx, &backgro
und, |
105 &backgroundOffset)) { | 114 &backgroundOffset)) { |
106 return false; | 115 return onFilterImage(proxy, src, ctx, result, offset); |
107 } | 116 } |
108 GrTexture* backgroundTex = background.getTexture(); | 117 GrTexture* backgroundTex = background.getTexture(); |
109 SkBitmap foreground = src; | 118 SkBitmap foreground = src; |
110 SkIPoint foregroundOffset = SkIPoint::Make(0, 0); | 119 SkIPoint foregroundOffset = SkIPoint::Make(0, 0); |
111 if (getInput(1) && !getInput(1)->getInputResultGPU(proxy, src, ctx, &foregro
und, | 120 if (getInput(1) && !getInput(1)->getInputResultGPU(proxy, src, ctx, &foregro
und, |
112 &foregroundOffset)) { | 121 &foregroundOffset)) { |
113 return false; | 122 return onFilterImage(proxy, src, ctx, result, offset); |
114 } | 123 } |
115 GrTexture* foregroundTex = foreground.getTexture(); | 124 GrTexture* foregroundTex = foreground.getTexture(); |
116 GrContext* context = foregroundTex->getContext(); | 125 GrContext* context = foregroundTex->getContext(); |
117 | 126 |
118 GrEffectRef* xferEffect = NULL; | 127 GrEffectRef* xferEffect = NULL; |
119 | 128 |
120 GrTextureDesc desc; | 129 GrTextureDesc desc; |
121 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; | 130 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
122 desc.fWidth = src.width(); | 131 desc.fWidth = src.width(); |
123 desc.fHeight = src.height(); | 132 desc.fHeight = src.height(); |
124 desc.fConfig = kSkia8888_GrPixelConfig; | 133 desc.fConfig = kSkia8888_GrPixelConfig; |
125 | 134 |
126 GrAutoScratchTexture ast(context, desc); | 135 GrAutoScratchTexture ast(context, desc); |
127 SkAutoTUnref<GrTexture> dst(ast.detach()); | 136 SkAutoTUnref<GrTexture> dst(ast.detach()); |
128 | 137 |
129 GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); | 138 GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); |
130 | 139 |
131 SkXfermode::Coeff sm, dm; | 140 if (!fMode || !fMode->asNewEffect(&xferEffect, backgroundTex)) { |
132 if (!SkXfermode::AsNewEffectOrCoeff(fMode, &xferEffect, &sm, &dm, background
Tex)) { | 141 // canFilterImageGPU() should've taken care of this |
| 142 SkASSERT(false); |
133 return false; | 143 return false; |
134 } | 144 } |
135 | 145 |
136 SkMatrix foregroundMatrix = GrEffect::MakeDivByTextureWHMatrix(foregroundTex
); | 146 SkMatrix foregroundMatrix = GrEffect::MakeDivByTextureWHMatrix(foregroundTex
); |
137 foregroundMatrix.preTranslate(SkIntToScalar(backgroundOffset.fX-foregroundOf
fset.fX), | 147 foregroundMatrix.preTranslate(SkIntToScalar(backgroundOffset.fX-foregroundOf
fset.fX), |
138 SkIntToScalar(backgroundOffset.fY-foregroundOf
fset.fY)); | 148 SkIntToScalar(backgroundOffset.fY-foregroundOf
fset.fY)); |
139 | 149 |
140 | 150 |
141 SkRect srcRect; | 151 SkRect srcRect; |
142 src.getBounds(&srcRect); | 152 src.getBounds(&srcRect); |
143 if (NULL != xferEffect) { | |
144 GrPaint paint; | |
145 paint.addColorTextureEffect(foregroundTex, foregroundMatrix); | |
146 paint.addColorEffect(xferEffect)->unref(); | |
147 context->drawRect(paint, srcRect); | |
148 } else { | |
149 GrPaint backgroundPaint; | |
150 SkMatrix backgroundMatrix = GrEffect::MakeDivByTextureWHMatrix(backgroun
dTex); | |
151 backgroundPaint.addColorTextureEffect(backgroundTex, backgroundMatrix); | |
152 context->drawRect(backgroundPaint, srcRect); | |
153 | 153 |
154 GrPaint foregroundPaint; | 154 GrPaint paint; |
155 foregroundPaint.setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblen
d(dm)); | 155 paint.addColorTextureEffect(foregroundTex, foregroundMatrix); |
156 foregroundPaint.addColorTextureEffect(foregroundTex, foregroundMatrix); | 156 paint.addColorEffect(xferEffect)->unref(); |
157 context->drawRect(foregroundPaint, srcRect); | 157 context->drawRect(paint, srcRect); |
158 } | 158 |
159 offset->fX = backgroundOffset.fX; | 159 offset->fX = backgroundOffset.fX; |
160 offset->fY = backgroundOffset.fY; | 160 offset->fY = backgroundOffset.fY; |
161 WrapTexture(dst, src.width(), src.height(), result); | 161 WrapTexture(dst, src.width(), src.height(), result); |
162 return true; | 162 return true; |
163 } | 163 } |
164 | 164 |
165 #endif | 165 #endif |
OLD | NEW |