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 "SkImage.h" |
10 #include "SkImageSource.h" | |
10 #include "SkOffsetImageFilter.h" | 11 #include "SkOffsetImageFilter.h" |
12 #include "SkSurface.h" | |
11 #include "gm.h" | 13 #include "gm.h" |
12 | 14 |
13 #define WIDTH 600 | 15 #define WIDTH 600 |
14 #define HEIGHT 100 | 16 #define HEIGHT 100 |
15 #define MARGIN 12 | 17 #define MARGIN 12 |
16 | 18 |
17 class OffsetImageFilterGM : public skiagm::GM { | 19 class OffsetImageFilterGM : public skiagm::GM { |
18 public: | 20 public: |
19 OffsetImageFilterGM() { | 21 OffsetImageFilterGM() { |
20 this->setBGColor(0xFF000000); | 22 this->setBGColor(0xFF000000); |
21 } | 23 } |
22 | 24 |
23 protected: | 25 protected: |
24 SkString onShortName() override { | 26 SkString onShortName() override { |
25 return SkString("offsetimagefilter"); | 27 return SkString("offsetimagefilter"); |
26 } | 28 } |
27 | 29 |
28 SkISize onISize() override { | 30 SkISize onISize() override { |
29 return SkISize::Make(WIDTH, HEIGHT); | 31 return SkISize::Make(WIDTH, HEIGHT); |
30 } | 32 } |
31 | 33 |
robertphillips
2015/09/15 17:43:52
can this be static ?
f(malita)
2015/09/15 18:12:53
Done.
| |
32 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai nt& paint, | 34 void drawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint, |
33 SkScalar scale, const SkIRect& cropRect) { | 35 SkScalar scale, const SkIRect& cropRect) { |
34 SkRect clipRect = SkRect::MakeIWH(bitmap.width(), bitmap.height()); | 36 SkRect clipRect = SkRect::MakeIWH(image->width(), image->height()); |
35 | 37 |
36 canvas->save(); | 38 canvas->save(); |
37 canvas->clipRect(clipRect); | 39 canvas->clipRect(clipRect); |
38 canvas->scale(scale, scale); | 40 canvas->scale(scale, scale); |
39 canvas->drawBitmap(bitmap, 0, 0, &paint); | 41 canvas->drawImage(image, 0, 0, &paint); |
40 canvas->restore(); | 42 canvas->restore(); |
41 | 43 |
42 // Draw a boundary rect around the intersection of the clip rect and cro p rect. | 44 // Draw a boundary rect around the intersection of the clip rect and cro p rect. |
43 SkRect cropRectFloat; | 45 SkRect cropRectFloat; |
44 SkMatrix::MakeScale(scale, scale).mapRect(&cropRectFloat, SkRect::Make(c ropRect)); | 46 SkMatrix::MakeScale(scale, scale).mapRect(&cropRectFloat, SkRect::Make(c ropRect)); |
45 if (clipRect.intersect(cropRectFloat)) { | 47 if (clipRect.intersect(cropRectFloat)) { |
46 SkPaint strokePaint; | 48 SkPaint strokePaint; |
47 strokePaint.setStyle(SkPaint::kStroke_Style); | 49 strokePaint.setStyle(SkPaint::kStroke_Style); |
48 strokePaint.setStrokeWidth(2); | 50 strokePaint.setStrokeWidth(2); |
49 strokePaint.setColor(SK_ColorRED); | 51 strokePaint.setColor(SK_ColorRED); |
50 canvas->drawRect(clipRect, strokePaint); | 52 canvas->drawRect(clipRect, strokePaint); |
51 } | 53 } |
52 } | 54 } |
53 | 55 |
54 void onOnceBeforeDraw() override { | 56 void onOnceBeforeDraw() override { |
55 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65 , 96, "e"); | 57 fBitmap.reset(SkImage::NewFromBitmap( |
58 sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e"))); | |
56 | 59 |
57 fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( | 60 fCheckerboard.reset(SkImage::NewFromBitmap( |
58 80, 80, | 61 sk_tool_utils::create_checkerboard_bitmap(80, 80, |
59 sk_tool_utils::color_to_ 565(0xFFA0A0A0), | 62 sk_tool_utils::color_to_56 5(0xFFA0A0A0), |
60 sk_tool_utils::color_to_ 565(0xFF404040), | 63 sk_tool_utils::color_to_56 5(0xFF404040), |
61 8); | 64 8))); |
62 } | 65 } |
63 | 66 |
64 void onDraw(SkCanvas* canvas) override { | 67 void onDraw(SkCanvas* canvas) override { |
65 canvas->clear(SK_ColorBLACK); | 68 canvas->clear(SK_ColorBLACK); |
66 SkPaint paint; | 69 SkPaint paint; |
67 | 70 |
68 for (int i = 0; i < 4; i++) { | 71 for (int i = 0; i < 4; i++) { |
69 const SkBitmap* bitmap = (i & 0x01) ? &fCheckerboard : &fBitmap; | 72 const SkImage* image = (i & 0x01) ? fCheckerboard : fBitmap; |
70 SkIRect cropRect = SkIRect::MakeXYWH(i * 12, | 73 SkIRect cropRect = SkIRect::MakeXYWH(i * 12, |
71 i * 8, | 74 i * 8, |
72 bitmap->width() - i * 8, | 75 image->width() - i * 8, |
73 bitmap->height() - i * 12); | 76 image->height() - i * 12); |
74 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 77 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
75 SkAutoTUnref<SkImageFilter> tileInput(SkBitmapSource::Create(*bitmap )); | 78 SkAutoTUnref<SkImageFilter> tileInput(SkImageSource::Create(image)); |
76 SkScalar dx = SkIntToScalar(i*5); | 79 SkScalar dx = SkIntToScalar(i*5); |
77 SkScalar dy = SkIntToScalar(i*10); | 80 SkScalar dy = SkIntToScalar(i*10); |
78 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, d y, tileInput, | 81 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, d y, tileInput, |
79 &rect )); | 82 &rect )); |
80 paint.setImageFilter(filter); | 83 paint.setImageFilter(filter); |
81 drawClippedBitmap(canvas, *bitmap, paint, 1, cropRect); | 84 drawClippedImage(canvas, image, paint, 1, cropRect); |
82 canvas->translate(SkIntToScalar(bitmap->width() + MARGIN), 0); | 85 canvas->translate(SkIntToScalar(image->width() + MARGIN), 0); |
83 } | 86 } |
84 | 87 |
85 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); | 88 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); |
86 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 89 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
87 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10, nullptr, &rect)); | 90 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10, nullptr, &rect)); |
88 paint.setImageFilter(filter); | 91 paint.setImageFilter(filter); |
89 drawClippedBitmap(canvas, fBitmap, paint, 2, cropRect); | 92 drawClippedImage(canvas, fBitmap, paint, 2, cropRect); |
90 } | 93 } |
91 private: | 94 private: |
92 SkBitmap fBitmap, fCheckerboard; | 95 SkAutoTUnref<SkImage> fBitmap, fCheckerboard; |
93 | 96 |
94 typedef skiagm::GM INHERITED; | 97 typedef skiagm::GM INHERITED; |
95 }; | 98 }; |
96 DEF_GM( return new OffsetImageFilterGM; ) | 99 DEF_GM( return new OffsetImageFilterGM; ) |
97 | 100 |
98 ////////////////////////////////////////////////////////////////////////////// | 101 ////////////////////////////////////////////////////////////////////////////// |
99 | 102 |
100 class SimpleOffsetImageFilterGM : public skiagm::GM { | 103 class SimpleOffsetImageFilterGM : public skiagm::GM { |
101 public: | 104 public: |
102 SimpleOffsetImageFilterGM() {} | 105 SimpleOffsetImageFilterGM() {} |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 | 198 |
196 // crop==clip==dst | 199 // crop==clip==dst |
197 canvas->translate(100, 0); | 200 canvas->translate(100, 0); |
198 this->doDraw(canvas, r, SkOffsetImageFilter::Create(40, 0, nullptr, &cr2 ), &r2); | 201 this->doDraw(canvas, r, SkOffsetImageFilter::Create(40, 0, nullptr, &cr2 ), &r2); |
199 } | 202 } |
200 | 203 |
201 private: | 204 private: |
202 typedef skiagm::GM INHERITED; | 205 typedef skiagm::GM INHERITED; |
203 }; | 206 }; |
204 DEF_GM( return new SimpleOffsetImageFilterGM; ) | 207 DEF_GM( return new SimpleOffsetImageFilterGM; ) |
OLD | NEW |