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
8 #include "Sample.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 #include "SkPictureRecorder.h"
14 #include "SkRegion.h"
15 #include "SkShader.h"
16 #include "SkUTF.h"
17 #include "SkColorPriv.h"
18 #include "SkColorFilter.h"
19 #include "SkPicture.h"
20 #include "SkTextUtils.h"
21 #include "SkTypeface.h"
22
23 // effects
24 #include "SkGradientShader.h"
25 #include "SkBlurMask.h"
26 #include "SkBlurDrawLooper.h"
27
makebm(SkBitmap * bm,SkColorType ct,int w,int h)28 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
29 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
30 bm->eraseColor(SK_ColorTRANSPARENT);
31
32 SkCanvas canvas(*bm);
33 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
34 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
35 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
36 SkPaint paint;
37
38 paint.setDither(true);
39 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
40 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
41 canvas.drawPaint(paint);
42 }
43
setup(SkPaint * paint,const SkBitmap & bm,bool filter,SkShader::TileMode tmx,SkShader::TileMode tmy)44 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
45 SkShader::TileMode tmx, SkShader::TileMode tmy) {
46 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
47 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
48 }
49
50 static const SkColorType gColorTypes[] = {
51 kN32_SkColorType,
52 kRGB_565_SkColorType,
53 };
54 static const int gWidth = 32;
55 static const int gHeight = 32;
56
57 class TilingView : public Sample {
58 sk_sp<SkPicture> fTextPicture;
59 sk_sp<SkDrawLooper> fLooper;
60 public:
TilingView()61 TilingView()
62 : fLooper(SkBlurDrawLooper::Make(0x88000000,
63 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
64 SkIntToScalar(2), SkIntToScalar(2))) {
65 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
66 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
67 }
68 }
69
~TilingView()70 virtual ~TilingView() {
71 }
72
73 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
74
75 protected:
onQuery(Sample::Event * evt)76 virtual bool onQuery(Sample::Event* evt) {
77 if (Sample::TitleQ(*evt)) {
78 Sample::TitleR(evt, "Tiling");
79 return true;
80 }
81 return this->INHERITED::onQuery(evt);
82 }
83
onDrawContent(SkCanvas * canvas)84 virtual void onDrawContent(SkCanvas* canvas) {
85 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
86
87 static const char* gConfigNames[] = { "8888", "565", "4444" };
88
89 static const bool gFilters[] = { false, true };
90 static const char* gFilterNames[] = { "point", "bilinear" };
91
92 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
93 static const char* gModeNames[] = { "C", "R", "M" };
94
95 SkScalar y = SkIntToScalar(24);
96 SkScalar x = SkIntToScalar(10);
97
98 SkPictureRecorder recorder;
99 SkCanvas* textCanvas = nullptr;
100 if (nullptr == fTextPicture) {
101 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
102 }
103
104 if (textCanvas) {
105 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
106 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
107 SkPaint p;
108 SkString str;
109 p.setDither(true);
110 p.setLooper(fLooper);
111 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
112
113 SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
114 SkTextUtils::kCenter_Align);
115
116 x += r.width() * 4 / 3;
117 }
118 }
119 }
120
121 y += SkIntToScalar(16);
122
123 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
124 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
125 x = SkIntToScalar(10);
126 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
127 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
128 SkPaint paint;
129 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
130 paint.setDither(true);
131
132 canvas->save();
133 canvas->translate(x, y);
134 canvas->drawRect(r, paint);
135 canvas->restore();
136
137 x += r.width() * 4 / 3;
138 }
139 }
140 if (textCanvas) {
141 SkPaint p;
142 p.setLooper(fLooper);
143 textCanvas->drawString(
144 SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
145 x, y + r.height() * 2 / 3, SkFont(), p);
146 }
147
148 y += r.height() * 4 / 3;
149 }
150 }
151
152 if (textCanvas) {
153 SkASSERT(nullptr == fTextPicture);
154 fTextPicture = recorder.finishRecordingAsPicture();
155 }
156
157 SkASSERT(fTextPicture);
158 canvas->drawPicture(fTextPicture.get());
159 }
160
161 private:
162 typedef Sample INHERITED;
163 };
164
165 //////////////////////////////////////////////////////////////////////////////
166
167 DEF_SAMPLE( return new TilingView(); )
168