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