blob: cc3b52915d50eab3051f0fe62c04fae3a2593262 [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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000013#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
17#include "SkColorPriv.h"
18#include "SkColorFilter.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000019#include "SkPicture.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkTypeface.h"
21
22// effects
23#include "SkGradientShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkUnitMappers.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000025#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkBlurDrawLooper.h"
27
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000028static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
29 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
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
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000058static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000059 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000060 kRGB_565_SkColorType,
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 {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000066 SkAutoTUnref<SkPicture> fTextPicture;
67 SkAutoTUnref<SkBlurDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000068public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000069 TilingView()
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000070 : fLooper(SkBlurDrawLooper::Create(0x88000000,
71 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
72 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000073 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
74 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
76 }
77
robertphillips@google.com84b18c72014-04-13 19:09:42 +000078 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000079 }
80
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000081 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
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
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000107 SkPictureRecorder recorder;
reed@android.comf2b98d62010-12-20 18:26:13 +0000108 SkCanvas* textCanvas = NULL;
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000109 if (NULL == fTextPicture) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000110 textCanvas = recorder.beginRecording(1000, 1000, NULL, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +0000111 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000113 if (NULL != textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000114 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
115 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000116 SkPaint p;
117 SkString str;
118 p.setAntiAlias(true);
119 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000120 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000121 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
122
123 p.setTextAlign(SkPaint::kCenter_Align);
124 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed@android.comf2b98d62010-12-20 18:26:13 +0000126 x += r.width() * 4 / 3;
127 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 }
129 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 y += SkIntToScalar(16);
132
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000133 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000134 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000136 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
137 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 SkPaint paint;
139 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
140 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 canvas->save();
143 canvas->translate(x, y);
144 canvas->drawRect(r, paint);
145 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 x += r.width() * 4 / 3;
148 }
149 }
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000150 if (NULL != textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 SkPaint p;
152 SkString str;
153 p.setAntiAlias(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000154 p.setLooper(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000156 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 }
158
159 y += r.height() * 4 / 3;
160 }
161 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000162
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000163 if (NULL != textCanvas) {
164 SkASSERT(NULL == fTextPicture);
165 fTextPicture.reset(recorder.endRecording());
166 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000167
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000168 SkASSERT(NULL != fTextPicture);
junov@google.comf3cf9422011-09-13 18:15:52 +0000169 canvas->drawPicture(*fTextPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172private:
reed@google.comf2183392011-04-22 14:10:48 +0000173 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174};
175
176//////////////////////////////////////////////////////////////////////////////
177
178static SkView* MyFactory() { return new TilingView; }
179static SkViewRegister reg(MyFactory);