blob: 743916f55ae1d859390ca7936bb23d8f5605637b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
14#include "include/core/SkPicture.h"
15#include "include/core/SkPictureRecorder.h"
16#include "include/core/SkRegion.h"
17#include "include/core/SkShader.h"
18#include "include/core/SkTypeface.h"
19#include "include/utils/SkTextUtils.h"
20#include "samplecode/Sample.h"
21#include "src/utils/SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
23// effects
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkBlurDrawLooper.h"
25#include "include/effects/SkGradientShader.h"
26#include "src/core/SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
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,
Mike Reedfae8fce2019-04-03 10:27:45 -040040 SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 canvas.drawPaint(paint);
42}
43
Mike Reedfae8fce2019-04-03 10:27:45 -040044static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, SkTileMode tmx, SkTileMode tmy) {
Mike Reed50acf8f2019-04-08 13:20:23 -040045 paint->setShader(bm.makeShader(tmx, tmy));
reed93a12152015-03-16 10:08:34 -070046 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000049static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000050 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000051 kRGB_565_SkColorType,
reed@android.com8a1c16f2008-12-17 15:59:43 +000052};
53static const int gWidth = 32;
54static const int gHeight = 32;
55
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040056class TilingView : public Sample {
reedca2622b2016-03-18 07:25:55 -070057 sk_sp<SkPicture> fTextPicture;
reed7b380d02016-03-21 13:25:16 -070058 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000060 TilingView()
reed7b380d02016-03-21 13:25:16 -070061 : fLooper(SkBlurDrawLooper::Make(0x88000000,
62 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
63 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000064 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
65 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 }
67 }
68
robertphillips@google.com84b18c72014-04-13 19:09:42 +000069 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000070 }
71
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000072 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074protected:
Hal Canary8a027312019-07-03 10:55:44 -040075 virtual SkString name() { return SkString("Tiling"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@google.comf2183392011-04-22 14:10:48 +000077 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
79
80 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 static const bool gFilters[] = { false, true };
83 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
Mike Reedfae8fce2019-04-03 10:27:45 -040085 static const SkTileMode gModes[] = { SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
86 static const char* gModeNames[] = { "C", "R", "M" };
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
88 SkScalar y = SkIntToScalar(24);
89 SkScalar x = SkIntToScalar(10);
90
robertphillips@google.com84b18c72014-04-13 19:09:42 +000091 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070092 SkCanvas* textCanvas = nullptr;
93 if (nullptr == fTextPicture) {
94 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +000095 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000096
bsalomon49f085d2014-09-05 13:34:00 -070097 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000098 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
99 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000100 SkPaint p;
101 SkString str;
reed@android.comf2b98d62010-12-20 18:26:13 +0000102 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000103 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000104 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
105
Mike Reedb579f072019-01-03 15:45:53 -0500106 SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
Mike Reed3a42ec02018-10-30 12:53:21 -0400107 SkTextUtils::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@android.comf2b98d62010-12-20 18:26:13 +0000109 x += r.width() * 4 / 3;
110 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 }
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 y += SkIntToScalar(16);
115
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000116 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000117 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000119 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
120 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 SkPaint paint;
122 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
123 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 canvas->save();
126 canvas->translate(x, y);
127 canvas->drawRect(r, paint);
128 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 x += r.width() * 4 / 3;
131 }
132 }
bsalomon49f085d2014-09-05 13:34:00 -0700133 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 SkPaint p;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000135 p.setLooper(fLooper);
Hal Canary4484b8f2019-01-08 14:00:08 -0500136 textCanvas->drawString(
137 SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
138 x, y + r.height() * 2 / 3, SkFont(), p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 }
140
141 y += r.height() * 4 / 3;
142 }
143 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000144
bsalomon49f085d2014-09-05 13:34:00 -0700145 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700146 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700147 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000148 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000149
bsalomon49f085d2014-09-05 13:34:00 -0700150 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700151 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400155 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156};
157
158//////////////////////////////////////////////////////////////////////////////
159
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400160DEF_SAMPLE( return new TilingView(); )