OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "gm.h" | 8 #include "gm.h" |
| 9 #include "SkBitmapDevice.h" |
| 10 #include "SkBitmapSource.h" |
9 #include "SkColor.h" | 11 #include "SkColor.h" |
10 #include "SkResizeImageFilter.h" | 12 #include "SkResizeImageFilter.h" |
| 13 #include "SkRefCnt.h" |
11 | 14 |
12 namespace skiagm { | 15 namespace skiagm { |
13 | 16 |
14 class ResizeGM : public GM { | 17 class ResizeGM : public GM { |
15 public: | 18 public: |
16 ResizeGM() { | 19 ResizeGM() { |
17 this->setBGColor(0x00000000); | 20 this->setBGColor(0x00000000); |
18 } | 21 } |
19 | 22 |
20 protected: | 23 protected: |
21 virtual SkString onShortName() { | 24 virtual SkString onShortName() { |
22 return SkString("resizeimagefilter"); | 25 return SkString("resizeimagefilter"); |
23 } | 26 } |
24 | 27 |
25 void draw(SkCanvas* canvas, | 28 void draw(SkCanvas* canvas, |
26 const SkRect& rect, | 29 const SkRect& rect, |
27 const SkSize& deviceSize, | 30 const SkSize& deviceSize, |
28 SkPaint::FilterLevel filterLevel) { | 31 SkPaint::FilterLevel filterLevel, |
| 32 SkImageFilter* input = NULL) { |
29 SkRect dstRect; | 33 SkRect dstRect; |
30 canvas->getTotalMatrix().mapRect(&dstRect, rect); | 34 canvas->getTotalMatrix().mapRect(&dstRect, rect); |
31 canvas->save(); | 35 canvas->save(); |
32 SkScalar deviceScaleX = SkScalarDiv(deviceSize.width(), dstRect.width())
; | 36 SkScalar deviceScaleX = SkScalarDiv(deviceSize.width(), dstRect.width())
; |
33 SkScalar deviceScaleY = SkScalarDiv(deviceSize.height(), dstRect.height(
)); | 37 SkScalar deviceScaleY = SkScalarDiv(deviceSize.height(), dstRect.height(
)); |
34 canvas->translate(rect.x(), rect.y()); | 38 canvas->translate(rect.x(), rect.y()); |
35 canvas->scale(deviceScaleX, deviceScaleY); | 39 canvas->scale(deviceScaleX, deviceScaleY); |
36 canvas->translate(-rect.x(), -rect.y()); | 40 canvas->translate(-rect.x(), -rect.y()); |
37 SkAutoTUnref<SkImageFilter> imageFilter( | 41 SkAutoTUnref<SkImageFilter> imageFilter( |
38 SkResizeImageFilter::Create(SkScalarInvert(deviceScaleX), | 42 SkResizeImageFilter::Create(SkScalarInvert(deviceScaleX), |
39 SkScalarInvert(deviceScaleY), | 43 SkScalarInvert(deviceScaleY), |
40 filterLevel)); | 44 filterLevel, |
| 45 input)); |
41 SkPaint filteredPaint; | 46 SkPaint filteredPaint; |
42 filteredPaint.setImageFilter(imageFilter.get()); | 47 filteredPaint.setImageFilter(imageFilter.get()); |
43 canvas->saveLayer(&rect, &filteredPaint); | 48 canvas->saveLayer(&rect, &filteredPaint); |
44 SkPaint paint; | 49 SkPaint paint; |
45 paint.setColor(0xFF00FF00); | 50 paint.setColor(0xFF00FF00); |
46 SkRect ovalRect = rect; | 51 SkRect ovalRect = rect; |
47 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4)); | 52 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4)); |
48 canvas->drawOval(ovalRect, paint); | 53 canvas->drawOval(ovalRect, paint); |
49 canvas->restore(); // for saveLayer | 54 canvas->restore(); // for saveLayer |
50 canvas->restore(); | 55 canvas->restore(); |
51 } | 56 } |
52 | 57 |
53 virtual SkISize onISize() { | 58 virtual SkISize onISize() { |
54 return make_isize(420, 100); | 59 return make_isize(520, 100); |
55 } | 60 } |
56 | 61 |
57 virtual void onDraw(SkCanvas* canvas) { | 62 virtual void onDraw(SkCanvas* canvas) { |
58 canvas->clear(0x00000000); | 63 canvas->clear(0x00000000); |
59 | 64 |
60 SkRect srcRect = SkRect::MakeWH(96, 96); | 65 SkRect srcRect = SkRect::MakeWH(96, 96); |
61 | 66 |
62 SkSize deviceSize = SkSize::Make(16, 16); | 67 SkSize deviceSize = SkSize::Make(16, 16); |
63 draw(canvas, | 68 draw(canvas, |
64 srcRect, | 69 srcRect, |
(...skipping 10 matching lines...) Expand all Loading... |
75 draw(canvas, | 80 draw(canvas, |
76 srcRect, | 81 srcRect, |
77 deviceSize, | 82 deviceSize, |
78 SkPaint::kMedium_FilterLevel); | 83 SkPaint::kMedium_FilterLevel); |
79 | 84 |
80 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); | 85 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); |
81 draw(canvas, | 86 draw(canvas, |
82 srcRect, | 87 srcRect, |
83 deviceSize, | 88 deviceSize, |
84 SkPaint::kHigh_FilterLevel); | 89 SkPaint::kHigh_FilterLevel); |
| 90 |
| 91 SkBitmap bitmap; |
| 92 bitmap.allocN32Pixels(16, 16); |
| 93 bitmap.eraseARGB(0x00, 0x00, 0x00, 0x00); |
| 94 { |
| 95 SkBitmapDevice bitmapDevice(bitmap); |
| 96 SkCanvas bitmapCanvas(&bitmapDevice); |
| 97 SkPaint paint; |
| 98 paint.setColor(0xFF00FF00); |
| 99 SkRect ovalRect = SkRect::MakeWH(16, 16); |
| 100 ovalRect.inset(SkScalarDiv(2.0f, 3.0f), SkScalarDiv(2.0f, 3.0f)); |
| 101 bitmapCanvas.drawOval(ovalRect, paint); |
| 102 } |
| 103 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20); |
| 104 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120); |
| 105 SkAutoTUnref<SkBitmapSource> source(SkBitmapSource::Create(bitmap, inRec
t, outRect)); |
| 106 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); |
| 107 draw(canvas, |
| 108 srcRect, |
| 109 deviceSize, |
| 110 SkPaint::kHigh_FilterLevel, |
| 111 source.get()); |
85 } | 112 } |
86 | 113 |
87 private: | 114 private: |
88 typedef GM INHERITED; | 115 typedef GM INHERITED; |
89 }; | 116 }; |
90 | 117 |
91 ////////////////////////////////////////////////////////////////////////////// | 118 ////////////////////////////////////////////////////////////////////////////// |
92 | 119 |
93 static GM* MyFactory(void*) { return new ResizeGM; } | 120 static GM* MyFactory(void*) { return new ResizeGM; } |
94 static GMRegistry reg(MyFactory); | 121 static GMRegistry reg(MyFactory); |
95 | 122 |
96 } | 123 } |
OLD | NEW |