blob: a599418723f9b64574608eb8e23040be27e6fd3e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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.com8a1c16f2008-12-17 15:59:43 +000016#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
24static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) {
25 SkRect r;
26 SkPaint p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 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.comae933ce2012-08-23 18:19:56 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 // SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN };
36 SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 };
reed8a21c9f2016-03-08 18:50:00 -080037 p.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
38 colors, nullptr, SK_ARRAY_COUNT(colors)));
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkAutoCanvasRestore acr(c, true);
41
42 c->translate(r.centerX(), r.centerY());
43 c->rotate(angle);
44 c->translate(-r.centerX(), -r.centerY());
45
46 SkRect bounds = r;
47 r.inset(p.getStrokeWidth(), p.getStrokeWidth());
48 SkRect innerBounds = r;
49
50 if (true) {
51 c->drawOval(r, p);
52 } else {
53 SkScalar x = r.centerX();
54 SkScalar y = r.centerY();
55 SkScalar radius = r.width() / 2;
56 SkScalar thickness = p.getStrokeWidth();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000057 SkScalar sweep = 360.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkPath path;
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 path.moveTo(x + radius, y);
61 // outer top
62 path.lineTo(x + radius + thickness, y);
63 // outer arc
64 path.arcTo(bounds, 0, sweep, false);
65 // inner arc
66 path.arcTo(innerBounds, sweep, -sweep, false);
67 path.close();
68 }
69}
70
reed@google.com961ddb02011-05-05 14:03:48 +000071static void make_bm(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000072 bm->allocN32Pixels(100, 100);
junov@google.comdbfac8a2012-12-06 21:47:40 +000073 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
75 SkCanvas c(*bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 draw_sweep(&c, bm->width(), bm->height(), 0);
77}
78
reed@google.com261b8e22011-04-14 17:53:24 +000079static void pre_dither(const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkAutoLockPixels alp(bm);
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@google.com261b8e22011-04-14 17:53:24 +000082 for (int y = 0; y < bm.height(); y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 DITHER_4444_SCAN(y);
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 SkPMColor* p = bm.getAddr32(0, y);
reed@google.com261b8e22011-04-14 17:53:24 +000086 for (int x = 0; x < bm.width(); x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkPMColor c = *p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 unsigned a = SkGetPackedA32(c);
90 unsigned r = SkGetPackedR32(c);
91 unsigned g = SkGetPackedG32(c);
92 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 unsigned d = DITHER_VALUE(x);
95
96 a = SkDITHER_A32To4444(a, d);
97 r = SkDITHER_R32To4444(r, d);
98 g = SkDITHER_G32To4444(g, d);
99 b = SkDITHER_B32To4444(b, d);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 a = SkA4444ToA32(a);
102 r = SkR4444ToR32(r);
103 g = SkG4444ToG32(g);
104 b = SkB4444ToB32(b);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 *p++ = SkPackARGB32(a, r, g, b);
107 }
108 }
109}
110
reed@google.com961ddb02011-05-05 14:03:48 +0000111class DitherView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112public:
113 SkBitmap fBM, fBMPreDither, fBM16;
114 SkScalar fAngle;
115
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116 DitherView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 make_bm(&fBM);
118 make_bm(&fBMPreDither);
119 pre_dither(fBMPreDither);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000120 fBM.copyTo(&fBM16, kARGB_4444_SkColorType);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 fAngle = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@google.com961ddb02011-05-05 14:03:48 +0000124 this->setBGColor(0xFF181818);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 }
126
127protected:
128 // overrides from SkEventSink
129 virtual bool onQuery(SkEvent* evt) {
130 if (SampleCode::TitleQ(*evt)) {
131 SampleCode::TitleR(evt, "Dither");
132 return true;
133 }
134 return this->INHERITED::onQuery(evt);
135 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@google.com961ddb02011-05-05 14:03:48 +0000137 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 SkPaint paint;
139 SkScalar x = SkIntToScalar(10);
140 SkScalar y = SkIntToScalar(10);
141 const SkScalar DX = SkIntToScalar(fBM.width() + 10);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 if (true) {
146 canvas->drawBitmap(fBM, x, y, &paint);
147 x += DX;
148 paint.setDither(true);
149 canvas->drawBitmap(fBM, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 x += DX;
152 paint.setDither(false);
153 canvas->drawBitmap(fBMPreDither, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 x += DX;
156 canvas->drawBitmap(fBM16, x, y, &paint);
157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 canvas->translate(DX, DX*2);
160 draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
161 canvas->translate(DX, 0);
162 draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
163 canvas->translate(DX, 0);
164 draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
165
166 fAngle += SK_Scalar1/2;
halcanary96fcdcc2015-08-27 07:41:13 -0700167 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 }
169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170private:
reed@google.com961ddb02011-05-05 14:03:48 +0000171 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172};
173
174//////////////////////////////////////////////////////////////////////////////
175
176static SkView* MyFactory() { return new DitherView; }
177static SkViewRegister reg(MyFactory);