blob: 84346b0f7286105871577dddbca41e8b8355974d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000012#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#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"
robertphillips@google.comb7061172013-09-06 14:16:12 +000023#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkBlurDrawLooper.h"
25
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000026static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
27 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000028 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 SkCanvas canvas(*bm);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000031 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
33 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
34 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 paint.setDither(true);
reed8a21c9f2016-03-08 18:50:00 -080037 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
38 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 canvas.drawPaint(paint);
40}
41
42static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
43 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed8a21c9f2016-03-08 18:50:00 -080044 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070045 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046}
47
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000048static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000049 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000050 kRGB_565_SkColorType,
reed@android.com8a1c16f2008-12-17 15:59:43 +000051};
52static const int gWidth = 32;
53static const int gHeight = 32;
54
reed@google.comf2183392011-04-22 14:10:48 +000055class TilingView : public SampleView {
reedca2622b2016-03-18 07:25:55 -070056 sk_sp<SkPicture> fTextPicture;
reed7b380d02016-03-21 13:25:16 -070057 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000058public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000059 TilingView()
reed7b380d02016-03-21 13:25:16 -070060 : fLooper(SkBlurDrawLooper::Make(0x88000000,
61 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
62 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000063 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
64 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 }
66 }
67
robertphillips@google.com84b18c72014-04-13 19:09:42 +000068 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000069 }
70
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000071 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073protected:
74 // overrides from SkEventSink
75 virtual bool onQuery(SkEvent* evt) {
76 if (SampleCode::TitleQ(*evt)) {
77 SampleCode::TitleR(evt, "Tiling");
78 return true;
79 }
80 return this->INHERITED::onQuery(evt);
81 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@google.comf2183392011-04-22 14:10:48 +000083 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
85
86 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 static const bool gFilters[] = { false, true };
89 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
92 static const char* gModeNames[] = { "C", "R", "M" };
93
94 SkScalar y = SkIntToScalar(24);
95 SkScalar x = SkIntToScalar(10);
96
robertphillips@google.com84b18c72014-04-13 19:09:42 +000097 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070098 SkCanvas* textCanvas = nullptr;
99 if (nullptr == fTextPicture) {
100 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +0000101 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102
bsalomon49f085d2014-09-05 13:34:00 -0700103 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000104 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
105 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000106 SkPaint p;
107 SkString str;
108 p.setAntiAlias(true);
109 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000110 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000111 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
112
113 p.setTextAlign(SkPaint::kCenter_Align);
114 textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.comf2b98d62010-12-20 18:26:13 +0000116 x += r.width() * 4 / 3;
117 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 }
119 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 y += SkIntToScalar(16);
122
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000123 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000124 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000126 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
127 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 SkPaint paint;
129 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
130 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 canvas->save();
133 canvas->translate(x, y);
134 canvas->drawRect(r, paint);
135 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 x += r.width() * 4 / 3;
138 }
139 }
bsalomon49f085d2014-09-05 13:34:00 -0700140 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkPaint p;
142 SkString str;
143 p.setAntiAlias(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000144 p.setLooper(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000146 textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 }
148
149 y += r.height() * 4 / 3;
150 }
151 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000152
bsalomon49f085d2014-09-05 13:34:00 -0700153 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700154 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700155 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000156 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000157
bsalomon49f085d2014-09-05 13:34:00 -0700158 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700159 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162private:
reed@google.comf2183392011-04-22 14:10:48 +0000163 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164};
165
166//////////////////////////////////////////////////////////////////////////////
167
168static SkView* MyFactory() { return new TilingView; }
169static SkViewRegister reg(MyFactory);