epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 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" |
| 16 | #include "SkUtils.h" |
| 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" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkTypeface.h" |
| 21 | |
| 22 | // effects |
| 23 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 24 | #include "SkBlurMask.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkBlurDrawLooper.h" |
| 26 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 27 | static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) { |
| 28 | bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType)); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 29 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | SkCanvas canvas(*bm); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 32 | SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 34 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 35 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | paint.setDither(true); |
| 38 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 39 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | canvas.drawPaint(paint); |
| 41 | } |
| 42 | |
| 43 | static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
| 44 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
| 45 | SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
| 46 | paint->setShader(shader)->unref(); |
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 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 57 | class TilingView : public SampleView { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 58 | SkAutoTUnref<SkPicture> fTextPicture; |
| 59 | SkAutoTUnref<SkBlurDrawLooper> 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() |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 62 | : fLooper(SkBlurDrawLooper::Create(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: |
| 76 | // overrides from SkEventSink |
| 77 | virtual bool onQuery(SkEvent* evt) { |
| 78 | if (SampleCode::TitleQ(*evt)) { |
| 79 | SampleCode::TitleR(evt, "Tiling"); |
| 80 | return true; |
| 81 | } |
| 82 | return this->INHERITED::onQuery(evt); |
| 83 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 84 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 85 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; |
| 87 | |
| 88 | static const char* gConfigNames[] = { "8888", "565", "4444" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | static const bool gFilters[] = { false, true }; |
| 91 | static const char* gFilterNames[] = { "point", "bilinear" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; |
| 94 | static const char* gModeNames[] = { "C", "R", "M" }; |
| 95 | |
| 96 | SkScalar y = SkIntToScalar(24); |
| 97 | SkScalar x = SkIntToScalar(10); |
| 98 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 99 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 100 | SkCanvas* textCanvas = nullptr; |
| 101 | if (nullptr == fTextPicture) { |
| 102 | textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 103 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 105 | if (textCanvas) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 106 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 107 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 108 | SkPaint p; |
| 109 | SkString str; |
| 110 | p.setAntiAlias(true); |
| 111 | p.setDither(true); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 112 | p.setLooper(fLooper); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 113 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 114 | |
| 115 | p.setTextAlign(SkPaint::kCenter_Align); |
| 116 | textCanvas->drawText(str.c_str(), str.size(), x + r.width()/2, y, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 117 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 118 | x += r.width() * 4 / 3; |
| 119 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | y += SkIntToScalar(16); |
| 124 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 125 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 126 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | x = SkIntToScalar(10); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 128 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 129 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | SkPaint paint; |
| 131 | setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 132 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | canvas->save(); |
| 135 | canvas->translate(x, y); |
| 136 | canvas->drawRect(r, paint); |
| 137 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | x += r.width() * 4 / 3; |
| 140 | } |
| 141 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 142 | if (textCanvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | SkPaint p; |
| 144 | SkString str; |
| 145 | p.setAntiAlias(true); |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 146 | p.setLooper(fLooper); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 148 | textCanvas->drawText(str.c_str(), str.size(), x, y + r.height() * 2 / 3, p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | y += r.height() * 4 / 3; |
| 152 | } |
| 153 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 154 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 155 | if (textCanvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | SkASSERT(nullptr == fTextPicture); |
commit-bot@chromium.org | 0dd01ee | 2014-04-15 23:00:57 +0000 | [diff] [blame] | 157 | fTextPicture.reset(recorder.endRecording()); |
| 158 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 159 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 160 | SkASSERT(fTextPicture); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 161 | canvas->drawPicture(fTextPicture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 163 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | private: |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 165 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | ////////////////////////////////////////////////////////////////////////////// |
| 169 | |
| 170 | static SkView* MyFactory() { return new TilingView; } |
| 171 | static SkViewRegister reg(MyFactory); |