blob: 981266615adcb588152ee1dd93e2b6ce0de748f8 [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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040075 virtual bool onQuery(Sample::Event* evt) {
76 if (Sample::TitleQ(*evt)) {
77 Sample::TitleR(evt, "Tiling");
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 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
Mike Reedfae8fce2019-04-03 10:27:45 -040091 static const SkTileMode gModes[] = { SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
92 static const char* gModeNames[] = { "C", "R", "M" };
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
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;
reed@android.comf2b98d62010-12-20 18:26:13 +0000108 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000109 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000110 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
111
Mike Reedb579f072019-01-03 15:45:53 -0500112 SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
Mike Reed3a42ec02018-10-30 12:53:21 -0400113 SkTextUtils::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@android.comf2b98d62010-12-20 18:26:13 +0000115 x += r.width() * 4 / 3;
116 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 }
118 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 y += SkIntToScalar(16);
121
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000122 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000123 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000125 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
126 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 SkPaint paint;
128 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
129 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 canvas->save();
132 canvas->translate(x, y);
133 canvas->drawRect(r, paint);
134 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 x += r.width() * 4 / 3;
137 }
138 }
bsalomon49f085d2014-09-05 13:34:00 -0700139 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SkPaint p;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000141 p.setLooper(fLooper);
Hal Canary4484b8f2019-01-08 14:00:08 -0500142 textCanvas->drawString(
143 SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
144 x, y + r.height() * 2 / 3, SkFont(), p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 }
146
147 y += r.height() * 4 / 3;
148 }
149 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000150
bsalomon49f085d2014-09-05 13:34:00 -0700151 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700152 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700153 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000154 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000155
bsalomon49f085d2014-09-05 13:34:00 -0700156 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700157 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400161 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162};
163
164//////////////////////////////////////////////////////////////////////////////
165
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400166DEF_SAMPLE( return new TilingView(); )