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.setFilterQuality(kLow_SkFilterQuality); | 58 paint.setFilterQuality(kLow_SkFilterQuality); |
49 | 59 canvas.scale(scale, scale); |
50 canvas->scale(fScale, fScale); | 60 canvas.drawBitmap(src, 0, 0, &paint); |
51 source->draw(canvas, 0, 0, &paint); | 61 tmp = dev->accessBitmap(false); |
52 | |
53 tmp = surf->makeImageSnapshot(); | |
54 } | 62 } |
55 | 63 |
56 // upscale | 64 // upscale |
57 { | 65 { |
58 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); |
59 | 71 |
60 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); | 72 canvas.drawBitmapRect(tmp, SkRect::MakeIWH(src.width(), src.height()), n
ullptr); |
61 if (!surf) { | 73 *result = dev->accessBitmap(false); |
62 return nullptr; | |
63 } | |
64 | |
65 SkCanvas* canvas = surf->getCanvas(); | |
66 SkASSERT(canvas); | |
67 | |
68 canvas->scale(SkScalarInvert(fScale), SkScalarInvert(fScale)); | |
69 tmp->draw(canvas, 0, 0, nullptr); | |
70 | |
71 return surf->makeImageSnapshot(); | |
72 } | 74 } |
| 75 return true; |
73 } | 76 } |
74 | 77 |
75 SkFlattenable* SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { | 78 SkFlattenable* SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) { |
76 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | 79 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
77 return Create(buffer.readScalar(), common.getInput(0).get()); | 80 return Create(buffer.readScalar(), common.getInput(0).get()); |
78 } | 81 } |
79 | 82 |
80 void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { | 83 void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const { |
81 this->INHERITED::flatten(buffer); | 84 this->INHERITED::flatten(buffer); |
82 buffer.writeScalar(fScale); | 85 buffer.writeScalar(fScale); |
83 } | 86 } |
84 | 87 |
85 #ifndef SK_IGNORE_TO_STRING | 88 #ifndef SK_IGNORE_TO_STRING |
86 void SkDownSampleImageFilter::toString(SkString* str) const { | 89 void SkDownSampleImageFilter::toString(SkString* str) const { |
87 str->appendf("SkDownSampleImageFilter: ("); | 90 str->appendf("SkDownSampleImageFilter: ("); |
88 str->append(")"); | 91 str->append(")"); |
89 } | 92 } |
90 #endif | 93 #endif |
OLD | NEW |