blob: 79adc629fa04b1f7a86345afa04cebbe294373f5 [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"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040022#include "sk_tool_utils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
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);
bungeman7438bfc2016-07-12 15:01:19 -070041 c->rotate(angle, r.centerX(), r.centerY());
reed@android.com8a1c16f2008-12-17 15:59:43 +000042
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.org4b413c82013-11-25 19:44:07 +000054 SkScalar sweep = 360.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkPath path;
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 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.com961ddb02011-05-05 14:03:48 +000068static void make_bm(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000069 bm->allocN32Pixels(100, 100);
junov@google.comdbfac8a2012-12-06 21:47:40 +000070 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
72 SkCanvas c(*bm);
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 draw_sweep(&c, bm->width(), bm->height(), 0);
74}
75
reed@google.com261b8e22011-04-14 17:53:24 +000076static void pre_dither(const SkBitmap& bm) {
reed@google.com261b8e22011-04-14 17:53:24 +000077 for (int y = 0; y < bm.height(); y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 DITHER_4444_SCAN(y);
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkPMColor* p = bm.getAddr32(0, y);
reed@google.com261b8e22011-04-14 17:53:24 +000081 for (int x = 0; x < bm.width(); x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkPMColor c = *p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 unsigned a = SkGetPackedA32(c);
85 unsigned r = SkGetPackedR32(c);
86 unsigned g = SkGetPackedG32(c);
87 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 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.comae933ce2012-08-23 18:19:56 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 a = SkA4444ToA32(a);
97 r = SkR4444ToR32(r);
98 g = SkG4444ToG32(g);
99 b = SkB4444ToB32(b);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 *p++ = SkPackARGB32(a, r, g, b);
102 }
103 }
104}
105
reed@google.com961ddb02011-05-05 14:03:48 +0000106class DitherView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107public:
108 SkBitmap fBM, fBMPreDither, fBM16;
109 SkScalar fAngle;
110
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111 DitherView() {
Cary Clark1ad81982017-12-19 09:18:59 -0500112 }
113
114protected:
115 void onOnceBeforeDraw() override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 make_bm(&fBM);
117 make_bm(&fBMPreDither);
118 pre_dither(fBMPreDither);
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400119 sk_tool_utils::copy_to(&fBM16, kARGB_4444_SkColorType, fBM);
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 // overrides from SkEventSink
Cary Clark1ad81982017-12-19 09:18:59 -0500127 bool onQuery(SkEvent* evt) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 if (SampleCode::TitleQ(*evt)) {
129 SampleCode::TitleR(evt, "Dither");
130 return true;
131 }
132 return this->INHERITED::onQuery(evt);
133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
Cary Clark1ad81982017-12-19 09:18:59 -0500135 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkPaint paint;
137 SkScalar x = SkIntToScalar(10);
138 SkScalar y = SkIntToScalar(10);
139 const SkScalar DX = SkIntToScalar(fBM.width() + 10);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 if (true) {
144 canvas->drawBitmap(fBM, x, y, &paint);
145 x += DX;
146 paint.setDither(true);
147 canvas->drawBitmap(fBM, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 x += DX;
150 paint.setDither(false);
151 canvas->drawBitmap(fBMPreDither, x, y, &paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 x += DX;
154 canvas->drawBitmap(fBM16, x, y, &paint);
155 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 canvas->translate(DX, DX*2);
158 draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
159 canvas->translate(DX, 0);
160 draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
161 canvas->translate(DX, 0);
162 draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
163
164 fAngle += SK_Scalar1/2;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 }
166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167private:
reed@google.com961ddb02011-05-05 14:03:48 +0000168 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169};
170
171//////////////////////////////////////////////////////////////////////////////
172
173static SkView* MyFactory() { return new DitherView; }
174static SkViewRegister reg(MyFactory);