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