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