1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright 2013 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkXfermodeImageFilter.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkColorPriv.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkXfermode.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "effects/GrSimpleTextureEffect.h"
#include "SkGr.h"
#endif
///////////////////////////////////////////////////////////////////////////////
SkXfermodeImageFilter::SkXfermodeImageFilter(SkXfermode* mode,
SkImageFilter* inputs[2],
const CropRect* cropRect,
uint32_t uniqueID)
: INHERITED(2, inputs, cropRect, uniqueID), fMode(mode) {
SkSafeRef(fMode);
}
SkXfermodeImageFilter::~SkXfermodeImageFilter() {
SkSafeUnref(fMode);
}
#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
SkXfermodeImageFilter::SkXfermodeImageFilter(SkReadBuffer& buffer)
: INHERITED(2, buffer) {
fMode = buffer.readXfermode();
}
#endif
SkFlattenable* SkXfermodeImageFilter::CreateProc(SkReadBuffer& buffer) {
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2);
SkAutoTUnref<SkXfermode> mode(buffer.readXfermode());
return Create(mode, common.getInput(0), common.getInput(1), &common.cropRect(), common.uniqueID());
}
void SkXfermodeImageFilter::flatten(SkWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.writeFlattenable(fMode);
}
bool SkXfermodeImageFilter::onFilterImage(Proxy* proxy,
const SkBitmap& src,
const Context& ctx,
SkBitmap* dst,
SkIPoint* offset) const {
SkBitmap background = src, foreground = src;
SkImageFilter* backgroundInput = getInput(0);
SkImageFilter* foregroundInput = getInput(1);
SkIPoint backgroundOffset = SkIPoint::Make(0, 0);
if (backgroundInput &&
!backgroundInput->filterImage(proxy, src, ctx, &background, &backgroundOffset)) {
background.reset();
}
SkIPoint foregroundOffset = SkIPoint::Make(0, 0);
if (foregroundInput &&
!foregroundInput->filterImage(proxy, src, ctx, &foreground, &foregroundOffset)) {
foreground.reset();
}
SkIRect bounds, foregroundBounds;
if (!applyCropRect(ctx, foreground, foregroundOffset, &foregroundBounds)) {
foregroundBounds.setEmpty();
foreground.reset();
}
if (!applyCropRect(ctx, background, backgroundOffset, &bounds)) {
bounds.setEmpty();
background.reset();
}
bounds.join(foregroundBounds);
if (bounds.isEmpty()) {
return false;
}
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
if (NULL == device.get()) {
return false;
}
SkCanvas canvas(device);
canvas.translate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
canvas.drawBitmap(background, SkIntToScalar(backgroundOffset.fX),
SkIntToScalar(backgroundOffset.fY), &paint);
paint.setXfermode(fMode);
canvas.drawBitmap(foreground, SkIntToScalar(foregroundOffset.fX),
SkIntToScalar(foregroundOffset.fY), &paint);
canvas.clipRect(SkRect::Make(foregroundBounds), SkRegion::kDifference_Op);
paint.setColor(SK_ColorTRANSPARENT);
canvas.drawPaint(paint);
*dst = device->accessBitmap(false);
offset->fX = bounds.left();
offset->fY = bounds.top();
return true;
}
#if SK_SUPPORT_GPU
bool SkXfermodeImageFilter::canFilterImageGPU() const {
return fMode && fMode->asNewEffect(NULL, NULL) && !cropRectIsSet();
}
bool SkXfermodeImageFilter::filterImageGPU(Proxy* proxy,
const SkBitmap& src,
const Context& ctx,
SkBitmap* result,
SkIPoint* offset) const {
SkBitmap background = src;
SkIPoint backgroundOffset = SkIPoint::Make(0, 0);
if (getInput(0) && !getInput(0)->getInputResultGPU(proxy, src, ctx, &background,
&backgroundOffset)) {
return onFilterImage(proxy, src, ctx, result, offset);
}
GrTexture* backgroundTex = background.getTexture();
SkBitmap foreground = src;
SkIPoint foregroundOffset = SkIPoint::Make(0, 0);
if (getInput(1) && !getInput(1)->getInputResultGPU(proxy, src, ctx, &foreground,
&foregroundOffset)) {
return onFilterImage(proxy, src, ctx, result, offset);
}
GrTexture* foregroundTex = foreground.getTexture();
GrContext* context = foregroundTex->getContext();
GrEffect* xferEffect = NULL;
GrTextureDesc desc;
desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
desc.fWidth = src.width();
desc.fHeight = src.height();
desc.fConfig = kSkia8888_GrPixelConfig;
GrAutoScratchTexture ast(context, desc);
if (NULL == ast.texture()) {
return false;
}
SkAutoTUnref<GrTexture> dst(ast.detach());
GrContext::AutoRenderTarget art(context, dst->asRenderTarget());
if (!fMode || !fMode->asNewEffect(&xferEffect, backgroundTex)) {
// canFilterImageGPU() should've taken care of this
SkASSERT(false);
return false;
}
SkMatrix foregroundMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(foregroundTex);
foregroundMatrix.preTranslate(SkIntToScalar(backgroundOffset.fX-foregroundOffset.fX),
SkIntToScalar(backgroundOffset.fY-foregroundOffset.fY));
SkRect srcRect;
src.getBounds(&srcRect);
GrPaint paint;
paint.addColorTextureEffect(foregroundTex, foregroundMatrix);
paint.addColorEffect(xferEffect)->unref();
context->drawRect(paint, srcRect);
offset->fX = backgroundOffset.fX;
offset->fY = backgroundOffset.fY;
WrapTexture(dst, src.width(), src.height(), result);
return true;
}
#endif
|