blob: c35cf888463d6eb1aaeacb94d443552a8bae9c15 [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"
robertphillips@google.comb7061172013-09-06 14:16:12 +000024#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkBlurDrawLooper.h"
26
27static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
28 bm->setConfig(config, w, h);
29 bm->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000030 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 SkCanvas canvas(*bm);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000033 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
35 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
36 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
38 SkUnitMapper* um = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40 um = new SkCosineMapper;
41// um = new SkDiscreteMapper(12);
42
43 SkAutoUnref au(um);
44
45 paint.setDither(true);
46 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
47 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
48 canvas.drawPaint(paint);
49}
50
51static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
52 SkShader::TileMode tmx, SkShader::TileMode tmy) {
53 SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy);
54 paint->setShader(shader)->unref();
reed@google.com44699382013-10-31 17:28:30 +000055 paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +000056}
57
58static const SkBitmap::Config gConfigs[] = {
59 SkBitmap::kARGB_8888_Config,
60 SkBitmap::kRGB_565_Config,
reed@android.com8a1c16f2008-12-17 15:59:43 +000061};
62static const int gWidth = 32;
63static const int gHeight = 32;
64
reed@google.comf2183392011-04-22 14:10:48 +000065class TilingView : public SampleView {
junov@google.comf3cf9422011-09-13 18:15:52 +000066 SkPicture* fTextPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkBlurDrawLooper fLooper;
68public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000069 TilingView()
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000070 : fLooper(0x88000000,
robertphillips@google.comb7061172013-09-06 14:16:12 +000071 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
72 SkIntToScalar(2), SkIntToScalar(2)) {
junov@google.comf3cf9422011-09-13 18:15:52 +000073 fTextPicture = new SkPicture();
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000074 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
76 }
77 }
78
junov@google.comf3cf9422011-09-13 18:15:52 +000079 ~TilingView() {
80 fTextPicture->unref();
81 }
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085protected:
86 // overrides from SkEventSink
87 virtual bool onQuery(SkEvent* evt) {
88 if (SampleCode::TitleQ(*evt)) {
89 SampleCode::TitleR(evt, "Tiling");
90 return true;
91 }
92 return this->INHERITED::onQuery(evt);
93 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@google.comf2183392011-04-22 14:10:48 +000095 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
97
98 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 static const bool gFilters[] = { false, true };
101 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
104 static const char* gModeNames[] = { "C", "R", "M" };
105
106 SkScalar y = SkIntToScalar(24);
107 SkScalar x = SkIntToScalar(10);
108
reed@android.comf2b98d62010-12-20 18:26:13 +0000109 SkCanvas* textCanvas = NULL;
junov@google.comf3cf9422011-09-13 18:15:52 +0000110 if (fTextPicture->width() == 0) {
111 textCanvas = fTextPicture->beginRecording(1000, 1000);
reed@android.comf2b98d62010-12-20 18:26:13 +0000112 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113
reed@android.comf2b98d62010-12-20 18:26:13 +0000114 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000115 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
116 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000117 SkPaint p;
118 SkString str;
119 p.setAntiAlias(true);
120 p.setDither(true);
121 p.setLooper(&fLooper);
122 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
123
124 p.setTextAlign(SkPaint::kCenter_Align);
125 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@android.comf2b98d62010-12-20 18:26:13 +0000127 x += r.width() * 4 / 3;
128 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 }
130 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 y += SkIntToScalar(16);
133
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000134 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
135 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000137 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
138 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 SkPaint paint;
140 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
141 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 canvas->save();
144 canvas->translate(x, y);
145 canvas->drawRect(r, paint);
146 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 x += r.width() * 4 / 3;
149 }
150 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000151 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 SkPaint p;
153 SkString str;
154 p.setAntiAlias(true);
155 p.setLooper(&fLooper);
156 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000157 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 }
159
160 y += r.height() * 4 / 3;
161 }
162 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000163
junov@google.comf3cf9422011-09-13 18:15:52 +0000164 canvas->drawPicture(*fTextPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167private:
reed@google.comf2183392011-04-22 14:10:48 +0000168 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169};
170
171//////////////////////////////////////////////////////////////////////////////
172
173static SkView* MyFactory() { return new TilingView; }
174static SkViewRegister reg(MyFactory);