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@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkRegion.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "Sk1DPathEffect.h" |
| 16 | #include "SkCornerPathEffect.h" |
| 17 | #include "SkPathMeasure.h" |
| 18 | #include "SkRandom.h" |
| 19 | #include "SkColorPriv.h" |
| 20 | #include "SkColorFilter.h" |
| 21 | #include "SkDither.h" |
| 22 | |
| 23 | static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) { |
| 24 | SkRect r; |
| 25 | SkPaint p; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | p.setAntiAlias(true); |
| 28 | // p.setDither(true); |
| 29 | p.setStrokeWidth(SkIntToScalar(width/10)); |
| 30 | p.setStyle(SkPaint::kStroke_Style); |
| 31 | |
| 32 | r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | // SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN }; |
| 35 | SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 36 | p.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(), |
| 37 | colors, nullptr, SK_ARRAY_COUNT(colors))); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | SkAutoCanvasRestore acr(c, true); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 40 | c->rotate(angle, r.centerX(), r.centerY()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | |
| 42 | SkRect bounds = r; |
| 43 | r.inset(p.getStrokeWidth(), p.getStrokeWidth()); |
| 44 | SkRect innerBounds = r; |
| 45 | |
| 46 | if (true) { |
| 47 | c->drawOval(r, p); |
| 48 | } else { |
| 49 | SkScalar x = r.centerX(); |
| 50 | SkScalar y = r.centerY(); |
| 51 | SkScalar radius = r.width() / 2; |
| 52 | SkScalar thickness = p.getStrokeWidth(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 53 | SkScalar sweep = 360.0f; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkPath path; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | path.moveTo(x + radius, y); |
| 57 | // outer top |
| 58 | path.lineTo(x + radius + thickness, y); |
| 59 | // outer arc |
| 60 | path.arcTo(bounds, 0, sweep, false); |
| 61 | // inner arc |
| 62 | path.arcTo(innerBounds, sweep, -sweep, false); |
| 63 | path.close(); |
| 64 | } |
| 65 | } |
| 66 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 67 | static void make_bm(SkBitmap* bm) { |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 68 | bm->allocN32Pixels(100, 100); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 69 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
| 71 | SkCanvas c(*bm); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | draw_sweep(&c, bm->width(), bm->height(), 0); |
| 73 | } |
| 74 | |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 75 | static void pre_dither(const SkBitmap& bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkAutoLockPixels alp(bm); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 78 | for (int y = 0; y < bm.height(); y++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | DITHER_4444_SCAN(y); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | SkPMColor* p = bm.getAddr32(0, y); |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 82 | for (int x = 0; x < bm.width(); x++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | SkPMColor c = *p; |
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 | unsigned a = SkGetPackedA32(c); |
| 86 | unsigned r = SkGetPackedR32(c); |
| 87 | unsigned g = SkGetPackedG32(c); |
| 88 | unsigned b = SkGetPackedB32(c); |
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 | unsigned d = DITHER_VALUE(x); |
| 91 | |
| 92 | a = SkDITHER_A32To4444(a, d); |
| 93 | r = SkDITHER_R32To4444(r, d); |
| 94 | g = SkDITHER_G32To4444(g, d); |
| 95 | b = SkDITHER_B32To4444(b, d); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | a = SkA4444ToA32(a); |
| 98 | r = SkR4444ToR32(r); |
| 99 | g = SkG4444ToG32(g); |
| 100 | b = SkB4444ToB32(b); |
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 | *p++ = SkPackARGB32(a, r, g, b); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 107 | class DitherView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | public: |
| 109 | SkBitmap fBM, fBMPreDither, fBM16; |
| 110 | SkScalar fAngle; |
| 111 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | DitherView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | make_bm(&fBM); |
| 114 | make_bm(&fBMPreDither); |
| 115 | pre_dither(fBMPreDither); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 116 | fBM.copyTo(&fBM16, kARGB_4444_SkColorType); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 117 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | fAngle = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 120 | this->setBGColor(0xFF181818); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | protected: |
| 124 | // overrides from SkEventSink |
| 125 | virtual bool onQuery(SkEvent* evt) { |
| 126 | if (SampleCode::TitleQ(*evt)) { |
| 127 | SampleCode::TitleR(evt, "Dither"); |
| 128 | return true; |
| 129 | } |
| 130 | return this->INHERITED::onQuery(evt); |
| 131 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 132 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 133 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | SkPaint paint; |
| 135 | SkScalar x = SkIntToScalar(10); |
| 136 | SkScalar y = SkIntToScalar(10); |
| 137 | const SkScalar DX = SkIntToScalar(fBM.width() + 10); |
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 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | if (true) { |
| 142 | canvas->drawBitmap(fBM, x, y, &paint); |
| 143 | x += DX; |
| 144 | paint.setDither(true); |
| 145 | canvas->drawBitmap(fBM, x, y, &paint); |
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 += DX; |
| 148 | paint.setDither(false); |
| 149 | canvas->drawBitmap(fBMPreDither, x, y, &paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | x += DX; |
| 152 | canvas->drawBitmap(fBM16, x, y, &paint); |
| 153 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | canvas->translate(DX, DX*2); |
| 156 | draw_sweep(canvas, fBM.width(), fBM.height(), fAngle); |
| 157 | canvas->translate(DX, 0); |
| 158 | draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle); |
| 159 | canvas->translate(DX, 0); |
| 160 | draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle); |
| 161 | |
| 162 | fAngle += SK_Scalar1/2; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | this->inval(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03: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 DitherView; } |
| 173 | static SkViewRegister reg(MyFactory); |