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 "include/core/SkBitmap.h"
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkShader.h"
10 #include "samplecode/Sample.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,SkTileMode tm)34 static void make_paint(SkPaint* paint, SkTileMode tm) {
35 SkBitmap bm;
36 make_bitmap(&bm);
37
38 paint->setShader(bm.makeShader(tm, tm));
39 }
40
41 class RepeatTileView : public Sample {
42 public:
RepeatTileView()43 RepeatTileView() {
44 this->setBGColor(SK_ColorGRAY);
45 }
46
47 protected:
name()48 SkString name() override { return SkString("RepeatTile"); }
49
onDrawContent(SkCanvas * canvas)50 void onDrawContent(SkCanvas* canvas) override {
51 SkPaint paint;
52 make_paint(&paint, SkTileMode::kRepeat);
53
54 // canvas->scale(SK_Scalar1*2, SK_Scalar1);
55 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
56 canvas->drawPaint(paint);
57 }
58
59 private:
60 typedef Sample INHERITED;
61 };
62
63 //////////////////////////////////////////////////////////////////////////////
64
65 DEF_SAMPLE( return new RepeatTileView(); )
66