blob: ef7d19f51da8f4cf08acf813fe73e870f5df5390 [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
Mike Reedb7dad442019-07-22 14:51:10 -040023#ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025// effects
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/effects/SkBlurDrawLooper.h"
27#include "include/effects/SkGradientShader.h"
28#include "src/core/SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000030static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
31 bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
junov@google.comdbfac8a2012-12-06 21:47:40 +000032 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 SkCanvas canvas(*bm);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000035 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
37 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
38 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 paint.setDither(true);
reed8a21c9f2016-03-08 18:50:00 -080041 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
Mike Reedfae8fce2019-04-03 10:27:45 -040042 SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 canvas.drawPaint(paint);
44}
45
Mike Reedfae8fce2019-04-03 10:27:45 -040046static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, SkTileMode tmx, SkTileMode tmy) {
Mike Reed50acf8f2019-04-08 13:20:23 -040047 paint->setShader(bm.makeShader(tmx, tmy));
reed93a12152015-03-16 10:08:34 -070048 paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000049}
50
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000051static const SkColorType gColorTypes[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000052 kN32_SkColorType,
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000053 kRGB_565_SkColorType,
reed@android.com8a1c16f2008-12-17 15:59:43 +000054};
55static const int gWidth = 32;
56static const int gHeight = 32;
57
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040058class TilingView : public Sample {
reedca2622b2016-03-18 07:25:55 -070059 sk_sp<SkPicture> fTextPicture;
reed7b380d02016-03-21 13:25:16 -070060 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000061public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000062 TilingView()
reed7b380d02016-03-21 13:25:16 -070063 : fLooper(SkBlurDrawLooper::Make(0x88000000,
64 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
65 SkIntToScalar(2), SkIntToScalar(2))) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000066 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
67 makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 }
69 }
70
robertphillips@google.com84b18c72014-04-13 19:09:42 +000071 virtual ~TilingView() {
junov@google.comf3cf9422011-09-13 18:15:52 +000072 }
73
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000074 SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076protected:
Hal Canary8a027312019-07-03 10:55:44 -040077 virtual SkString name() { return SkString("Tiling"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@google.comf2183392011-04-22 14:10:48 +000079 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
81
82 static const char* gConfigNames[] = { "8888", "565", "4444" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 static const bool gFilters[] = { false, true };
85 static const char* gFilterNames[] = { "point", "bilinear" };
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
Mike Reedfae8fce2019-04-03 10:27:45 -040087 static const SkTileMode gModes[] = { SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
88 static const char* gModeNames[] = { "C", "R", "M" };
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90 SkScalar y = SkIntToScalar(24);
91 SkScalar x = SkIntToScalar(10);
92
robertphillips@google.com84b18c72014-04-13 19:09:42 +000093 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070094 SkCanvas* textCanvas = nullptr;
95 if (nullptr == fTextPicture) {
96 textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
reed@android.comf2b98d62010-12-20 18:26:13 +000097 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
bsalomon49f085d2014-09-05 13:34:00 -070099 if (textCanvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000100 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
101 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000102 SkPaint p;
103 SkString str;
reed@android.comf2b98d62010-12-20 18:26:13 +0000104 p.setDither(true);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000105 p.setLooper(fLooper);
reed@android.comf2b98d62010-12-20 18:26:13 +0000106 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
107
Mike Reedb579f072019-01-03 15:45:53 -0500108 SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
Mike Reed3a42ec02018-10-30 12:53:21 -0400109 SkTextUtils::kCenter_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.comf2b98d62010-12-20 18:26:13 +0000111 x += r.width() * 4 / 3;
112 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 }
114 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 y += SkIntToScalar(16);
117
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000118 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000119 for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 x = SkIntToScalar(10);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000121 for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
122 for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkPaint paint;
124 setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
125 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 canvas->save();
128 canvas->translate(x, y);
129 canvas->drawRect(r, paint);
130 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 x += r.width() * 4 / 3;
133 }
134 }
bsalomon49f085d2014-09-05 13:34:00 -0700135 if (textCanvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkPaint p;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000137 p.setLooper(fLooper);
Hal Canary4484b8f2019-01-08 14:00:08 -0500138 textCanvas->drawString(
139 SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
140 x, y + r.height() * 2 / 3, SkFont(), p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 }
142
143 y += r.height() * 4 / 3;
144 }
145 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000146
bsalomon49f085d2014-09-05 13:34:00 -0700147 if (textCanvas) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 SkASSERT(nullptr == fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700149 fTextPicture = recorder.finishRecordingAsPicture();
commit-bot@chromium.org0dd01ee2014-04-15 23:00:57 +0000150 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000151
bsalomon49f085d2014-09-05 13:34:00 -0700152 SkASSERT(fTextPicture);
reedca2622b2016-03-18 07:25:55 -0700153 canvas->drawPicture(fTextPicture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400157 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158};
159
160//////////////////////////////////////////////////////////////////////////////
161
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400162DEF_SAMPLE( return new TilingView(); )
Mike Reedb7dad442019-07-22 14:51:10 -0400163
164#endif