blob: f465fe4d38c0a5a67f14d207ecb2a1dc8cc21202 [file] [log] [blame]
robertphillips63195182015-06-08 06:21:14 -07001/*
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
robertphillipsc3176aa2015-06-16 09:44:56 -070012static 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
robertphillips63195182015-06-08 06:21:14 -070025namespace skiagm {
26
27class BigTileImageFilterGM : public GM {
28public:
29 BigTileImageFilterGM() {
30 this->setBGColor(0xFF000000);
31 }
32
33protected:
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 {
robertphillipsc3176aa2015-06-16 09:44:56 -070044 fRedBitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
45 create_circle_texture(&fRedBitmap, SK_ColorRED);
robertphillips63195182015-06-08 06:21:14 -070046
robertphillipsc3176aa2015-06-16 09:44:56 -070047 fGreenBitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
48 create_circle_texture(&fGreenBitmap, SK_ColorGREEN);
robertphillips63195182015-06-08 06:21:14 -070049 }
50
51 void onDraw(SkCanvas* canvas) override {
52 canvas->clear(SK_ColorBLACK);
53
robertphillipsc3176aa2015-06-16 09:44:56 -070054 {
55 SkPaint p;
robertphillips63195182015-06-08 06:21:14 -070056
robertphillipsc3176aa2015-06-16 09:44:56 -070057 SkRect bound = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
58 SkAutoTUnref<SkBitmapSource> bms(SkBitmapSource::Create(fRedBitmap));
59 SkAutoTUnref<SkTileImageFilter> tif(SkTileImageFilter::Create(
robertphillips63195182015-06-08 06:21:14 -070060 SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)),
61 SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)),
62 bms));
robertphillipsc3176aa2015-06-16 09:44:56 -070063 p.setImageFilter(tif);
robertphillips63195182015-06-08 06:21:14 -070064
robertphillipsc3176aa2015-06-16 09:44:56 -070065 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));
87 canvas->drawBitmapRectToRect(fGreenBitmap, &bound2, bound3);
88 canvas->restore();
89 }
robertphillips63195182015-06-08 06:21:14 -070090 }
91
92private:
93 static const int kWidth = 512;
94 static const int kHeight = 512;
95 static const int kBitmapSize = 64;
96
robertphillipsc3176aa2015-06-16 09:44:56 -070097 SkBitmap fRedBitmap;
98 SkBitmap fGreenBitmap;
robertphillips63195182015-06-08 06:21:14 -070099
100 typedef GM INHERITED;
101};
102
103//////////////////////////////////////////////////////////////////////////////
104
105DEF_GM( return SkNEW(BigTileImageFilterGM); )
106
107}