OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
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 "SkPaintImageFilter.h" | 8 #include "SkPaintImageFilter.h" |
9 #include "SkBitmap.h" | |
10 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
11 #include "SkDevice.h" | |
12 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkSpecialImage.h" |
| 12 #include "SkSpecialSurface.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 | 14 |
15 SkImageFilter* SkPaintImageFilter::Create(const SkPaint& paint, const CropRect*
cropRect) { | 15 SkImageFilter* SkPaintImageFilter::Create(const SkPaint& paint, const CropRect*
cropRect) { |
16 return new SkPaintImageFilter(paint, cropRect); | 16 return new SkPaintImageFilter(paint, cropRect); |
17 } | 17 } |
18 | 18 |
19 SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cro
pRect) | 19 SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cro
pRect) |
20 : INHERITED(0, nullptr, cropRect) | 20 : INHERITED(0, nullptr, cropRect) |
21 , fPaint(paint) { | 21 , fPaint(paint) { |
22 } | 22 } |
23 | 23 |
24 SkFlattenable* SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) { | 24 SkFlattenable* SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) { |
25 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); | 25 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
26 SkPaint paint; | 26 SkPaint paint; |
27 buffer.readPaint(&paint); | 27 buffer.readPaint(&paint); |
28 return Create(paint, &common.cropRect()); | 28 return Create(paint, &common.cropRect()); |
29 } | 29 } |
30 | 30 |
31 void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const { | 31 void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const { |
32 this->INHERITED::flatten(buffer); | 32 this->INHERITED::flatten(buffer); |
33 buffer.writePaint(fPaint); | 33 buffer.writePaint(fPaint); |
34 } | 34 } |
35 | 35 |
36 bool SkPaintImageFilter::onFilterImageDeprecated(Proxy* proxy, | 36 SkSpecialImage* SkPaintImageFilter::onFilterImage(SkSpecialImage* source, |
37 const SkBitmap& source, | 37 const Context& ctx, |
38 const Context& ctx, | 38 SkIPoint* offset) const { |
39 SkBitmap* result, | |
40 SkIPoint* offset) const { | |
41 SkIRect bounds; | 39 SkIRect bounds; |
42 if (!this->applyCropRect(ctx, source.bounds(), &bounds)) { | 40 const SkIRect srcBounds = SkIRect::MakeWH(source->width(), source->height())
; |
43 return false; | 41 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
| 42 return nullptr; |
44 } | 43 } |
45 | 44 |
46 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), | 45 SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
47 bounds.height())); | 46 kPremul_SkAlphaType); |
48 if (nullptr == device.get()) { | 47 |
49 return false; | 48 SkAutoTUnref<SkSpecialSurface> surf(source->newSurface(info)); |
| 49 if (!surf) { |
| 50 return nullptr; |
50 } | 51 } |
51 SkCanvas canvas(device.get()); | 52 |
| 53 SkCanvas* canvas = surf->getCanvas(); |
| 54 SkASSERT(canvas); |
| 55 |
| 56 canvas->clear(0x0); |
52 | 57 |
53 SkMatrix matrix(ctx.ctm()); | 58 SkMatrix matrix(ctx.ctm()); |
54 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to
p())); | 59 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to
p())); |
55 SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bo
unds.height())); | 60 SkRect rect = SkRect::MakeIWH(bounds.width(), bounds.height()); |
56 SkMatrix inverse; | 61 SkMatrix inverse; |
57 if (matrix.invert(&inverse)) { | 62 if (matrix.invert(&inverse)) { |
58 inverse.mapRect(&rect); | 63 inverse.mapRect(&rect); |
59 } | 64 } |
60 canvas.setMatrix(matrix); | 65 canvas->setMatrix(matrix); |
61 canvas.drawRect(rect, fPaint); | 66 canvas->drawRect(rect, fPaint); |
62 | 67 |
63 *result = device.get()->accessBitmap(false); | |
64 offset->fX = bounds.fLeft; | 68 offset->fX = bounds.fLeft; |
65 offset->fY = bounds.fTop; | 69 offset->fY = bounds.fTop; |
66 return true; | 70 return surf->newImageSnapshot(); |
67 } | 71 } |
68 | 72 |
69 bool SkPaintImageFilter::canComputeFastBounds() const { | 73 bool SkPaintImageFilter::canComputeFastBounds() const { |
70 // http:skbug.com/4627: "make computeFastBounds and onFilterBounds() CropRec
t-aware" | 74 // http:skbug.com/4627: "make computeFastBounds and onFilterBounds() CropRec
t-aware" |
71 // computeFastBounds() doesn't currently take the crop rect into account, | 75 // computeFastBounds() doesn't currently take the crop rect into account, |
72 // so we can't compute it. If a full crop rect is set, we should return true
here. | 76 // so we can't compute it. If a full crop rect is set, we should return true
here. |
73 return false; | 77 return false; |
74 } | 78 } |
75 | 79 |
76 #ifndef SK_IGNORE_TO_STRING | 80 #ifndef SK_IGNORE_TO_STRING |
77 void SkPaintImageFilter::toString(SkString* str) const { | 81 void SkPaintImageFilter::toString(SkString* str) const { |
78 str->appendf("SkPaintImageFilter: ("); | 82 str->appendf("SkPaintImageFilter: ("); |
79 str->append(")"); | 83 str->append(")"); |
80 } | 84 } |
81 #endif | 85 #endif |
OLD | NEW |