OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 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 "SkOffsetImageFilter.h" | 8 #include "SkOffsetImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
12 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 #include "SkMatrix.h" | 14 #include "SkMatrix.h" |
15 #include "SkPaint.h" | 15 #include "SkPaint.h" |
| 16 #include "SkSpecialImage.h" |
| 17 #include "SkSpecialSurface.h" |
| 18 |
| 19 SkSpecialImage* SkOffsetImageFilter::onFilterImage(SkSpecialImage* srcIn, |
| 20 const Context& ctx, |
| 21 SkIPoint* offset) const { |
| 22 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 23 SkAutoTUnref<SkSpecialImage> src(this->filterInput(0, srcIn, ctx, &srcOffset
)); |
| 24 if (!src) { |
| 25 return nullptr; |
| 26 } |
| 27 |
| 28 SkVector vec; |
| 29 ctx.ctm().mapVectors(&vec, &fOffset, 1); |
| 30 |
| 31 if (!this->cropRectIsSet()) { |
| 32 offset->fX = srcOffset.fX + SkScalarRoundToInt(vec.fX); |
| 33 offset->fY = srcOffset.fY + SkScalarRoundToInt(vec.fY); |
| 34 return src.release(); |
| 35 } else { |
| 36 SkIRect bounds; |
| 37 SkIRect srcBounds = SkIRect::MakeWH(srcIn->width(), srcIn->height()); |
| 38 srcBounds.offset(srcOffset); |
| 39 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
| 40 return false; |
| 41 } |
| 42 |
| 43 SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 44 kPremul_SkAlphaType); |
| 45 SkAutoTUnref<SkSpecialSurface> surf(src->newSurface(info)); |
| 46 if (!surf) { |
| 47 return nullptr; |
| 48 } |
| 49 |
| 50 SkCanvas* canvas = surf->getCanvas(); |
| 51 |
| 52 canvas->clear(0x0); |
| 53 |
| 54 SkPaint paint; |
| 55 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 56 canvas->translate(SkIntToScalar(srcOffset.fX - bounds.fLeft), |
| 57 SkIntToScalar(srcOffset.fY - bounds.fTop)); |
| 58 |
| 59 src->draw(canvas, vec.x(), vec.y(), &paint); |
| 60 |
| 61 offset->fX = bounds.fLeft; |
| 62 offset->fY = bounds.fTop; |
| 63 return surf->newImageSnapshot(); |
| 64 } |
| 65 } |
16 | 66 |
17 bool SkOffsetImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap&
source, | 67 bool SkOffsetImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap&
source, |
18 const Context& ctx, | 68 const Context& ctx, |
19 SkBitmap* result, | 69 SkBitmap* result, |
20 SkIPoint* offset) const { | 70 SkIPoint* offset) const { |
21 SkBitmap src = source; | 71 SkAutoTUnref<SkSpecialImage> specialSrc(SkSpecialImage::internal_fromBM(prox
y, source)); |
22 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 72 if (!specialSrc) { |
23 if (!cropRectIsSet()) { | 73 return false; |
24 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset
)) { | 74 } |
25 return false; | |
26 } | |
27 | 75 |
28 SkVector vec; | 76 SkAutoTUnref<SkSpecialImage> tmp(this->onFilterImage(specialSrc, ctx, offset
)); |
29 ctx.ctm().mapVectors(&vec, &fOffset, 1); | 77 if (!tmp) { |
| 78 return false; |
| 79 } |
30 | 80 |
31 offset->fX = srcOffset.fX + SkScalarRoundToInt(vec.fX); | 81 return tmp->internal_getBM(result); |
32 offset->fY = srcOffset.fY + SkScalarRoundToInt(vec.fY); | |
33 *result = src; | |
34 } else { | |
35 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset
)) { | |
36 return false; | |
37 } | |
38 | |
39 SkIRect bounds; | |
40 SkIRect srcBounds = src.bounds(); | |
41 srcBounds.offset(srcOffset); | |
42 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { | |
43 return false; | |
44 } | |
45 | |
46 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bo
unds.height())); | |
47 if (nullptr == device.get()) { | |
48 return false; | |
49 } | |
50 SkCanvas canvas(device); | |
51 SkPaint paint; | |
52 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
53 canvas.translate(SkIntToScalar(srcOffset.fX - bounds.fLeft), | |
54 SkIntToScalar(srcOffset.fY - bounds.fTop)); | |
55 SkVector vec; | |
56 ctx.ctm().mapVectors(&vec, &fOffset, 1); | |
57 canvas.drawBitmap(src, vec.x(), vec.y(), &paint); | |
58 *result = device->accessBitmap(false); | |
59 offset->fX = bounds.fLeft; | |
60 offset->fY = bounds.fTop; | |
61 } | |
62 return true; | |
63 } | 82 } |
64 | 83 |
65 void SkOffsetImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) cons
t { | 84 void SkOffsetImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) cons
t { |
66 if (getInput(0)) { | 85 if (getInput(0)) { |
67 getInput(0)->computeFastBounds(src, dst); | 86 getInput(0)->computeFastBounds(src, dst); |
68 } else { | 87 } else { |
69 *dst = src; | 88 *dst = src; |
70 } | 89 } |
71 dst->offset(fOffset.fX, fOffset.fY); | 90 dst->offset(fOffset.fX, fOffset.fY); |
72 } | 91 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 void SkOffsetImageFilter::toString(SkString* str) const { | 124 void SkOffsetImageFilter::toString(SkString* str) const { |
106 str->appendf("SkOffsetImageFilter: ("); | 125 str->appendf("SkOffsetImageFilter: ("); |
107 str->appendf("offset: (%f, %f) ", fOffset.fX, fOffset.fY); | 126 str->appendf("offset: (%f, %f) ", fOffset.fX, fOffset.fY); |
108 str->append("input: ("); | 127 str->append("input: ("); |
109 if (this->getInput(0)) { | 128 if (this->getInput(0)) { |
110 this->getInput(0)->toString(str); | 129 this->getInput(0)->toString(str); |
111 } | 130 } |
112 str->append("))"); | 131 str->append("))"); |
113 } | 132 } |
114 #endif | 133 #endif |
OLD | NEW |