1 /*
2 * Copyright 2011 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 #include "Sample.h"
8 #include "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkShader.h"
11
make_bitmap(SkBitmap * bm)12 static void make_bitmap(SkBitmap* bm) {
13 const int W = 100;
14 const int H = 100;
15 bm->allocN32Pixels(W, H);
16
17 SkPaint paint;
18 SkCanvas canvas(*bm);
19 canvas.drawColor(SK_ColorWHITE);
20
21 const SkColor colors[] = {
22 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
23 };
24
25 for (int ix = 0; ix < W; ix += 1) {
26 SkScalar x = SkIntToScalar(ix) + SK_ScalarHalf;
27 paint.setColor(colors[ix & 3]);
28 canvas.drawLine(x, 0, x, SkIntToScalar(H - 1), paint);
29 }
30 paint.setColor(SK_ColorGRAY);
31 canvas.drawLine(0, 0, SkIntToScalar(W), 0, paint);
32 }
33
make_paint(SkPaint * paint,SkShader::TileMode tm)34 static void make_paint(SkPaint* paint, SkShader::TileMode tm) {
35 SkBitmap bm;
36 make_bitmap(&bm);
37
38 paint->setShader(SkShader::MakeBitmapShader(bm, tm, tm));
39 }
40
41 class RepeatTileView : public Sample {
42 public:
RepeatTileView()43 RepeatTileView() {
44 this->setBGColor(SK_ColorGRAY);
45 }
46
47 protected:
onQuery(Sample::Event * evt)48 bool onQuery(Sample::Event* evt) override {
49 if (Sample::TitleQ(*evt)) {
50 Sample::TitleR(evt, "RepeatTile");
51 return true;
52 }
53 return this->INHERITED::onQuery(evt);
54 }
55
onDrawContent(SkCanvas * canvas)56 void onDrawContent(SkCanvas* canvas) override {
57 SkPaint paint;
58 make_paint(&paint, SkShader::kRepeat_TileMode);
59
60 // canvas->scale(SK_Scalar1*2, SK_Scalar1);
61 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
62 canvas->drawPaint(paint);
63 }
64
65 private:
66 typedef Sample INHERITED;
67 };
68
69 //////////////////////////////////////////////////////////////////////////////
70
71 DEF_SAMPLE( return new RepeatTileView(); )
72