1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "SampleCode.h"
9 #include "SkView.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 "SkUtils.h"
17 #include "SkColorPriv.h"
18 #include "SkColorFilter.h"
19 #include "SkPicture.h"
20 #include "SkTypeface.h"
21 
22 // effects
23 #include "SkGradientShader.h"
24 #include "SkBlurMask.h"
25 #include "SkBlurDrawLooper.h"
26 
makebm(SkBitmap * bm,SkColorType ct,int w,int h)27 static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
28     bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
29     bm->eraseColor(SK_ColorTRANSPARENT);
30 
31     SkCanvas    canvas(*bm);
32     SkPoint     pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
33     SkColor     colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
34     SkScalar    pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
35     SkPaint     paint;
36 
37     paint.setDither(true);
38     paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
39                 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
40     canvas.drawPaint(paint);
41 }
42 
setup(SkPaint * paint,const SkBitmap & bm,bool filter,SkShader::TileMode tmx,SkShader::TileMode tmy)43 static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
44                   SkShader::TileMode tmx, SkShader::TileMode tmy) {
45     SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
46     paint->setShader(shader)->unref();
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 SampleView {
58     SkAutoTUnref<SkPicture>        fTextPicture;
59     SkAutoTUnref<SkBlurDrawLooper> fLooper;
60 public:
TilingView()61     TilingView()
62             : fLooper(SkBlurDrawLooper::Create(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:
76     // overrides from SkEventSink
onQuery(SkEvent * evt)77     virtual bool onQuery(SkEvent* evt) {
78         if (SampleCode::TitleQ(*evt)) {
79             SampleCode::TitleR(evt, "Tiling");
80             return true;
81         }
82         return this->INHERITED::onQuery(evt);
83     }
84 
onDrawContent(SkCanvas * canvas)85     virtual void onDrawContent(SkCanvas* canvas) {
86         SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
87 
88         static const char* gConfigNames[] = { "8888", "565", "4444" };
89 
90         static const bool           gFilters[] = { false, true };
91         static const char*          gFilterNames[] = {     "point",                     "bilinear" };
92 
93         static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
94         static const char*          gModeNames[] = {    "C",                    "R",                   "M" };
95 
96         SkScalar y = SkIntToScalar(24);
97         SkScalar x = SkIntToScalar(10);
98 
99         SkPictureRecorder recorder;
100         SkCanvas* textCanvas = NULL;
101         if (NULL == fTextPicture) {
102             textCanvas = recorder.beginRecording(1000, 1000, NULL, 0);
103         }
104 
105         if (textCanvas) {
106             for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
107                 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
108                     SkPaint p;
109                     SkString str;
110                     p.setAntiAlias(true);
111                     p.setDither(true);
112                     p.setLooper(fLooper);
113                     str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
114 
115                     p.setTextAlign(SkPaint::kCenter_Align);
116                     textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
117 
118                     x += r.width() * 4 / 3;
119                 }
120             }
121         }
122 
123         y += SkIntToScalar(16);
124 
125         for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
126             for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
127                 x = SkIntToScalar(10);
128                 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
129                     for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
130                         SkPaint paint;
131                         setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
132                         paint.setDither(true);
133 
134                         canvas->save();
135                         canvas->translate(x, y);
136                         canvas->drawRect(r, paint);
137                         canvas->restore();
138 
139                         x += r.width() * 4 / 3;
140                     }
141                 }
142                 if (textCanvas) {
143                     SkPaint p;
144                     SkString str;
145                     p.setAntiAlias(true);
146                     p.setLooper(fLooper);
147                     str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
148                     textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
149                 }
150 
151                 y += r.height() * 4 / 3;
152             }
153         }
154 
155         if (textCanvas) {
156             SkASSERT(NULL == fTextPicture);
157             fTextPicture.reset(recorder.endRecording());
158         }
159 
160         SkASSERT(fTextPicture);
161         canvas->drawPicture(fTextPicture);
162     }
163 
164 private:
165     typedef SampleView INHERITED;
166 };
167 
168 //////////////////////////////////////////////////////////////////////////////
169 
MyFactory()170 static SkView* MyFactory() { return new TilingView; }
171 static SkViewRegister reg(MyFactory);
172