blob: 8c14fb5110bfd409c6d5d045cc311df44bcfab91 [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 };
37 SkShader* s = SkGradientShader::CreateSweep(r.centerX(), r.centerY(),
halcanary96fcdcc2015-08-27 07:41:13 -070038 colors, nullptr, SK_ARRAY_COUNT(colors));
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 p.setShader(s)->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 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.org4b413c82013-11-25 19:44:07 +000058 SkScalar sweep = 360.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 SkPath path;
rmistry@google.comae933ce2012-08-23 18:19:56 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 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.com961ddb02011-05-05 14:03:48 +000072static void make_bm(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000073 bm->allocN32Pixels(100, 100);
junov@google.comdbfac8a2012-12-06 21:47:40 +000074 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
76 SkCanvas c(*bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 draw_sweep(&c, bm->width(), bm->height(), 0);
78}
79
reed@google.com261b8e22011-04-14 17:53:24 +000080static void pre_dither(const SkBitmap& bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkAutoLockPixels alp(bm);
rmistry@google.comae933ce2012-08-23 18:19:56 +000082
reed@google.com261b8e22011-04-14 17:53:24 +000083 for (int y = 0; y < bm.height(); y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 DITHER_4444_SCAN(y);
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkPMColor* p = bm.getAddr32(0, y);
reed@google.com261b8e22011-04-14 17:53:24 +000087 for (int x = 0; x < bm.width(); x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 SkPMColor c = *p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 unsigned a = SkGetPackedA32(c);
91 unsigned r = SkGetPackedR32(c);
92 unsigned g = SkGetPackedG32(c);
93 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 unsigned d = DITHER_VALUE(x);
96
97 a = SkDITHER_A32To4444(a, d);
98 r = SkDITHER_R32To4444(r, d);
99 g = SkDITHER_G32To4444(g, d);
100 b = SkDITHER_B32To4444(b, d);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 a = SkA4444ToA32(a);
103 r = SkR4444ToR32(r);
104 g = SkG4444ToG32(g);
105 b = SkB4444ToB32(b);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 *p++ = SkPackARGB32(a, r, g, b);
108 }
109 }
110}
111
reed@google.com961ddb02011-05-05 14:03:48 +0000112class DitherView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113public:
114 SkBitmap fBM, fBMPreDither, fBM16;
115 SkScalar fAngle;
116
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117 DitherView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 make_bm(&fBM);
119 make_bm(&fBMPreDither);
120 pre_dither(fBMPreDither);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000121 fBM.copyTo(&fBM16, kARGB_4444_SkColorType);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 fAngle = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@google.com961ddb02011-05-05 14:03:48 +0000125 this->setBGColor(0xFF181818);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 }
127
128protected:
129 // overrides from SkEventSink
130 virtual bool onQuery(SkEvent* evt) {
131 if (SampleCode::TitleQ(*evt)) {
132 SampleCode::TitleR(evt, "Dither");
133 return true;
134 }
135 return this->INHERITED::onQuery(evt);
136 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@google.com961ddb02011-05-05 14:03:48 +0000138 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 SkPaint paint;
140 SkScalar x = SkIntToScalar(10);
141 SkScalar y = SkIntToScalar(10);
142 const SkScalar DX = SkIntToScalar(fBM.width() + 10);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 if (true) {
147 canvas->drawBitmap(fBM, x, y, &paint);
148 x += DX;
149 paint.setDither(true);
150 canvas->drawBitmap(fBM, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 x += DX;
153 paint.setDither(false);
154 canvas->drawBitmap(fBMPreDither, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 x += DX;
157 canvas->drawBitmap(fBM16, x, y, &paint);
158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 canvas->translate(DX, DX*2);
161 draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
162 canvas->translate(DX, 0);
163 draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
164 canvas->translate(DX, 0);
165 draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
166
167 fAngle += SK_Scalar1/2;
halcanary96fcdcc2015-08-27 07:41:13 -0700168 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 }
170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171private:
reed@google.com961ddb02011-05-05 14:03:48 +0000172 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173};
174
175//////////////////////////////////////////////////////////////////////////////
176
177static SkView* MyFactory() { return new DitherView; }
178static SkViewRegister reg(MyFactory);