3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SampleCode.h"
13 #include "SkPictureRecorder.h"
17 #include "SkColorPriv.h"
18 #include "SkColorFilter.h"
19 #include "SkPicture.h"
20 #include "SkTypeface.h"
23 #include "SkGradientShader.h"
24 #include "SkBlurMask.h"
25 #include "SkBlurDrawLooper.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);
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 };
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);
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->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
50 static const SkColorType gColorTypes[] = {
54 static const int gWidth = 32;
55 static const int gHeight = 32;
57 class TilingView : public SampleView {
58 SkAutoTUnref<SkPicture> fTextPicture;
59 SkAutoTUnref<SkBlurDrawLooper> fLooper;
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);
70 virtual ~TilingView() {
73 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
76 // overrides from SkEventSink
77 virtual bool onQuery(SkEvent* evt) {
78 if (SampleCode::TitleQ(*evt)) {
79 SampleCode::TitleR(evt, "Tiling");
82 return this->INHERITED::onQuery(evt);
85 virtual void onDrawContent(SkCanvas* canvas) {
86 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
88 static const char* gConfigNames[] = { "8888", "565", "4444" };
90 static const bool gFilters[] = { false, true };
91 static const char* gFilterNames[] = { "point", "bilinear" };
93 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
94 static const char* gModeNames[] = { "C", "R", "M" };
96 SkScalar y = SkIntToScalar(24);
97 SkScalar x = SkIntToScalar(10);
99 SkPictureRecorder recorder;
100 SkCanvas* textCanvas = NULL;
101 if (NULL == fTextPicture) {
102 textCanvas = recorder.beginRecording(1000, 1000, NULL, 0);
105 if (NULL != 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++) {
110 p.setAntiAlias(true);
112 p.setLooper(fLooper);
113 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
115 p.setTextAlign(SkPaint::kCenter_Align);
116 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
118 x += r.width() * 4 / 3;
123 y += SkIntToScalar(16);
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++) {
131 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
132 paint.setDither(true);
135 canvas->translate(x, y);
136 canvas->drawRect(r, paint);
139 x += r.width() * 4 / 3;
142 if (NULL != textCanvas) {
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);
151 y += r.height() * 4 / 3;
155 if (NULL != textCanvas) {
156 SkASSERT(NULL == fTextPicture);
157 fTextPicture.reset(recorder.endRecording());
160 SkASSERT(NULL != fTextPicture);
161 canvas->drawPicture(fTextPicture);
165 typedef SampleView INHERITED;
168 //////////////////////////////////////////////////////////////////////////////
170 static SkView* MyFactory() { return new TilingView; }
171 static SkViewRegister reg(MyFactory);