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 | */ |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 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 "SkGradientShader.h" |
| 12 | #include "SkGraphics.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkRegion.h" |
| 16 | #include "SkShader.h" |
| 17 | #include "SkUtils.h" |
| 18 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
| 20 | #include "SkColorFilter.h" |
| 21 | #include "SkTime.h" |
| 22 | |
| 23 | static const char* gNames[] = { |
| 24 | "/skimages/background_01.png" |
| 25 | }; |
| 26 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 27 | class Filter2View : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | public: |
| 29 | SkBitmap* fBitmaps; |
| 30 | int fBitmapCount; |
| 31 | int fCurrIndex; |
| 32 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | Filter2View() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | fBitmapCount = SK_ARRAY_COUNT(gNames)*2; |
| 35 | fBitmaps = new SkBitmap[fBitmapCount]; |
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 | for (int i = 0; i < fBitmapCount/2; i++) { |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 38 | SkImageDecoder::DecodeFile(gNames[i], &fBitmaps[i], kN32_SkColorType, |
| 39 | SkImageDecoder::kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
| 41 | for (int i = fBitmapCount/2; i < fBitmapCount; i++) { |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 42 | SkImageDecoder::DecodeFile(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType, |
| 43 | SkImageDecoder::kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | } |
| 45 | fCurrIndex = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 47 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | virtual ~Filter2View() { |
| 51 | delete[] fBitmaps; |
| 52 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | protected: |
| 55 | // overrides from SkEventSink |
| 56 | virtual bool onQuery(SkEvent* evt) { |
| 57 | if (SampleCode::TitleQ(*evt)) { |
| 58 | SkString str("Filter/Dither "); |
| 59 | str.append(gNames[fCurrIndex]); |
| 60 | SampleCode::TitleR(evt, str.c_str()); |
| 61 | return true; |
| 62 | } |
| 63 | return this->INHERITED::onQuery(evt); |
| 64 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 66 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | canvas->translate(SkIntToScalar(10), SkIntToScalar(50)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1); |
| 70 | const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1); |
| 71 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 73 | const SkScalar scale = 0.897917f; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | canvas->scale(SK_Scalar1, scale); |
| 75 | |
| 76 | for (int k = 0; k < 2; k++) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 77 | paint.setFilterLevel(k == 1 ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | for (int j = 0; j < 2; j++) { |
| 79 | paint.setDither(j == 1); |
| 80 | for (int i = 0; i < fBitmapCount; i++) { |
| 81 | SkScalar x = (k * fBitmapCount + j) * W; |
| 82 | SkScalar y = i * H; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 83 | x = SkScalarRoundToScalar(x); |
| 84 | y = SkScalarRoundToScalar(y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | canvas->drawBitmap(fBitmaps[i], x, y, &paint); |
| 86 | if (i == 0) { |
| 87 | SkPaint p; |
| 88 | p.setAntiAlias(true); |
| 89 | p.setTextAlign(SkPaint::kCenter_Align); |
| 90 | p.setTextSize(SkIntToScalar(18)); |
| 91 | SkString s("dither="); |
| 92 | s.appendS32(paint.isDither()); |
| 93 | s.append(" filter="); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 94 | s.appendS32(paint.getFilterLevel() != SkPaint::kNone_FilterLevel); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | canvas->drawText(s.c_str(), s.size(), x + W/2, |
| 96 | y - p.getTextSize(), p); |
| 97 | } |
| 98 | if (k+j == 2) { |
| 99 | SkPaint p; |
| 100 | p.setAntiAlias(true); |
| 101 | p.setTextSize(SkIntToScalar(18)); |
| 102 | SkString s; |
| 103 | s.append(" depth="); |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 104 | s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4), |
| 106 | y + H/2, p); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 114 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | ////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | static SkView* MyFactory() { return new Filter2View; } |
| 120 | static SkViewRegister reg(MyFactory); |