blob: 69c77106364ec3ace12516327044844083dcca35 [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();
junov@google.comdbfac8a2012-12-06 21:47:40 +000029 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
37 SkUnitMapper* um = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
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,
reed@android.com8a1c16f2008-12-17 15:59:43 +000060};
61static const int gWidth = 32;
62static const int gHeight = 32;
63
reed@google.comf2183392011-04-22 14:10:48 +000064class TilingView : public SampleView {
junov@google.comf3cf9422011-09-13 18:15:52 +000065 SkPicture* fTextPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 SkBlurDrawLooper fLooper;
67public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000068 TilingView()
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 : fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
70 0x88000000) {
junov@google.comf3cf9422011-09-13 18:15:52 +000071 fTextPicture = new SkPicture();
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
junov@google.comf3cf9422011-09-13 18:15:52 +000077 ~TilingView() {
78 fTextPicture->unref();
79 }
80
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083protected:
84 // overrides from SkEventSink
85 virtual bool onQuery(SkEvent* evt) {
86 if (SampleCode::TitleQ(*evt)) {
87 SampleCode::TitleR(evt, "Tiling");
88 return true;
89 }
90 return this->INHERITED::onQuery(evt);
91 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@google.comf2183392011-04-22 14:10:48 +000093 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
95
96 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 static const bool gFilters[] = { false, true };
99 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
102 static const char* gModeNames[] = { "C", "R", "M" };
103
104 SkScalar y = SkIntToScalar(24);
105 SkScalar x = SkIntToScalar(10);
106
reed@android.comf2b98d62010-12-20 18:26:13 +0000107 SkCanvas* textCanvas = NULL;
junov@google.comf3cf9422011-09-13 18:15:52 +0000108 if (fTextPicture->width() == 0) {
109 textCanvas = fTextPicture->beginRecording(1000, 1000);
reed@android.comf2b98d62010-12-20 18:26:13 +0000110 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111
reed@android.comf2b98d62010-12-20 18:26:13 +0000112 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000113 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
114 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000115 SkPaint p;
116 SkString str;
117 p.setAntiAlias(true);
118 p.setDither(true);
119 p.setLooper(&fLooper);
120 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
121
122 p.setTextAlign(SkPaint::kCenter_Align);
123 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.comf2b98d62010-12-20 18:26:13 +0000125 x += r.width() * 4 / 3;
126 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 }
128 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 y += SkIntToScalar(16);
131
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000132 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
133 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000135 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
136 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkPaint paint;
138 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
139 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 canvas->save();
142 canvas->translate(x, y);
143 canvas->drawRect(r, paint);
144 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 x += r.width() * 4 / 3;
147 }
148 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000149 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 SkPaint p;
151 SkString str;
152 p.setAntiAlias(true);
153 p.setLooper(&fLooper);
154 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000155 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 }
157
158 y += r.height() * 4 / 3;
159 }
160 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000161
junov@google.comf3cf9422011-09-13 18:15:52 +0000162 canvas->drawPicture(*fTextPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165private:
reed@google.comf2183392011-04-22 14:10:48 +0000166 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167};
168
169//////////////////////////////////////////////////////////////////////////////
170
171static SkView* MyFactory() { return new TilingView; }
172static SkViewRegister reg(MyFactory);