OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkBitmapSource.h" | 8 #include "SkImageSource.h" |
| 9 #include "SkSurface.h" |
9 #include "SkTileImageFilter.h" | 10 #include "SkTileImageFilter.h" |
10 #include "gm.h" | 11 #include "gm.h" |
11 | 12 |
12 static void create_circle_texture(SkBitmap* bm, SkColor color) { | 13 static SkImage* create_circle_texture(int size, SkColor color) { |
13 SkCanvas canvas(*bm); | 14 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(size, size)); |
14 canvas.clear(0xFF000000); | 15 SkCanvas* canvas = surface->getCanvas(); |
| 16 canvas->clear(0xFF000000); |
15 | 17 |
16 SkPaint paint; | 18 SkPaint paint; |
17 paint.setColor(color); | 19 paint.setColor(color); |
18 paint.setStrokeWidth(3); | 20 paint.setStrokeWidth(3); |
19 paint.setStyle(SkPaint::kStroke_Style); | 21 paint.setStyle(SkPaint::kStroke_Style); |
20 | 22 |
21 canvas.drawCircle(SkScalarHalf(bm->width()), SkScalarHalf(bm->height()), | 23 canvas->drawCircle(SkScalarHalf(size), SkScalarHalf(size), SkScalarHalf(size
), paint); |
22 SkScalarHalf(bm->width()), paint); | 24 |
| 25 return surface->newImageSnapshot(); |
23 } | 26 } |
24 | 27 |
25 namespace skiagm { | 28 namespace skiagm { |
26 | 29 |
27 class BigTileImageFilterGM : public GM { | 30 class BigTileImageFilterGM : public GM { |
28 public: | 31 public: |
29 BigTileImageFilterGM() { | 32 BigTileImageFilterGM() { |
30 this->setBGColor(0xFF000000); | 33 this->setBGColor(0xFF000000); |
31 } | 34 } |
32 | 35 |
33 protected: | 36 protected: |
34 | 37 |
35 SkString onShortName() override { | 38 SkString onShortName() override { |
36 return SkString("bigtileimagefilter"); | 39 return SkString("bigtileimagefilter"); |
37 } | 40 } |
38 | 41 |
39 SkISize onISize() override{ | 42 SkISize onISize() override{ |
40 return SkISize::Make(kWidth, kHeight); | 43 return SkISize::Make(kWidth, kHeight); |
41 } | 44 } |
42 | 45 |
43 void onOnceBeforeDraw() override { | 46 void onOnceBeforeDraw() override { |
44 fRedBitmap.allocN32Pixels(kBitmapSize, kBitmapSize); | 47 fRedImage.reset(create_circle_texture(kBitmapSize, SK_ColorRED)); |
45 create_circle_texture(&fRedBitmap, SK_ColorRED); | 48 fGreenImage.reset(create_circle_texture(kBitmapSize, SK_ColorGREEN)); |
46 | |
47 fGreenBitmap.allocN32Pixels(kBitmapSize, kBitmapSize); | |
48 create_circle_texture(&fGreenBitmap, SK_ColorGREEN); | |
49 } | 49 } |
50 | 50 |
51 void onDraw(SkCanvas* canvas) override { | 51 void onDraw(SkCanvas* canvas) override { |
52 canvas->clear(SK_ColorBLACK); | 52 canvas->clear(SK_ColorBLACK); |
53 | 53 |
54 { | 54 { |
55 SkPaint p; | 55 SkPaint p; |
56 | 56 |
57 SkRect bound = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(k
Height)); | 57 SkRect bound = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(k
Height)); |
58 SkAutoTUnref<SkBitmapSource> bms(SkBitmapSource::Create(fRedBitmap))
; | 58 SkAutoTUnref<SkImageFilter> imageSource(SkImageSource::Create(fRedIm
age)); |
59 SkAutoTUnref<SkTileImageFilter> tif(SkTileImageFilter::Create( | 59 SkAutoTUnref<SkTileImageFilter> tif(SkTileImageFilter::Create( |
60 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)), | 60 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)), |
61 SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(
kHeight)), | 61 SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(
kHeight)), |
62 bms)); | 62 imageSource)); |
63 p.setImageFilter(tif); | 63 p.setImageFilter(tif); |
64 | 64 |
65 canvas->saveLayer(&bound, &p); | 65 canvas->saveLayer(&bound, &p); |
66 canvas->restore(); | 66 canvas->restore(); |
67 } | 67 } |
68 | 68 |
69 { | 69 { |
70 SkPaint p2; | 70 SkPaint p2; |
71 | 71 |
72 SkRect bound2 = SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)); | 72 SkRect bound2 = SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)); |
73 | 73 |
74 SkAutoTUnref<SkTileImageFilter> tif2(SkTileImageFilter::Create( | 74 SkAutoTUnref<SkTileImageFilter> tif2(SkTileImageFilter::Create( |
75 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)), | 75 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)), |
76 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)), | 76 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToSc
alar(kBitmapSize)), |
77 nullptr)); | 77 nullptr)); |
78 p2.setImageFilter(tif2); | 78 p2.setImageFilter(tif2); |
79 | 79 |
80 canvas->translate(320, 320); | 80 canvas->translate(320, 320); |
81 canvas->saveLayer(&bound2, &p2); | 81 canvas->saveLayer(&bound2, &p2); |
82 canvas->setMatrix(SkMatrix::I()); | 82 canvas->setMatrix(SkMatrix::I()); |
83 | 83 |
84 SkRect bound3 = SkRect::MakeXYWH(320, 320, | 84 SkRect bound3 = SkRect::MakeXYWH(320, 320, |
85 SkIntToScalar(kBitmapSize), | 85 SkIntToScalar(kBitmapSize), |
86 SkIntToScalar(kBitmapSize)); | 86 SkIntToScalar(kBitmapSize)); |
87 canvas->drawBitmapRect(fGreenBitmap, bound2, bound3, nullptr, | 87 canvas->drawImageRect(fGreenImage, bound2, bound3, nullptr, |
88 SkCanvas::kStrict_SrcRectConstraint); | 88 SkCanvas::kStrict_SrcRectConstraint); |
89 canvas->restore(); | 89 canvas->restore(); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 private: | 93 private: |
94 static const int kWidth = 512; | 94 static const int kWidth = 512; |
95 static const int kHeight = 512; | 95 static const int kHeight = 512; |
96 static const int kBitmapSize = 64; | 96 static const int kBitmapSize = 64; |
97 | 97 |
98 SkBitmap fRedBitmap; | 98 SkAutoTUnref<SkImage> fRedImage; |
99 SkBitmap fGreenBitmap; | 99 SkAutoTUnref<SkImage> fGreenImage; |
100 | 100 |
101 typedef GM INHERITED; | 101 typedef GM INHERITED; |
102 }; | 102 }; |
103 | 103 |
104 ////////////////////////////////////////////////////////////////////////////// | 104 ////////////////////////////////////////////////////////////////////////////// |
105 | 105 |
106 DEF_GM(return new BigTileImageFilterGM;) | 106 DEF_GM(return new BigTileImageFilterGM;) |
107 } | 107 } |
OLD | NEW |