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 "SkGradientShader.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "Sk1DPathEffect.h" |
| 17 | #include "SkCornerPathEffect.h" |
| 18 | #include "SkPathMeasure.h" |
| 19 | #include "SkRandom.h" |
| 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkDither.h" |
| 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 }; |
| 37 | SkShader* s = SkGradientShader::CreateSweep(r.centerX(), r.centerY(), |
| 38 | colors, NULL, SK_ARRAY_COUNT(colors)); |
| 39 | p.setShader(s)->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | SkAutoCanvasRestore acr(c, true); |
| 42 | |
| 43 | c->translate(r.centerX(), r.centerY()); |
| 44 | c->rotate(angle); |
| 45 | c->translate(-r.centerX(), -r.centerY()); |
| 46 | |
| 47 | SkRect bounds = r; |
| 48 | r.inset(p.getStrokeWidth(), p.getStrokeWidth()); |
| 49 | SkRect innerBounds = r; |
| 50 | |
| 51 | if (true) { |
| 52 | c->drawOval(r, p); |
| 53 | } else { |
| 54 | SkScalar x = r.centerX(); |
| 55 | SkScalar y = r.centerY(); |
| 56 | SkScalar radius = r.width() / 2; |
| 57 | SkScalar thickness = p.getStrokeWidth(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 58 | SkScalar sweep = 360.0f; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | SkPath path; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 60 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | path.moveTo(x + radius, y); |
| 62 | // outer top |
| 63 | path.lineTo(x + radius + thickness, y); |
| 64 | // outer arc |
| 65 | path.arcTo(bounds, 0, sweep, false); |
| 66 | // inner arc |
| 67 | path.arcTo(innerBounds, sweep, -sweep, false); |
| 68 | path.close(); |
| 69 | } |
| 70 | } |
| 71 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 72 | static void make_bm(SkBitmap* bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | bm->setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 74 | bm->allocPixels(); |
| 75 | #if 0 |
| 76 | bm->eraseColor(SK_ColorBLUE); |
| 77 | return; |
| 78 | #else |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 79 | bm->eraseColor(SK_ColorTRANSPARENT); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
| 82 | SkCanvas c(*bm); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | draw_sweep(&c, bm->width(), bm->height(), 0); |
| 84 | } |
| 85 | |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 86 | static void pre_dither(const SkBitmap& bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | SkAutoLockPixels alp(bm); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 89 | for (int y = 0; y < bm.height(); y++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | DITHER_4444_SCAN(y); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | SkPMColor* p = bm.getAddr32(0, y); |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 93 | for (int x = 0; x < bm.width(); x++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | SkPMColor c = *p; |
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 | unsigned a = SkGetPackedA32(c); |
| 97 | unsigned r = SkGetPackedR32(c); |
| 98 | unsigned g = SkGetPackedG32(c); |
| 99 | unsigned b = SkGetPackedB32(c); |
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 | unsigned d = DITHER_VALUE(x); |
| 102 | |
| 103 | a = SkDITHER_A32To4444(a, d); |
| 104 | r = SkDITHER_R32To4444(r, d); |
| 105 | g = SkDITHER_G32To4444(g, d); |
| 106 | b = SkDITHER_B32To4444(b, d); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 107 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | a = SkA4444ToA32(a); |
| 109 | r = SkR4444ToR32(r); |
| 110 | g = SkG4444ToG32(g); |
| 111 | b = SkB4444ToB32(b); |
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 | *p++ = SkPackARGB32(a, r, g, b); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 118 | class DitherView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | public: |
| 120 | SkBitmap fBM, fBMPreDither, fBM16; |
| 121 | SkScalar fAngle; |
| 122 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 123 | DitherView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | make_bm(&fBM); |
| 125 | make_bm(&fBMPreDither); |
| 126 | pre_dither(fBMPreDither); |
| 127 | fBM.copyTo(&fBM16, SkBitmap::kARGB_4444_Config); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | fAngle = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 131 | this->setBGColor(0xFF181818); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | protected: |
| 135 | // overrides from SkEventSink |
| 136 | virtual bool onQuery(SkEvent* evt) { |
| 137 | if (SampleCode::TitleQ(*evt)) { |
| 138 | SampleCode::TitleR(evt, "Dither"); |
| 139 | return true; |
| 140 | } |
| 141 | return this->INHERITED::onQuery(evt); |
| 142 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 143 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 144 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | SkPaint paint; |
| 146 | SkScalar x = SkIntToScalar(10); |
| 147 | SkScalar y = SkIntToScalar(10); |
| 148 | const SkScalar DX = SkIntToScalar(fBM.width() + 10); |
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 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | if (true) { |
| 153 | canvas->drawBitmap(fBM, x, y, &paint); |
| 154 | x += DX; |
| 155 | paint.setDither(true); |
| 156 | canvas->drawBitmap(fBM, x, y, &paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | x += DX; |
| 159 | paint.setDither(false); |
| 160 | canvas->drawBitmap(fBMPreDither, x, y, &paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 161 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | x += DX; |
| 163 | canvas->drawBitmap(fBM16, x, y, &paint); |
| 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 | canvas->translate(DX, DX*2); |
| 167 | draw_sweep(canvas, fBM.width(), fBM.height(), fAngle); |
| 168 | canvas->translate(DX, 0); |
| 169 | draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle); |
| 170 | canvas->translate(DX, 0); |
| 171 | draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle); |
| 172 | |
| 173 | fAngle += SK_Scalar1/2; |
| 174 | this->inval(NULL); |
| 175 | } |
| 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 178 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
| 183 | static SkView* MyFactory() { return new DitherView; } |
| 184 | static SkViewRegister reg(MyFactory); |