blob: fc4a5fd53ecafb5647e4f7b4c28acfbf0c348549 [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 */
Mike Reed75ae4212018-01-23 11:24:08 -05007
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
Mike Reed75ae4212018-01-23 11:24:08 -05009#include "SkBitmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#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"
Hal Canaryea60b952018-08-21 11:45:46 -040016#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkColorPriv.h"
18#include "SkColorFilter.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000019#include "SkPicture.h"
Mike Reeda697df92018-10-26 07:28:30 -040020#include "SkTextUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkTypeface.h"
22
23// effects
24#include "SkGradientShader.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
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 paint.setDither(true);
reed8a21c9f2016-03-08 18:50:00 -080039 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
40 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 canvas.drawPaint(paint);
42}
43
44static void setup(SkPaint* paint, const SkBitmap& bm, bool filter,
45 SkShader::TileMode tmx, SkShader::TileMode tmy) {
reed8a21c9f2016-03-08 18:50:00 -080046 paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy));
reed93a12152015-03-16 10:08:34 -070047 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048}
49
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000050static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000051 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000052 kRGB_565_SkColorType,
reed@android.com8a1c16f2008-12-17 15:59:43 +000053};
54static const int gWidth = 32;
55static const int gHeight = 32;
56
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040057class TilingView : public Sample {
reedca2622b2016-03-18 07:25:55 -070058 sk_sp<SkPicture> fTextPicture;
reed7b380d02016-03-21 13:25:16 -070059 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000061 TilingView()
reed7b380d02016-03-21 13:25:16 -070062 : fLooper(SkBlurDrawLooper::Make(0x88000000,
63 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
64 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000065 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
66 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
68 }
69
robertphillips@google.com84b18c72014-04-13 19:09:42 +000070 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000071 }
72
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000073 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040076 virtual bool onQuery(Sample::Event* evt) {
77 if (Sample::TitleQ(*evt)) {
78 Sample::TitleR(evt, "Tiling");
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 return true;
80 }
81 return this->INHERITED::onQuery(evt);
82 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reed@google.comf2183392011-04-22 14:10:48 +000084 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
86
87 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 static const bool gFilters[] = { false, true };
90 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode };
93 static const char* gModeNames[] = { "C", "R", "M" };
94
95 SkScalar y = SkIntToScalar(24);
96 SkScalar x = SkIntToScalar(10);
97
robertphillips@google.com84b18c72014-04-13 19:09:42 +000098 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070099 SkCanvas* textCanvas = nullptr;
100 if (nullptr == fTextPicture) {
101 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +0000102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
bsalomon49f085d2014-09-05 13:34:00 -0700104 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000105 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
106 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000107 SkPaint p;
108 SkString str;
109 p.setAntiAlias(true);
110 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000111 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000112 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
113
Mike Reeda697df92018-10-26 07:28:30 -0400114 SkTextUtils::DrawString(textCanvas, str, x + r.width()/2, y, p,
Mike Reed3a42ec02018-10-30 12:53:21 -0400115 SkTextUtils::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.comf2b98d62010-12-20 18:26:13 +0000117 x += r.width() * 4 / 3;
118 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 }
120 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 y += SkIntToScalar(16);
123
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000124 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000125 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000127 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
128 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 SkPaint paint;
130 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
131 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 canvas->save();
134 canvas->translate(x, y);
135 canvas->drawRect(r, paint);
136 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 x += r.width() * 4 / 3;
139 }
140 }
bsalomon49f085d2014-09-05 13:34:00 -0700141 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 SkPaint p;
143 SkString str;
144 p.setAntiAlias(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000145 p.setLooper(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 str.printf("%s, %s", gConfigNames[i], gFilterNames[j]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400147 textCanvas->drawString(str, x, y + r.height() * 2 / 3, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 }
149
150 y += r.height() * 4 / 3;
151 }
152 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000153
bsalomon49f085d2014-09-05 13:34:00 -0700154 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700156 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000157 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000158
bsalomon49f085d2014-09-05 13:34:00 -0700159 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700160 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400164 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165};
166
167//////////////////////////////////////////////////////////////////////////////
168
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400169DEF_SAMPLE( return new TilingView(); )