epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 7 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 9 | #include "SkBitmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkPath.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 13 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkRegion.h" |
| 15 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 16 | #include "SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkColorPriv.h" |
| 18 | #include "SkColorFilter.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 19 | #include "SkPicture.h" |
Mike Reed | a697df9 | 2018-10-26 07:28:30 -0400 | [diff] [blame] | 20 | #include "SkTextUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | #include "SkTypeface.h" |
| 22 | |
| 23 | // effects |
| 24 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 25 | #include "SkBlurMask.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #include "SkBlurDrawLooper.h" |
| 27 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 28 | static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
| 29 | bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 30 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | SkCanvas canvas(*bm); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 33 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 35 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 36 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 37 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | paint.setDither(true); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 39 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, |
| 40 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | canvas.drawPaint(paint); |
| 42 | } |
| 43 | |
| 44 | static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
| 45 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 46 | paint->setShader(SkShader::MakeBitmapShader(bm, tmx, tmy)); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 47 | paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 50 | static const SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 51 | kN32_SkColorType, |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 52 | kRGB_565_SkColorType, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | }; |
| 54 | static const int gWidth = 32; |
| 55 | static const int gHeight = 32; |
| 56 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 57 | class TilingView : public Sample { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 58 | sk_sp<SkPicture> fTextPicture; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 59 | sk_sp<SkDrawLooper> fLooper; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | TilingView() |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 62 | : fLooper(SkBlurDrawLooper::Make(0x88000000, |
| 63 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), |
| 64 | SkIntToScalar(2), SkIntToScalar(2))) { |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 65 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
| 66 | makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 70 | virtual ~TilingView() { |
junov@google.com | f3cf942 | 2011-09-13 18:15:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 73 | SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 76 | virtual bool onQuery(Sample::Event* evt) { |
| 77 | if (Sample::TitleQ(*evt)) { |
| 78 | Sample::TitleR(evt, "Tiling"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | return this->INHERITED::onQuery(evt); |
| 82 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 84 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; |
| 86 | |
| 87 | static const char* gConfigNames[] = { "8888", "565", "4444" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | static const bool gFilters[] = { false, true }; |
| 90 | static const char* gFilterNames[] = { "point", "bilinear" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | 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.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 98 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | SkCanvas* textCanvas = nullptr; |
| 100 | if (nullptr == fTextPicture) { |
| 101 | textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 102 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 104 | if (textCanvas) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 105 | 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.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 107 | SkPaint p; |
| 108 | SkString str; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 109 | p.setDither(true); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 110 | p.setLooper(fLooper); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 111 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 112 | |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 113 | SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p, |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 114 | SkTextUtils::kCenter_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 116 | x += r.width() * 4 / 3; |
| 117 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | y += SkIntToScalar(16); |
| 122 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 123 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 124 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | x = SkIntToScalar(10); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 126 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | SkPaint paint; |
| 129 | setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 130 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 131 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | canvas->save(); |
| 133 | canvas->translate(x, y); |
| 134 | canvas->drawRect(r, paint); |
| 135 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | x += r.width() * 4 / 3; |
| 138 | } |
| 139 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 140 | if (textCanvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | SkPaint p; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 142 | p.setLooper(fLooper); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 143 | textCanvas->drawString( |
| 144 | SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]), |
| 145 | x, y + r.height() * 2 / 3, SkFont(), p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | y += r.height() * 4 / 3; |
| 149 | } |
| 150 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 151 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 152 | if (textCanvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | SkASSERT(nullptr == fTextPicture); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 154 | fTextPicture = recorder.finishRecordingAsPicture(); |
commit-bot@chromium.org | 0dd01ee | 2014-04-15 23:00:57 +0000 | [diff] [blame] | 155 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 156 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 157 | SkASSERT(fTextPicture); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 158 | canvas->drawPicture(fTextPicture.get()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 160 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 162 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 167 | DEF_SAMPLE( return new TilingView(); ) |