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" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
| 16 | #include "SkColorPriv.h" |
| 17 | #include "SkColorFilter.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 18 | #include "SkPicture.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkTypeface.h" |
| 20 | |
| 21 | // effects |
| 22 | #include "SkGradientShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkUnitMappers.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 | |
| 37 | SkUnitMapper* um = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
| 39 | um = new SkCosineMapper; |
| 40 | // um = new SkDiscreteMapper(12); |
| 41 | |
| 42 | SkAutoUnref au(um); |
| 43 | |
| 44 | paint.setDither(true); |
| 45 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
| 46 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); |
| 47 | canvas.drawPaint(paint); |
| 48 | } |
| 49 | |
| 50 | static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, |
| 51 | SkShader::TileMode tmx, SkShader::TileMode tmy) { |
| 52 | SkShader* shader = SkShader::CreateBitmapShader(bm, tmx, tmy); |
| 53 | paint->setShader(shader)->unref(); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 54 | paint->setFilterLevel(filter ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | } |
| 56 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 57 | static const SkColorType gColorTypes[] = { |
| 58 | kPMColor_SkColorType, |
| 59 | kRGB_565_SkColorType, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | }; |
| 61 | static const int gWidth = 32; |
| 62 | static const int gHeight = 32; |
| 63 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 64 | class TilingView : public SampleView { |
junov@google.com | f3cf942 | 2011-09-13 18:15:52 +0000 | [diff] [blame] | 65 | SkPicture* fTextPicture; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | SkBlurDrawLooper fLooper; |
| 67 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | TilingView() |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 69 | : fLooper(0x88000000, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 70 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)), |
| 71 | SkIntToScalar(2), SkIntToScalar(2)) { |
junov@google.com | f3cf942 | 2011-09-13 18:15:52 +0000 | [diff] [blame] | 72 | fTextPicture = new SkPicture(); |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 73 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
| 74 | makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
junov@google.com | f3cf942 | 2011-09-13 18:15:52 +0000 | [diff] [blame] | 78 | ~TilingView() { |
| 79 | fTextPicture->unref(); |
| 80 | } |
| 81 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 82 | SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | protected: |
| 85 | // overrides from SkEventSink |
| 86 | virtual bool onQuery(SkEvent* evt) { |
| 87 | if (SampleCode::TitleQ(*evt)) { |
| 88 | SampleCode::TitleR(evt, "Tiling"); |
| 89 | return true; |
| 90 | } |
| 91 | return this->INHERITED::onQuery(evt); |
| 92 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 93 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 94 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) }; |
| 96 | |
| 97 | static const char* gConfigNames[] = { "8888", "565", "4444" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | static const bool gFilters[] = { false, true }; |
| 100 | static const char* gFilterNames[] = { "point", "bilinear" }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | static const SkShader::TileMode gModes[] = { SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode }; |
| 103 | static const char* gModeNames[] = { "C", "R", "M" }; |
| 104 | |
| 105 | SkScalar y = SkIntToScalar(24); |
| 106 | SkScalar x = SkIntToScalar(10); |
| 107 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 108 | SkCanvas* textCanvas = NULL; |
junov@google.com | f3cf942 | 2011-09-13 18:15:52 +0000 | [diff] [blame] | 109 | if (fTextPicture->width() == 0) { |
| 110 | textCanvas = fTextPicture->beginRecording(1000, 1000); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 111 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 113 | if (textCanvas) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 114 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 115 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 116 | SkPaint p; |
| 117 | SkString str; |
| 118 | p.setAntiAlias(true); |
| 119 | p.setDither(true); |
| 120 | p.setLooper(&fLooper); |
| 121 | str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]); |
| 122 | |
| 123 | p.setTextAlign(SkPaint::kCenter_Align); |
| 124 | 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] | 125 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 126 | x += r.width() * 4 / 3; |
| 127 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | y += SkIntToScalar(16); |
| 132 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 133 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 134 | for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | x = SkIntToScalar(10); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 136 | for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) { |
| 137 | for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | SkPaint paint; |
| 139 | setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]); |
| 140 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 141 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | canvas->save(); |
| 143 | canvas->translate(x, y); |
| 144 | canvas->drawRect(r, paint); |
| 145 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | x += r.width() * 4 / 3; |
| 148 | } |
| 149 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 150 | if (textCanvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | SkPaint p; |
| 152 | SkString str; |
| 153 | p.setAntiAlias(true); |
| 154 | p.setLooper(&fLooper); |
| 155 | str.printf("%s, %s", gConfigNames[i], gFilterNames[j]); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 156 | 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] | 157 | } |
| 158 | |
| 159 | y += r.height() * 4 / 3; |
| 160 | } |
| 161 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 162 | |
junov@google.com | f3cf942 | 2011-09-13 18:15:52 +0000 | [diff] [blame] | 163 | canvas->drawPicture(*fTextPicture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | private: |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 167 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | ////////////////////////////////////////////////////////////////////////////// |
| 171 | |
| 172 | static SkView* MyFactory() { return new TilingView; } |
| 173 | static SkViewRegister reg(MyFactory); |