robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkBitmapSource.h" |
| 9 | #include "SkTileImageFilter.h" |
| 10 | #include "gm.h" |
| 11 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 12 | static void create_circle_texture(SkBitmap* bm, SkColor color) { |
| 13 | SkCanvas canvas(*bm); |
| 14 | canvas.clear(0xFF000000); |
| 15 | |
| 16 | SkPaint paint; |
| 17 | paint.setColor(color); |
| 18 | paint.setStrokeWidth(3); |
| 19 | paint.setStyle(SkPaint::kStroke_Style); |
| 20 | |
| 21 | canvas.drawCircle(SkScalarHalf(bm->width()), SkScalarHalf(bm->height()), |
| 22 | SkScalarHalf(bm->width()), paint); |
| 23 | } |
| 24 | |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
| 27 | class BigTileImageFilterGM : public GM { |
| 28 | public: |
| 29 | BigTileImageFilterGM() { |
| 30 | this->setBGColor(0xFF000000); |
| 31 | } |
| 32 | |
| 33 | protected: |
| 34 | |
| 35 | SkString onShortName() override { |
| 36 | return SkString("bigtileimagefilter"); |
| 37 | } |
| 38 | |
| 39 | SkISize onISize() override{ |
| 40 | return SkISize::Make(kWidth, kHeight); |
| 41 | } |
| 42 | |
| 43 | void onOnceBeforeDraw() override { |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 44 | fRedBitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 45 | create_circle_texture(&fRedBitmap, SK_ColorRED); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 46 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 47 | fGreenBitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 48 | create_circle_texture(&fGreenBitmap, SK_ColorGREEN); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void onDraw(SkCanvas* canvas) override { |
| 52 | canvas->clear(SK_ColorBLACK); |
| 53 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 54 | { |
| 55 | SkPaint p; |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 56 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 57 | SkRect bound = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 58 | SkAutoTUnref<SkBitmapSource> bms(SkBitmapSource::Create(fRedBitmap)); |
| 59 | SkAutoTUnref<SkTileImageFilter> tif(SkTileImageFilter::Create( |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 60 | SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)), |
| 61 | SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), |
| 62 | bms)); |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 63 | p.setImageFilter(tif); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 64 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 65 | canvas->saveLayer(&bound, &p); |
| 66 | canvas->restore(); |
| 67 | } |
| 68 | |
| 69 | { |
| 70 | SkPaint p2; |
| 71 | |
| 72 | SkRect bound2 = SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)); |
| 73 | |
| 74 | SkAutoTUnref<SkTileImageFilter> tif2(SkTileImageFilter::Create( |
| 75 | SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)), |
| 76 | SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)), |
| 77 | NULL)); |
| 78 | p2.setImageFilter(tif2); |
| 79 | |
| 80 | canvas->translate(320, 320); |
| 81 | canvas->saveLayer(&bound2, &p2); |
| 82 | canvas->setMatrix(SkMatrix::I()); |
| 83 | |
| 84 | SkRect bound3 = SkRect::MakeXYWH(320, 320, |
| 85 | SkIntToScalar(kBitmapSize), |
| 86 | SkIntToScalar(kBitmapSize)); |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 87 | canvas->drawBitmapRect(fGreenBitmap, &bound2, bound3, NULL, |
| 88 | SkCanvas::kStrict_SrcRectConstraint); |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 89 | canvas->restore(); |
| 90 | } |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | static const int kWidth = 512; |
| 95 | static const int kHeight = 512; |
| 96 | static const int kBitmapSize = 64; |
| 97 | |
robertphillips | c3176aa | 2015-06-16 09:44:56 -0700 | [diff] [blame] | 98 | SkBitmap fRedBitmap; |
| 99 | SkBitmap fGreenBitmap; |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 100 | |
| 101 | typedef GM INHERITED; |
| 102 | }; |
| 103 | |
| 104 | ////////////////////////////////////////////////////////////////////////////// |
| 105 | |
| 106 | DEF_GM( return SkNEW(BigTileImageFilterGM); ) |
| 107 | |
| 108 | } |