OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkTestImageFilters.h" | 8 #include "SkTestImageFilters.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkDevice.h" |
10 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
11 #include "SkSpecialImage.h" | |
12 #include "SkSpecialSurface.h" | |
13 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
14 | 13 |
| 14 // Simple helper canvas that "takes ownership" of the provided device, so that |
| 15 // when this canvas goes out of scope, so will its device. Could be replaced |
| 16 // with the following: |
| 17 // |
| 18 // SkCanvas canvas(device); |
| 19 // SkAutoTUnref<SkBaseDevice> aur(device); |
| 20 // |
| 21 class OwnDeviceCanvas : public SkCanvas { |
| 22 public: |
| 23 OwnDeviceCanvas(SkBaseDevice* device) : SkCanvas(device) { |
| 24 SkSafeUnref(device); |
| 25 } |
| 26 }; |
| 27 |
15 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
16 | 29 |
17 sk_sp<SkSpecialImage> SkDownSampleImageFilter::onFilterImage(SkSpecialImage* sou
rce, | 30 bool SkDownSampleImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitm
ap& src, |
18 const Context& ctx, | 31 const Context&, |
19 SkIPoint* offset) c
onst { | 32 SkBitmap* result, SkIPoint
*) const { |
20 if (fScale > SK_Scalar1 || fScale <= 0) { | 33 SkScalar scale = fScale; |
21 return nullptr; | 34 if (scale > SK_Scalar1 || scale <= 0) { |
| 35 return false; |
22 } | 36 } |
23 | 37 |
24 int dstW = SkScalarRoundToInt(source->width() * fScale); | 38 int dstW = SkScalarRoundToInt(src.width() * scale); |
25 int dstH = SkScalarRoundToInt(source->height() * fScale); | 39 int dstH = SkScalarRoundToInt(src.height() * scale); |
26 if (dstW < 1) { | 40 if (dstW < 1) { |
27 dstW = 1; | 41 dstW = 1; |
28 } | 42 } |
29 if (dstH < 1) { | 43 if (dstH < 1) { |
30 dstH = 1; | 44 dstH = 1; |
31 } | 45 } |
32 | 46 |
33 sk_sp<SkSpecialImage> tmp; | 47 SkBitmap tmp; |
34 | 48 |
35 // downsample | 49 // downsample |
36 { | 50 { |
37 const SkImageInfo info = SkImageInfo::MakeN32Premul(dstW, dstH); | 51 SkBaseDevice* dev = proxy->createDevice(dstW, dstH); |
| 52 if (nullptr == dev) { |
| 53 return false; |
| 54 } |
| 55 OwnDeviceCanvas canvas(dev); |
| 56 SkPaint paint; |
38 | 57 |
39 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); | |
40 if (!surf) { | |
41 return nullptr; | |
42 } | |
43 | |
44 SkCanvas* canvas = surf->getCanvas(); | |
45 SkASSERT(canvas); | |
46 | |
47 SkPaint paint; | |
48 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
49 paint.setFilterQuality(kLow_SkFilterQuality); | 58 paint.setFilterQuality(kLow_SkFilterQuality); |
50 | 59 canvas.scale(scale, scale); |
51 canvas->scale(fScale, fScale); | 60 canvas.drawBitmap(src, 0, 0, &paint); |
52 source->draw(canvas, 0, 0, &paint); | 61 tmp = dev->accessBitmap(false); |
53 | |
54 tmp = surf->makeImageSnapshot(); | |
55 } | 62 } |
56 | 63 |
57 // upscale | 64 // upscale |
58 { | 65 { |
59 const SkImageInfo info = SkImageInfo::MakeN32Premul(source->width(), sou
rce->height()); | 66 SkBaseDevice* dev = proxy->createDevice(src.width(), src.height()); |
| 67 if (nullptr == dev) { |
| 68 return false; |
| 69 } |
| 70 OwnDeviceCanvas canvas(dev); |
60 | 71 |
61 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); | 72 canvas.drawBitmapRect(tmp, SkRect::MakeIWH(src.width(), src.height()), n
ullptr); |
62 if (!surf) { | 73 *result = dev->accessBitmap(false); |
63 return nullptr; | |
64 } | |
65 | |
66 SkCanvas* canvas = surf->getCanvas(); | |
67 SkASSERT(canvas); | |
68 | |
69 SkPaint paint; | |
70 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
71 | |
72 canvas->scale(SkScalarInvert(fScale), SkScalarInvert(fScale)); | |
73 tmp->draw(canvas, 0, 0, &paint); | |
74 | |
75 return surf->makeImageSnapshot(); | |
76 } | 74 } |
| 75 return true; |
77 } | 76 } |
78 | 77 |
79 sk_sp<SkFlattenable> SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { | 78 sk_sp<SkFlattenable> SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { |
80 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | 79 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
81 return sk_sp<SkFlattenable>(Create(buffer.readScalar(), common.getInput(0).g
et())); | 80 return sk_sp<SkFlattenable>(Create(buffer.readScalar(), common.getInput(0).g
et())); |
82 } | 81 } |
83 | 82 |
84 void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { | 83 void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { |
85 this->INHERITED::flatten(buffer); | 84 this->INHERITED::flatten(buffer); |
86 buffer.writeScalar(fScale); | 85 buffer.writeScalar(fScale); |
87 } | 86 } |
88 | 87 |
89 #ifndef SK_IGNORE_TO_STRING | 88 #ifndef SK_IGNORE_TO_STRING |
90 void SkDownSampleImageFilter::toString(SkString* str) const { | 89 void SkDownSampleImageFilter::toString(SkString* str) const { |
91 str->appendf("SkDownSampleImageFilter: ("); | 90 str->appendf("SkDownSampleImageFilter: ("); |
92 str->append(")"); | 91 str->append(")"); |
93 } | 92 } |
94 #endif | 93 #endif |
OLD | NEW |