blob: a18a16e66126af7311d65b3fd6778473cdb13b3a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
15#include "SkUtils.h"
16#include "SkColorPriv.h"
17#include "SkColorFilter.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000018#include "SkPicture.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkTypeface.h"
20
21// effects
22#include "SkGradientShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkUnitMappers.h"
24#include "SkBlurDrawLooper.h"
25
26static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
27 bm->setConfig(config, w, h);
28 bm->allocPixels();
29 bm->eraseColor(0);
30
31 SkCanvas canvas(*bm);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000032 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
34 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
35 SkPaint paint;
36
37 SkUnitMapper* um = NULL;
38
39 um = new SkCosineMapper;
40// um = new SkDiscreteMapper(12);
41
42 SkAutoUnref au(um);
43
44 paint.setDither(true);
45 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
46 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
47 canvas.drawPaint(paint);
48}
49
50static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
51 SkShader::TileMode tmx, SkShader::TileMode tmy) {
52 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
53 paint->setShader(shader)->unref();
54 paint->setFilterBitmap(filter);
55}
56
57static const SkBitmap::Config gConfigs[] = {
58 SkBitmap::kARGB_8888_Config,
59 SkBitmap::kRGB_565_Config,
60 SkBitmap::kARGB_4444_Config
61};
62static const int gWidth = 32;
63static const int gHeight = 32;
64
reed@google.comf2183392011-04-22 14:10:48 +000065class TilingView : public SampleView {
reed@android.comf2b98d62010-12-20 18:26:13 +000066 SkPicture fTextPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkBlurDrawLooper fLooper;
68public:
69 TilingView()
70 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
71 0x88000000) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000072 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
74 }
75 }
76
77 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
78
79protected:
80 // overrides from SkEventSink
81 virtual bool onQuery(SkEvent* evt) {
82 if (SampleCode::TitleQ(*evt)) {
83 SampleCode::TitleR(evt, "Tiling");
84 return true;
85 }
86 return this->INHERITED::onQuery(evt);
87 }
88
reed@google.comf2183392011-04-22 14:10:48 +000089 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
91
92 static const char* gConfigNames[] = { "8888", "565", "4444" };
93
94 static const bool gFilters[] = { false, true };
95 static const char* gFilterNames[] = { "point", "bilinear" };
96
97 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
98 static const char* gModeNames[] = { "C", "R", "M" };
99
100 SkScalar y = SkIntToScalar(24);
101 SkScalar x = SkIntToScalar(10);
102
reed@android.comf2b98d62010-12-20 18:26:13 +0000103 SkCanvas* textCanvas = NULL;
104 if (fTextPicture.width() == 0) {
105 textCanvas = fTextPicture.beginRecording(1000, 1000);
106 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107
reed@android.comf2b98d62010-12-20 18:26:13 +0000108 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000109 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
110 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000111 SkPaint p;
112 SkString str;
113 p.setAntiAlias(true);
114 p.setDither(true);
115 p.setLooper(&fLooper);
116 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
117
118 p.setTextAlign(SkPaint::kCenter_Align);
119 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
120
121 x += r.width() * 4 / 3;
122 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 }
124 }
125
126 y += SkIntToScalar(16);
127
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000128 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
129 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000131 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
132 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SkPaint paint;
134 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
135 paint.setDither(true);
136
137 canvas->save();
138 canvas->translate(x, y);
139 canvas->drawRect(r, paint);
140 canvas->restore();
141
142 x += r.width() * 4 / 3;
143 }
144 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000145 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkPaint p;
147 SkString str;
148 p.setAntiAlias(true);
149 p.setLooper(&fLooper);
150 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000151 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 }
153
154 y += r.height() * 4 / 3;
155 }
156 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000157
158 canvas->drawPicture(fTextPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 }
160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161private:
reed@google.comf2183392011-04-22 14:10:48 +0000162 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163};
164
165//////////////////////////////////////////////////////////////////////////////
166
167static SkView* MyFactory() { return new TilingView; }
168static SkViewRegister reg(MyFactory);
169