blob: 99b33f0f0ef6dfafe18106e255e157b733267a7a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00007#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.com8a1c16f2008-12-17 15:59:43 +000015#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
23static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) {
24 SkRect r;
25 SkPaint p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 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.comae933ce2012-08-23 18:19:56 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 // SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN };
35 SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 };
reed8a21c9f2016-03-08 18:50:00 -080036 p.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
37 colors, nullptr, SK_ARRAY_COUNT(colors)));
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkAutoCanvasRestore acr(c, true);
40
41 c->translate(r.centerX(), r.centerY());
42 c->rotate(angle);
43 c->translate(-r.centerX(), -r.centerY());
44
45 SkRect bounds = r;
46 r.inset(p.getStrokeWidth(), p.getStrokeWidth());
47 SkRect innerBounds = r;
48
49 if (true) {
50 c->drawOval(r, p);
51 } else {
52 SkScalar x = r.centerX();
53 SkScalar y = r.centerY();
54 SkScalar radius = r.width() / 2;
55 SkScalar thickness = p.getStrokeWidth();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000056 SkScalar sweep = 360.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 SkPath path;
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 path.moveTo(x + radius, y);
60 // outer top
61 path.lineTo(x + radius + thickness, y);
62 // outer arc
63 path.arcTo(bounds, 0, sweep, false);
64 // inner arc
65 path.arcTo(innerBounds, sweep, -sweep, false);
66 path.close();
67 }
68}
69
reed@google.com961ddb02011-05-05 14:03:48 +000070static void make_bm(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000071 bm->allocN32Pixels(100, 100);
junov@google.comdbfac8a2012-12-06 21:47:40 +000072 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
74 SkCanvas c(*bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 draw_sweep(&c, bm->width(), bm->height(), 0);
76}
77
reed@google.com261b8e22011-04-14 17:53:24 +000078static void pre_dither(const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkAutoLockPixels alp(bm);
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@google.com261b8e22011-04-14 17:53:24 +000081 for (int y = 0; y < bm.height(); y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 DITHER_4444_SCAN(y);
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 SkPMColor* p = bm.getAddr32(0, y);
reed@google.com261b8e22011-04-14 17:53:24 +000085 for (int x = 0; x < bm.width(); x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkPMColor c = *p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 unsigned a = SkGetPackedA32(c);
89 unsigned r = SkGetPackedR32(c);
90 unsigned g = SkGetPackedG32(c);
91 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 unsigned d = DITHER_VALUE(x);
94
95 a = SkDITHER_A32To4444(a, d);
96 r = SkDITHER_R32To4444(r, d);
97 g = SkDITHER_G32To4444(g, d);
98 b = SkDITHER_B32To4444(b, d);
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 a = SkA4444ToA32(a);
101 r = SkR4444ToR32(r);
102 g = SkG4444ToG32(g);
103 b = SkB4444ToB32(b);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 *p++ = SkPackARGB32(a, r, g, b);
106 }
107 }
108}
109
reed@google.com961ddb02011-05-05 14:03:48 +0000110class DitherView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111public:
112 SkBitmap fBM, fBMPreDither, fBM16;
113 SkScalar fAngle;
114
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115 DitherView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 make_bm(&fBM);
117 make_bm(&fBMPreDither);
118 pre_dither(fBMPreDither);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000119 fBM.copyTo(&fBM16, kARGB_4444_SkColorType);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 fAngle = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@google.com961ddb02011-05-05 14:03:48 +0000123 this->setBGColor(0xFF181818);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 }
125
126protected:
127 // overrides from SkEventSink
128 virtual bool onQuery(SkEvent* evt) {
129 if (SampleCode::TitleQ(*evt)) {
130 SampleCode::TitleR(evt, "Dither");
131 return true;
132 }
133 return this->INHERITED::onQuery(evt);
134 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
reed@google.com961ddb02011-05-05 14:03:48 +0000136 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkPaint paint;
138 SkScalar x = SkIntToScalar(10);
139 SkScalar y = SkIntToScalar(10);
140 const SkScalar DX = SkIntToScalar(fBM.width() + 10);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 if (true) {
145 canvas->drawBitmap(fBM, x, y, &paint);
146 x += DX;
147 paint.setDither(true);
148 canvas->drawBitmap(fBM, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 x += DX;
151 paint.setDither(false);
152 canvas->drawBitmap(fBMPreDither, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 x += DX;
155 canvas->drawBitmap(fBM16, x, y, &paint);
156 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 canvas->translate(DX, DX*2);
159 draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
160 canvas->translate(DX, 0);
161 draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
162 canvas->translate(DX, 0);
163 draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
164
165 fAngle += SK_Scalar1/2;
halcanary96fcdcc2015-08-27 07:41:13 -0700166 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 }
168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169private:
reed@google.com961ddb02011-05-05 14:03:48 +0000170 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171};
172
173//////////////////////////////////////////////////////////////////////////////
174
175static SkView* MyFactory() { return new DitherView; }
176static SkViewRegister reg(MyFactory);