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