blob: dbd41c2ebd68b22d438de3f014d47e5f154d84e4 [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));
reede47829b2015-08-06 10:02:53 -070087 canvas->drawBitmapRect(fGreenBitmap, bound2, bound3, nullptr,
reeda5517e22015-07-14 10:54:12 -070088 SkCanvas::kStrict_SrcRectConstraint);
robertphillipsc3176aa2015-06-16 09:44:56 -070089 canvas->restore();
90 }
robertphillips63195182015-06-08 06:21:14 -070091 }
92
93private:
94 static const int kWidth = 512;
95 static const int kHeight = 512;
96 static const int kBitmapSize = 64;
97
robertphillipsc3176aa2015-06-16 09:44:56 -070098 SkBitmap fRedBitmap;
99 SkBitmap fGreenBitmap;
robertphillips63195182015-06-08 06:21:14 -0700100
101 typedef GM INHERITED;
102};
103
104//////////////////////////////////////////////////////////////////////////////
105
halcanary385fe4d2015-08-26 13:07:48 -0700106DEF_GM(return new BigTileImageFilterGM;)
robertphillips63195182015-06-08 06:21:14 -0700107}