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 "sk_tool_utils.h" | 8 #include "sk_tool_utils.h" |
9 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
10 #include "SkOffsetImageFilter.h" | 10 #include "SkOffsetImageFilter.h" |
11 #include "gm.h" | 11 #include "gm.h" |
12 | 12 |
13 #define WIDTH 600 | 13 #define WIDTH 600 |
14 #define HEIGHT 100 | 14 #define HEIGHT 100 |
15 #define MARGIN 12 | 15 #define MARGIN 12 |
16 | 16 |
17 namespace skiagm { | 17 class OffsetImageFilterGM : public skiagm::GM { |
18 | |
19 class OffsetImageFilterGM : public GM { | |
20 public: | 18 public: |
21 OffsetImageFilterGM() : fInitialized(false) { | 19 OffsetImageFilterGM() { |
22 this->setBGColor(0xFF000000); | 20 this->setBGColor(0xFF000000); |
23 } | 21 } |
24 | 22 |
25 protected: | 23 protected: |
26 virtual SkString onShortName() { | 24 SkString onShortName() override { |
27 return SkString("offsetimagefilter"); | 25 return SkString("offsetimagefilter"); |
28 } | 26 } |
29 | 27 |
30 void make_bitmap() { | 28 void make_bitmap() { |
31 fBitmap.allocN32Pixels(80, 80); | 29 fBitmap.allocN32Pixels(80, 80); |
32 SkCanvas canvas(fBitmap); | 30 SkCanvas canvas(fBitmap); |
33 canvas.clear(0x00000000); | 31 canvas.clear(0); |
34 SkPaint paint; | 32 SkPaint paint; |
35 paint.setAntiAlias(true); | 33 paint.setAntiAlias(true); |
36 sk_tool_utils::set_portable_typeface(&paint); | 34 sk_tool_utils::set_portable_typeface(&paint); |
37 paint.setColor(0xD000D000); | 35 paint.setColor(0xD000D000); |
38 paint.setTextSize(SkIntToScalar(96)); | 36 paint.setTextSize(96); |
39 const char* str = "e"; | 37 canvas.drawText("e", 1, 15, 65, paint); |
40 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65),
paint); | |
41 } | 38 } |
42 | 39 |
43 virtual SkISize onISize() { | 40 SkISize onISize() override { |
44 return SkISize::Make(WIDTH, HEIGHT); | 41 return SkISize::Make(WIDTH, HEIGHT); |
45 } | 42 } |
46 | 43 |
47 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai
nt& paint, SkScalar scale, const SkIRect& cropRect) { | 44 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai
nt& paint, |
| 45 SkScalar scale, const SkIRect& cropRect) { |
| 46 SkRect clipRect = SkRect::MakeIWH(bitmap.width(), bitmap.height()); |
| 47 |
48 canvas->save(); | 48 canvas->save(); |
49 SkRect clipRect = SkRect::MakeWH( | |
50 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); | |
51 canvas->clipRect(clipRect); | 49 canvas->clipRect(clipRect); |
52 canvas->scale(scale, scale); | 50 canvas->scale(scale, scale); |
53 canvas->drawBitmap(bitmap, 0, 0, &paint); | 51 canvas->drawBitmap(bitmap, 0, 0, &paint); |
54 canvas->restore(); | 52 canvas->restore(); |
55 SkPaint strokePaint; | |
56 strokePaint.setStyle(SkPaint::kStroke_Style); | |
57 strokePaint.setColor(SK_ColorRED); | |
58 | 53 |
59 // Draw a boundary rect around the intersection of the clip rect | 54 // Draw a boundary rect around the intersection of the clip rect and cro
p rect. |
60 // and crop rect. | |
61 SkMatrix scaleMatrix; | |
62 scaleMatrix.setScale(scale, scale); | |
63 SkRect cropRectFloat; | 55 SkRect cropRectFloat; |
64 scaleMatrix.mapRect(&cropRectFloat, SkRect::Make(cropRect)); | 56 SkMatrix::MakeScale(scale, scale).mapRect(&cropRectFloat, SkRect::Make(c
ropRect)); |
65 if (clipRect.intersect(cropRectFloat)) { | 57 if (clipRect.intersect(cropRectFloat)) { |
| 58 SkPaint strokePaint; |
| 59 strokePaint.setStyle(SkPaint::kStroke_Style); |
| 60 strokePaint.setColor(SK_ColorRED); |
66 canvas->drawRect(clipRect, strokePaint); | 61 canvas->drawRect(clipRect, strokePaint); |
67 } | 62 } |
68 } | 63 } |
69 | 64 |
70 virtual void onDraw(SkCanvas* canvas) { | 65 void onOnceBeforeDraw() override { |
71 if (!fInitialized) { | 66 make_bitmap(); |
72 make_bitmap(); | 67 |
| 68 fCheckerboard.allocN32Pixels(80, 80); |
| 69 SkCanvas checkerboardCanvas(fCheckerboard); |
| 70 sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF40
4040, 8); |
| 71 } |
73 | 72 |
74 fCheckerboard.allocN32Pixels(80, 80); | 73 void onDraw(SkCanvas* canvas) override { |
75 SkCanvas checkerboardCanvas(fCheckerboard); | |
76 sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0x
FF404040, 8); | |
77 | |
78 fInitialized = true; | |
79 } | |
80 canvas->clear(SK_ColorBLACK); | 74 canvas->clear(SK_ColorBLACK); |
81 SkPaint paint; | 75 SkPaint paint; |
82 | 76 |
83 for (int i = 0; i < 4; i++) { | 77 for (int i = 0; i < 4; i++) { |
84 SkBitmap* bitmap = (i & 0x01) ? &fCheckerboard : &fBitmap; | 78 const SkBitmap* bitmap = (i & 0x01) ? &fCheckerboard : &fBitmap; |
85 SkIRect cropRect = SkIRect::MakeXYWH(i * 12, | 79 SkIRect cropRect = SkIRect::MakeXYWH(i * 12, |
86 i * 8, | 80 i * 8, |
87 bitmap->width() - i * 8, | 81 bitmap->width() - i * 8, |
88 bitmap->height() - i * 12); | 82 bitmap->height() - i * 12); |
89 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 83 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
90 SkAutoTUnref<SkImageFilter> tileInput(SkBitmapSource::Create(*bitmap
)); | 84 SkAutoTUnref<SkImageFilter> tileInput(SkBitmapSource::Create(*bitmap
)); |
91 SkScalar dx = SkIntToScalar(i*5); | 85 SkScalar dx = SkIntToScalar(i*5); |
92 SkScalar dy = SkIntToScalar(i*10); | 86 SkScalar dy = SkIntToScalar(i*10); |
93 SkAutoTUnref<SkImageFilter> filter( | 87 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, d
y, tileInput, |
94 SkOffsetImageFilter::Create(dx, dy, tileInput, &rect)); | 88 &rect
)); |
95 paint.setImageFilter(filter); | 89 paint.setImageFilter(filter); |
96 drawClippedBitmap(canvas, *bitmap, paint, SK_Scalar1, cropRect); | 90 drawClippedBitmap(canvas, *bitmap, paint, 1, cropRect); |
97 canvas->translate(SkIntToScalar(bitmap->width() + MARGIN), 0); | 91 canvas->translate(SkIntToScalar(bitmap->width() + MARGIN), 0); |
98 } | 92 } |
99 | 93 |
100 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); | 94 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); |
101 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 95 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
102 SkAutoTUnref<SkImageFilter> filter( | 96 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10,
NULL, &rect)); |
103 SkOffsetImageFilter::Create(SkIntToScalar(-5), SkIntToScalar(-10), N
ULL, &rect)); | |
104 paint.setImageFilter(filter); | 97 paint.setImageFilter(filter); |
105 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(2), cropRect); | 98 drawClippedBitmap(canvas, fBitmap, paint, 2, cropRect); |
106 } | 99 } |
107 private: | 100 private: |
108 typedef GM INHERITED; | 101 typedef skiagm::GM INHERITED; |
109 SkBitmap fBitmap, fCheckerboard; | 102 SkBitmap fBitmap, fCheckerboard; |
110 bool fInitialized; | |
111 }; | 103 }; |
| 104 DEF_GM( return new OffsetImageFilterGM; ) |
112 | 105 |
113 ////////////////////////////////////////////////////////////////////////////// | 106 ////////////////////////////////////////////////////////////////////////////// |
114 | 107 |
115 static GM* MyFactory(void*) { return new OffsetImageFilterGM; } | |
116 static GMRegistry reg(MyFactory); | |
117 | |
118 } | |
OLD | NEW |